From 662e533eacc3977e9a8a4c9f9f8f7073e5f52d1e Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Sun, 12 Oct 2025 21:34:10 +0200 Subject: [PATCH 01/60] wip --- .../src/encryption/compressed/gglwe_atk.rs | 137 ++-- .../src/encryption/compressed/gglwe_ct.rs | 156 +++-- .../src/encryption/compressed/gglwe_ksk.rs | 101 ++- .../src/encryption/compressed/gglwe_tsk.rs | 121 ++-- .../src/encryption/compressed/ggsw_ct.rs | 170 ++--- poulpy-core/src/encryption/gglwe_atk.rs | 127 ++-- poulpy-core/src/encryption/gglwe_ct.rs | 158 +++-- poulpy-core/src/encryption/ggsw_ct.rs | 147 +++-- poulpy-core/src/encryption/glwe_ct.rs | 620 +++++++++++------- poulpy-core/src/encryption/glwe_pk.rs | 88 +-- poulpy-core/src/encryption/mod.rs | 2 - .../src/layouts/compressed/gglwe_atk.rs | 44 +- .../src/layouts/compressed/gglwe_ct.rs | 38 +- .../src/layouts/compressed/gglwe_ksk.rs | 64 +- .../src/layouts/compressed/gglwe_tsk.rs | 46 +- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 38 +- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 12 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 12 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 12 +- poulpy-core/src/layouts/gglwe_atk.rs | 23 +- poulpy-core/src/layouts/gglwe_ct.rs | 32 +- poulpy-core/src/layouts/gglwe_ksk.rs | 20 +- poulpy-core/src/layouts/ggsw_ct.rs | 34 +- poulpy-core/src/layouts/glwe_pk.rs | 32 +- poulpy-core/src/layouts/glwe_pt.rs | 28 + poulpy-core/src/layouts/glwe_sk.rs | 28 +- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 18 +- poulpy-core/src/layouts/prepared/glwe_pk.rs | 32 +- poulpy-core/src/layouts/prepared/glwe_sk.rs | 28 +- poulpy-core/src/tests/serialization.rs | 5 +- .../tests/test_suite/encryption/gglwe_ct.rs | 6 +- .../tests/test_suite/encryption/glwe_ct.rs | 2 +- 32 files changed, 1594 insertions(+), 787 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 95dcf20..f202223 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,19 +1,15 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - }, + api::{ScratchAvailable, SvpPPolAllocBytes, VecZnxAutomorphism, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, + TakeGLWESecret, + encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, LWEInfos, - compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + compressed::{GGLWEAutomorphismKeyCompressed, GGLWEAutomorphismKeyCompressedToMut, GGLWEKeyCompressed}, }, }; @@ -24,8 +20,75 @@ impl GGLWEAutomorphismKeyCompressed> { Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { assert_eq!(module.n() as u32, infos.n()); - GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GGLWEKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + } +} + +pub trait GGLWEAutomorphismKeyCompressedEncryptSk { + fn gglwe_automorphism_key_compressed_encrypt_sk( + &self, + res: &mut R, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWEAutomorphismKeyCompressedToMut, + S: GLWESecretToRef; +} + +impl GGLWEAutomorphismKeyCompressedEncryptSk for Module +where + Module: + GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxAutomorphism, + Scratch: TakeGLWESecret + ScratchAvailable, +{ + fn gglwe_automorphism_key_compressed_encrypt_sk( + &self, + res: &mut R, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWEAutomorphismKeyCompressedToMut, + S: GLWESecretToRef, + { + let res: &mut GGLWEAutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + + #[cfg(debug_assertions)] + { + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); + assert!( + scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", + scratch.available(), + GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) + ) + } + + let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); + + { + (0..res.rank_out().into()).for_each(|i| { + self.vec_znx_automorphism( + self.galois_element_inv(p), + &mut sk_out.data.as_vec_znx_mut(), + i, + &sk.data.as_vec_znx(), + i, + ); + }); + } + + self.gglwe_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + + res.p = p; } } @@ -40,56 +103,8 @@ impl GGLWEAutomorphismKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAutomorphism - + SvpPrepare - + SvpPPolAllocBytes - + VecZnxSwitchRing - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Module: GGLWEAutomorphismKeyCompressedEncryptSk, { - #[cfg(debug_assertions)] - { - assert_eq!(self.n(), sk.n()); - assert_eq!(self.rank_out(), self.rank_in()); - assert_eq!(sk.rank(), self.rank_out()); - assert!( - scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", - scratch.available(), - GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self) - ) - } - - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); - - { - (0..self.rank_out().into()).for_each(|i| { - module.vec_znx_automorphism( - module.galois_element_inv(p), - &mut sk_out.data.as_vec_znx_mut(), - i, - &sk.data.as_vec_znx(), - i, - ); - }); - } - - self.key - .encrypt_sk(module, sk, &sk_out, seed_xa, source_xe, scratch_1); - - self.p = p; + module.gglwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 76871da..c2b4de3 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -1,29 +1,22 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, + ZnNormalizeInplace, }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ TakeGLWEPt, - encryption::{SIGMA, glwe_encrypt_sk_internal}, - layouts::{GGLWECiphertext, GGLWEInfos, LWEInfos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, + layouts::{ + GGLWECiphertext, GGLWEInfos, LWEInfos, + compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; -impl GGLWECiphertextCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize - where - A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, - { - GGLWECiphertext::encrypt_sk_scratch_space(module, infos) - } -} - impl GGLWECiphertextCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( @@ -35,83 +28,124 @@ impl GGLWECiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGLWECompressedEncryptSk, { + module.gglwe_compressed_encrypt_sk(self, pt, sk, seed, source_xe, scratch); + } +} + +impl GGLWECiphertextCompressed> { + pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + { + GGLWECiphertext::encrypt_sk_scratch_space(module, infos) + } +} + +pub trait GGLWECompressedEncryptSk { + fn gglwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWECiphertextCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal + + VecZnxNormalizeInplace + + VecZnxNormalizeTmpBytes + + VecZnxDftAllocBytes + + VecZnxAddScalarInplace + + ZnNormalizeInplace, + Scratch: TakeGLWEPt + ScratchAvailable, +{ + fn gglwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWECiphertextCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + #[cfg(debug_assertions)] { use poulpy_hal::layouts::ZnxInfos; + let sk = &sk.to_ref(); assert_eq!( - self.rank_in(), + res.rank_in(), pt.cols() as u32, - "self.rank_in(): {} != pt.cols(): {}", - self.rank_in(), + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), pt.cols() ); assert_eq!( - self.rank_out(), + res.rank_out(), sk.rank(), - "self.rank_out(): {} != sk.rank(): {}", - self.rank_out(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), sk.rank() ); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self), + scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self) + GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res) ); assert!( - self.dnum().0 * self.dsize().0 * self.base2k().0 <= self.k().0, - "self.dnum() : {} * self.dsize() : {} * self.base2k() : {} = {} >= self.k() = {}", - self.dnum(), - self.dsize(), - self.base2k(), - self.dnum().0 * self.dsize().0 * self.base2k().0, - self.k() + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() ); } - let dnum: usize = self.dnum().into(); - let dsize: usize = self.dsize().into(); - let base2k: usize = self.base2k().into(); - let rank_in: usize = self.rank_in().into(); - let cols: usize = (self.rank_out() + 1).into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + let base2k: usize = res.base2k().into(); + let rank_in: usize = res.rank_in().into(); + let cols: usize = (res.rank_out() + 1).into(); let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res); (0..rank_in).for_each(|col_i| { (0..dnum).for_each(|d_i| { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt tmp_pt.data.zero(); // zeroes for next iteration - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); let (seed, mut source_xa_tmp) = source_xa.branch(); - self.seed[col_i * dnum + d_i] = seed; + res.seed[col_i * dnum + d_i] = seed; - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.at_mut(d_i, col_i).data, + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(d_i, col_i).data, cols, true, Some((&tmp_pt, 0)), diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 8dd177f..e5b3716 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,9 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes, + VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, @@ -11,13 +9,15 @@ use poulpy_hal::{ use crate::{ TakeGLWESecretPrepared, + encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, LWEInfos, compressed::GGLWESwitchingKeyCompressed, + Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut}, prepared::GLWESecretPrepared, }, }; -impl GGLWESwitchingKeyCompressed> { +impl GGLWEKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -29,7 +29,7 @@ impl GGLWESwitchingKeyCompressed> { } } -impl GGLWESwitchingKeyCompressed { +impl GGLWEKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -40,36 +40,65 @@ impl GGLWESwitchingKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpPPolAllocBytes - + VecZnxSwitchRing - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Module: GGLWEKeyCompressedEncryptSk, { + module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); + } +} + +pub trait GGLWEKeyCompressedEncryptSk { + fn gglwe_key_compressed_encrypt_sk( + &self, + res: &mut R, + sk_in: &SI, + sk_out: &SO, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWEKeyCompressedToMut, + SI: GLWESecretToRef, + SO: GLWESecretToRef; +} + +impl GGLWEKeyCompressedEncryptSk for Module +where + Module: GGLWECompressedEncryptSk + + SvpPPolAllocBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftAllocBytes + + VecZnxSwitchRing + + SvpPrepare, + Scratch: ScratchAvailable + TakeScalarZnx + TakeGLWESecretPrepared, +{ + fn gglwe_key_compressed_encrypt_sk( + &self, + res: &mut R, + sk_in: &SI, + sk_out: &SO, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWEKeyCompressedToMut, + SI: GLWESecretToRef, + SO: GLWESecretToRef, + { + let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); + let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); + #[cfg(debug_assertions)] { use crate::layouts::GGLWESwitchingKey; - assert!(sk_in.n().0 <= module.n() as u32); - assert!(sk_out.n().0 <= module.n() as u32); + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), + scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GGLWESwitchingKey::encrypt_sk_scratch_space(self, res) ) } @@ -77,7 +106,7 @@ impl GGLWESwitchingKeyCompressed { let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into()); (0..sk_in.rank().into()).for_each(|i| { - module.vec_znx_switch_ring( + self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), @@ -89,20 +118,20 @@ impl GGLWESwitchingKeyCompressed { { let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); (0..sk_out.rank().into()).for_each(|i| { - module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); - module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); + self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); + self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); }); } - self.key.encrypt_sk( - module, + self.gglwe_compressed_encrypt_sk( + &mut res.key, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2, ); - self.sk_in_n = sk_in.n().into(); - self.sk_out_n = sk_out.n().into(); + res.sk_in_n = sk_in.n().into(); + res.sk_out_n = sk_out.n().into(); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 6a75a57..be78f3e 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,9 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, - TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDft, SvpPPolAllocBytes, SvpPrepare, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAllocBytes, VecZnxBigNormalize, + VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, @@ -11,8 +9,10 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, + encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, compressed::GGLWETensorKeyCompressed, + GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, + compressed::{GGLWETensorKeyCompressed, GGLWETensorKeyCompressedToMut}, prepared::Prepare, }, }; @@ -28,59 +28,59 @@ impl GGLWETensorKeyCompressed> { } } -impl GGLWETensorKeyCompressed { - pub fn encrypt_sk( - &mut self, - module: &Module, - sk: &GLWESecret, +pub trait GGLWETensorKeyCompressedEncryptSk { + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + SvpPrepare - + SvpPPolAllocBytes - + SvpPPolAlloc, - Scratch: ScratchAvailable - + TakeScalarZnx - + TakeVecZnxDft - + TakeGLWESecretPrepared - + ScratchAvailable - + TakeVecZnx - + TakeVecZnxBig, + R: GGLWETensorKeyCompressedToMut, + S: GLWESecretToRef; +} + +impl GGLWETensorKeyCompressedEncryptSk for Module +where + Module: GGLWEKeyCompressedEncryptSk + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + + SvpPrepare, + Scratch: TakeGLWESecretPrepared + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecret, +{ + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWETensorKeyCompressedToMut, + S: GLWESecretToRef, { + let res: &mut GGLWETensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.rank_out(), sk.rank()); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.rank_out(), sk.rank()); + assert_eq!(res.n(), sk.n()); } let n: usize = sk.n().into(); - let rank: usize = self.rank_out().into(); + let rank: usize = res.rank_out().into(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), self.rank_out()); - sk_dft_prep.prepare(module, sk, scratch_1); + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), res.rank_out()); + sk_dft_prep.prepare(self, sk, scratch_1); let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1); for i in 0..rank { - module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); + self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); } let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1); @@ -91,14 +91,14 @@ impl GGLWETensorKeyCompressed { for i in 0..rank { for j in i..rank { - module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); - module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); - module.vec_znx_big_normalize( - self.base2k().into(), + self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); + self.vec_znx_big_normalize( + res.base2k().into(), &mut sk_ij.data.as_vec_znx_mut(), 0, - self.base2k().into(), + res.base2k().into(), &sk_ij_big, 0, scratch_5, @@ -106,9 +106,30 @@ impl GGLWETensorKeyCompressed { let (seed_xa_tmp, _) = source_xa.branch(); - self.at_mut(i, j) - .encrypt_sk(module, &sk_ij, sk, seed_xa_tmp, source_xe, scratch_5); + self.gglwe_key_compressed_encrypt_sk( + res.at_mut(i, j), + &sk_ij, + sk, + seed_xa_tmp, + source_xe, + scratch_5, + ); } } } } + +impl GGLWETensorKeyCompressed { + pub fn encrypt_sk( + &mut self, + module: &Module, + sk: &GLWESecret, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + Module: GGLWETensorKeyCompressedEncryptSk, + { + module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + } +} diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index e49f246..bdad6be 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,18 +1,16 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + api::{VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ TakeGLWEPt, - encryption::{SIGMA, glwe_encrypt_sk_internal}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared, + GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, + compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -26,6 +24,95 @@ impl GGSWCiphertextCompressed> { } } +pub trait GGSWCompressedEncryptSk { + fn ggsw_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCiphertextCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGSWCompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: TakeGLWEPt, +{ + fn ggsw_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCiphertextCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGSWCiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + } + + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); + + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + + let mut source = Source::new(seed_xa); + + res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols]; + + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); + + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + + for col_j in 0..rank + 1 { + // rlwe encrypt of vec_znx_pt into vec_znx_ct + + let (seed, mut source_xa_tmp) = source.branch(); + + res.seed[row_i * cols + col_j] = seed; + + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(row_i, col_j).data, + cols, + true, + Some((&tmp_pt, col_j)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scratch_1, + ); + } + } + } +} + impl GGSWCiphertextCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( @@ -37,71 +124,8 @@ impl GGSWCiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGSWCompressedEncryptSk, { - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(self.rank(), sk.rank()); - assert_eq!(self.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = self.base2k().into(); - let rank: usize = self.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = self.dsize().into(); - - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&self.glwe_layout()); - - let mut source = Source::new(seed_xa); - - self.seed = vec![[0u8; 32]; self.dnum().0 as usize * cols]; - - (0..self.dnum().into()).for_each(|row_i| { - tmp_pt.data.zero(); - - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - - (0..rank + 1).for_each(|col_j| { - // rlwe encrypt of vec_znx_pt into vec_znx_ct - - let (seed, mut source_xa_tmp) = source.branch(); - - self.seed[row_i * cols + col_j] = seed; - - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.at_mut(row_i, col_j).data, - cols, - true, - Some((&tmp_pt, col_j)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scratch_1, - ); - }); - }); + module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 6d45b37..9065c2e 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -5,20 +5,23 @@ use poulpy_hal::{ VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos}, + layouts::{ + GGLWEAutomorphismKey, GGLWEAutomorphismKeyToMut, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, GLWESecretToRef, + LWEInfos, + }, }; impl GGLWEAutomorphismKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { assert_eq!( infos.rank_in(), @@ -28,7 +31,7 @@ impl GGLWEAutomorphismKey> { GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) } - pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { @@ -41,58 +44,98 @@ impl GGLWEAutomorphismKey> { } } -impl GGLWEAutomorphismKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, +pub trait GGLWEAutomorphismKeyEncryptSk { + fn gglwe_automorphism_key_encrypt_sk( + &self, + res: &mut A, p: i64, - sk: &GLWESecret, + sk: &B, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolAllocBytes - + VecZnxAutomorphism, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + A: GGLWEAutomorphismKeyToMut, + B: GLWESecretToRef; +} + +impl GGLWEAutomorphismKey +where + Self: GGLWEAutomorphismKeyToMut, +{ + pub fn encrypt_sk( + &mut self, + module: &Module, + p: i64, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretToRef, + Module: GGLWEAutomorphismKeyEncryptSk, { + module.gglwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + } +} + +impl GGLWEAutomorphismKeyEncryptSk for Module +where + Module: VecZnxAddScalarInplace + + VecZnxDftAllocBytes + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalizeTmpBytes + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + SvpPPolAllocBytes + + VecZnxAutomorphism, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, +{ + fn gglwe_automorphism_key_encrypt_sk( + &self, + res: &mut A, + p: i64, + sk: &B, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + A: GGLWEAutomorphismKeyToMut, + B: GLWESecretToRef, + { + let res: &mut GGLWEAutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + #[cfg(debug_assertions)] { use crate::layouts::{GLWEInfos, LWEInfos}; - assert_eq!(self.n(), sk.n()); - assert_eq!(self.rank_out(), self.rank_in()); - assert_eq!(sk.rank(), self.rank_out()); + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self), + scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", scratch.available(), - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self) + GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res) ) } let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); { - (0..self.rank_out().into()).for_each(|i| { - module.vec_znx_automorphism( - module.galois_element_inv(p), + (0..res.rank_out().into()).for_each(|i| { + self.vec_znx_automorphism( + self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), i, &sk.data.as_vec_znx(), @@ -101,9 +144,9 @@ impl GGLWEAutomorphismKey { }); } - self.key - .encrypt_sk(module, sk, &sk_out, source_xa, source_xe, scratch_1); + res.key + .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); - self.p = p; + res.p = p; } } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 51054cb..3928cf4 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,16 +1,19 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ TakeGLWEPt, - layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}, + encryption::glwe_ct::GLWEEncryptSk, + layouts::{ + GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; impl GGLWECiphertext> { @@ -31,78 +34,89 @@ impl GGLWECiphertext> { } } -impl GGLWECiphertext { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, +pub trait GGLWEEncryptSk { + fn gglwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + R: GGLWECiphertextToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGLWEEncryptSk for Module +where + Module: + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +{ + fn gglwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWECiphertextToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, { + let res: &mut GGLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + #[cfg(debug_assertions)] { use poulpy_hal::layouts::ZnxInfos; + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); assert_eq!( - self.rank_in(), + res.rank_in(), pt.cols() as u32, - "self.rank_in(): {} != pt.cols(): {}", - self.rank_in(), + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), pt.cols() ); assert_eq!( - self.rank_out(), + res.rank_out(), sk.rank(), - "self.rank_out(): {} != sk.rank(): {}", - self.rank_out(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), sk.rank() ); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(module, self), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", + scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}", scratch.available(), - self.rank_out(), - self.size(), - GGLWECiphertext::encrypt_sk_scratch_space(module, self) + res.rank_out(), + res.size(), + GGLWECiphertext::encrypt_sk_scratch_space(self, res) ); assert!( - self.dnum().0 * self.dsize().0 * self.base2k().0 <= self.k().0, - "self.dnum() : {} * self.dsize() : {} * self.base2k() : {} = {} >= self.k() = {}", - self.dnum(), - self.dsize(), - self.base2k(), - self.dnum().0 * self.dsize().0 * self.base2k().0, - self.k() + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() ); } - let dnum: usize = self.dnum().into(); - let dsize: usize = self.dsize().into(); - let base2k: usize = self.base2k().into(); - let rank_in: usize = self.rank_in().into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + 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); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res); // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns // // Example for ksk rank 2 to rank 3: @@ -114,17 +128,39 @@ impl GGLWECiphertext { // // (-(a*s) + s0, a) // (-(b*s) + s1, b) - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|row_i| { + + for col_i in 0..rank_in { + for row_i in 0..dnum { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt tmp_pt.data.zero(); // zeroes for next iteration - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, col_i); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); - - // rlwe encrypt of vec_znx_pt into vec_znx_ct - self.at_mut(row_i, col_i) - .encrypt_sk(module, &tmp_pt, sk, source_xa, source_xe, scrach_1); - }); - }); + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + self.glwe_encrypt_sk( + &mut res.at_mut(row_i, col_i), + &tmp_pt, + sk, + source_xa, + source_xe, + scrach_1, + ); + } + } + } +} + +impl GGLWECiphertext { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: &ScalarZnx, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + Module: GGLWEEncryptSk, + { + module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 6195458..d9e35bd 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,16 +1,16 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, VecZnx, ZnxZero}, + api::{VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, - layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESecretPrepared}, + SIGMA, TakeGLWEPt, + encryption::glwe_ct::GLWEEncryptSkInternal, + layouts::{ + GGSWCiphertext, GGSWCiphertextToMut, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; impl GGSWCiphertext> { @@ -27,6 +27,85 @@ impl GGSWCiphertext> { } } +pub trait GGSWEncryptSk { + fn ggsw_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCiphertextToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGSWEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: TakeGLWEPt, +{ + fn ggsw_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCiphertextToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGSWCiphertext<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(pt.n(), self.n()); + assert_eq!(sk.n(), self.n() as u32); + } + + let k: usize = res.k().into(); + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let dsize: usize = res.dsize().into(); + let cols: usize = (rank + 1).into(); + + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + for col_j in 0..rank + 1 { + self.glwe_encrypt_sk_internal( + base2k, + k, + res.at_mut(row_i, col_j).data_mut(), + cols, + false, + Some((&tmp_pt, col_j)), + sk, + source_xa, + source_xe, + SIGMA, + scratch_1, + ); + } + } + } +} + impl GGSWCiphertext { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( @@ -38,56 +117,8 @@ impl GGSWCiphertext { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGSWEncryptSk, { - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(self.rank(), sk.rank()); - assert_eq!(self.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = self.base2k().into(); - let rank: usize = self.rank().into(); - let dsize: usize = self.dsize().into(); - - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&self.glwe_layout()); - - (0..self.dnum().into()).for_each(|row_i| { - tmp_pt.data.zero(); - - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - - (0..rank + 1).for_each(|col_j| { - // rlwe encrypt of vec_znx_pt into vec_znx_ct - - self.at_mut(row_i, col_j).encrypt_sk_internal( - module, - Some((&tmp_pt, col_j)), - sk, - source_xa, - source_xe, - scratch_1, - ); - }); - }); + module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 8ecacc6..d05ffc6 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, ZnxInfos, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, }; @@ -13,8 +13,8 @@ use crate::{ dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, - prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, + GLWECiphertext, GLWECiphertextToMut, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -44,126 +44,127 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( +impl GLWECiphertext { + pub fn encrypt_sk( &mut self, module: &Module, - pt: &GLWEPlaintext, - sk: &GLWESecretPrepared, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + Module: GLWEEncryptSk, { + module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); + } + + pub fn encrypt_zero_sk( + &mut self, + module: &Module, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretPreparedToRef, + Module: GLWEEncryptZeroSk, + { + module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); + } + + pub fn encrypt_pk( + &mut self, + module: &Module, + pt: &P, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptPk, + { + module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); + } + + pub fn encrypt_zero_pk( + &mut self, + module: &Module, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptZeroPk, + { + module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); + } +} + +pub trait GLWEEncryptSk { + fn glwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Scratch: ScratchAvailable, +{ + fn glwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + { + let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk.rank()); - assert_eq!(sk.n(), self.n()); - assert_eq!(pt.n(), self.n()); + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert_eq!(pt.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self), + scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self) + GLWECiphertext::encrypt_sk_scratch_space(self, &res) ) } - self.encrypt_sk_internal(module, Some((pt, 0)), sk, source_xa, source_xe, scratch); - } - - pub fn encrypt_zero_sk( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), sk.rank()); - assert_eq!(sk.n(), self.n()); - assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", - scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self) - ) - } - self.encrypt_sk_internal( - module, - None::<(&GLWEPlaintext>, usize)>, - sk, - source_xa, - source_xe, - scratch, - ); - } - - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_sk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - let cols: usize = (self.rank() + 1).into(); - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.data, + let cols: usize = (res.rank() + 1).into(); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + res.data_mut(), cols, false, - pt, + Some((&pt, 0)), sk, source_xa, source_xe, @@ -171,46 +172,136 @@ impl GLWECiphertext { scratch, ); } +} - #[allow(clippy::too_many_arguments)] - pub fn encrypt_pk( - &mut self, - module: &Module, - pt: &GLWEPlaintext, - pk: &GLWEPublicKeyPrepared, - source_xu: &mut Source, +pub trait GLWEEncryptZeroSk { + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWECiphertextToMut, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptZeroSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Scratch: ScratchAvailable, +{ + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + S: GLWESecretPreparedToRef, { - self.encrypt_pk_internal::(module, Some((pt, 0)), pk, source_xu, source_xe, scratch); + let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + + #[cfg(debug_assertions)] + { + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert!( + scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available(), + GLWECiphertext::encrypt_sk_scratch_space(self, &res) + ) + } + + let cols: usize = (res.rank() + 1).into(); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + res.data_mut(), + cols, + false, + None::<(&GLWEPlaintext>, usize)>, + sk, + source_xa, + source_xe, + SIGMA, + scratch, + ); } +} - pub fn encrypt_zero_pk( - &mut self, - module: &Module, - pk: &GLWEPublicKeyPrepared, +pub trait GLWEEncryptPk { + fn glwe_encrypt_pk( + &self, + res: &mut R, + pt: &P, + pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptPk for Module +where + Module: GLWEEncryptPkInternal, +{ + fn glwe_encrypt_pk( + &self, + res: &mut R, + pt: &P, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, { - self.encrypt_pk_internal::, DataPk, B>( - module, + self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); + } +} + +pub trait GLWEEncryptZeroPk { + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptZeroPk for Module +where + Module: GLWEEncryptPkInternal, +{ + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + K: GLWEPublicKeyPreparedToRef, + { + self.glwe_encrypt_pk_internal( + res, None::<(&GLWEPlaintext>, usize)>, pk, source_xu, @@ -218,45 +309,69 @@ impl GLWECiphertext { scratch, ); } +} - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_pk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - pk: &GLWEPublicKeyPrepared, +pub(crate) trait GLWEEncryptPkInternal { + fn glwe_encrypt_pk_internal( + &self, + res: &mut R, + pt: Option<(&P, usize)>, + pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptPkInternal for Module +where + Module: SvpPrepare + + SvpApplyDftToDft + + VecZnxIdftApplyConsume + + VecZnxBigAddNormal + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize, + Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, +{ + fn glwe_encrypt_pk_internal( + &self, + res: &mut R, + pt: Option<(&P, usize)>, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, { + let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.base2k(), pk.base2k()); - assert_eq!(self.n(), pk.n()); - assert_eq!(self.rank(), pk.rank()); + assert_eq!(res.base2k(), pk.base2k()); + assert_eq!(res.n(), pk.n()); + assert_eq!(res.rank(), pk.rank()); if let Some((pt, _)) = pt { - assert_eq!(pt.base2k(), pk.base2k()); - assert_eq!(pt.n(), pk.n()); + assert_eq!(pt.to_ref().base2k(), pk.base2k()); + assert_eq!(pt.to_ref().n(), pk.n()); } } let base2k: usize = pk.base2k().into(); let size_pk: usize = pk.size(); - let cols: usize = (self.rank() + 1).into(); + let cols: usize = (res.rank() + 1).into(); // Generates u according to the underlying secret distribution. - let (mut u_dft, scratch_1) = scratch.take_svp_ppol(self.n().into(), 1); + let (mut u_dft, scratch_1) = scratch.take_svp_ppol(res.n().into(), 1); { - let (mut u, _) = scratch_1.take_scalar_znx(self.n().into(), 1); + let (mut u, _) = scratch_1.take_scalar_znx(res.n().into(), 1); match pk.dist { Distribution::NONE => panic!( "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \ @@ -270,20 +385,20 @@ impl GLWECiphertext { Distribution::ZERO => {} } - module.svp_prepare(&mut u_dft, 0, &u, 0); + 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.n().into(), 1, size_pk); + let (mut ci_dft, scratch_2) = scratch_1.take_vec_znx_dft(res.n().into(), 1, size_pk); // ci_dft = DFT(u) * DFT(pk[i]) - module.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); + self.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); // ci_big = u * p[i] - let mut ci_big = module.vec_znx_idft_apply_consume(ci_dft); + let mut ci_big = self.vec_znx_idft_apply_consume(ci_dft); // ci_big = u * pk[i] + e - module.vec_znx_big_add_normal( + self.vec_znx_big_add_normal( base2k, &mut ci_big, 0, @@ -297,30 +412,37 @@ impl GLWECiphertext { if let Some((pt, col)) = pt && col == i { - module.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.data, 0); + self.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.to_ref().data, 0); } // ct[i] = norm(ci_big) - module.vec_znx_big_normalize(base2k, &mut self.data, i, base2k, &ci_big, 0, scratch_2); + self.vec_znx_big_normalize(base2k, &mut res.data, i, base2k, &ci_big, 0, scratch_2); }); } } -#[allow(clippy::too_many_arguments)] -pub(crate) fn glwe_encrypt_sk_internal( - module: &Module, - base2k: usize, - k: usize, - ct: &mut VecZnx, - cols: usize, - compressed: bool, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, -) where +pub(crate) trait GLWEEncryptSkInternal { + fn glwe_encrypt_sk_internal( + &self, + base2k: usize, + k: usize, + res: &mut R, + cols: usize, + compressed: bool, + pt: Option<(&P, usize)>, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + R: VecZnxToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptSkInternal for Module +where Module: VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftApply @@ -336,72 +458,94 @@ pub(crate) fn glwe_encrypt_sk_internal: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { - #[cfg(debug_assertions)] + fn glwe_encrypt_sk_internal( + &self, + base2k: usize, + k: usize, + res: &mut R, + cols: usize, + compressed: bool, + pt: Option<(&P, usize)>, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + R: VecZnxToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, { - if compressed { - assert_eq!( - ct.cols(), - 1, - "invalid ciphertext: compressed tag=true but #cols={} != 1", - ct.cols() - ) - } - } + let ct: &mut VecZnx<&mut [u8]> = &mut res.to_mut(); + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); - let size: usize = ct.size(); - - let (mut c0, scratch_1) = scratch.take_vec_znx(ct.n(), 1, size); - c0.zero(); - - { - let (mut ci, scratch_2) = scratch_1.take_vec_znx(ct.n(), 1, size); - - // ct[i] = uniform - // ct[0] -= c[i] * s[i], - (1..cols).for_each(|i| { - let col_ct: usize = if compressed { 0 } else { i }; - - // ct[i] = uniform (+ pt) - module.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); - - let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(ct.n(), 1, size); - - // ci = ct[i] - pt - // i.e. we act as we sample ct[i] already as uniform + pt - // and if there is a pt, then we subtract it before applying DFT - if let Some((pt, col)) = pt { - if i == col { - module.vec_znx_sub(&mut ci, 0, ct, col_ct, &pt.data, 0); - module.vec_znx_normalize_inplace(base2k, &mut ci, 0, scratch_3); - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &ci, 0); - } else { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); - } - } else { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + #[cfg(debug_assertions)] + { + if compressed { + assert_eq!( + ct.cols(), + 1, + "invalid ciphertext: compressed tag=true but #cols={} != 1", + ct.cols() + ) } + } - module.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); - let ci_big: VecZnxBig<&mut [u8], B> = module.vec_znx_idft_apply_consume(ci_dft); + let size: usize = ct.size(); - // use c[0] as buffer, which is overwritten later by the normalization step - module.vec_znx_big_normalize(base2k, &mut ci, 0, base2k, &ci_big, 0, scratch_3); + let (mut c0, scratch_1) = scratch.take_vec_znx(ct.n(), 1, size); + c0.zero(); - // c0_tmp = -c[i] * s[i] (use c[0] as buffer) - module.vec_znx_sub_inplace(&mut c0, 0, &ci, 0); - }); + { + let (mut ci, scratch_2) = scratch_1.take_vec_znx(ct.n(), 1, size); + + // ct[i] = uniform + // ct[0] -= c[i] * s[i], + (1..cols).for_each(|i| { + let col_ct: usize = if compressed { 0 } else { i }; + + // ct[i] = uniform (+ pt) + self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); + + let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(ct.n(), 1, size); + + // ci = ct[i] - pt + // i.e. we act as we sample ct[i] already as uniform + pt + // and if there is a pt, then we subtract it before applying DFT + if let Some((pt, col)) = pt { + if i == col { + self.vec_znx_sub(&mut ci, 0, ct, col_ct, &pt.to_ref().data, 0); + self.vec_znx_normalize_inplace(base2k, &mut ci, 0, scratch_3); + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &ci, 0); + } else { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + } + } else { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + } + + self.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); + let ci_big: VecZnxBig<&mut [u8], B> = self.vec_znx_idft_apply_consume(ci_dft); + + // use c[0] as buffer, which is overwritten later by the normalization step + self.vec_znx_big_normalize(base2k, &mut ci, 0, base2k, &ci_big, 0, scratch_3); + + // c0_tmp = -c[i] * s[i] (use c[0] as buffer) + self.vec_znx_sub_inplace(&mut c0, 0, &ci, 0); + }); + } + + // c[0] += e + self.vec_znx_add_normal(base2k, &mut c0, 0, k, source_xe, sigma, SIGMA_BOUND); + + // c[0] += m if col = 0 + if let Some((pt, col)) = pt + && col == 0 + { + self.vec_znx_add_inplace(&mut c0, 0, &pt.to_ref().data, 0); + } + + // c[0] = norm(c[0]) + self.vec_znx_normalize(base2k, ct, 0, base2k, &c0, 0, scratch_1); } - - // c[0] += e - module.vec_znx_add_normal(base2k, &mut c0, 0, k, source_xe, sigma, SIGMA_BOUND); - - // c[0] += m if col = 0 - if let Some((pt, col)) = pt - && col == 0 - { - module.vec_znx_add_inplace(&mut c0, 0, &pt.data, 0); - } - - // c[0] = norm(c[0]) - module.vec_znx_normalize(base2k, ct, 0, base2k, &c0, 0, scratch_1); } diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index c7cdaeb..c0de69b 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,50 +1,43 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, - oep::{ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxDftImpl, TakeVecZnxImpl}, source::Source, }; -use crate::layouts::{GLWECiphertext, GLWEPublicKey, prepared::GLWESecretPrepared}; +use crate::{ + encryption::glwe_ct::GLWEEncryptZeroSk, + layouts::{ + GLWECiphertext, GLWEPublicKey, GLWEPublicKeyToMut, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, +}; -impl GLWEPublicKey { - pub fn generate_from_sk( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module:, - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl, +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, + S: GLWESecretPreparedToRef; +} + +impl GLWEPublicKeyGenerate for Module +where + Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, +{ + fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + R: GLWEPublicKeyToMut, + S: GLWESecretPreparedToRef, { + let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + #[cfg(debug_assertions)] { use crate::{Distribution, layouts::LWEInfos}; - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); if sk.dist == Distribution::NONE { panic!("invalid sk: SecretDistribution::NONE") @@ -52,10 +45,25 @@ impl GLWEPublicKey { } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(self, res)); - let mut tmp: GLWECiphertext> = GLWECiphertext::alloc(self); - tmp.encrypt_zero_sk(module, sk, source_xa, source_xe, scratch.borrow()); - self.dist = sk.dist; + let mut tmp: GLWECiphertext> = GLWECiphertext::alloc(res); + + tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); + res.dist = sk.dist; + } +} + +impl GLWEPublicKey { + pub fn generate( + &mut self, + module: &Module, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + ) where + Module: GLWEPublicKeyGenerate, + { + module.glwe_public_key_generate(self, sk, source_xa, source_xe); } } diff --git a/poulpy-core/src/encryption/mod.rs b/poulpy-core/src/encryption/mod.rs index 9380933..fb9a459 100644 --- a/poulpy-core/src/encryption/mod.rs +++ b/poulpy-core/src/encryption/mod.rs @@ -11,7 +11,5 @@ mod lwe_ct; mod lwe_ksk; mod lwe_to_glwe_ksk; -pub(crate) use glwe_ct::glwe_encrypt_sk_internal; - pub const SIGMA: f64 = 3.2; pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA; diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 2a10765..2b6a54d 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -6,14 +6,14 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GGLWEAutomorphismKeyCompressed { - pub(crate) key: GGLWESwitchingKeyCompressed, + pub(crate) key: GGLWEKeyCompressed, pub(crate) p: i64, } @@ -83,14 +83,14 @@ impl GGLWEAutomorphismKeyCompressed> { { debug_assert_eq!(infos.rank_in(), infos.rank_out()); Self { - key: GGLWESwitchingKeyCompressed::alloc(infos), + key: GGLWEKeyCompressed::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { Self { - key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), + key: GGLWEKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -100,11 +100,11 @@ impl GGLWEAutomorphismKeyCompressed> { A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) + GGLWEKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) } } @@ -131,3 +131,35 @@ where self.p = other.p; } } + +pub trait GGLWEAutomorphismKeyCompressedToRef { + fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]>; +} + +impl GGLWEAutomorphismKeyCompressedToRef for GGLWEAutomorphismKeyCompressed +where + GGLWEKeyCompressed: GGLWEKeyCompressedToRef, +{ + fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]> { + GGLWEAutomorphismKeyCompressed { + key: self.key.to_ref(), + p: self.p, + } + } +} + +pub trait GGLWEAutomorphismKeyCompressedToMut { + fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]>; +} + +impl GGLWEAutomorphismKeyCompressedToMut for GGLWEAutomorphismKeyCompressed +where + GGLWEKeyCompressed: GGLWEKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]> { + GGLWEAutomorphismKeyCompressed { + p: self.p, + key: self.key.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index f7a4df9..6e14756 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -1,6 +1,8 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; @@ -289,3 +291,37 @@ where }); } } + +pub trait GGLWECiphertextCompressedToMut { + fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]>; +} + +impl GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed { + fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> { + GGLWECiphertextCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + seed: self.seed.clone(), + rank_out: self.rank_out, + data: self.data.to_mut(), + } + } +} + +pub trait GGLWECiphertextCompressedToRef { + fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; +} + +impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { + fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { + GGLWECiphertextCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + seed: self.seed.clone(), + rank_out: self.rank_out, + data: self.data.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 60d9316..cbb2f8c 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -6,19 +6,19 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWECiphertextCompressed}, + compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWESwitchingKeyCompressed { +pub struct GGLWEKeyCompressed { pub(crate) key: GGLWECiphertextCompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKeyCompressed { +impl LWEInfos for GGLWEKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -35,13 +35,13 @@ impl LWEInfos for GGLWESwitchingKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWESwitchingKeyCompressed { +impl GLWEInfos for GGLWEKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyCompressed { +impl GGLWEInfos for GGLWEKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -59,19 +59,19 @@ impl GGLWEInfos for GGLWESwitchingKeyCompressed { } } -impl fmt::Debug for GGLWESwitchingKeyCompressed { +impl fmt::Debug for GGLWEKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWESwitchingKeyCompressed { +impl FillUniform for GGLWEKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWESwitchingKeyCompressed { +impl fmt::Display for GGLWEKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -81,12 +81,12 @@ impl fmt::Display for GGLWESwitchingKeyCompressed { } } -impl GGLWESwitchingKeyCompressed> { +impl GGLWEKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { - GGLWESwitchingKeyCompressed { + GGLWEKeyCompressed { key: GGLWECiphertextCompressed::alloc(infos), sk_in_n: 0, sk_out_n: 0, @@ -102,7 +102,7 @@ impl GGLWESwitchingKeyCompressed> { dnum: Dnum, dsize: Dsize, ) -> Self { - GGLWESwitchingKeyCompressed { + GGLWEKeyCompressed { key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, @@ -121,7 +121,7 @@ impl GGLWESwitchingKeyCompressed> { } } -impl ReaderFrom for GGLWESwitchingKeyCompressed { +impl ReaderFrom for GGLWEKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -129,7 +129,7 @@ impl ReaderFrom for GGLWESwitchingKeyCompressed { } } -impl WriterTo for GGLWESwitchingKeyCompressed { +impl WriterTo for GGLWEKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; @@ -137,13 +137,47 @@ impl WriterTo for GGLWESwitchingKeyCompressed { } } -impl Decompress> for GGLWESwitchingKey +impl Decompress> for GGLWESwitchingKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWESwitchingKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &GGLWEKeyCompressed) { self.key.decompress(module, &other.key); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } } + +pub trait GGLWEKeyCompressedToMut { + fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]>; +} + +impl GGLWEKeyCompressedToMut for GGLWEKeyCompressed +where + GGLWECiphertextCompressed: GGLWECiphertextCompressedToMut, +{ + fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]> { + GGLWEKeyCompressed { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_mut(), + } + } +} + +pub trait GGLWEKeyCompressedToRef { + fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]>; +} + +impl GGLWEKeyCompressedToRef for GGLWEKeyCompressed +where + GGLWECiphertextCompressed: GGLWECiphertextCompressedToRef, +{ + fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]> { + GGLWEKeyCompressed { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index fef4647..0206788 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -6,14 +6,14 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GGLWETensorKeyCompressed { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for GGLWETensorKeyCompressed { @@ -66,7 +66,7 @@ impl FillUniform for GGLWETensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GGLWEKeyCompressed| key.fill_uniform(log_bound, source)) } } @@ -101,10 +101,10 @@ impl GGLWETensorKeyCompressed> { } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); + let mut keys: Vec>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyCompressed::alloc_with( + keys.push(GGLWEKeyCompressed::alloc_with( n, base2k, k, @@ -129,7 +129,7 @@ impl GGLWETensorKeyCompressed> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWESwitchingKeyCompressed::alloc_bytes_with( + * GGLWEKeyCompressed::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), @@ -141,7 +141,7 @@ impl GGLWETensorKeyCompressed> { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) + pairs * GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) } } @@ -172,7 +172,7 @@ impl WriterTo for GGLWETensorKeyCompressed { } impl GGLWETensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyCompressed { + pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEKeyCompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -205,3 +205,33 @@ where }); } } + +pub trait GGLWETensorKeyCompressedToMut { + fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]>; +} + +impl GGLWETensorKeyCompressedToMut for GGLWETensorKeyCompressed +where + GGLWEKeyCompressed: GGLWEKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]> { + GGLWETensorKeyCompressed { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} + +pub trait GGLWETensorKeyCompressedToRef { + fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]>; +} + +impl GGLWETensorKeyCompressedToRef for GGLWETensorKeyCompressed +where + GGLWEKeyCompressed: GGLWEKeyCompressedToRef, +{ + fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]> { + GGLWETensorKeyCompressed { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } + } +} diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index f0a62cc..b751bfc 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -1,6 +1,8 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; @@ -235,3 +237,37 @@ where }); } } + +pub trait GGSWCiphertextCompressedToMut { + fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]>; +} + +impl GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed { + fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> { + GGSWCiphertextCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + rank: self.rank(), + seed: self.seed.clone(), + data: self.data.to_mut(), + } + } +} + +pub trait GGSWCiphertextCompressedToRef { + fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>; +} + +impl GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed { + fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> { + GGSWCiphertextCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + rank: self.rank(), + seed: self.seed.clone(), + data: self.data.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 63933e8..317e0e5 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWESwitchingKeyCompressed, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWEKeyCompressed, }; #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); impl LWEInfos for GLWEToLWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -98,11 +98,11 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + Self(GGLWEKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( + Self(GGLWEKeyCompressed::alloc_with( n, base2k, k, @@ -127,10 +127,10 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) + GGLWEKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) + GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 480707b..9d45d0a 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + compressed::{Decompress, GGLWEKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct LWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); impl LWEInfos for LWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -103,11 +103,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + Self(GGLWEKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( + Self(GGLWEKeyCompressed::alloc_with( n, base2k, k, @@ -137,11 +137,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) + GGLWEKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 86c353b..bcc69a4 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + compressed::{Decompress, GGLWEKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { fn n(&self) -> Degree { @@ -98,11 +98,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + Self(GGLWEKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( + Self(GGLWEKeyCompressed::alloc_with( n, base2k, k, @@ -127,11 +127,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) + GGLWEKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 5c786d2..a88db86 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,7 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GGLWESwitchingKeyToMut, GLWECiphertext, GLWEInfos, LWEInfos, + Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -170,6 +171,26 @@ impl GGLWEAutomorphismKey> { } } +pub trait GGLWEAutomorphismKeyToMut { + fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]>; +} + +impl GGLWEAutomorphismKeyToMut for GGLWEAutomorphismKey +where + GGLWESwitchingKey: GGLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]> { + GGLWEAutomorphismKey { + key: self.key.to_mut(), + p: self.p, + } + } +} + +pub trait GGLWEAutomorphismKeyToRef { + fn to_ref(&self) -> GGLWEAutomorphismKey<&[u8]>; +} + impl GGLWEAutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index ca8236c..713df90 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; @@ -389,6 +389,36 @@ impl GGLWECiphertext> { } } +pub trait GGLWECiphertextToMut { + fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]>; +} + +impl GGLWECiphertextToMut for GGLWECiphertext { + fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]> { + GGLWECiphertext { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + data: self.data.to_mut(), + } + } +} + +pub trait GGLWECiphertextToRef { + fn to_ref(&self) -> GGLWECiphertext<&[u8]>; +} + +impl GGLWECiphertextToRef for GGLWECiphertext { + fn to_ref(&self) -> GGLWECiphertext<&[u8]> { + GGLWECiphertext { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + data: self.data.to_ref(), + } + } +} + impl ReaderFrom for GGLWECiphertext { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 31a483b..64b1694 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -4,7 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, + TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -180,6 +181,23 @@ impl GGLWESwitchingKey> { } } +pub trait GGLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]>; +} + +impl GGLWESwitchingKeyToMut for GGLWESwitchingKey +where + GGLWECiphertext: GGLWECiphertextToMut, +{ + fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]> { + GGLWESwitchingKey { + key: self.key.to_mut(), + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + } + } +} + impl GGLWESwitchingKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index f1bb228..dc64bce 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; use std::fmt; @@ -370,3 +370,35 @@ impl WriterTo for GGSWCiphertext { self.data.write_to(writer) } } + +pub trait GGSWCiphertextToMut { + fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]>; +} + +impl GGSWCiphertextToMut for GGSWCiphertext { + fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]> { + GGSWCiphertext::builder() + .base2k(self.base2k()) + .dsize(self.dsize()) + .k(self.k()) + .data(self.data.to_mut()) + .build() + .unwrap() + } +} + +pub trait GGSWCiphertextToRef { + fn to_ref(&self) -> GGSWCiphertext<&[u8]>; +} + +impl GGSWCiphertextToRef for GGSWCiphertext { + fn to_ref(&self) -> GGSWCiphertext<&[u8]> { + GGSWCiphertext::builder() + .base2k(self.base2k()) + .dsize(self.dsize()) + .k(self.k()) + .data(self.data.to_ref()) + .build() + .unwrap() + } +} diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index fc4b0fa..6e947f1 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -1,4 +1,4 @@ -use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; use crate::{ dist::Distribution, @@ -207,3 +207,33 @@ impl WriterTo for GLWEPublicKey { self.data.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, + } + } +} + +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(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index b565055..c32f229 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -200,3 +200,31 @@ impl GLWECiphertextToMut for GLWEPlaintext { .unwrap() } } + +pub trait GLWEPlaintextToRef { + fn to_ref(&self) -> GLWEPlaintext<&[u8]>; +} + +impl GLWEPlaintextToRef for GLWEPlaintext { + fn to_ref(&self) -> GLWEPlaintext<&[u8]> { + GLWEPlaintext { + data: self.data.to_ref(), + base2k: self.base2k, + k: self.k, + } + } +} + +pub trait GLWEPlaintextToMut { + fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>; +} + +impl GLWEPlaintextToMut for GLWEPlaintext { + fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> { + GLWEPlaintext { + base2k: self.base2k, + k: self.k, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 8870d35..e954c3b 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, WriterTo, ZnxInfos, ZnxZero}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero}, source::Source, }; @@ -136,6 +136,32 @@ impl GLWESecret { } } +pub trait GLWESecretToMut { + fn to_mut(&mut self) -> GLWESecret<&mut [u8]>; +} + +impl GLWESecretToMut for GLWESecret { + fn to_mut(&mut self) -> GLWESecret<&mut [u8]> { + GLWESecret { + dist: self.dist, + data: self.data.to_mut(), + } + } +} + +pub trait GLWESecretToRef { + fn to_ref(&self) -> GLWESecret<&[u8]>; +} + +impl GLWESecretToRef for GLWESecret { + fn to_ref(&self) -> GLWESecret<&[u8]> { + GLWESecret { + data: self.data.to_ref(), + dist: self.dist, + } + } +} + impl ReaderFrom for GLWESecret { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { match Distribution::read_from(reader) { diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index eb79a5a..a3871f6 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, oep::VmpPMatAllocBytesImpl, }; @@ -295,6 +295,22 @@ where } } +pub trait GGSWCiphertextPreparedToMut { + fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B>; +} + +impl GGSWCiphertextPreparedToMut for GGSWCiphertextPrepared { + fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B> { + GGSWCiphertextPrepared::builder() + .base2k(self.base2k()) + .dsize(self.dsize()) + .k(self.k()) + .data(self.data.to_mut()) + .build() + .unwrap() + } +} + pub trait GGSWCiphertextPreparedToRef { fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 6834f58..3211017 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, oep::VecZnxDftAllocBytesImpl, }; @@ -205,3 +205,33 @@ where self.dist = other.dist; } } + +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(), + } + } +} + +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, + } + } +} diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index d3f638b..234ea2a 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, }; use crate::{ @@ -113,3 +113,29 @@ where self.dist = other.dist } } + +pub trait GLWESecretPreparedToRef { + fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>; +} + +impl GLWESecretPreparedToRef for GLWESecretPrepared { + fn to_ref(&self) -> GLWESecretPrepared<&[u8], B> { + GLWESecretPrepared { + data: self.data.to_ref(), + dist: self.dist, + } + } +} + +pub trait GLWESecretPreparedToMut { + fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B>; +} + +impl GLWESecretPreparedToMut for GLWESecretPrepared { + fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B> { + GLWESecretPrepared { + dist: self.dist, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 8fe477c..6e08395 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -4,7 +4,7 @@ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GLWECiphertext, GLWEToLWEKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, compressed::{ - GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed, + GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWEKeyCompressed, GGLWETensorKeyCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, }, @@ -63,8 +63,7 @@ fn test_glwe_switching_key_serialization() { #[test] fn test_glwe_switching_key_compressed_serialization() { - let original: GGLWESwitchingKeyCompressed> = - GGLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } 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 60bb7e2..249161b 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -18,7 +18,7 @@ use crate::{ encryption::SIGMA, layouts::{ GGLWECiphertextLayout, GGLWESwitchingKey, GLWESecret, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + compressed::{Decompress, GGLWEKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -173,12 +173,12 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GGLWESwitchingKeyCompressed> = GGLWESwitchingKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc(&gglwe_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(GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEKeyCompressed::encrypt_sk_scratch_space( module, &gglwe_infos, )); 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 a1169f6..35938c3 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -363,7 +363,7 @@ where let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc(&glwe_infos); - pk.generate_from_sk(module, &sk_prepared, &mut source_xa, &mut source_xe); + pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); From cf377ff243a986abe095af6dd4d0cd8bb3abb79d Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 13 Oct 2025 12:14:11 +0200 Subject: [PATCH 02/60] wip --- .../benches/external_product_glwe_fft64.rs | 16 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 22 +-- poulpy-core/src/automorphism/gglwe_atk.rs | 14 +- poulpy-core/src/automorphism/ggsw_ct.rs | 24 +-- poulpy-core/src/automorphism/glwe_ct.rs | 18 +- .../src/encryption/compressed/gglwe_atk.rs | 19 +- .../src/encryption/compressed/gglwe_ct.rs | 4 +- .../src/encryption/compressed/gglwe_ksk.rs | 22 +-- .../src/encryption/compressed/gglwe_tsk.rs | 17 +- .../src/encryption/compressed/ggsw_ct.rs | 4 +- .../src/encryption/compressed/glwe_ct.rs | 127 +++++++------ poulpy-core/src/encryption/gglwe_atk.rs | 23 ++- poulpy-core/src/encryption/gglwe_ct.rs | 12 +- poulpy-core/src/encryption/gglwe_ksk.rs | 16 +- poulpy-core/src/encryption/gglwe_tsk.rs | 9 +- poulpy-core/src/encryption/ggsw_ct.rs | 12 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 10 +- poulpy-core/src/encryption/lwe_ksk.rs | 4 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 4 +- poulpy-core/src/external_product/gglwe_atk.rs | 16 +- poulpy-core/src/external_product/gglwe_ksk.rs | 12 +- poulpy-core/src/external_product/ggsw_ct.rs | 14 +- poulpy-core/src/external_product/glwe_ct.rs | 10 +- poulpy-core/src/glwe_packing.rs | 14 +- poulpy-core/src/glwe_trace.rs | 8 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 28 +-- poulpy-core/src/keyswitching/ggsw_ct.rs | 30 ++-- poulpy-core/src/keyswitching/glwe_ct.rs | 12 +- .../src/layouts/compressed/gglwe_atk.rs | 62 +++---- .../src/layouts/compressed/gglwe_ct.rs | 6 +- .../src/layouts/compressed/gglwe_ksk.rs | 50 +++--- .../src/layouts/compressed/gglwe_tsk.rs | 68 +++---- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 4 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 135 +++++++++++++- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 12 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 12 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 12 +- poulpy-core/src/layouts/gglwe_atk.rs | 80 +++++---- poulpy-core/src/layouts/gglwe_ct.rs | 56 +++--- poulpy-core/src/layouts/gglwe_ksk.rs | 77 ++++---- poulpy-core/src/layouts/gglwe_tsk.rs | 83 ++++++--- poulpy-core/src/layouts/ggsw_ct.rs | 72 ++++---- poulpy-core/src/layouts/glwe_ct.rs | 58 +++--- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 59 +++++-- poulpy-core/src/layouts/lwe_ct.rs | 38 ++-- poulpy-core/src/layouts/lwe_ksk.rs | 41 ++++- poulpy-core/src/layouts/lwe_sk.rs | 28 ++- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 41 ++++- poulpy-core/src/layouts/mod.rs | 1 + poulpy-core/src/layouts/prepared/gglwe_atk.rs | 131 +++++++++++--- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 155 ++++++++++++---- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 162 +++++++++++++---- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 167 ++++++++++++++---- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 155 +++++++++++----- poulpy-core/src/layouts/prepared/glwe_pk.rs | 5 +- poulpy-core/src/layouts/prepared/glwe_sk.rs | 9 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 24 +-- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 16 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 16 +- poulpy-core/src/layouts/prepared/mod.rs | 13 -- poulpy-core/src/noise/gglwe_ct.rs | 4 +- poulpy-core/src/noise/ggsw_ct.rs | 8 +- poulpy-core/src/scratch.rs | 78 ++++---- poulpy-core/src/tests/serialization.rs | 31 ++-- .../test_suite/automorphism/gglwe_atk.rs | 44 ++--- .../tests/test_suite/automorphism/ggsw_ct.rs | 54 +++--- .../tests/test_suite/automorphism/glwe_ct.rs | 23 ++- .../src/tests/test_suite/conversion.rs | 10 +- .../tests/test_suite/encryption/gglwe_atk.rs | 18 +- .../tests/test_suite/encryption/gglwe_ct.rs | 14 +- .../tests/test_suite/encryption/ggsw_ct.rs | 54 +----- .../tests/test_suite/encryption/glwe_tsk.rs | 18 +- .../test_suite/external_product/gglwe_ksk.rs | 36 ++-- .../test_suite/external_product/ggsw_ct.rs | 30 ++-- .../test_suite/external_product/glwe_ct.rs | 16 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 44 ++--- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 51 +++--- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 20 +-- poulpy-core/src/tests/test_suite/packing.rs | 14 +- poulpy-core/src/tests/test_suite/trace.rs | 15 +- .../benches/circuit_bootstrapping.rs | 10 +- .../examples/circuit_bootstrapping.rs | 14 +- .../ciphertexts/block_prepared.rs | 14 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 4 +- .../src/tfhe/bdd_arithmetic/eval.rs | 6 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 8 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 8 +- .../src/tfhe/blind_rotation/cggi_key.rs | 14 +- poulpy-schemes/src/tfhe/blind_rotation/key.rs | 4 +- .../src/tfhe/blind_rotation/key_prepared.rs | 4 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 10 +- .../src/tfhe/circuit_bootstrapping/key.rs | 52 +++--- .../src/tfhe/circuit_bootstrapping/mod.rs | 6 +- .../tests/circuit_bootstrapping.rs | 22 +-- 94 files changed, 1892 insertions(+), 1235 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 360a9f4..5027f3b 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -62,13 +62,13 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); let mut ct_glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) + GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); @@ -98,7 +98,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); @@ -163,12 +163,12 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) + GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), ); @@ -198,7 +198,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { let scratch_borrow = scratch.borrow(); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 2da2032..8316949 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::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWESwitchingKey, GGLWESwitchingKeyLayout, - GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWECiphertextLayout, GLWESecret, + GLWESwitchingKey, GLWESwitchingKeyLayout, Rank, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration}; @@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); - let gglwe_atk_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n, base2k, k: k_gglwe, @@ -62,12 +62,12 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&gglwe_atk_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc(&gglwe_atk_layout); let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) + GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::keyswitch_scratch_space( &module, @@ -102,7 +102,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWEAutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: AutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); @@ -157,7 +157,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); - let gglwe_layout: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_layout: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n, base2k, k: k_ksk, @@ -174,11 +174,11 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_layout); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) + GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), ); @@ -211,7 +211,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 9b08e68..57324e3 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::AutomorphismKeyPrepared}; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -36,16 +36,16 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWEAutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) + AutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn automorphism( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &AutomorphismKey, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -133,7 +133,7 @@ impl GGLWEAutomorphismKey { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index eef3082..09e2f94 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -9,11 +9,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared}, + GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, + prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, }; -impl GGSWCiphertext> { +impl GGSW> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -40,7 +40,7 @@ impl GGSWCiphertext> { &in_infos.glwe_layout(), key_infos, ); - let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); ci_dft + (ks_internal | expand) } @@ -60,17 +60,17 @@ impl GGSWCiphertext> { + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSWCiphertext::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) + GGSW::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) } } -impl GGSWCiphertext { +impl GGSW { pub fn automorphism( &mut self, module: &Module, - lhs: &GGSWCiphertext, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + lhs: &GGSW, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -121,7 +121,7 @@ impl GGSWCiphertext { self.rank(), tensor_key.rank_out() ); - assert!(scratch.available() >= GGSWCiphertext::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) + assert!(scratch.available() >= GGSW::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) }; // Keyswitch the j-th row of the col 0 @@ -137,8 +137,8 @@ impl GGSWCiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 79fcb12..f94b9bc 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; impl GLWECiphertext> { pub fn automorphism_scratch_space( @@ -41,7 +41,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -67,7 +67,7 @@ impl GLWECiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -94,7 +94,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -135,7 +135,7 @@ impl GLWECiphertext { pub fn automorphism_add_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -177,7 +177,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -219,7 +219,7 @@ impl GLWECiphertext { pub fn automorphism_sub_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -262,7 +262,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -304,7 +304,7 @@ impl GLWECiphertext { pub fn automorphism_sub_negate_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index f202223..9665c19 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -9,18 +9,19 @@ use crate::{ encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GGLWEAutomorphismKeyCompressed, GGLWEAutomorphismKeyCompressedToMut, GGLWEKeyCompressed}, + compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed}, }, }; -impl GGLWEAutomorphismKeyCompressed> { +impl AutomorphismKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { assert_eq!(module.n() as u32, infos.n()); - GGLWEKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) } } @@ -34,7 +35,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEAutomorphismKeyCompressedToMut, + R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef; } @@ -53,10 +54,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEAutomorphismKeyCompressedToMut, + R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef, { - let res: &mut GGLWEAutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -65,10 +66,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), + scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", scratch.available(), - GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) + AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) ) } @@ -92,7 +93,7 @@ where } } -impl GGLWEAutomorphismKeyCompressed { +impl AutomorphismKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index c2b4de3..4939fe5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ TakeGLWEPt, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGLWECiphertext, GGLWEInfos, LWEInfos, + GGLWE, GGLWEInfos, LWEInfos, compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -40,7 +40,7 @@ impl GGLWECiphertextCompressed> { A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GGLWECiphertext::encrypt_sk_scratch_space(module, infos) + GGLWE::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index e5b3716..91f1856 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -11,25 +11,25 @@ use crate::{ TakeGLWESecretPrepared, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut}, + Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, prepared::GLWESecretPrepared, }, }; -impl GGLWEKeyCompressed> { +impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) } } -impl GGLWEKeyCompressed { +impl GLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -56,7 +56,7 @@ pub trait GGLWEKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEKeyCompressedToMut, + R: GLWESwitchingKeyCompressedToMut, SI: GLWESecretToRef, SO: GLWESecretToRef; } @@ -80,25 +80,25 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEKeyCompressedToMut, + R: GLWESwitchingKeyCompressedToMut, SI: GLWESecretToRef, SO: GLWESecretToRef, { - let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); #[cfg(debug_assertions)] { - use crate::layouts::GGLWESwitchingKey; + use crate::layouts::GLWESwitchingKey; assert!(sk_in.n().0 <= self.n() as u32); assert!(sk_out.n().0 <= self.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res), + scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(self, res) + GLWESwitchingKey::encrypt_sk_scratch_space(self, res) ) } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index be78f3e..65012d6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -11,20 +11,19 @@ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, - compressed::{GGLWETensorKeyCompressed, GGLWETensorKeyCompressedToMut}, - prepared::Prepare, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; -impl GGLWETensorKeyCompressed> { +impl TensorKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GGLWETensorKey::encrypt_sk_scratch_space(module, infos) + TensorKey::encrypt_sk_scratch_space(module, infos) } } @@ -37,7 +36,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWETensorKeyCompressedToMut, + R: TensorKeyCompressedToMut, S: GLWESecretToRef; } @@ -59,10 +58,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWETensorKeyCompressedToMut, + R: TensorKeyCompressedToMut, S: GLWESecretToRef, { - let res: &mut GGLWETensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -119,7 +118,7 @@ where } } -impl GGLWETensorKeyCompressed { +impl TensorKeyCompressed { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index bdad6be..e9e5017 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ TakeGLWEPt, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -20,7 +20,7 @@ impl GGSWCiphertextCompressed> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 834f968..86f86cc 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,17 +1,15 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - encryption::{SIGMA, glwe_ct::glwe_encrypt_sk_internal}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared, + GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -25,6 +23,60 @@ impl GLWECiphertextCompressed> { } } +pub trait GLWECompressedEncryptSk { + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextCompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal, +{ + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextCompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let mut source_xa: Source = Source::new(seed_xa); + let cols: usize = (res.rank() + 1).into(); + + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.data, + cols, + true, + Some((pt, 0)), + sk, + &mut source_xa, + source_xe, + SIGMA, + scratch, + ); + + res.seed = seed_xa; + } +} + impl GLWECiphertextCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( @@ -36,65 +88,8 @@ impl GLWECiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GLWECompressedEncryptSk, { - self.encrypt_sk_internal(module, Some((pt, 0)), sk, seed_xa, source_xe, scratch); - } - - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_sk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - let mut source_xa = Source::new(seed_xa); - let cols: usize = (self.rank() + 1).into(); - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.data, - cols, - true, - pt, - sk, - &mut source_xa, - source_xe, - SIGMA, - scratch, - ); - self.seed = seed_xa; + module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 9065c2e..a269847 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -12,12 +12,11 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyToMut, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, GLWESecretToRef, - LWEInfos, + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, }, }; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -28,7 +27,7 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize @@ -40,7 +39,7 @@ impl GGLWEAutomorphismKey> { _infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) + GLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) } } @@ -54,13 +53,13 @@ pub trait GGLWEAutomorphismKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - A: GGLWEAutomorphismKeyToMut, + A: AutomorphismKeyToMut, B: GLWESecretToRef; } -impl GGLWEAutomorphismKey +impl AutomorphismKey where - Self: GGLWEAutomorphismKeyToMut, + Self: AutomorphismKeyToMut, { pub fn encrypt_sk( &mut self, @@ -109,10 +108,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - A: GGLWEAutomorphismKeyToMut, + A: AutomorphismKeyToMut, B: GLWESecretToRef, { - let res: &mut GGLWEAutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -123,10 +122,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res), + scratch.available() >= AutomorphismKey::encrypt_sk_scratch_space(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", scratch.available(), - GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res) + AutomorphismKey::encrypt_sk_scratch_space(self, res) ) } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 3928cf4..a7a98ce 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -11,12 +11,12 @@ use crate::{ TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, + GGLWE, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGLWECiphertext> { +impl GGLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -68,7 +68,7 @@ where P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); #[cfg(debug_assertions)] @@ -93,12 +93,12 @@ where assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(self, res), + scratch.available() >= GGLWE::encrypt_sk_scratch_space(self, res), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}", scratch.available(), res.rank_out(), res.size(), - GGLWECiphertext::encrypt_sk_scratch_space(self, res) + GGLWE::encrypt_sk_scratch_space(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, @@ -148,7 +148,7 @@ where } } -impl GGLWECiphertext { +impl GGLWE { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 0629bec..ce10a51 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -11,18 +11,16 @@ use poulpy_hal::{ use crate::{ TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos, prepared::GLWESecretPrepared, - }, + layouts::{Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared}, }; -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) } @@ -31,11 +29,11 @@ impl GGLWESwitchingKey> { where A: GGLWEInfos, { - GGLWECiphertext::encrypt_pk_scratch_space(module, _infos) + GGLWE::encrypt_pk_scratch_space(module, _infos) } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -70,10 +68,10 @@ impl GGLWESwitchingKey { assert!(sk_in.n().0 <= module.n() as u32); assert!(sk_out.n().0 <= module.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), + scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(module, self), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GLWESwitchingKey::encrypt_sk_scratch_space(module, self) ) } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 1946929..50a646a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -12,12 +12,11 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, - prepared::{GLWESecretPrepared, Prepare}, + Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, }, }; -impl GGLWETensorKey> { +impl TensorKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -29,11 +28,11 @@ impl GGLWETensorKey> { + module.vec_znx_big_alloc_bytes(1, 1) + module.vec_znx_dft_alloc_bytes(1, 1) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } -impl GGLWETensorKey { +impl TensorKey { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index d9e35bd..ad713b5 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -8,12 +8,12 @@ use crate::{ SIGMA, TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ - GGSWCiphertext, GGSWCiphertextToMut, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWECiphertext, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertext> { +impl GGSW> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, @@ -37,7 +37,7 @@ pub trait GGSWEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextToMut, + R: GGSWToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -56,11 +56,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextToMut, + R: GGSWToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGSWCiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); @@ -106,7 +106,7 @@ where } } -impl GGSWCiphertext { +impl GGSW { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index b65ce4e..aee218d 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -11,22 +11,24 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{GGLWEInfos, GGLWESwitchingKey, GLWESecret, GLWEToLWEKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared}, + layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, + }, }; -impl GLWEToLWEKey> { +impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) - + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) } } -impl GLWEToLWEKey { +impl GLWEToLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 66ae685..4c8b866 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -12,7 +12,7 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWESwitchingKey, Rank, + Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::GLWESecretPrepared, }, }; @@ -40,7 +40,7 @@ impl LWESwitchingKey> { ); GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) + GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 204e84b..5cb9339 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -11,7 +11,7 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, + layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, }; impl LWEToGLWESwitchingKey> { @@ -25,7 +25,7 @@ impl LWEToGLWESwitchingKey> { Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) } } diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index cb35a4c..7228c28 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GGSWInfos, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GGSWInfos, GLWESwitchingKey, prepared::GGSWPrepared}; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn external_product_scratch_space( module: &Module, out_infos: &OUT, @@ -22,7 +22,7 @@ impl GGLWEAutomorphismKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) + GLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) } pub fn external_product_inplace_scratch_space( @@ -35,16 +35,16 @@ impl GGLWEAutomorphismKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) + GLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn external_product( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGSWCiphertextPrepared, + lhs: &AutomorphismKey, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -64,7 +64,7 @@ impl GGLWEAutomorphismKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 2eff45c..bfeb146 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGLWESwitchingKey, GGSWInfos, GLWECiphertext, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{GGLWEInfos, GGSWInfos, GLWECiphertext, GLWESwitchingKey, prepared::GGSWPrepared}; -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn external_product_scratch_space( module: &Module, out_infos: &OUT, @@ -44,12 +44,12 @@ impl GGLWESwitchingKey> { } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn external_product( &mut self, module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGSWCiphertextPrepared, + lhs: &GLWESwitchingKey, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -107,7 +107,7 @@ impl GGLWESwitchingKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index a458de1..1610cef 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWPrepared}; -impl GGSWCiphertext> { +impl GGSW> { #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, @@ -45,12 +45,12 @@ impl GGSWCiphertext> { } } -impl GGSWCiphertext { +impl GGSW { pub fn external_product( &mut self, module: &Module, - lhs: &GGSWCiphertext, - rhs: &GGSWCiphertextPrepared, + lhs: &GGSW, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -86,7 +86,7 @@ impl GGSWCiphertext { rhs.rank() ); - assert!(scratch.available() >= GGSWCiphertext::external_product_scratch_space(module, self, lhs, rhs)) + assert!(scratch.available() >= GGSW::external_product_scratch_space(module, self, lhs, rhs)) } let min_dnum: usize = self.dnum().min(lhs.dnum()).into(); @@ -105,7 +105,7 @@ impl GGSWCiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index d764507..5a72049 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ GLWEExternalProduct, GLWEExternalProductInplace, layouts::{ GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, }; @@ -74,7 +74,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProduct, @@ -85,7 +85,7 @@ impl GLWECiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductInplace, @@ -113,7 +113,7 @@ where D: GGSWCiphertextPreparedToRef, { let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &ggsw.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); let basek_in: usize = res.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); @@ -237,7 +237,7 @@ where let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &rhs.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref(); let basek_in: usize = lhs.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 3da962a..927e098 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}, + layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -116,7 +116,7 @@ impl GLWEPacker { &mut self, module: &Module, a: Option<&GLWECiphertext>, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -191,7 +191,7 @@ fn pack_core( a: Option<&GLWECiphertext>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -285,7 +285,7 @@ fn combine( acc: &mut Accumulator, b: Option<&GLWECiphertext>, i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -397,7 +397,7 @@ pub fn glwe_packing( module: &Module, cts: &mut HashMap>, log_gap_out: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, @@ -439,7 +439,7 @@ pub fn glwe_packing( (0..log_n - log_gap_out).for_each(|i| { let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); - let auto_key: &GGLWEAutomorphismKeyPrepared = if i == 0 { + let auto_key: &AutomorphismKeyPrepared = if i == 0 { auto_keys.get(&-1).unwrap() } else { auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() @@ -466,7 +466,7 @@ fn pack_internal( a: &mut Option<&mut GLWECiphertext>, b: &mut Option<&mut GLWECiphertext>, i: usize, - auto_key: &GGLWEAutomorphismKeyPrepared, + auto_key: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxRotateInplace diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 4e1769e..397a7e1 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -11,9 +11,7 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared, - }, + layouts::{Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; @@ -72,7 +70,7 @@ impl GLWECiphertext { start: usize, end: usize, lhs: &GLWECiphertext, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -100,7 +98,7 @@ impl GLWECiphertext { module: &Module, start: usize, end: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 9f6caa5..ee86cbc 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -8,11 +8,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared}, + AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, + prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, }; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -25,7 +25,7 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) + GLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) } pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize @@ -34,16 +34,16 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWESwitchingKeyPrepared, + lhs: &AutomorphismKey, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -65,7 +65,7 @@ impl GGLWEAutomorphismKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -85,7 +85,7 @@ impl GGLWEAutomorphismKey { } } -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -111,12 +111,12 @@ impl GGLWESwitchingKey> { } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGLWESwitchingKeyPrepared, + lhs: &GLWESwitchingKey, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -187,7 +187,7 @@ impl GGLWESwitchingKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index d261f03..3ec738e 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -10,13 +10,13 @@ use poulpy_hal::{ use crate::{ layouts::{ - GGLWECiphertext, GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared}, + GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, operations::GLWEOperations, }; -impl GGSWCiphertext> { +impl GGSW> { pub(crate) fn expand_row_scratch_space(module: &Module, out_infos: &OUT, tsk_infos: &TSK) -> usize where OUT: GGSWInfos, @@ -77,7 +77,7 @@ impl GGSWCiphertext> { let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out); let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); - let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); if in_infos.base2k() == tsk_infos.base2k() { @@ -109,16 +109,16 @@ impl GGSWCiphertext> { + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSWCiphertext::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) + GGSW::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) } } -impl GGSWCiphertext { +impl GGSW { pub fn from_gglwe( &mut self, module: &Module, - a: &GGLWECiphertext, - tsk: &GGLWETensorKeyPrepared, + a: &GGLWE, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where DataA: DataRef, @@ -157,9 +157,9 @@ impl GGSWCiphertext { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGSWCiphertext, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, + lhs: &GGSW, + ksk: &GLWESwitchingKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -192,8 +192,8 @@ impl GGSWCiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, + ksk: &GLWESwitchingKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -226,7 +226,7 @@ impl GGSWCiphertext { pub fn expand_row( &mut self, module: &Module, - tsk: &GGLWETensorKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -246,7 +246,7 @@ impl GGSWCiphertext { let basek_in: usize = self.base2k().into(); let basek_tsk: usize = tsk.base2k().into(); - assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self, tsk)); + assert!(scratch.available() >= GGSW::expand_row_scratch_space(module, self, tsk)); let n: usize = self.n().into(); let rank: usize = self.rank().into(); diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index 07d95e9..e7afcbe 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWESwitchingKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; impl GLWECiphertext> { pub fn keyswitch_scratch_space( @@ -68,7 +68,7 @@ impl GLWECiphertext { &self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where DataLhs: DataRef, @@ -117,7 +117,7 @@ impl GLWECiphertext { pub(crate) fn assert_keyswitch_inplace( &self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where DataRhs: DataRef, @@ -149,7 +149,7 @@ impl GLWECiphertext { &mut self, module: &Module, glwe_in: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -191,7 +191,7 @@ impl GLWECiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -237,7 +237,7 @@ impl GLWECiphertext { &self, module: &Module, res_dft: VecZnxDft, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) -> VecZnxBig where diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 2b6a54d..a62e4a7 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -5,19 +5,19 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKeyCompressed { - pub(crate) key: GGLWEKeyCompressed, +pub struct AutomorphismKeyCompressed { + pub(crate) key: GLWESwitchingKeyCompressed, pub(crate) p: i64, } -impl LWEInfos for GGLWEAutomorphismKeyCompressed { +impl LWEInfos for AutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -34,13 +34,13 @@ impl LWEInfos for GGLWEAutomorphismKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GLWEInfos for AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GGLWEInfos for AutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -58,39 +58,39 @@ impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { } } -impl fmt::Debug for GGLWEAutomorphismKeyCompressed { +impl fmt::Debug for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKeyCompressed { +impl FillUniform for AutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKeyCompressed { +impl fmt::Display for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKeyCompressed> { +impl AutomorphismKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); Self { - key: GGLWEKeyCompressed::alloc(infos), + key: GLWESwitchingKeyCompressed::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { Self { - key: GGLWEKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), + key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -100,64 +100,64 @@ impl GGLWEAutomorphismKeyCompressed> { A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) } } -impl ReaderFrom for GGLWEAutomorphismKeyCompressed { +impl ReaderFrom for AutomorphismKeyCompressed { 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 GGLWEAutomorphismKeyCompressed { +impl WriterTo for AutomorphismKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } } -impl Decompress> for GGLWEAutomorphismKey +impl Decompress> for AutomorphismKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWEAutomorphismKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &AutomorphismKeyCompressed) { self.key.decompress(module, &other.key); self.p = other.p; } } -pub trait GGLWEAutomorphismKeyCompressedToRef { - fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]>; +pub trait AutomorphismKeyCompressedToRef { + fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; } -impl GGLWEAutomorphismKeyCompressedToRef for GGLWEAutomorphismKeyCompressed +impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToRef, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { - fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]> { - GGLWEAutomorphismKeyCompressed { + fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { + AutomorphismKeyCompressed { key: self.key.to_ref(), p: self.p, } } } -pub trait GGLWEAutomorphismKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]>; +pub trait AutomorphismKeyCompressedToMut { + fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>; } -impl GGLWEAutomorphismKeyCompressedToMut for GGLWEAutomorphismKeyCompressed +impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToMut, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { - fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]> { - GGLWEAutomorphismKeyCompressed { + fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { + AutomorphismKeyCompressed { p: self.p, key: self.key.to_mut(), } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 6e14756..a75a9a8 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GLWECiphertextCompressed}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -235,7 +235,7 @@ impl WriterTo for GGLWECiphertextCompressed { } } -impl Decompress> for GGLWECiphertext +impl Decompress> for GGLWE where Module: VecZnxFillUniform + VecZnxCopy, { @@ -313,7 +313,7 @@ pub trait GGLWECiphertextCompressedToRef { fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; } -impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { +impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { GGLWECiphertextCompressed { k: self.k(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index cbb2f8c..76d60a7 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -5,20 +5,20 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEKeyCompressed { +pub struct GLWESwitchingKeyCompressed { pub(crate) key: GGLWECiphertextCompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWEKeyCompressed { +impl LWEInfos for GLWESwitchingKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -35,13 +35,13 @@ impl LWEInfos for GGLWEKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWEKeyCompressed { +impl GLWEInfos for GLWESwitchingKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEKeyCompressed { +impl GGLWEInfos for GLWESwitchingKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -59,19 +59,19 @@ impl GGLWEInfos for GGLWEKeyCompressed { } } -impl fmt::Debug for GGLWEKeyCompressed { +impl fmt::Debug for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEKeyCompressed { +impl FillUniform for GLWESwitchingKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEKeyCompressed { +impl fmt::Display for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -81,12 +81,12 @@ impl fmt::Display for GGLWEKeyCompressed { } } -impl GGLWEKeyCompressed> { +impl GLWESwitchingKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { - GGLWEKeyCompressed { + GLWESwitchingKeyCompressed { key: GGLWECiphertextCompressed::alloc(infos), sk_in_n: 0, sk_out_n: 0, @@ -102,7 +102,7 @@ impl GGLWEKeyCompressed> { dnum: Dnum, dsize: Dsize, ) -> Self { - GGLWEKeyCompressed { + GLWESwitchingKeyCompressed { key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, @@ -121,7 +121,7 @@ impl GGLWEKeyCompressed> { } } -impl ReaderFrom for GGLWEKeyCompressed { +impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -129,7 +129,7 @@ impl ReaderFrom for GGLWEKeyCompressed { } } -impl WriterTo for GGLWEKeyCompressed { +impl WriterTo for GLWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; @@ -137,27 +137,27 @@ impl WriterTo for GGLWEKeyCompressed { } } -impl Decompress> for GGLWESwitchingKey +impl Decompress> for GLWESwitchingKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWEKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &GLWESwitchingKeyCompressed) { self.key.decompress(module, &other.key); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } } -pub trait GGLWEKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]>; +pub trait GLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; } -impl GGLWEKeyCompressedToMut for GGLWEKeyCompressed +impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed where GGLWECiphertextCompressed: GGLWECiphertextCompressedToMut, { - fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]> { - GGLWEKeyCompressed { + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { + GLWESwitchingKeyCompressed { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_mut(), @@ -165,16 +165,16 @@ where } } -pub trait GGLWEKeyCompressedToRef { - fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]>; +pub trait GLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; } -impl GGLWEKeyCompressedToRef for GGLWEKeyCompressed +impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed where GGLWECiphertextCompressed: GGLWECiphertextCompressedToRef, { - fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]> { - GGLWEKeyCompressed { + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { + GLWESwitchingKeyCompressed { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_ref(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 0206788..7255594 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -5,18 +5,18 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TorusPrecision, + compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKeyCompressed { - pub(crate) keys: Vec>, +pub struct TensorKeyCompressed { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyCompressed { +impl LWEInfos for TensorKeyCompressed { fn n(&self) -> Degree { self.keys[0].n() } @@ -32,13 +32,13 @@ impl LWEInfos for GGLWETensorKeyCompressed { self.keys[0].size() } } -impl GLWEInfos for GGLWETensorKeyCompressed { +impl GLWEInfos for TensorKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyCompressed { +impl GGLWEInfos for TensorKeyCompressed { fn rank_in(&self) -> Rank { self.rank_out() } @@ -56,21 +56,21 @@ impl GGLWEInfos for GGLWETensorKeyCompressed { } } -impl fmt::Debug for GGLWETensorKeyCompressed { +impl fmt::Debug for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKeyCompressed { +impl FillUniform for TensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWEKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKeyCompressed { +impl fmt::Display for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKeyCompressed)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -80,7 +80,7 @@ impl fmt::Display for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed> { +impl TensorKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -101,10 +101,10 @@ impl GGLWETensorKeyCompressed> { } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); + let mut keys: Vec>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWEKeyCompressed::alloc_with( + keys.push(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -129,7 +129,7 @@ impl GGLWETensorKeyCompressed> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWEKeyCompressed::alloc_bytes_with( + * GLWESwitchingKeyCompressed::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), @@ -141,11 +141,11 @@ impl GGLWETensorKeyCompressed> { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) + pairs * GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) } } -impl ReaderFrom for GGLWETensorKeyCompressed { +impl ReaderFrom for TensorKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -161,7 +161,7 @@ impl ReaderFrom for GGLWETensorKeyCompressed { } } -impl WriterTo for GGLWETensorKeyCompressed { +impl WriterTo for TensorKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -171,8 +171,8 @@ impl WriterTo for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEKeyCompressed { +impl TensorKeyCompressed { + pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -181,11 +181,11 @@ impl GGLWETensorKeyCompressed { } } -impl Decompress> for GGLWETensorKey +impl Decompress> for TensorKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWETensorKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &TensorKeyCompressed) { #[cfg(debug_assertions)] { assert_eq!( @@ -206,31 +206,31 @@ where } } -pub trait GGLWETensorKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]>; +pub trait TensorKeyCompressedToMut { + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]>; } -impl GGLWETensorKeyCompressedToMut for GGLWETensorKeyCompressed +impl TensorKeyCompressedToMut for TensorKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToMut, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { - fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]> { - GGLWETensorKeyCompressed { + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { + TensorKeyCompressed { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } -pub trait GGLWETensorKeyCompressedToRef { - fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]>; +pub trait TensorKeyCompressedToRef { + fn to_ref(&self) -> TensorKeyCompressed<&[u8]>; } -impl GGLWETensorKeyCompressedToRef for GGLWETensorKeyCompressed +impl TensorKeyCompressedToRef for TensorKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToRef, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { - fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]> { - GGLWETensorKeyCompressed { + fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { + TensorKeyCompressed { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index b751bfc..3d15e3e 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GLWECiphertextCompressed}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -217,7 +217,7 @@ impl WriterTo for GGSWCiphertextCompressed { } } -impl Decompress> for GGSWCiphertext +impl Decompress> for GGSW where Module: VecZnxFillUniform + VecZnxCopy, { diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 30a3733..576774d 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -1,10 +1,14 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; +use crate::layouts::{ + Base2K, BuildError, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -96,6 +100,101 @@ impl GLWECiphertextCompressed> { } } +pub struct GLWECiphertextCompressedBuilder { + data: Option>, + base2k: Option, + k: Option, + rank: Option, +} + +impl GLWECiphertextCompressed { + #[inline] + pub fn builder() -> GLWECiphertextCompressedBuilder { + GLWECiphertextCompressedBuilder { + data: None, + base2k: None, + k: None, + rank: None, + } + } +} + +impl GLWECiphertextCompressedBuilder> { + #[inline] + pub fn layout(mut self, layout: &A) -> Self + where + A: GLWEInfos, + { + self.data = Some(VecZnx::alloc(layout.n().into(), 1, layout.size())); + self.base2k = Some(layout.base2k()); + self.k = Some(layout.k()); + self.rank = Some(layout.rank()); + self + } +} + +impl GLWECiphertextCompressedBuilder { + #[inline] + pub fn data(mut self, data: VecZnx) -> Self { + self.data = Some(data); + self + } + #[inline] + pub fn base2k(mut self, base2k: Base2K) -> Self { + self.base2k = Some(base2k); + self + } + #[inline] + pub fn k(mut self, k: TorusPrecision) -> Self { + self.k = Some(k); + self + } + #[inline] + pub fn rank(mut self, rank: Rank) -> Self { + self.rank = Some(rank); + self + } + + pub fn build(self) -> Result, BuildError> { + let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; + let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; + let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; + let rank: Rank = self.rank.ok_or(BuildError::MissingK)?; + + if base2k == 0_u32 { + return Err(BuildError::ZeroBase2K); + } + + if k == 0_u32 { + return Err(BuildError::ZeroTorusPrecision); + } + + if rank == 0_u32 { + return Err(BuildError::ZeroRank); + } + + if data.n() == 0 { + return Err(BuildError::ZeroDegree); + } + + if data.cols() != 1 { + return Err(BuildError::VecZnxColsNotOne); + } + + if data.size() == 0 { + return Err(BuildError::ZeroLimbs); + } + + Ok(GLWECiphertextCompressed { + data, + base2k, + k, + rank, + seed: [0u8; 32], + }) + } +} + impl ReaderFrom for GLWECiphertextCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -176,3 +275,35 @@ impl GLWECiphertext { self.k = other.k; } } + +pub trait GLWECiphertextCompressedToRef { + fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>; +} + +impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { + fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { + GLWECiphertextCompressed::builder() + .k(self.k()) + .base2k(self.base2k()) + .rank(self.rank()) + .data(self.data.to_ref()) + .build() + .unwrap() + } +} + +pub trait GLWECiphertextCompressedToMut { + fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>; +} + +impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { + fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { + GLWECiphertextCompressed::builder() + .k(self.k()) + .base2k(self.base2k()) + .rank(self.rank()) + .data(self.data.to_mut()) + .build() + .unwrap() + } +} diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 317e0e5..c3969b2 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWEKeyCompressed, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed, }; #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for GLWEToLWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -98,11 +98,11 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -127,10 +127,10 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 9d45d0a..d7ea93b 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed}, + compressed::{Decompress, GLWESwitchingKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct LWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -103,11 +103,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -137,11 +137,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index bcc69a4..c1000ff 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed}, + compressed::{Decompress, GLWESwitchingKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { fn n(&self) -> Degree { @@ -98,11 +98,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -127,11 +127,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index a88db86..f9244e1 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,15 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GGLWESwitchingKeyToMut, GLWECiphertext, GLWEInfos, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWEAutomorphismKeyLayout { +pub struct AutomorphismKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -22,18 +22,18 @@ pub struct GGLWEAutomorphismKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKey { - pub(crate) key: GGLWESwitchingKey, +pub struct AutomorphismKey { + pub(crate) key: GLWESwitchingKey, pub(crate) p: i64, } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for GGLWEAutomorphismKey { +impl LWEInfos for AutomorphismKey { fn n(&self) -> Degree { self.key.n() } @@ -51,13 +51,13 @@ impl LWEInfos for GGLWEAutomorphismKey { } } -impl GLWEInfos for GGLWEAutomorphismKey { +impl GLWEInfos for AutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKey { +impl GGLWEInfos for AutomorphismKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -75,7 +75,7 @@ impl GGLWEInfos for GGLWEAutomorphismKey { } } -impl LWEInfos for GGLWEAutomorphismKeyLayout { +impl LWEInfos for AutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -89,13 +89,13 @@ impl LWEInfos for GGLWEAutomorphismKeyLayout { } } -impl GLWEInfos for GGLWEAutomorphismKeyLayout { +impl GLWEInfos for AutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } -impl GGLWEInfos for GGLWEAutomorphismKeyLayout { +impl GGLWEInfos for AutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -113,25 +113,25 @@ impl GGLWEInfos for GGLWEAutomorphismKeyLayout { } } -impl fmt::Debug for GGLWEAutomorphismKey { +impl fmt::Debug for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKey { +impl FillUniform for AutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKey { +impl fmt::Display for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -141,15 +141,15 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc(infos), + AutomorphismKey { + key: GLWESwitchingKey::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), + AutomorphismKey { + key: GLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -163,54 +163,66 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) } } -pub trait GGLWEAutomorphismKeyToMut { - fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]>; +pub trait AutomorphismKeyToMut { + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; } -impl GGLWEAutomorphismKeyToMut for GGLWEAutomorphismKey +impl AutomorphismKeyToMut for AutomorphismKey where - GGLWESwitchingKey: GGLWESwitchingKeyToMut, + GLWESwitchingKey: GLWESwitchingKeyToMut, { - fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]> { - GGLWEAutomorphismKey { + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> { + AutomorphismKey { key: self.key.to_mut(), p: self.p, } } } -pub trait GGLWEAutomorphismKeyToRef { - fn to_ref(&self) -> GGLWEAutomorphismKey<&[u8]>; +pub trait AutomorphismKeyToRef { + fn to_ref(&self) -> AutomorphismKey<&[u8]>; } -impl GGLWEAutomorphismKey { +impl AutomorphismKeyToRef for AutomorphismKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> AutomorphismKey<&[u8]> { + AutomorphismKey { + p: self.p, + key: self.key.to_ref(), + } + } +} + +impl AutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWEAutomorphismKey { +impl ReaderFrom for AutomorphismKey { 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 GGLWEAutomorphismKey { +impl WriterTo for AutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 713df90..8546499 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -29,6 +29,10 @@ where } } +pub trait SetGGLWEInfos { + fn set_dsize(&mut self, dsize: usize); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWECiphertextLayout { pub n: Degree, @@ -79,14 +83,14 @@ impl GGLWEInfos for GGLWECiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertext { +pub struct GGLWE { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertext { +impl LWEInfos for GGLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -104,13 +108,13 @@ impl LWEInfos for GGLWECiphertext { } } -impl GLWEInfos for GGLWECiphertext { +impl GLWEInfos for GGLWE { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertext { +impl GGLWEInfos for GGLWE { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -135,7 +139,7 @@ pub struct GGLWECiphertextBuilder { dsize: Option, } -impl GGLWECiphertext { +impl GGLWE { #[inline] pub fn builder() -> GGLWECiphertextBuilder { GGLWECiphertextBuilder { @@ -190,7 +194,7 @@ impl GGLWECiphertextBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -220,7 +224,7 @@ impl GGLWECiphertextBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGLWECiphertext { + Ok(GGLWE { data, base2k, k, @@ -229,31 +233,31 @@ impl GGLWECiphertextBuilder { } } -impl GGLWECiphertext { +impl GGLWE { pub fn data(&self) -> &MatZnx { &self.data } } -impl GGLWECiphertext { +impl GGLWE { pub fn data_mut(&mut self) -> &mut MatZnx { &mut self.data } } -impl fmt::Debug for GGLWECiphertext { +impl fmt::Debug for GGLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertext { +impl FillUniform for GGLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertext { +impl fmt::Display for GGLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -266,7 +270,7 @@ impl fmt::Display for GGLWECiphertext { } } -impl GGLWECiphertext { +impl GGLWE { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.at(row, col)) @@ -277,7 +281,7 @@ impl GGLWECiphertext { } } -impl GGLWECiphertext { +impl GGLWE { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .base2k(self.base2k()) @@ -288,7 +292,7 @@ impl GGLWECiphertext { } } -impl GGLWECiphertext> { +impl GGLWE> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -390,12 +394,12 @@ impl GGLWECiphertext> { } pub trait GGLWECiphertextToMut { - fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]>; + fn to_mut(&mut self) -> GGLWE<&mut [u8]>; } -impl GGLWECiphertextToMut for GGLWECiphertext { - fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]> { - GGLWECiphertext { +impl GGLWECiphertextToMut for GGLWE { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + GGLWE { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -404,13 +408,13 @@ impl GGLWECiphertextToMut for GGLWECiphertext { } } -pub trait GGLWECiphertextToRef { - fn to_ref(&self) -> GGLWECiphertext<&[u8]>; +pub trait GGLWEToRef { + fn to_ref(&self) -> GGLWE<&[u8]>; } -impl GGLWECiphertextToRef for GGLWECiphertext { - fn to_ref(&self) -> GGLWECiphertext<&[u8]> { - GGLWECiphertext { +impl GGLWEToRef for GGLWE { + fn to_ref(&self) -> GGLWE<&[u8]> { + GGLWE { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -419,7 +423,7 @@ impl GGLWECiphertextToRef for GGLWECiphertext { } } -impl ReaderFrom for GGLWECiphertext { +impl ReaderFrom for GGLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -428,7 +432,7 @@ impl ReaderFrom for GGLWECiphertext { } } -impl WriterTo for GGLWECiphertext { +impl WriterTo for GGLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 64b1694..e8a8b92 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -12,7 +12,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWESwitchingKeyLayout { +pub struct GLWESwitchingKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -22,7 +22,7 @@ pub struct GGLWESwitchingKeyLayout { pub dsize: Dsize, } -impl LWEInfos for GGLWESwitchingKeyLayout { +impl LWEInfos for GLWESwitchingKeyLayout { fn n(&self) -> Degree { self.n } @@ -36,13 +36,13 @@ impl LWEInfos for GGLWESwitchingKeyLayout { } } -impl GLWEInfos for GGLWESwitchingKeyLayout { +impl GLWEInfos for GLWESwitchingKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyLayout { +impl GGLWEInfos for GLWESwitchingKeyLayout { fn rank_in(&self) -> Rank { self.rank_in } @@ -61,13 +61,13 @@ impl GGLWEInfos for GGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWESwitchingKey { - pub(crate) key: GGLWECiphertext, +pub struct GLWESwitchingKey { + pub(crate) key: GGLWE, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKey { +impl LWEInfos for GLWESwitchingKey { fn n(&self) -> Degree { self.key.n() } @@ -85,13 +85,13 @@ impl LWEInfos for GGLWESwitchingKey { } } -impl GLWEInfos for GGLWESwitchingKey { +impl GLWEInfos for GLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKey { +impl GGLWEInfos for GLWESwitchingKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -109,13 +109,13 @@ impl GGLWEInfos for GGLWESwitchingKey { } } -impl fmt::Debug for GGLWESwitchingKey { +impl fmt::Debug for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GGLWESwitchingKey { +impl fmt::Display for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -127,19 +127,19 @@ impl fmt::Display for GGLWESwitchingKey { } } -impl FillUniform for GGLWESwitchingKey { +impl FillUniform for GLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc(infos), + GLWESwitchingKey { + key: GGLWE::alloc(infos), sk_in_n: 0, sk_out_n: 0, } @@ -154,8 +154,8 @@ impl GGLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> Self { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + GLWESwitchingKey { + key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } @@ -165,7 +165,7 @@ impl GGLWESwitchingKey> { where A: GGLWEInfos, { - GGLWECiphertext::alloc_bytes(infos) + GGLWE::alloc_bytes(infos) } pub fn alloc_bytes_with( @@ -177,20 +177,20 @@ impl GGLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> usize { - GGLWECiphertext::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GGLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]>; +pub trait GLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; } -impl GGLWESwitchingKeyToMut for GGLWESwitchingKey +impl GLWESwitchingKeyToMut for GLWESwitchingKey where - GGLWECiphertext: GGLWECiphertextToMut, + GGLWE: GGLWECiphertextToMut, { - fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]> { - GGLWESwitchingKey { + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { + GLWESwitchingKey { key: self.key.to_mut(), sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, @@ -198,19 +198,36 @@ where } } -impl GGLWESwitchingKey { +pub trait GLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; +} + +impl GLWESwitchingKeyToRef for GLWESwitchingKey +where + GGLWE: GGLWEToRef, +{ + fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { + GLWESwitchingKey { + key: self.key.to_ref(), + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + } + } +} + +impl GLWESwitchingKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWESwitchingKey { +impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -218,7 +235,7 @@ impl ReaderFrom for GGLWESwitchingKey { } } -impl WriterTo for GGLWESwitchingKey { +impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index a949b7e..e6eae2d 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -3,13 +3,16 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWETensorKeyLayout { +pub struct TensorKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -19,11 +22,11 @@ pub struct GGLWETensorKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKey { - pub(crate) keys: Vec>, +pub struct TensorKey { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKey { +impl LWEInfos for TensorKey { fn n(&self) -> Degree { self.keys[0].n() } @@ -41,13 +44,13 @@ impl LWEInfos for GGLWETensorKey { } } -impl GLWEInfos for GGLWETensorKey { +impl GLWEInfos for TensorKey { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl GGLWEInfos for GGLWETensorKey { +impl GGLWEInfos for TensorKey { fn rank_in(&self) -> Rank { self.rank_out() } @@ -65,7 +68,7 @@ impl GGLWEInfos for GGLWETensorKey { } } -impl LWEInfos for GGLWETensorKeyLayout { +impl LWEInfos for TensorKeyLayout { fn n(&self) -> Degree { self.n } @@ -79,13 +82,13 @@ impl LWEInfos for GGLWETensorKeyLayout { } } -impl GLWEInfos for GGLWETensorKeyLayout { +impl GLWEInfos for TensorKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyLayout { +impl GGLWEInfos for TensorKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -103,21 +106,21 @@ impl GGLWEInfos for GGLWETensorKeyLayout { } } -impl fmt::Debug for GGLWETensorKey { +impl fmt::Debug for TensorKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKey { +impl FillUniform for TensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWESwitchingKey| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKey| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKey { +impl fmt::Display for TensorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -127,7 +130,7 @@ impl fmt::Display for GGLWETensorKey { } } -impl GGLWETensorKey> { +impl TensorKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -148,10 +151,10 @@ impl GGLWETensorKey> { } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); + let mut keys: Vec>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKey::alloc_with( + keys.push(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -176,7 +179,7 @@ impl GGLWETensorKey> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWESwitchingKey::alloc_bytes_with( + * GLWESwitchingKey::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), @@ -189,13 +192,13 @@ impl GGLWETensorKey> { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) } } -impl GGLWETensorKey { +impl TensorKey { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKey { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -204,9 +207,9 @@ impl GGLWETensorKey { } } -impl GGLWETensorKey { +impl TensorKey { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKey { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -215,7 +218,7 @@ impl GGLWETensorKey { } } -impl ReaderFrom for GGLWETensorKey { +impl ReaderFrom for TensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -231,7 +234,7 @@ impl ReaderFrom for GGLWETensorKey { } } -impl WriterTo for GGLWETensorKey { +impl WriterTo for TensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -240,3 +243,33 @@ impl WriterTo for GGLWETensorKey { Ok(()) } } + +pub trait TensorKeyToRef { + fn to_ref(&self) -> TensorKey<&[u8]>; +} + +impl TensorKeyToRef for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> TensorKey<&[u8]> { + TensorKey { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } + } +} + +pub trait TensorKeyToMut { + fn to_ref(&mut self) -> TensorKey<&mut [u8]>; +} + +impl TensorKeyToMut for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_ref(&mut self) -> TensorKey<&mut [u8]> { + TensorKey { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index dc64bce..2d8b271 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -64,14 +64,14 @@ impl GGSWInfos for GGSWCiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertext { +pub struct GGSW { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertext { +impl LWEInfos for GGSW { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -89,13 +89,13 @@ impl LWEInfos for GGSWCiphertext { } } -impl GLWEInfos for GGSWCiphertext { +impl GLWEInfos for GGSW { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertext { +impl GGSWInfos for GGSW { fn dsize(&self) -> Dsize { self.dsize } @@ -112,7 +112,7 @@ pub struct GGSWCiphertextBuilder { dsize: Option, } -impl GGSWCiphertext { +impl GGSW { #[inline] pub fn builder() -> GGSWCiphertextBuilder { GGSWCiphertextBuilder { @@ -182,7 +182,7 @@ impl GGSWCiphertextBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -212,7 +212,7 @@ impl GGSWCiphertextBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGSWCiphertext { + Ok(GGSW { data, base2k, k, @@ -221,13 +221,13 @@ impl GGSWCiphertextBuilder { } } -impl fmt::Debug for GGSWCiphertext { +impl fmt::Debug for GGSW { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertext { +impl fmt::Display for GGSW { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -240,13 +240,13 @@ impl fmt::Display for GGSWCiphertext { } } -impl FillUniform for GGSWCiphertext { +impl FillUniform for GGSW { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertext { +impl GGSW { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.at(row, col)) @@ -257,7 +257,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext { +impl GGSW { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .base2k(self.base2k()) @@ -268,7 +268,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext> { +impl GGSW> { pub fn alloc(infos: &A) -> Self where A: GGSWInfos, @@ -353,7 +353,7 @@ impl GGSWCiphertext> { use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for GGSWCiphertext { +impl ReaderFrom for GGSW { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -362,7 +362,7 @@ impl ReaderFrom for GGSWCiphertext { } } -impl WriterTo for GGSWCiphertext { +impl WriterTo for GGSW { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -371,34 +371,32 @@ impl WriterTo for GGSWCiphertext { } } -pub trait GGSWCiphertextToMut { - fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]>; +pub trait GGSWToMut { + fn to_mut(&mut self) -> GGSW<&mut [u8]>; } -impl GGSWCiphertextToMut for GGSWCiphertext { - fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]> { - GGSWCiphertext::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl GGSWToMut for GGSW { + fn to_mut(&mut self) -> GGSW<&mut [u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } -pub trait GGSWCiphertextToRef { - fn to_ref(&self) -> GGSWCiphertext<&[u8]>; +pub trait GGSWToRef { + fn to_ref(&self) -> GGSW<&[u8]>; } -impl GGSWCiphertextToRef for GGSWCiphertext { - fn to_ref(&self) -> GGSWCiphertext<&[u8]> { - GGSWCiphertext::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GGSWToRef for GGSW { + fn to_ref(&self) -> GGSW<&[u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 23b6ef9..c8cfa4f 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -253,36 +253,6 @@ impl GLWECiphertext> { } } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; -} - -impl GLWECiphertextToRef for GLWECiphertext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_ref()) - .build() - .unwrap() - } -} - -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; -} - -impl GLWECiphertextToMut for GLWECiphertext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() - } -} - impl ReaderFrom for GLWECiphertext { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -298,3 +268,31 @@ impl WriterTo for GLWECiphertext { self.data.write_to(writer) } } + +pub trait GLWECiphertextToRef { + fn to_ref(&self) -> GLWECiphertext<&[u8]>; +} + +impl GLWECiphertextToRef for GLWECiphertext { + fn to_ref(&self) -> GLWECiphertext<&[u8]> { + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } + } +} + +pub trait GLWECiphertextToMut { + fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; +} + +impl GLWECiphertextToMut for GLWECiphertext { + fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index f227c9c..ec5a4a3 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -3,7 +3,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; use std::fmt; @@ -56,9 +59,9 @@ impl GGLWEInfos for GLWEToLWEKeyLayout { /// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext]. #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWEKey(pub(crate) GGLWESwitchingKey); +pub struct GLWEToLWESwitchingKey(pub(crate) GLWESwitchingKey); -impl LWEInfos for GLWEToLWEKey { +impl LWEInfos for GLWEToLWESwitchingKey { fn base2k(&self) -> Base2K { self.0.base2k() } @@ -76,12 +79,12 @@ impl LWEInfos for GLWEToLWEKey { } } -impl GLWEInfos for GLWEToLWEKey { +impl GLWEInfos for GLWEToLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GLWEToLWEKey { +impl GGLWEInfos for GLWEToLWESwitchingKey { fn rank_in(&self) -> Rank { self.0.rank_in() } @@ -99,37 +102,37 @@ impl GGLWEInfos for GLWEToLWEKey { } } -impl fmt::Debug for GLWEToLWEKey { +impl fmt::Debug for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GLWEToLWEKey { +impl FillUniform for GLWEToLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.0.fill_uniform(log_bound, source); } } -impl fmt::Display for GLWEToLWEKey { +impl fmt::Display for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(GLWEToLWESwitchingKey) {}", self.0) } } -impl ReaderFrom for GLWEToLWEKey { +impl ReaderFrom for GLWEToLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.0.read_from(reader) } } -impl WriterTo for GLWEToLWEKey { +impl WriterTo for GLWEToLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { self.0.write_to(writer) } } -impl GLWEToLWEKey> { +impl GLWEToLWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -144,11 +147,11 @@ impl GLWEToLWEKey> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -173,10 +176,36 @@ impl GLWEToLWEKey> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + } +} + +pub trait GLWEToLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; +} + +impl GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> { + GLWEToLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { + GLWEToLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 1560ea4..ce5b13a 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -27,6 +27,11 @@ pub trait LWEInfos { } } +pub trait SetLWEInfos { + fn set_k(&mut self, k: TorusPrecision); + fn set_base2k(&mut self, base2k: Base2K); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWECiphertextLayout { pub n: Degree, @@ -47,7 +52,6 @@ impl LWEInfos for LWECiphertextLayout { self.n } } - #[derive(PartialEq, Eq, Clone)] pub struct LWECiphertext { pub(crate) data: Zn, @@ -72,6 +76,16 @@ impl LWEInfos for LWECiphertext { } } +impl SetLWEInfos for LWECiphertext { + fn set_base2k(&mut self, base2k: Base2K) { + self.base2k = base2k + } + + fn set_k(&mut self, k: TorusPrecision) { + self.k = k + } +} + impl LWECiphertext { pub fn data(&self) -> &Zn { &self.data @@ -221,12 +235,11 @@ pub trait LWECiphertextToRef { impl LWECiphertextToRef for LWECiphertext { fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() + LWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } @@ -237,12 +250,11 @@ pub trait LWECiphertextToMut { impl LWECiphertextToMut for LWECiphertext { fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() + LWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 314322c..8f4a2ed 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -5,7 +5,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { @@ -54,7 +57,7 @@ impl GGLWEInfos for LWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWESwitchingKey { fn base2k(&self) -> Base2K { @@ -118,11 +121,11 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -152,11 +155,11 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } @@ -189,3 +192,29 @@ impl WriterTo for LWESwitchingKey { self.0.write_to(writer) } } + +pub trait LWEToLWEKeyToRef { + fn to_ref(&self) -> LWESwitchingKey<&[u8]>; +} + +impl LWEToLWEKeyToRef for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWESwitchingKey<&[u8]> { + LWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWEToLWEKeyToMut { + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; +} + +impl LWEToLWEKeyToMut for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { + LWESwitchingKey(self.0.to_mut()) + } +} diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index a5b7d4e..f2ba869 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -84,3 +84,29 @@ impl LWESecret { self.dist = Distribution::ZERO; } } + +pub trait LWESecretToRef { + fn to_ref(&self) -> LWESecret<&[u8]>; +} + +impl LWESecretToRef for LWESecret { + fn to_ref(&self) -> LWESecret<&[u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_ref(), + } + } +} + +pub trait LWESecretToMut { + fn to_mut(&mut self) -> LWESecret<&mut [u8]>; +} + +impl LWESecretToMut for LWESecret { + fn to_mut(&mut self) -> LWESecret<&mut [u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index b3ba74b..947e79d 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -5,7 +5,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEToGLWESwitchingKeyLayout { @@ -55,7 +58,7 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWEToGLWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWEToGLWESwitchingKey { fn base2k(&self) -> Base2K { @@ -143,11 +146,11 @@ impl LWEToGLWESwitchingKey> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -172,10 +175,36 @@ impl LWEToGLWESwitchingKey> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + } +} + +pub trait LWEToGLWESwitchingKeyToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; +} + +impl LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> { + LWEToGLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { + LWEToGLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 2b13751..775fb6f 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -47,6 +47,7 @@ pub enum BuildError { ZeroLimbs, ZeroRank, ZeroDigits, + VecZnxColsNotOne, } /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 594aa0a..2eda4e1 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -4,23 +4,25 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWEAutomorphismKeyPrepared { - pub(crate) key: GGLWESwitchingKeyPrepared, +pub struct AutomorphismKeyPrepared { + pub(crate) key: GLWESwitchingKeyPrepared, pub(crate) p: i64, } -impl GGLWEAutomorphismKeyPrepared { +impl AutomorphismKeyPrepared { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for GGLWEAutomorphismKeyPrepared { +impl LWEInfos for AutomorphismKeyPrepared { fn n(&self) -> Degree { self.key.n() } @@ -38,13 +40,23 @@ impl LWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GLWEInfos for GGLWEAutomorphismKeyPrepared { +pub trait SetP { + fn set_p(&mut self, p: i64); +} + +impl SetP for AutomorphismKeyPrepared { + fn set_p(&mut self, p: i64) { + self.p = p + } +} + +impl GLWEInfos for AutomorphismKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { +impl GGLWEInfos for AutomorphismKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -62,7 +74,7 @@ impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GGLWEAutomorphismKeyPrepared, B> { +impl AutomorphismKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -73,8 +85,8 @@ impl GGLWEAutomorphismKeyPrepared, B> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - GGLWEAutomorphismKeyPrepared::, B> { - key: GGLWESwitchingKeyPrepared::alloc(module, infos), + AutomorphismKeyPrepared::, B> { + key: GLWESwitchingKeyPrepared::alloc(module, infos), p: 0, } } @@ -83,8 +95,8 @@ impl GGLWEAutomorphismKeyPrepared, B> { where Module: VmpPMatAlloc, { - GGLWEAutomorphismKeyPrepared { - key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), + AutomorphismKeyPrepared { + key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -99,43 +111,104 @@ impl GGLWEAutomorphismKeyPrepared, B> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) } } -impl PrepareScratchSpace for GGLWEAutomorphismKeyPrepared, B> +pub trait AutomorphismKeyPrepareTmpBytes { + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos; +} + +impl AutomorphismKeyPrepareTmpBytes for Module where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: GLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.glwe_switching_key_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWEAutomorphismKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); +impl AutomorphismKeyPrepared { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: AutomorphismKeyPrepareTmpBytes, + { + module.automorphism_key_prepare_tmp_bytes(self); + } +} + +pub trait AutomorphismKeyPrepare { + fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) + where + R: AutomorphismKeyPreparedToMut, + O: AutomorphismKeyToRef; +} + +impl AutomorphismKeyPrepare for Module { + fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) + where + R: AutomorphismKeyPreparedToMut, + O: AutomorphismKeyToRef, + { + self.key.prepare(self, &other.to_ref().key, scratch); self.p = other.p; } } -impl PrepareAlloc, B>> for GGLWEAutomorphismKey +pub trait AutomorphismKeyPrepareAlloc { + fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> + where + O: AutomorphismKeyToRef; +} + +impl AutomorphismKeyPrepareAlloc for Module where Module: VmpPMatAlloc + VmpPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWEAutomorphismKeyPrepared, B> { - let mut atk_prepared: GGLWEAutomorphismKeyPrepared, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); + fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> + where + O: AutomorphismKeyToRef, + { + let mut atk_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref()); + atk_prepared.prepare(self, &other.to_ref(), scratch); atk_prepared } } + +pub trait AutomorphismKeyPreparedToMut { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; +} + +impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_mut(), + } + } +} + +pub trait AutomorphismKeyPreparedToRef { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; +} + +impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 4f22e6e..a19fd92 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,23 +1,22 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, BuildError, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGLWECiphertextPrepared { +pub struct GGLWEPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertextPrepared { +impl LWEInfos for GGLWEPrepared { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -35,13 +34,13 @@ impl LWEInfos for GGLWECiphertextPrepared { } } -impl GLWEInfos for GGLWECiphertextPrepared { +impl GLWEInfos for GGLWEPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextPrepared { +impl GGLWEInfos for GGLWEPrepared { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -59,17 +58,17 @@ impl GGLWEInfos for GGLWECiphertextPrepared { } } -pub struct GGLWECiphertextPreparedBuilder { +pub struct GGLWEPreparedBuilder { data: Option>, base2k: Option, k: Option, dsize: Option, } -impl GGLWECiphertextPrepared { +impl GGLWEPrepared { #[inline] - pub fn builder() -> GGLWECiphertextPreparedBuilder { - GGLWECiphertextPreparedBuilder { + pub fn builder() -> GGLWEPreparedBuilder { + GGLWEPreparedBuilder { data: None, base2k: None, k: None, @@ -78,7 +77,7 @@ impl GGLWECiphertextPrepared { } } -impl GGLWECiphertextPreparedBuilder, B> { +impl GGLWEPreparedBuilder, B> { #[inline] pub fn layout(mut self, infos: &A) -> Self where @@ -99,7 +98,7 @@ impl GGLWECiphertextPreparedBuilder, B> { } } -impl GGLWECiphertextPreparedBuilder { +impl GGLWEPreparedBuilder { #[inline] pub fn data(mut self, data: VmpPMat) -> Self { self.data = Some(data); @@ -122,7 +121,7 @@ impl GGLWECiphertextPreparedBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -152,7 +151,7 @@ impl GGLWECiphertextPreparedBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGLWECiphertextPrepared { + Ok(GGLWEPrepared { data, base2k, k, @@ -161,7 +160,7 @@ impl GGLWECiphertextPreparedBuilder { } } -impl GGLWECiphertextPrepared, B> { +impl GGLWEPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -260,12 +259,21 @@ impl GGLWECiphertextPrepared, B> { } } -impl PrepareScratchSpace for GGLWECiphertextPrepared, B> +pub trait GGLWEPrepareTmpBytes { + fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GGLWEPrepareTmpBytes for Module where Module: VmpPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + fn gglwe_prepare_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(), @@ -274,25 +282,106 @@ where } } -impl Prepare> for GGLWECiphertextPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; +impl GGLWEPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) + where + Module: GGLWEPrepareTmpBytes, + { + module.gglwe_prepare_tmp_bytes(self) } } -impl PrepareAlloc, B>> for GGLWECiphertext +pub trait GGLWEPrepare { + fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut, + O: GGLWEToRef; +} + +impl GGLWEPrepare for Module +where + Module: VmpPrepare, +{ + fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut, + O: GGLWEToRef, + { + let mut res: GGLWEPrepared<&mut [u8], B> = self.to_mut(); + let other: GGLWE<&[u8]> = other.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(other.n(), self.n() as u32); + 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 GGLWEPrepared +where + Module: GGLWEPrepare, +{ + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GGLWEToRef, + { + module.gglwe_prepare(self, other, scratch); + } +} + +pub trait GGLWEPrepareAlloc { + fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B>; +} + +impl GGLWEPrepareAlloc for Module where Module: VmpPMatAlloc + VmpPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWECiphertextPrepared, B> { - let mut atk_prepared: GGLWECiphertextPrepared, B> = GGLWECiphertextPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared + fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B> { + let mut ct_prepared: GGLWEPrepared, B> = GGLWEPrepared::alloc(self, &other.to_ref()); + ct_prepared.prepare(self, &other.to_ref(), scratch); + ct_prepared + } +} + +impl GGLWE { + fn prepare_alloc(&self, module: &Module, scratch: &Scratch) -> GGLWEPrepared, B> + where + Module: GGLWEPrepareAlloc, + { + module.gglwe_prepare_alloc(self, scratch) + } +} + +pub trait GGLWEPreparedToMut { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; +} + +impl GGLWEPreparedToMut for GGLWEPrepared { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_mut(), + } + } +} + +pub trait GGLWEPreparedToRef { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B>; +} + +impl GGLWEPreparedToRef for GGLWEPrepared { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index c9110c1..7f2f887 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,21 +1,36 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + prepared::{GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] -pub struct GGLWESwitchingKeyPrepared { - pub(crate) key: GGLWECiphertextPrepared, +pub struct GLWESwitchingKeyPrepared { + pub(crate) key: GGLWEPrepared, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKeyPrepared { +pub(crate) trait GLWESwitchingKeyPreparedSetMetaData { + fn set_sk_in_n(&mut self, sk_in_n: usize); + fn set_sk_out_n(&mut self, sk_out_n: usize); +} + +impl GLWESwitchingKeyPreparedSetMetaData for GLWESwitchingKeyPrepared { + fn set_sk_in_n(&mut self, sk_in_n: usize) { + self.sk_in_n = sk_in_n + } + + fn set_sk_out_n(&mut self, sk_out_n: usize) { + self.sk_out_n = self.sk_out_n + } +} + +impl LWEInfos for GLWESwitchingKeyPrepared { fn n(&self) -> Degree { self.key.n() } @@ -33,13 +48,13 @@ impl LWEInfos for GGLWESwitchingKeyPrepared { } } -impl GLWEInfos for GGLWESwitchingKeyPrepared { +impl GLWEInfos for GLWESwitchingKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyPrepared { +impl GGLWEInfos for GLWESwitchingKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -57,15 +72,15 @@ impl GGLWEInfos for GGLWESwitchingKeyPrepared { } } -impl GGLWESwitchingKeyPrepared, B> { +impl GLWESwitchingKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, Module: VmpPMatAlloc, { debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc(module, infos), + GLWESwitchingKeyPrepared::, B> { + key: GGLWEPrepared::alloc(module, infos), sk_in_n: 0, sk_out_n: 0, } @@ -83,8 +98,8 @@ impl GGLWESwitchingKeyPrepared, B> { where Module: VmpPMatAlloc, { - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), + GLWESwitchingKeyPrepared::, B> { + key: GGLWEPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } @@ -96,7 +111,7 @@ impl GGLWESwitchingKeyPrepared, B> { Module: VmpPMatAllocBytes, { debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWECiphertextPrepared::alloc_bytes(module, infos) + GGLWEPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with( @@ -111,37 +126,122 @@ impl GGLWESwitchingKeyPrepared, B> { where Module: VmpPMatAllocBytes, { - GGLWECiphertextPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) + GGLWEPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) } } -impl PrepareScratchSpace for GGLWESwitchingKeyPrepared, B> +pub trait GLWESwitchingKeyPrepareTmpBytes { + fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyPrepareTmpBytes for Module where - GGLWECiphertextPrepared, B>: PrepareScratchSpace, + Module: GGLWEPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWECiphertextPrepared::prepare_scratch_space(module, infos) + fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.gglwe_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWESwitchingKeyPrepared +impl GLWESwitchingKeyPrepared, B> where - Module: VmpPrepare, + Module: GGLWEPrepareTmpBytes, { - fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { + module.gglwe_prepare_tmp_bytes(self) } } -impl PrepareAlloc, B>> for GGLWESwitchingKey +pub trait GLWESwitchingKeyPrepare { + fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + where + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, + O: GLWESwitchingKeyToRef; +} + +impl GLWESwitchingKeyPrepare for Module where - Module: VmpPMatAlloc + VmpPrepare, + Module: GGLWEPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWESwitchingKeyPrepared, B> { - let mut atk_prepared: GGLWESwitchingKeyPrepared, B> = GGLWESwitchingKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared + fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + where + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, + O: GLWESwitchingKeyToRef, + { + self.gglwe_prepare(&res.to_mut(), other, scratch); + res.set_sk_in_n(other.sk_in_n); + res.set_sk_out_n(other.sk_out_n); + } +} + +impl GLWESwitchingKeyPrepared { + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef, + Module: GLWESwitchingKeyPrepare, + { + module.glwe_switching_prepare(self, other, scratch); + } +} + +pub trait GLWESwitchingKeyPrepareAlloc { + fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef; +} + +impl GLWESwitchingKeyPrepareAlloc for Module +where + Module: GLWESwitchingKeyPrepare, +{ + fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef, + { + let mut ct_prepared: GLWESwitchingKeyPrepared, B> = GLWESwitchingKeyPrepared::alloc(self, self); + self.glwe_switching_prepare(&mut ct_prepared, other, scratch); + ct_prepared + } +} + +impl GLWESwitchingKey { + pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWESwitchingKeyPrepared, B> + where + Module: GLWESwitchingKeyPrepareAlloc, + { + module.glwe_switching_key_prepare_alloc(self, scratch); + } +} + +pub trait GLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_mut(), + } + } +} + +pub trait GLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; +} + +impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 4343e30..eed6ff2 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,19 +1,23 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + compressed::TensorKeyCompressedToMut, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWETensorKeyPrepared { - pub(crate) keys: Vec>, +pub struct TensorKeyPrepared { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyPrepared { +impl LWEInfos for TensorKeyPrepared { fn n(&self) -> Degree { self.keys[0].n() } @@ -31,13 +35,13 @@ impl LWEInfos for GGLWETensorKeyPrepared { } } -impl GLWEInfos for GGLWETensorKeyPrepared { +impl GLWEInfos for TensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyPrepared { +impl GGLWEInfos for TensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } @@ -55,7 +59,7 @@ impl GGLWEInfos for GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared, B> { +impl TensorKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -80,10 +84,10 @@ impl GGLWETensorKeyPrepared, B> { where Module: VmpPMatAlloc, { - let mut keys: Vec, B>> = Vec::new(); + let mut keys: Vec, B>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyPrepared::alloc_with( + keys.push(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -109,7 +113,7 @@ impl GGLWETensorKeyPrepared, B> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWESwitchingKeyPrepared::alloc_bytes_with( + * GLWESwitchingKeyPrepared::alloc_bytes_with( module, infos.base2k(), infos.k(), @@ -125,13 +129,13 @@ impl GGLWETensorKeyPrepared, B> { Module: VmpPMatAllocBytes, { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPrepared { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -140,9 +144,9 @@ impl GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -151,40 +155,125 @@ impl GGLWETensorKeyPrepared { } } -impl PrepareScratchSpace for GGLWETensorKeyPrepared, B> +pub trait TensorKeyPrepareTmpBytes { + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize; +} + +impl TensorKeyPrepareTmpBytes for Module where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: GLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize { + self.glwe_switching_key_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWETensorKeyPrepared +impl TensorKeyPrepared, B> where - Module: VmpPrepare, + Module: TensorKeyPrepareTmpBytes, { - fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) { - #[cfg(debug_assertions)] - { - assert_eq!(self.keys.len(), other.keys.len()); + fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.tensor_key_prepare_tmp_bytes(infos) + } +} + +pub trait TensorKeyPrepare { + fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef; +} + +impl TensorKeyPrepare for Module +where + Module: GLWESwitchingKeyPrepare, +{ + fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef, + { + let res = res.to_mut(); + let other = other.to_ref(); + + assert_eq!(self.keys.len(), other.keys.len()); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.glwe_switching_prepare(a, b, scratch); } - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.prepare(module, b, scratch); - }); } } -impl PrepareAlloc, B>> for GGLWETensorKey +impl TensorKeyPrepared where - Module: VmpPMatAlloc + VmpPrepare, + Module: TensorKeyPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWETensorKeyPrepared, B> { - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, self); - tsk_prepared.prepare(module, self, scratch); - tsk_prepared + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef, + { + module.tensor_key_prepare(self, other, scratch); + } +} + +pub trait TensorKeyPrepareAlloc { + fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef; +} + +impl TensorKeyPrepareAlloc for Module +where + Module: TensorKeyPrepare, +{ + fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef, + { + let mut ct_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(self, other); + self.tensor_key_prepare(ct_prepared, other, scratch); + ct_prepared + } +} + +impl TensorKey { + pub fn prepare_alloc(&self, module: &Module, scratch: &Scratch) + where + Module: TensorKeyPrepareAlloc, + { + module.tensor_key_prepare_alloc(self, scratch); + } +} + +pub trait TensorKeyPreparedToMut { + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +} + +impl TensorKeyPreparedToMut for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { + TensorKeyPrepared { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} + +pub trait TensorKeyPreparedToRef { + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +} + +impl TensorKeyPreparedToRef for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { + TensorKeyPrepared { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } } } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index a3871f6..120c9ed 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -5,19 +5,18 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGSWCiphertextPrepared { +pub struct GGSWPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertextPrepared { +impl LWEInfos for GGSWPrepared { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -35,13 +34,13 @@ impl LWEInfos for GGSWCiphertextPrepared { } } -impl GLWEInfos for GGSWCiphertextPrepared { +impl GLWEInfos for GGSWPrepared { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertextPrepared { +impl GGSWInfos for GGSWPrepared { fn dsize(&self) -> Dsize { self.dsize } @@ -58,7 +57,7 @@ pub struct GGSWCiphertextPreparedBuilder { dsize: Option, } -impl GGSWCiphertextPrepared { +impl GGSWPrepared { #[inline] pub fn builder() -> GGSWCiphertextPreparedBuilder { GGSWCiphertextPreparedBuilder { @@ -129,7 +128,7 @@ impl GGSWCiphertextPreparedBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -159,7 +158,7 @@ impl GGSWCiphertextPreparedBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGSWCiphertextPrepared { + Ok(GGSWPrepared { data, base2k, k, @@ -168,7 +167,7 @@ impl GGSWCiphertextPreparedBuilder { } } -impl GGSWCiphertextPrepared, B> { +impl GGSWPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGSWInfos, @@ -252,18 +251,27 @@ impl GGSWCiphertextPrepared, B> { } } -impl GGSWCiphertextPrepared { +impl GGSWPrepared { pub fn data(&self) -> &VmpPMat { &self.data } } -impl PrepareScratchSpace for GGSWCiphertextPrepared, B> +pub trait GGSWPrepareTmpBytes { + fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; +} + +impl GGSWPrepareTmpBytes for Module where Module: VmpPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), @@ -272,57 +280,110 @@ where } } -impl Prepare> for GGSWCiphertextPrepared +impl GGSWPrepared, B> +where + Module: GGSWPrepareTmpBytes, +{ + pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepare_tmp_bytes(self) + } +} + +pub trait GGSWPrepare { + fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGSWPreparedToMut, + O: GGSWToRef; +} + +impl GGSWPrepare for Module where Module: VmpPrepare, { - fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; + 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.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 PrepareAlloc, B>> for GGSWCiphertext +impl GGSWPrepared where - Module: VmpPMatAlloc + VmpPrepare, + Module: GGSWPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGSWCiphertextPrepared, B> { - let mut ggsw_prepared: GGSWCiphertextPrepared, B> = GGSWCiphertextPrepared::alloc(module, self); - ggsw_prepared.prepare(module, self, scratch); - ggsw_prepared + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef, + { + module.ggsw_prepare(self, other, scratch); } } -pub trait GGSWCiphertextPreparedToMut { - fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B>; +pub trait GGSWPrepareAlloc { + fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef; } -impl GGSWCiphertextPreparedToMut for GGSWCiphertextPrepared { - fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B> { - GGSWCiphertextPrepared::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl GGSWPrepareAlloc for Module +where + Module: GGSWPrepare, +{ + fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef, + { + let mut ct_prepared: GGSWPrepared, B> = GGSWPrepared::alloc(self, other); + self.ggsw_prepare(&mut ct_prepared, other, scratch); + ct_prepared + } +} + +impl GGSW { + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) + where + Module: GGSWPrepareAlloc, + { + module.ggsw_prepare_alloc(self, scratch); + } +} + +pub trait GGSWPreparedToMut { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; +} + +impl GGSWPreparedToMut for GGSWPrepared { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_mut(), + } } } pub trait GGSWCiphertextPreparedToRef { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>; + fn to_ref(&self) -> GGSWPrepared<&[u8], B>; } -impl GGSWCiphertextPreparedToRef for GGSWCiphertextPrepared { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B> { - GGSWCiphertextPrepared::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GGSWCiphertextPreparedToRef for GGSWPrepared { + fn to_ref(&self) -> GGSWPrepared<&[u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 3211017..0cbbdd0 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -6,10 +6,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{ - Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, - }, + layouts::{Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 234ea2a..a8e436d 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, - }, + layouts::{Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision}, }; pub struct GLWESecretPrepared { @@ -128,11 +125,11 @@ impl GLWESecretPreparedToRef for GLWESecretPrepared { - fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B>; + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B>; } impl GLWESecretPreparedToMut for GLWESecretPrepared { - fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B> { + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B> { GLWESecretPrepared { dist: self.dist, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index f241c6d..cea8b41 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -4,12 +4,12 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWEKey, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision, + prepared::GLWESwitchingKeyPrepared, }; #[derive(PartialEq, Eq)] -pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for GLWEToLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -69,14 +69,14 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -102,27 +102,27 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } } impl PrepareScratchSpace for GLWEToLWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } -impl PrepareAlloc, B>> for GLWEToLWEKey +impl PrepareAlloc, B>> for GLWEToLWESwitchingKey where Module: VmpPrepare + VmpPMatAlloc, { @@ -133,11 +133,11 @@ where } } -impl Prepare> for GLWEToLWESwitchingKeyPrepared +impl Prepare> for GLWEToLWESwitchingKeyPrepared where Module: VmpPrepare, { - fn prepare(&mut self, module: &Module, other: &GLWEToLWEKey, scratch: &mut Scratch) { + fn prepare(&mut self, module: &Module, other: &GLWEToLWESwitchingKey, scratch: &mut Scratch) { self.0.prepare(module, &other.0, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 5f0cf14..8e21f23 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + prepared::GLWESwitchingKeyPrepared, }; #[derive(PartialEq, Eq)] -pub struct LWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -73,14 +73,14 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -111,23 +111,23 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } impl PrepareScratchSpace for LWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 7c2023a..253c8cc 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + prepared::GLWESwitchingKeyPrepared, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. #[derive(PartialEq, Eq)] -pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWEToGLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -70,14 +70,14 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -103,23 +103,23 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } } impl PrepareScratchSpace for LWEToGLWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/prepared/mod.rs b/poulpy-core/src/layouts/prepared/mod.rs index eb47848..296144a 100644 --- a/poulpy-core/src/layouts/prepared/mod.rs +++ b/poulpy-core/src/layouts/prepared/mod.rs @@ -19,16 +19,3 @@ pub use glwe_sk::*; pub use glwe_to_lwe_ksk::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; -use poulpy_hal::layouts::{Backend, Module, Scratch}; - -pub trait PrepareScratchSpace { - fn prepare_scratch_space(module: &Module, infos: &T) -> usize; -} - -pub trait PrepareAlloc { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> T; -} - -pub trait Prepare { - fn prepare(&mut self, module: &Module, other: &T, scratch: &mut Scratch); -} diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 0712b7f..70a8ef3 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGLWE, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGLWECiphertext { +impl GGLWE { pub fn assert_noise( &self, module: &Module, diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 03bb0c0..84a2f87 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -9,11 +9,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{ - GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, -}; +use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGSWCiphertext { +impl GGSW { pub fn assert_noise( &self, module: &Module, @@ -87,7 +85,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext { +impl GGSW { pub fn print_noise( &self, module: &Module, diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 1a5a6ce..0a94b39 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, + GLWESecret, GLWESwitchingKey, Rank, TensorKey, prepared::{ - GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, - GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + GLWESwitchingKeyPrepared, TensorKeyPrepared, }, }, }; @@ -34,31 +34,31 @@ pub trait TakeGLWEPt { } pub trait TakeGGLWE { - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEPrepared { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGSW { - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos; } pub trait TakeGGSWPrepared { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos; } pub trait TakeGGSWPreparedSlice { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GGSWInfos; } @@ -84,37 +84,37 @@ pub trait TakeGLWEPkPrepared { } pub trait TakeGLWESwitchingKey { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWESwitchingKeyPrepared { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeTensorKey { - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWETensorKeyPrepared { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEAutomorphismKey { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEAutomorphismKeyPrepared { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } @@ -184,7 +184,7 @@ impl TakeGGLWE for Scratch where Scratch: TakeMatZnx, { - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, { @@ -196,7 +196,7 @@ where infos.size(), ); ( - GGLWECiphertext::builder() + GGLWE::builder() .base2k(infos.base2k()) .k(infos.k()) .dsize(infos.dsize()) @@ -212,7 +212,7 @@ impl TakeGGLWEPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { @@ -224,7 +224,7 @@ where infos.size(), ); ( - GGLWECiphertextPrepared::builder() + GGLWEPrepared::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -240,7 +240,7 @@ impl TakeGGSW for Scratch where Scratch: TakeMatZnx, { - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, { @@ -252,7 +252,7 @@ where infos.size(), ); ( - GGSWCiphertext::builder() + GGSW::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -268,7 +268,7 @@ impl TakeGGSWPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos, { @@ -280,7 +280,7 @@ where infos.size(), ); ( - GGSWCiphertextPrepared::builder() + GGSWPrepared::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -296,12 +296,12 @@ impl TakeGGSWPreparedSlice for Scratch where Scratch: TakeGGSWPrepared, { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GGSWInfos, { let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { let (ct, new_scratch) = scratch.take_ggsw_prepared(infos); scratch = new_scratch; @@ -390,13 +390,13 @@ impl TakeGLWESwitchingKey for Scratch where Scratch: TakeMatZnx, { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe(infos); ( - GGLWESwitchingKey { + GLWESwitchingKey { key: data, sk_in_n: 0, sk_out_n: 0, @@ -410,13 +410,13 @@ impl TakeGGLWESwitchingKeyPrepared for Scratch where Scratch: TakeGGLWEPrepared, { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe_prepared(infos); ( - GGLWESwitchingKeyPrepared { + GLWESwitchingKeyPrepared { key: data, sk_in_n: 0, sk_out_n: 0, @@ -430,12 +430,12 @@ impl TakeGGLWEAutomorphismKey for Scratch where Scratch: TakeMatZnx, { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_glwe_switching_key(infos); - (GGLWEAutomorphismKey { key: data, p: 0 }, scratch) + (AutomorphismKey { key: data, p: 0 }, scratch) } } @@ -443,12 +443,12 @@ impl TakeGGLWEAutomorphismKeyPrepared for Scratch where Scratch: TakeGGLWESwitchingKeyPrepared, { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); - (GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) + (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } } @@ -456,7 +456,7 @@ impl TakeTensorKey for Scratch where Scratch: TakeMatZnx, { - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { @@ -465,7 +465,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Scratch = self; @@ -483,7 +483,7 @@ where scratch = s; keys.push(gglwe); } - (GGLWETensorKey { keys }, scratch) + (TensorKey { keys }, scratch) } } @@ -491,7 +491,7 @@ impl TakeGGLWETensorKeyPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { @@ -501,7 +501,7 @@ where "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Scratch = self; @@ -519,6 +519,6 @@ where scratch = s; keys.push(gglwe); } - (GGLWETensorKeyPrepared { keys }, scratch) + (TensorKeyPrepared { keys }, scratch) } } diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 6e08395..50dc0d6 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,12 +1,12 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, - GLWECiphertext, GLWEToLWEKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey, + LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWEKeyCompressed, GGLWETensorKeyCompressed, - GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, - LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, + GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, + LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed, }, }; @@ -44,7 +44,7 @@ fn lwe_compressed_serialization() { #[test] fn test_gglwe_serialization() { - let original: GGLWECiphertext> = GGLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -57,44 +57,45 @@ fn test_gglwe_compressed_serialization() { #[test] fn test_glwe_switching_key_serialization() { - let original: GGLWESwitchingKey> = GGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { - let original: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKeyCompressed> = + GLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let original: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey> = AutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let original: GGLWEAutomorphismKeyCompressed> = - GGLWEAutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = + AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: GGLWETensorKey> = GGLWETensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWEKey> = GLWEToLWEKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } @@ -132,7 +133,7 @@ fn lwe_switching_key_compressed_serialization() { #[test] fn ggsw_serialization() { - let original: GGSWCiphertext> = GGSWCiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc_with(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 1dd3e58..34fccab 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -84,7 +84,7 @@ where let dnum_out: usize = k_out / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_in_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +93,7 @@ where rank: rank.into(), }; - let auto_key_out_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -102,7 +102,7 @@ where rank: rank.into(), }; - let auto_key_apply_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -111,18 +111,18 @@ where rank: rank.into(), }; - let mut auto_key_in: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) - | GGLWEAutomorphismKey::automorphism_scratch_space( + AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) + | AutomorphismKey::automorphism_scratch_space( module, &auto_key_out_infos, &auto_key_in_infos, @@ -153,8 +153,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -293,7 +293,7 @@ where let dnum_in: usize = k_in / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -302,7 +302,7 @@ where rank: rank.into(), }; - let auto_key_apply_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -311,17 +311,17 @@ where rank: rank.into(), }; - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) - | GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), + AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) + | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key); @@ -347,8 +347,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(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 2fd7151..8d34288 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -19,8 +19,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -110,7 +110,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -119,7 +119,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -128,10 +128,10 @@ where rank: rank.into(), }; - let mut ct_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_layout); - let mut ct_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct_in: GGSW> = GGSW::alloc(&ggsw_in_layout); + let mut ct_out: GGSW> = GGSW::alloc(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -139,10 +139,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct_in) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), + GGSW::encrypt_sk_scratch_space(module, &ct_in) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) + | GGSW::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; @@ -178,11 +178,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -291,7 +291,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -300,7 +300,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -309,9 +309,9 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct: GGSW> = GGSW::alloc(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -319,10 +319,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), + GGSW::encrypt_sk_scratch_space(module, &ct) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) + | GGSW::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; @@ -358,11 +358,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); 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 5828c48..a15b2c1 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -91,7 +91,7 @@ where rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -100,7 +100,7 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&ct_in_infos); let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); @@ -112,7 +112,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) + AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) | GLWECiphertext::decrypt_scratch_space(module, &ct_out) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in) | GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), @@ -140,8 +140,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey_infos); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); @@ -226,7 +226,7 @@ where rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -235,7 +235,7 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); @@ -246,7 +246,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) + AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) | GLWECiphertext::decrypt_scratch_space(module, &ct) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct) | GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey), @@ -274,8 +274,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(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 c2c81dc..7e3beba 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -16,9 +16,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, - TorusPrecision, + Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, + GLWEToLWESwitchingKey, LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, + LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, }; @@ -214,7 +214,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWEToLWEKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) + GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) | LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), ); @@ -241,7 +241,7 @@ where scratch.borrow(), ); - let mut ksk: GLWEToLWEKey> = GLWEToLWEKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); ksk.encrypt_sk( module, 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 e9b7c93..476abd1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, - compressed::{Decompress, GGLWEAutomorphismKeyCompressed}, + AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, + compressed::{AutomorphismKeyCompressed, Decompress}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -75,7 +75,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -84,13 +84,13 @@ where rank: rank.into(), }; - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( module, &atk_infos, )); @@ -178,7 +178,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -187,12 +187,12 @@ where rank: rank.into(), }; - let mut atk_compressed: GGLWEAutomorphismKeyCompressed> = GGLWEAutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&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(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( module, &atk_infos, )); @@ -217,7 +217,7 @@ where }); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); atk.decompress(module, &atk_compressed); atk.key 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 249161b..b0a8a8b 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWECiphertextLayout, GGLWESwitchingKey, GLWESecret, - compressed::{Decompress, GGLWEKeyCompressed}, + GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey, + compressed::{Decompress, GLWESwitchingKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -81,13 +81,13 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_scratch_space( module, &gglwe_infos, )); @@ -173,12 +173,12 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed> = GLWESwitchingKeyCompressed::alloc(&gglwe_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(GGLWEKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_scratch_space( module, &gglwe_infos, )); @@ -201,7 +201,7 @@ where scratch.borrow(), ); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key 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 9eacf38..13ed1de 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -17,53 +17,16 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, + GGSW, GGSWCiphertextLayout, GLWESecret, compressed::{Decompress, GGSWCiphertextCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; -pub fn test_ggsw_encrypt_sk(module: &Module) +pub fn test_ggsw_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolAllocBytes - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxBigAllocBytes - + VecZnxBigAddInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc, + Module: SvpPrepare, { let base2k: usize = 12; let k: usize = 54; @@ -82,7 +45,7 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -92,10 +55,7 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertext::encrypt_sk_scratch_space( - module, - &ggsw_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -206,7 +166,7 @@ where let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); 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 8bb9730..2944d59 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - Dsize, GGLWETensorKey, GGLWETensorKeyLayout, GLWEPlaintext, GLWESecret, - compressed::{Decompress, GGLWETensorKeyCompressed}, + Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, + compressed::{Decompress, TensorKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -71,7 +71,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos = GGLWETensorKeyLayout { + let tensor_key_infos = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -80,13 +80,13 @@ where rank: rank.into(), }; - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_scratch_space( module, &tensor_key_infos, )); @@ -190,7 +190,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -199,12 +199,12 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc(&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(GGLWETensorKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_scratch_space( module, &tensor_key_infos, )); @@ -217,7 +217,7 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&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 2cfa618..15ac048 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 @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -81,7 +81,7 @@ where let dnum: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_in_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_in_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -91,7 +91,7 @@ where rank_out: rank_out.into(), }; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,9 +110,9 @@ where rank: rank_out.into(), }; - let mut ct_gglwe_in: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -121,14 +121,14 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) - | GGLWESwitchingKey::external_product_scratch_space( + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) + | GLWESwitchingKey::external_product_scratch_space( module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos, ) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), ); let r: usize = 1; @@ -163,7 +163,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); @@ -263,7 +263,7 @@ where let dsize_in: usize = 1; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -282,8 +282,8 @@ where rank: rank_out.into(), }; - let mut ct_gglwe: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -292,9 +292,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) - | GGLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) + | GLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), ); let r: usize = 1; @@ -329,7 +329,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); 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 464eac2..32f15e9 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 @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -128,9 +128,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGSWCiphertext::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) + | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); @@ -155,7 +155,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); @@ -265,8 +265,8 @@ where rank: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -282,9 +282,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGSWCiphertext::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) + | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); @@ -309,7 +309,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); 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 1a21a0c..efd5af0 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 @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -96,7 +96,7 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -116,7 +116,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); @@ -143,7 +143,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); @@ -240,7 +240,7 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); @@ -259,7 +259,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), ); @@ -286,7 +286,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); 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 f131556..dfde51e 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -80,7 +80,7 @@ where let dnum_apply: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -90,7 +90,7 @@ where rank_out: rank_out_s0s1.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -100,7 +100,7 @@ where rank_out: rank_out_s1s2.into(), }; - let gglwe_s0s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,20 +110,20 @@ where rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_scratch_space( module, &gglwe_s0s1_infos, &gglwe_s0s2_infos, @@ -160,7 +160,7 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) @@ -246,7 +246,7 @@ where let dnum: usize = k_out.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -256,7 +256,7 @@ where rank_out: rank_out.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -266,18 +266,18 @@ where rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_inplace_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_scratch_space( module, &gglwe_s0s1_infos, &gglwe_s1s2_infos, @@ -315,13 +315,13 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow()); - let gglwe_s0s2: GGLWESwitchingKey> = gglwe_s0s1; + let gglwe_s0s2: GLWESwitchingKey> = gglwe_s0s1; let max_noise: f64 = log2_std_noise_gglwe_product( n as f64, 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 c7e7c82..91114a7 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -18,9 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, - GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, TensorKey, TensorKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -103,7 +102,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -112,7 +111,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -122,10 +121,10 @@ where rank_out: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -133,10 +132,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_scratch_space( + GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) + | GGSW::keyswitch_scratch_space( module, &ggsw_out_infos, &ggsw_in_infos, @@ -182,8 +181,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch( module, @@ -282,7 +281,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -291,7 +290,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -301,9 +300,9 @@ where rank_out: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -311,10 +310,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) + | GGSW::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), ); let var_xs: f64 = 0.5; @@ -354,8 +353,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); 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 d745f1d..7c8c1ec 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -91,7 +91,7 @@ where rank: rank_out.into(), }; - let key_apply: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -101,7 +101,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply); let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); @@ -113,7 +113,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) + GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); @@ -144,7 +144,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -224,7 +224,7 @@ where rank: rank.into(), }; - let key_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -234,7 +234,7 @@ where rank_out: rank.into(), }; - let mut key_apply: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply_infos); + let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); @@ -245,7 +245,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), ); @@ -276,7 +276,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index de7fc9d..354738b 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -21,8 +21,8 @@ use poulpy_hal::{ use crate::{ GLWEOperations, GLWEPacker, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; @@ -95,7 +95,7 @@ where rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -106,7 +106,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), ); @@ -124,8 +124,8 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = HashMap::new(); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -135,7 +135,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, 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 bf348ca..d3a8976 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -21,9 +21,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - LWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWEInfos, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product, }; @@ -90,7 +89,7 @@ where rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_autokey.into(), @@ -110,7 +109,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), ); @@ -135,9 +134,9 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut auto_keys: HashMap, B>> = HashMap::new(); let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -147,7 +146,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 47c848e..12a44fd 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,8 +3,8 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, LWECiphertext, - LWECiphertextLayout, LWESecret, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret, + TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_hal::{ api::{ @@ -218,7 +218,7 @@ where scratch.borrow(), ); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(¶ms.ggsw_infos); + let mut res: GGSW> = GGSW::alloc(¶ms.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || { @@ -261,7 +261,7 @@ where dnum: 3_u32.into(), rank: 2_u32.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -269,7 +269,7 @@ where dsize: Dsize(1), rank: 2_u32.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 4fec699..2ac8eb2 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,9 +1,9 @@ use poulpy_core::{ GLWEOperations, layouts::{ - GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, - GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; use std::time::Instant; @@ -89,7 +89,7 @@ fn main() { dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_trace.into(), @@ -97,7 +97,7 @@ fn main() { dsize: 1_u32.into(), rank: rank.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -187,7 +187,7 @@ fn main() { println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = @@ -249,7 +249,7 @@ fn main() { ); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWCiphertextPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let res_prepared: GGSWPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); // Apply GLWE x GGSW ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 3cd50a6..3213894 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -1,12 +1,10 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{ - Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWCiphertextPrepared, -}; +use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}; #[cfg(test)] use poulpy_core::{ TakeGGSW, - layouts::{GGSWCiphertext, prepared::GLWESecretPrepared}, + layouts::{GGSW, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ api::VmpPMatAlloc, @@ -29,7 +27,7 @@ use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, Unsigne #[cfg(test)] pub(crate) struct FheUintBlocksPrepDebug { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -62,7 +60,7 @@ impl FheUintBlocksPrepDebug, T> { ) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertext::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) + .map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) .collect(), _base: 1, _phantom: PhantomData, @@ -72,7 +70,7 @@ impl FheUintBlocksPrepDebug, T> { /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocksPrep { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -103,7 +101,7 @@ where { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertextPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) + .map(|_| GGSWPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index cc754bc..b09b8a5 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, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, layouts::{ GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared}, + prepared::{AutomorphismKeyPrepared, 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/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 98e1083..13f34e1 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -3,7 +3,7 @@ use poulpy_core::{ GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, layouts::{ GLWECiphertext, GLWECiphertextToMut, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, }; use poulpy_hal::{ @@ -164,7 +164,7 @@ pub trait Cmux { out: &mut GLWECiphertext, t: &GLWECiphertext, f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, + s: &GGSWPrepared, scratch: &mut Scratch, ) where O: DataMut, @@ -182,7 +182,7 @@ where out: &mut GLWECiphertext, t: &GLWECiphertext, f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, + s: &GGSWPrepared, scratch: &mut Scratch, ) where O: DataMut, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index febabed..322a2e2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -11,7 +11,7 @@ use crate::tfhe::{ use poulpy_core::{ TakeGGSW, TakeGLWECt, layouts::{ - GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, }; @@ -56,7 +56,7 @@ where BRA: BlindRotationAlgo, { cbt: CircuitBootstrappingKey, - ks: GLWEToLWEKey, + ks: GLWEToLWESwitchingKey, } impl BDDKey, Vec, BRA> { @@ -98,7 +98,7 @@ impl BDDKey, Vec, BRA> { + VecZnxAutomorphismInplace, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, { - let mut ks: GLWEToLWEKey> = GLWEToLWEKey::alloc(&infos.ks_infos()); + let mut ks: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos()); ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); Self { @@ -131,7 +131,7 @@ impl PrepareAll for BDDKey where CircuitBootstrappingKey: PrepareAlloc>, - GLWEToLWEKey: PrepareAlloc>, + GLWEToLWESwitchingKey: PrepareAlloc>, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BDDKeyPrepared { BDDKeyPrepared { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 6b56f79..5a7ae97 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,7 +1,7 @@ #[cfg(test)] use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, GLWECiphertextLayout, - GLWEToLWEKeyLayout, Rank, TorusPrecision, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank, + TensorKeyLayout, TorusPrecision, }; #[cfg(test)] @@ -53,7 +53,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), rank: Rank(TEST_RANK), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), @@ -61,7 +61,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index fbf506b..a8fa39a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -14,9 +14,9 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - GGSWCiphertext, GGSWInfos, LWESecret, + GGSW, GGSWInfos, LWESecret, compressed::GGSWCiphertextCompressed, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; @@ -30,9 +30,9 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSWCiphertext::alloc(infos)); + data.push(GGSW::alloc(infos)); } Self { @@ -49,7 +49,7 @@ impl BlindRotationKey, CGGI> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_scratch_space(module, infos) } } @@ -121,8 +121,8 @@ where where A: BlindRotationKeyInfos, { - let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextPrepared::alloc(module, infos))); + let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos))); Self { data, dist: Distribution::NONE, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/key.rs index 86dbd21..d9bb0a9 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -98,7 +98,7 @@ pub trait BlindRotationKeyEncryptSk { #[derive(Clone)] pub struct BlindRotationKey { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs index 8719de4..2001083 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -9,7 +9,7 @@ use poulpy_core::{ Distribution, layouts::{ Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGSWCiphertextPrepared, Prepare, PrepareAlloc}, + prepared::{GGSWPrepared, Prepare, PrepareAlloc}, }, }; @@ -23,7 +23,7 @@ pub trait BlindRotationKeyPreparedAlloc { #[derive(PartialEq, Eq)] pub struct BlindRotationKeyPrepared { - pub(crate) data: Vec>, + pub(crate) data: Vec>, pub(crate) dist: Distribution, pub(crate) x_pow_a: Option, B>>>, pub(crate) _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 45b9717..22e7a7a 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::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -74,7 +74,7 @@ where fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -97,7 +97,7 @@ where &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -122,7 +122,7 @@ pub fn circuit_bootstrap_core( to_exponent: bool, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -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 427cf75..3d91943 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::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GGLWETensorKey, GGLWETensorKeyLayout, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, + TensorKey, TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap; @@ -24,19 +24,19 @@ use crate::tfhe::blind_rotation::{ pub trait CircuitBootstrappingKeyInfos { fn brk_infos(&self) -> BlindRotationKeyLayout; - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout; - fn tsk_infos(&self) -> GGLWETensorKeyLayout; + fn atk_infos(&self) -> AutomorphismKeyLayout; + fn tsk_infos(&self) -> TensorKeyLayout; } #[derive(Debug, Clone, Copy)] pub struct CircuitBootstrappingKeyLayout { pub layout_brk: BlindRotationKeyLayout, - pub layout_atk: GGLWEAutomorphismKeyLayout, - pub layout_tsk: GGLWETensorKeyLayout, + pub layout_atk: AutomorphismKeyLayout, + pub layout_tsk: TensorKeyLayout, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { self.layout_atk } @@ -44,7 +44,7 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { self.layout_brk } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { self.layout_tsk } } @@ -68,8 +68,8 @@ pub trait CircuitBootstrappingKeyEncryptSk { pub struct CircuitBootstrappingKey { pub(crate) brk: BlindRotationKey, - pub(crate) tsk: GGLWETensorKey>, - pub(crate) atk: HashMap>>, + pub(crate) tsk: TensorKey>, + pub(crate) atk: HashMap>>, } impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> @@ -117,14 +117,14 @@ where assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); - let atk_infos: GGLWEAutomorphismKeyLayout = cbt_infos.atk_infos(); + let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: GGLWETensorKeyLayout = cbt_infos.tsk_infos(); + let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); - let mut auto_keys: HashMap>> = HashMap::new(); + let mut auto_keys: HashMap>> = HashMap::new(); let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); auto_keys.insert(*gal_el, key); }); @@ -141,7 +141,7 @@ where scratch, ); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&trk_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self { @@ -154,14 +154,14 @@ where pub struct CircuitBootstrappingKeyPrepared { pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: GGLWETensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, + pub(crate) tsk: TensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { let (_, atk) = self.atk.iter().next().expect("atk is empty"); - GGLWEAutomorphismKeyLayout { + AutomorphismKeyLayout { n: atk.n(), base2k: atk.base2k(), k: atk.k(), @@ -182,8 +182,8 @@ impl CircuitBootstrappingKeyInfo } } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { - GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { + TensorKeyLayout { n: self.tsk.n(), base2k: self.tsk.base2k(), k: self.tsk.k(), @@ -199,13 +199,13 @@ impl PrepareAlloc: VmpPMatAlloc + VmpPrepare, BlindRotationKey: PrepareAlloc, BRA, B>>, - GGLWETensorKey: PrepareAlloc, B>>, - GGLWEAutomorphismKey: PrepareAlloc, B>>, + TensorKey: 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: GGLWETensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap, B>> = HashMap::new(); + let tsk: TensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); + let mut atk: HashMap, B>> = HashMap::new(); for (key, value) in &self.atk { atk.insert(*key, value.prepare_alloc(module, scratch)); } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index 86dcf5e..e316d34 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -5,7 +5,7 @@ pub mod tests; pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWECiphertext}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; @@ -13,7 +13,7 @@ pub trait CirtuitBootstrappingExecute { fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -25,7 +25,7 @@ pub trait CirtuitBootstrappingExecute { &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 40f5448..a0dbb42 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -32,12 +32,12 @@ use crate::tfhe::{ }; use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, LWECiphertextLayout, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_core::layouts::{ - GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared}, }; pub fn test_circuit_bootstrapping_to_exponent(module: &Module) @@ -143,7 +143,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -151,7 +151,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -206,7 +206,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); let log_gap_out = 1; @@ -249,7 +249,7 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); @@ -365,7 +365,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -373,7 +373,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -428,7 +428,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); @@ -462,7 +462,7 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); From d28ccb4c8f0e94118d4cf1ece9c10ee9a6f8ccb9 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 13 Oct 2025 12:55:06 +0200 Subject: [PATCH 03/60] wip --- poulpy-core/src/layouts/prepared/glwe_pk.rs | 122 ++++++++++++++++---- poulpy-core/src/layouts/prepared/glwe_sk.rs | 86 +++++++++++--- 2 files changed, 169 insertions(+), 39 deletions(-) diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 0cbbdd0..f730cf8 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] @@ -17,6 +17,16 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } +pub(crate) trait SetDist { + fn set_dist(&mut self, dist: Distribution); +} + +impl SetDist for GLWEPublicKeyPrepared { + fn set_dist(&mut self, dist: Distribution) { + self.dist = dist + } +} + impl LWEInfos for GLWEPublicKeyPrepared { fn base2k(&self) -> Base2K { self.base2k @@ -166,40 +176,104 @@ impl GLWEPublicKeyPrepared, B> { } } -impl PrepareAlloc, B>> for GLWEPublicKey -where - Module: VecZnxDftAlloc + VecZnxDftApply, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEPublicKeyPrepared, B> { - let mut pk_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(module, self); - pk_prepared.prepare(module, self, scratch); - pk_prepared - } +pub trait GLWEPublicKeyPrepareTmpBytes { + fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GLWEInfos; } -impl PrepareScratchSpace for GLWEPublicKeyPrepared, B> { - fn prepare_scratch_space(_module: &Module, _infos: &A) -> usize { +impl GLWEPublicKeyPrepareTmpBytes for Module { + fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GLWEInfos, + { 0 } } -impl Prepare> for GLWEPublicKeyPrepared +impl GLWEPublicKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GLWEInfos, + Module: GLWEPublicKeyPrepareTmpBytes, + { + module.glwe_public_key_prepare_tmp_bytes(infos); + } +} + +pub trait GLWEPublicKeyPrepare { + fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: GLWEPublicKeyPreparedToMut + SetDist, + O: GLWEPublicKeyToRef; +} + +impl GLWEPublicKeyPrepare for Module where - Module: VecZnxDftApply, + Module: VecZnxDftAlloc + VecZnxDftApply, { - fn prepare(&mut self, module: &Module, other: &GLWEPublicKey, _scratch: &mut Scratch) { - #[cfg(debug_assertions)] + fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: GLWEPublicKeyPreparedToMut + SetDist, + O: GLWEPublicKeyToRef, + { { - assert_eq!(self.n(), other.n()); - assert_eq!(self.size(), other.size()); + let res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); + let other: GLWEPublicKey<&[u8]> = other.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.size(), other.size()); + assert_eq!(res.k(), other.k()); + assert_eq!(res.base2k(), other.base2k()); + + for i in 0..(self.rank() + 1).into() { + self.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); + } } - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); - }); - self.k = other.k(); - self.base2k = other.base2k(); - self.dist = other.dist; + res.set_dist(other.dist); + } +} + +impl GLWEPublicKeyPrepared +where + Module: GLWEPublicKeyPrepare, +{ + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GLWEPublicKeyToRef, + { + module.glwe_public_key_prepare(self, other, scratch); + } +} + +pub trait GLWEPublicKeyPrepareAlloc { + fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWEPublicKeyToRef; +} + +impl GLWEPublicKeyPrepareAlloc for Module +where + Module: GLWEPublicKeyPrepare, +{ + fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWEPublicKeyToRef, + { + let mut ct_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(self, other); + self.glwe_public_key_prepare(&mut ct_prepared, ct_prepared, scratch); + ct_prepared + } +} + +impl GLWEPublicKey { + pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) + where + Module: GLWEPublicKeyPrepareAlloc, + { + module.glwe_public_key_prepare_alloc(self, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index a8e436d..c75f425 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -5,7 +5,10 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision}, + layouts::{ + Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToMut, GLWESecretToRef, LWEInfos, Rank, TorusPrecision, + prepared::SetDist, + }, }; pub struct GLWESecretPrepared { @@ -13,6 +16,12 @@ pub struct GLWESecretPrepared { pub(crate) dist: Distribution, } +impl SetDist for GLWESecretPrepared { + fn set_dist(&mut self, dist: Distribution) { + self.dist = dist + } +} + impl LWEInfos for GLWESecretPrepared { fn base2k(&self) -> Base2K { Base2K(0) @@ -82,32 +91,79 @@ impl GLWESecretPrepared { } } -impl PrepareScratchSpace for GLWESecretPrepared, B> { - fn prepare_scratch_space(_module: &Module, _infos: &A) -> usize { +pub trait GLWESecretPrepareTmpBytes { + fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) + where + A: GLWEInfos; +} + +impl GLWESecretPrepareTmpBytes for Module { + fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) + where + A: GLWEInfos, + { 0 } } -impl PrepareAlloc, B>> for GLWESecret +impl GLWESecretPrepared, B> where - Module: SvpPrepare + SvpPPolAlloc, + Module: GLWESecretPrepareTmpBytes, { - fn prepare_alloc(&self, module: &Module, _scratch: &mut Scratch) -> GLWESecretPrepared, B> { - let mut sk_dft: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, self); - sk_dft.prepare(module, self, _scratch); - sk_dft + fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + { + 0 } } -impl Prepare> for GLWESecretPrepared +pub trait GLWESecretPrepare { + fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GLWESecretPreparedToMut + SetDist, + O: GLWESecretToRef; +} + +impl GLWESecretPrepare for Module where Module: SvpPrepare, { - fn prepare(&mut self, module: &Module, other: &GLWESecret, _scratch: &mut Scratch) { - (0..self.rank().into()).for_each(|i| { - module.svp_prepare(&mut self.data, i, &other.data, i); - }); - self.dist = other.dist + fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GLWESecretPreparedToMut + SetDist, + O: GLWESecretToRef, + { + { + let res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); + let other: GLWESecret<&[u8]> = other.to_ref(); + + for i in 0..self.rank().into() { + self.svp_prepare(&mut res.data, i, &other.data, i); + } + } + + res.set_dist(other.dist); + } +} + +pub trait GLWESecretPrepareAlloc { + fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESecretToMut; +} + +impl GLWESecretPrepareAlloc for Module +where + Module: GLWESecretPrepare, +{ + fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESecretToMut, + { + let mut ct_prep: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(self, self); + self.glwe_secret_prepare(&mut ct_prep, other, scratch); + ct_prep } } From d32b4738c3ead1c504513fbd97e2f11271fce9b1 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 13 Oct 2025 17:19:02 +0200 Subject: [PATCH 04/60] wip --- poulpy-core/src/layouts/gglwe_ksk.rs | 30 +++ poulpy-core/src/layouts/lwe_ksk.rs | 8 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 204 ++++++++++----- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 234 +++++++++++------- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 217 ++++++++++------ poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 6 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 125 ++++++++-- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 121 +++++++-- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 121 +++++++-- .../test_suite/automorphism/gglwe_atk.rs | 4 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 4 +- .../tests/test_suite/automorphism/glwe_ct.rs | 5 +- 12 files changed, 776 insertions(+), 303 deletions(-) diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index e8a8b92..7b74193 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -67,6 +67,36 @@ pub struct GLWESwitchingKey { pub(crate) sk_out_n: usize, // Degree of sk_out } +pub(crate) trait GLWESwitchingKeySetMetaData { + fn set_sk_in_n(&mut self, sk_in_n: usize); + fn set_sk_out_n(&mut self, sk_out_n: usize); +} + +impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { + fn set_sk_in_n(&mut self, sk_in_n: usize) { + self.sk_in_n = sk_in_n + } + + fn set_sk_out_n(&mut self, sk_out_n: usize) { + self.sk_out_n = sk_out_n + } +} + +pub(crate) trait GLWESwtichingKeyGetMetaData { + fn sk_in_n(&self) -> usize; + fn sk_out_n(&self) -> usize; +} + +impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { + fn sk_in_n(&self) -> usize { + self.sk_in_n + } + + fn sk_out_n(&self) -> usize { + self.sk_out_n + } +} + impl LWEInfos for GLWESwitchingKey { fn n(&self) -> Degree { self.key.n() diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 8f4a2ed..2d4ba31 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -193,11 +193,11 @@ impl WriterTo for LWESwitchingKey { } } -pub trait LWEToLWEKeyToRef { +pub trait LWESwitchingKeyToRef { fn to_ref(&self) -> LWESwitchingKey<&[u8]>; } -impl LWEToLWEKeyToRef for LWESwitchingKey +impl LWESwitchingKeyToRef for LWESwitchingKey where GLWESwitchingKey: GLWESwitchingKeyToRef, { @@ -206,11 +206,11 @@ where } } -pub trait LWEToLWEKeyToMut { +pub trait LWESwitchingKeyToMut { fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; } -impl LWEToLWEKeyToMut for LWESwitchingKey +impl LWESwitchingKeyToMut for LWESwitchingKey where GLWESwitchingKey: GLWESwitchingKeyToMut, { diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 2eda4e1..32dd177 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,12 +1,11 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ - GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, + GLWESwitchingKeyPreparedAllocBytes, GLWESwitchingKeyPreparedAllocBytesFromInfos, GLWESwitchingKeyPreparedAllocFromInfos, + GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; @@ -16,12 +15,6 @@ pub struct AutomorphismKeyPrepared { pub(crate) p: i64, } -impl AutomorphismKeyPrepared { - pub fn p(&self) -> i64 { - self.p - } -} - impl LWEInfos for AutomorphismKeyPrepared { fn n(&self) -> Degree { self.key.n() @@ -40,11 +33,21 @@ impl LWEInfos for AutomorphismKeyPrepared { } } -pub trait SetP { +pub trait GetAutomorphismGaloisElement { + fn p(&self) -> i64; +} + +impl GetAutomorphismGaloisElement for AutomorphismKeyPrepared { + fn p(&self) -> i64 { + self.p + } +} + +pub trait SetAutomorphismGaloisElement { fn set_p(&mut self, p: i64); } -impl SetP for AutomorphismKeyPrepared { +impl SetAutomorphismGaloisElement for AutomorphismKeyPrepared { fn set_p(&mut self, p: i64) { self.p = p } @@ -74,56 +77,146 @@ impl GGLWEInfos for AutomorphismKeyPrepared { } } -impl AutomorphismKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait AutomorphismKeyPreparedAlloc { + fn automorphism_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyPrepared, B>; +} + +impl AutomorphismKeyPreparedAlloc for Module +where + Module: GLWESwitchingKeyPreparedAlloc, +{ + fn automorphism_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyPrepared, B> { + AutomorphismKeyPrepared::, B> { + key: self.glwe_switching_key_prepared_alloc(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } +} + +pub trait AutomorphismKeyPreparedAllocFromInfos { + fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> + where + A: GGLWEInfos; +} + +impl AutomorphismKeyPreparedAllocFromInfos for Module +where + Module: GLWESwitchingKeyPreparedAllocFromInfos, +{ + fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for AutomorphismKeyPrepared" + ); + AutomorphismKeyPrepared { + key: self.glwe_switching_key_prepared_alloc_from_infos(infos), + p: 0, + } + } +} + +pub trait AutomorphismKeyPreparedAllocBytes { + fn automorphism_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize; +} + +impl AutomorphismKeyPreparedAllocBytes for Module +where + Module: GLWESwitchingKeyPreparedAllocBytes, +{ + fn automorphism_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize) + } +} + +pub trait AutomorphismKeyPreparedAllocBytesFromInfos { + fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl AutomorphismKeyPreparedAllocBytesFromInfos for Module +where + Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, +{ + fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - AutomorphismKeyPrepared::, B> { - key: GLWESwitchingKeyPrepared::alloc(module, infos), - p: 0, - } + self.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } +} + +impl AutomorphismKeyPrepared, B> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: AutomorphismKeyPreparedAllocFromInfos, + { + module.automorphism_key_prepared_alloc_from_infos(infos) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: VmpPMatAlloc, + Module: AutomorphismKeyPreparedAlloc, { - AutomorphismKeyPrepared { - key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), - p: 0, - } + module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize) } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + Module: AutomorphismKeyPreparedAllocBytesFromInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" - ); - GLWESwitchingKeyPrepared::alloc_bytes(module, infos) + module.automorphism_key_prepared_alloc_bytes_from_infos(infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: VmpPMatAllocBytes, + Module: AutomorphismKeyPreparedAllocBytes, { - GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) + module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) } } pub trait AutomorphismKeyPrepareTmpBytes { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; } @@ -132,7 +225,7 @@ impl AutomorphismKeyPrepareTmpBytes for Module where Module: GLWESwitchingKeyPrepareTmpBytes, { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -145,45 +238,28 @@ impl AutomorphismKeyPrepared { where Module: AutomorphismKeyPrepareTmpBytes, { - module.automorphism_key_prepare_tmp_bytes(self); + module.automorphism_key_prepare_tmp_bytes(self) } } pub trait AutomorphismKeyPrepare { - fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) + fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: AutomorphismKeyPreparedToMut, - O: AutomorphismKeyToRef; + R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement; } -impl AutomorphismKeyPrepare for Module { - fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) - where - R: AutomorphismKeyPreparedToMut, - O: AutomorphismKeyToRef, - { - self.key.prepare(self, &other.to_ref().key, scratch); - self.p = other.p; - } -} - -pub trait AutomorphismKeyPrepareAlloc { - fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> - where - O: AutomorphismKeyToRef; -} - -impl AutomorphismKeyPrepareAlloc for Module +impl AutomorphismKeyPrepare for Module where - Module: VmpPMatAlloc + VmpPrepare, + Module: GLWESwitchingKeyPrepare, { - fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> + fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - O: AutomorphismKeyToRef, + R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, { - let mut atk_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref()); - atk_prepared.prepare(self, &other.to_ref(), scratch); - atk_prepared + self.glwe_switching_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + res.set_p(other.p()); } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index a19fd92..077ad7c 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -160,15 +160,70 @@ impl GGLWEPreparedBuilder { } } -impl GGLWEPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait GGLWEPreparedAlloc { + fn gglwe_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGLWEPrepared, B>; +} + +impl GGLWEPreparedAlloc for Module +where + Module: VmpPMatAlloc, +{ + fn gglwe_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGLWEPrepared, B> { + let size: usize = k.0.div_ceil(base2k.0) as usize; + debug_assert!( + size as u32 > dsize.0, + "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", + dsize.0 + ); + + assert!( + dnum.0 * dsize.0 <= size as u32, + "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", + dnum.0, + dsize.0, + ); + + GGLWEPrepared { + data: self.vmp_pmat_alloc(dnum.into(), rank_in.into(), (rank_out + 1).into(), size), + k, + base2k, + dsize, + } + } +} + +pub trait GGLWEPreparedAllocFromInfos { + fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> + where + A: GGLWEInfos; +} + +impl GGLWEPreparedAllocFromInfos for Module +where + Module: GGLWEPreparedAlloc, +{ + fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_with( - module, + assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + self.gglwe_prepared_alloc( infos.base2k(), infos.k(), infos.rank_in(), @@ -177,8 +232,87 @@ impl GGLWEPrepared, B> { infos.dsize(), ) } +} - pub fn alloc_with( +pub trait GGLWEPreparedAllocBytes { + fn gglwe_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize; +} + +impl GGLWEPreparedAllocBytes for Module +where + Module: VmpPMatAllocBytes, +{ + fn gglwe_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + let size: usize = k.0.div_ceil(base2k.0) as usize; + debug_assert!( + size as u32 > dsize.0, + "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", + dsize.0 + ); + + assert!( + dnum.0 * dsize.0 <= size as u32, + "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", + dnum.0, + dsize.0, + ); + + self.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + } +} + +pub trait GGLWEPreparedAllocBytesFromInfos { + fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GGLWEPreparedAllocBytesFromInfos for Module +where + Module: GGLWEPreparedAllocBytes, +{ + fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + self.gglwe_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGLWEPrepared, B> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GGLWEPreparedAllocFromInfos, + { + module.gglwe_prepared_alloc_from_infos(infos) + } + + pub fn alloc( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -188,48 +322,20 @@ impl GGLWEPrepared, B> { dsize: Dsize, ) -> Self where - Module: VmpPMatAlloc, + Module: GGLWEPreparedAlloc, { - let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( - size as u32 > dsize.0, - "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", - dsize.0 - ); - - assert!( - dnum.0 * dsize.0 <= size as u32, - "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", - dnum.0, - dsize.0, - ); - - Self { - data: module.vmp_pmat_alloc(dnum.into(), rank_in.into(), (rank_out + 1).into(), size), - k, - base2k, - dsize, - } + module.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + Module: GGLWEPreparedAllocBytesFromInfos, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_bytes_with( - module, - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + module.gglwe_prepared_alloc_bytes_from_infos(infos) } - pub fn alloc_bytes_with( + pub fn alloc_bytes( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -239,23 +345,9 @@ impl GGLWEPrepared, B> { dsize: Dsize, ) -> usize where - Module: VmpPMatAllocBytes, + Module: GGLWEPreparedAllocBytes, { - let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( - size as u32 > dsize.0, - "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", - dsize.0 - ); - - assert!( - dnum.0 * dsize.0 <= size as u32, - "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", - dnum.0, - dsize.0, - ); - - module.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + module.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -283,7 +375,7 @@ where } impl GGLWEPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module) + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize where Module: GGLWEPrepareTmpBytes, { @@ -307,7 +399,7 @@ where R: GGLWEPreparedToMut, O: GGLWEToRef, { - let mut res: GGLWEPrepared<&mut [u8], B> = self.to_mut(); + let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut(); let other: GGLWE<&[u8]> = other.to_ref(); assert_eq!(res.n(), self.n() as u32); @@ -332,30 +424,6 @@ where } } -pub trait GGLWEPrepareAlloc { - fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B>; -} - -impl GGLWEPrepareAlloc for Module -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B> { - let mut ct_prepared: GGLWEPrepared, B> = GGLWEPrepared::alloc(self, &other.to_ref()); - ct_prepared.prepare(self, &other.to_ref(), scratch); - ct_prepared - } -} - -impl GGLWE { - fn prepare_alloc(&self, module: &Module, scratch: &Scratch) -> GGLWEPrepared, B> - where - Module: GGLWEPrepareAlloc, - { - module.gglwe_prepare_alloc(self, scratch) - } -} - pub trait GGLWEPreparedToMut { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 7f2f887..7f3aa97 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,11 +1,12 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, + GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, + prepared::{ + GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedAllocBytes, + GGLWEPreparedAllocBytesFromInfos, GGLWEPreparedAllocFromInfos, GGLWEPreparedToMut, GGLWEPreparedToRef, + }, }; #[derive(PartialEq, Eq)] @@ -15,18 +16,23 @@ pub struct GLWESwitchingKeyPrepared { pub(crate) sk_out_n: usize, // Degree of sk_out } -pub(crate) trait GLWESwitchingKeyPreparedSetMetaData { - fn set_sk_in_n(&mut self, sk_in_n: usize); - fn set_sk_out_n(&mut self, sk_out_n: usize); -} - -impl GLWESwitchingKeyPreparedSetMetaData for GLWESwitchingKeyPrepared { +impl GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared { fn set_sk_in_n(&mut self, sk_in_n: usize) { self.sk_in_n = sk_in_n } fn set_sk_out_n(&mut self, sk_out_n: usize) { - self.sk_out_n = self.sk_out_n + self.sk_out_n = sk_out_n + } +} + +impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared { + fn sk_in_n(&self) -> usize { + self.sk_in_n + } + + fn sk_out_n(&self) -> usize { + self.sk_out_n } } @@ -72,21 +78,118 @@ impl GGLWEInfos for GLWESwitchingKeyPrepared { } } -impl GLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GGLWEInfos, - Module: VmpPMatAlloc, - { - debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); +pub trait GLWESwitchingKeyPreparedAlloc { + fn glwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GLWESwitchingKeyPrepared, B>; +} + +impl GLWESwitchingKeyPreparedAlloc for Module +where + Module: GGLWEPreparedAlloc, +{ + fn glwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, B> { - key: GGLWEPrepared::alloc(module, infos), + key: self.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } +} - pub fn alloc_with( +pub trait GLWESwitchingKeyPreparedAllocFromInfos { + fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyPreparedAllocFromInfos for Module +where + Module: GGLWEPreparedAllocFromInfos, +{ + fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> + where + A: GGLWEInfos, + { + GLWESwitchingKeyPrepared::, B> { + key: self.gglwe_prepared_alloc_from_infos(infos), + sk_in_n: 0, + sk_out_n: 0, + } + } +} + +pub trait GLWESwitchingKeyPreparedAllocBytes { + fn glwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize; +} + +impl GLWESwitchingKeyPreparedAllocBytes for Module +where + Module: GGLWEPreparedAllocBytes, +{ + fn glwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + } +} + +pub trait GLWESwitchingKeyPreparedAllocBytesFromInfos { + fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyPreparedAllocBytesFromInfos for Module +where + Module: GGLWEPreparedAllocBytesFromInfos, +{ + fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.gglwe_prepared_alloc_bytes_from_infos(infos) + } +} + +impl GLWESwitchingKeyPrepared, B> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWESwitchingKeyPreparedAllocFromInfos, + { + module.glwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -96,25 +199,20 @@ impl GLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> Self where - Module: VmpPMatAlloc, + Module: GLWESwitchingKeyPreparedAlloc, { - GLWESwitchingKeyPrepared::, B> { - key: GGLWEPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } + module.glwe_switching_key_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, { - debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWEPrepared::alloc_bytes(module, infos) + module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) } - pub fn alloc_bytes_with( + pub fn alloc_bytes( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -124,9 +222,9 @@ impl GLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> usize where - Module: VmpPMatAllocBytes, + Module: GLWESwitchingKeyPreparedAllocBytes, { - GGLWEPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) + module.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -158,66 +256,37 @@ where } pub trait GLWESwitchingKeyPrepare { - fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, - O: GLWESwitchingKeyToRef; + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData; } impl GLWESwitchingKeyPrepare for Module where Module: GGLWEPrepare, { - fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, - O: GLWESwitchingKeyToRef, + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, { - self.gglwe_prepare(&res.to_mut(), other, scratch); - res.set_sk_in_n(other.sk_in_n); - res.set_sk_out_n(other.sk_out_n); + self.gglwe_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + res.set_sk_in_n(other.sk_in_n()); + res.set_sk_out_n(other.sk_out_n()); } } impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where - O: GLWESwitchingKeyToRef, + O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, Module: GLWESwitchingKeyPrepare, { module.glwe_switching_prepare(self, other, scratch); } } -pub trait GLWESwitchingKeyPrepareAlloc { - fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESwitchingKeyToRef; -} - -impl GLWESwitchingKeyPrepareAlloc for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESwitchingKeyToRef, - { - let mut ct_prepared: GLWESwitchingKeyPrepared, B> = GLWESwitchingKeyPrepared::alloc(self, self); - self.glwe_switching_prepare(&mut ct_prepared, other, scratch); - ct_prepared - } -} - -impl GLWESwitchingKey { - pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWESwitchingKeyPrepared, B> - where - Module: GLWESwitchingKeyPrepareAlloc, - { - module.glwe_switching_key_prepare_alloc(self, scratch); - } -} - pub trait GLWESwitchingKeyPreparedToMut { fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index eed6ff2..c6cfc90 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -87,7 +87,7 @@ impl TensorKeyPrepared, B> { let mut keys: Vec, B>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKeyPrepared::alloc_with( + keys.push(GLWESwitchingKeyPrepared::alloc( module, base2k, k, @@ -113,7 +113,7 @@ impl TensorKeyPrepared, B> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GLWESwitchingKeyPrepared::alloc_bytes_with( + * GLWESwitchingKeyPrepared::alloc_bytes( module, infos.base2k(), infos.k(), @@ -129,7 +129,7 @@ impl TensorKeyPrepared, B> { Module: VmpPMatAllocBytes, { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index cea8b41..e2a8fdc 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,11 +1,15 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision, - prepared::GLWESwitchingKeyPrepared, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToMut, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, + TorusPrecision, + prepared::{ + GLWESecretPrepareTmpBytes, GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] @@ -69,14 +73,14 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - Self(GLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc( module, base2k, k, @@ -102,42 +106,117 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - GLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } } -impl PrepareScratchSpace for GLWEToLWESwitchingKeyPrepared, B> +pub trait GLWEToLWESwitchingKeyPrepareTmpBytes { + fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos; +} + +impl GLWEToLWESwitchingKeyPrepareTmpBytes for Module where - GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: GLWEToLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); } } -impl PrepareAlloc, B>> for GLWEToLWESwitchingKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEToLWESwitchingKeyPrepared, B> { - let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = GLWEToLWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +impl GLWEToLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GLWEInfos, + Module: GLWEToLWESwitchingKeyPrepareTmpBytes, + { + module.glwe_secret_prepare_tmp_bytes(infos); } } -impl Prepare> for GLWEToLWESwitchingKeyPrepared +pub trait GLWEToLWESwitchingKeyPrepare { + fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: GLWEToLWESwitchingKeyPreparedToMut, + O: GLWEToLWESwitchingKeyToRef; +} + +impl GLWEToLWESwitchingKeyPrepare for Module where - Module: VmpPrepare, + Module: GLWESwitchingKeyPrepare, { - fn prepare(&mut self, module: &Module, other: &GLWEToLWESwitchingKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); + fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: GLWEToLWESwitchingKeyPreparedToMut, + O: GLWEToLWESwitchingKeyToRef, + { + self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + } +} + +impl GLWEToLWESwitchingKeyPrepared { + fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + where + O: GLWEToLWESwitchingKeyToRef, + Module: GLWEToLWESwitchingKeyPrepare, + { + module.glwe_to_lwe_switching_key_prepare(self, other, scratch); + } +} + +pub trait GLWEToLWESwitchingKeyPrepareAlloc { + fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWEToLWESwitchingKeyToRef; +} + +impl GLWEToLWESwitchingKeyPrepareAlloc for Module +where + Module: GLWEToLWESwitchingKeyPrepare, +{ + fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWEToLWESwitchingKeyToRef, + { + let mut ct_prep: GLWEToLWESwitchingKeyPrepared, B> = GLWEToLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); + self.glwe_to_lwe_switching_key_prepare(&mut ct_prep, other, scratch); + ct_prep + } +} + +pub trait GLWEToLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; +} + +impl GLWEToLWESwitchingKeyPreparedToRef for GLWEToLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> { + GLWEToLWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl GLWEToLWESwitchingKeyPreparedToMut for GLWEToLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> { + GLWEToLWESwitchingKeyPrepared(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 8e21f23..cf5f31b 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - prepared::GLWESwitchingKeyPrepared, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, }; #[derive(PartialEq, Eq)] @@ -73,14 +73,14 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc( module, base2k, k, @@ -111,42 +111,117 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } -impl PrepareScratchSpace for LWESwitchingKeyPrepared, B> +pub trait LWESwitchingKeyPrepareTmpBytes { + fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos; +} + +impl LWESwitchingKeyPrepareTmpBytes for Module where - GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: LWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.lwe_switching_key_prepare_tmp_bytes(infos); } } -impl PrepareAlloc, B>> for LWESwitchingKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWESwitchingKeyPrepared, B> { - let mut ksk_prepared: LWESwitchingKeyPrepared, B> = LWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +impl LWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GLWEInfos, + Module: LWESwitchingKeyPrepareTmpBytes, + { + module.glwe_secret_prepare_tmp_bytes(infos); } } -impl Prepare> for LWESwitchingKeyPrepared +pub trait LWESwitchingKeyPrepare { + fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: LWESwitchingKeyPreparedToMut, + O: LWESwitchingKeyToRef; +} + +impl LWESwitchingKeyPrepare for Module where - Module: VmpPrepare, + Module: GLWESwitchingKeyPrepare, { - fn prepare(&mut self, module: &Module, other: &LWESwitchingKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); + fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: LWESwitchingKeyPreparedToMut, + O: LWESwitchingKeyToRef, + { + self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + } +} + +impl LWESwitchingKeyPrepared { + fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + where + O: LWESwitchingKeyToRef, + Module: LWESwitchingKeyPrepare, + { + module.lwe_switching_key_prepare(self, other, scratch); + } +} + +pub trait LWESwitchingKeyPrepareAlloc { + fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: LWESwitchingKeyToRef; +} + +impl LWESwitchingKeyPrepareAlloc for Module +where + Module: LWESwitchingKeyPrepare, +{ + fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: LWESwitchingKeyToRef, + { + let mut ct_prep: LWESwitchingKeyPrepared, B> = LWESwitchingKeyPrepared::alloc(self, &other.to_ref()); + self.lwe_switching_key_prepare(&mut ct_prep, other, scratch); + ct_prep + } +} + +pub trait LWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; +} + +impl LWESwitchingKeyPreparedToRef for LWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> { + LWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl LWESwitchingKeyPreparedToMut for LWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> { + LWESwitchingKeyPrepared(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 253c8cc..954553a 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - prepared::GLWESwitchingKeyPrepared, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. @@ -70,14 +70,14 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc( module, base2k, k, @@ -103,42 +103,117 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize where Module: VmpPMatAllocBytes, { - GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } } -impl PrepareScratchSpace for LWEToGLWESwitchingKeyPrepared, B> +pub trait LWEToGLWESwitchingKeyPrepareTmpBytes { + fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos; +} + +impl LWEToGLWESwitchingKeyPrepareTmpBytes for Module where - GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: LWEToGLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); } } -impl PrepareAlloc, B>> for LWEToGLWESwitchingKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWEToGLWESwitchingKeyPrepared, B> { - let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = LWEToGLWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +impl LWEToGLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GLWEInfos, + Module: LWEToGLWESwitchingKeyPrepareTmpBytes, + { + module.glwe_secret_prepare_tmp_bytes(infos); } } -impl Prepare> for LWEToGLWESwitchingKeyPrepared +pub trait LWEToGLWESwitchingKeyPrepare { + fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: LWEToGLWESwitchingKeyPreparedToMut, + O: LWEToGLWESwitchingKeyToRef; +} + +impl LWEToGLWESwitchingKeyPrepare for Module where - Module: VmpPrepare, + Module: GLWESwitchingKeyPrepare, { - fn prepare(&mut self, module: &Module, other: &LWEToGLWESwitchingKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); + fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: LWEToGLWESwitchingKeyPreparedToMut, + O: LWEToGLWESwitchingKeyToRef, + { + self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + } +} + +impl LWEToGLWESwitchingKeyPrepared { + fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + where + O: LWEToGLWESwitchingKeyToRef, + Module: LWEToGLWESwitchingKeyPrepare, + { + module.lwe_to_glwe_switching_key_prepare(self, other, scratch); + } +} + +pub trait LWEToGLWESwitchingKeyPrepareAlloc { + fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: LWEToGLWESwitchingKeyToRef; +} + +impl LWEToGLWESwitchingKeyPrepareAlloc for Module +where + Module: LWEToGLWESwitchingKeyPrepare, +{ + fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: LWEToGLWESwitchingKeyToRef, + { + let mut ct_prep: LWEToGLWESwitchingKeyPrepared, B> = LWEToGLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); + self.lwe_to_glwe_switching_key_prepare(&mut ct_prep, other, scratch); + ct_prep + } +} + +pub trait LWEToGLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; +} + +impl LWEToGLWESwitchingKeyPreparedToRef for LWEToGLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> { + LWEToGLWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl LWEToGLWESwitchingKeyPreparedToMut for LWEToGLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> { + LWEToGLWESwitchingKeyPrepared(self.0.to_mut()) } } 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 34fccab..b388e1c 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -154,7 +154,7 @@ where ); let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = - AutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -348,7 +348,7 @@ where ); let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = - AutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); + AutomorphismKeyPrepared::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 8d34288..5b066a1 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -179,7 +179,7 @@ where ); let mut auto_key_prepared: AutomorphismKeyPrepared, B> = - AutomorphismKeyPrepared::alloc(module, &auto_key_layout); + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); @@ -359,7 +359,7 @@ where ); let mut auto_key_prepared: AutomorphismKeyPrepared, B> = - AutomorphismKeyPrepared::alloc(module, &auto_key_layout); + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); 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 a15b2c1..5affb70 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -141,7 +141,7 @@ where ); let mut autokey_prepared: AutomorphismKeyPrepared, B> = - AutomorphismKeyPrepared::alloc(module, &autokey_infos); + AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); @@ -274,7 +274,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(module, &autokey); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow()); From 0533cdff8aa93711908c525d81ea5a329242519a Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 13 Oct 2025 23:49:09 +0200 Subject: [PATCH 05/60] wip --- .../src/encryption/compressed/gglwe_ksk.rs | 2 +- poulpy-core/src/encryption/gglwe_ksk.rs | 2 +- poulpy-core/src/encryption/gglwe_tsk.rs | 2 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/encryption/lwe_ksk.rs | 2 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 127 +------- poulpy-core/src/layouts/gglwe_ct.rs | 125 +------- poulpy-core/src/layouts/ggsw_ct.rs | 140 +------- poulpy-core/src/layouts/glwe_ct.rs | 82 +---- poulpy-core/src/layouts/glwe_pk.rs | 97 +----- poulpy-core/src/layouts/glwe_pt.rs | 89 +---- poulpy-core/src/layouts/lwe_ct.rs | 78 +---- poulpy-core/src/layouts/mod.rs | 25 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 137 +++----- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 231 ++----------- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 143 +++------ poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 205 ++++++------ poulpy-core/src/layouts/prepared/ggsw_ct.rs | 303 ++++++------------ poulpy-core/src/layouts/prepared/glwe_pk.rs | 280 +++++----------- poulpy-core/src/layouts/prepared/glwe_sk.rs | 141 ++++---- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 161 +++++----- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 156 ++++----- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 161 +++++----- poulpy-core/src/scratch.rs | 99 +++--- .../tests/test_suite/automorphism/ggsw_ct.rs | 4 +- .../ciphertexts/block_prepared.rs | 2 +- .../src/tfhe/blind_rotation/cggi_key.rs | 2 +- 27 files changed, 776 insertions(+), 2022 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 91f1856..f9549a5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -25,7 +25,7 @@ impl GLWESwitchingKeyCompressed> { { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + + GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) } } diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index ce10a51..5bea8d0 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -22,7 +22,7 @@ impl GLWESwitchingKey> { { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) + + GLWESecretPrepared::alloc_bytes_from_infos(module, &infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 50a646a..330ca0c 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -23,7 +23,7 @@ impl TensorKey> { Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1) + module.vec_znx_big_alloc_bytes(1, 1) + module.vec_znx_dft_alloc_bytes(1, 1) diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index aee218d..ebbd0f1 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -22,7 +22,7 @@ impl GLWEToLWESwitchingKey> { A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) + GLWESecretPrepared::alloc_bytes(module, infos.rank_in()) + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) } diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 4c8b866..4408e6e 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -39,7 +39,7 @@ impl LWESwitchingKey> { "rank_out > 1 is not supported for LWESwitchingKey" ); GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) + + GLWESecretPrepared::alloc_bytes(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 576774d..93cb829 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,9 +6,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - Base2K, BuildError, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress, -}; +use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -100,101 +98,6 @@ impl GLWECiphertextCompressed> { } } -pub struct GLWECiphertextCompressedBuilder { - data: Option>, - base2k: Option, - k: Option, - rank: Option, -} - -impl GLWECiphertextCompressed { - #[inline] - pub fn builder() -> GLWECiphertextCompressedBuilder { - GLWECiphertextCompressedBuilder { - data: None, - base2k: None, - k: None, - rank: None, - } - } -} - -impl GLWECiphertextCompressedBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc(layout.n().into(), 1, layout.size())); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self.rank = Some(layout.rank()); - self - } -} - -impl GLWECiphertextCompressedBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - #[inline] - pub fn rank(mut self, rank: Rank) -> Self { - self.rank = Some(rank); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let rank: Rank = self.rank.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if rank == 0_u32 { - return Err(BuildError::ZeroRank); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() != 1 { - return Err(BuildError::VecZnxColsNotOne); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWECiphertextCompressed { - data, - base2k, - k, - rank, - seed: [0u8; 32], - }) - } -} - impl ReaderFrom for GLWECiphertextCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -282,13 +185,13 @@ pub trait GLWECiphertextCompressedToRef { impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { - GLWECiphertextCompressed::builder() - .k(self.k()) - .base2k(self.base2k()) - .rank(self.rank()) - .data(self.data.to_ref()) - .build() - .unwrap() + GLWECiphertextCompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_ref(), + } } } @@ -298,12 +201,12 @@ pub trait GLWECiphertextCompressedToMut { impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { - GLWECiphertextCompressed::builder() - .k(self.k()) - .base2k(self.base2k()) - .rank(self.rank()) - .data(self.data.to_mut()) - .build() - .unwrap() + GLWECiphertextCompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 8546499..509e46e 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -3,7 +3,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -132,107 +132,6 @@ impl GGLWEInfos for GGLWE { } } -pub struct GGLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGLWE { - #[inline] - pub fn builder() -> GGLWECiphertextBuilder { - GGLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - { - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWE { - data, - base2k, - k, - dsize, - }) - } -} - impl GGLWE { pub fn data(&self) -> &MatZnx { &self.data @@ -272,23 +171,21 @@ impl fmt::Display for GGLWE { impl GGLWE { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } impl GGLWE { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index 2d8b271..b1fa240 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use std::fmt; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -105,122 +105,6 @@ impl GGSWInfos for GGSW { } } -pub struct GGSWCiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSW { - #[inline] - pub fn builder() -> GGSWCiphertextBuilder { - GGSWCiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSW { - data, - base2k, - k, - dsize, - }) - } -} - impl fmt::Debug for GGSW { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) @@ -248,23 +132,21 @@ impl FillUniform for GGSW { impl GGSW { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } impl GGSW { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index c8cfa4f..7776662 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -86,86 +86,6 @@ impl GLWECiphertext { } } -pub struct GLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWECiphertext { - #[inline] - pub fn builder() -> GLWECiphertextBuilder { - GLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWECiphertext { data, base2k, k }) - } -} - impl LWEInfos for GLWECiphertext { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 6e947f1..b270b33 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMu use crate::{ dist::Distribution, - layouts::{Base2K, BuildError, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -22,6 +22,16 @@ pub struct GLWEPublicKeyLayout { pub rank: Rank, } +pub trait GetDist { + fn get_dist(&self) -> Distribution; +} + +impl GetDist for GLWEPublicKey { + fn get_dist(&self) -> Distribution { + self.dist + } +} + impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k @@ -70,91 +80,6 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -pub struct GLWEPublicKeyBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKey { - #[inline] - pub fn builder() -> GLWEPublicKeyBuilder { - GLWEPublicKeyBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKey { - data, - base2k, - k, - dist: Distribution::NONE, - }) - } -} - impl GLWEPublicKey> { pub fn alloc(infos: &A) -> Self where diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index c32f229..dfd4b8d 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -3,8 +3,8 @@ use std::fmt; use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, BuildError, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, Rank, + TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -74,69 +74,6 @@ impl GLWEInfos for GLWEPlaintext { } } -pub struct GLWEPlaintextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPlaintext { - #[inline] - pub fn builder() -> GLWEPlaintextBuilder { - GLWEPlaintextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPlaintextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() != 1 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPlaintext { data, base2k, k }) - } -} - impl fmt::Display for GLWEPlaintext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -181,23 +118,21 @@ impl GLWEPlaintext> { impl GLWECiphertextToRef for GLWEPlaintext { fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.to_ref()) - .k(self.k()) - .base2k(self.base2k()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } impl GLWECiphertextToMut for GLWEPlaintext { fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index ce5b13a..8c73a4d 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { @@ -153,82 +153,6 @@ impl LWECiphertext> { } } -impl LWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: A) -> Self - where - A: LWEInfos, - { - self.data = Some(Zn::alloc((layout.n() + 1).into(), 1, layout.size())); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -pub struct LWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl LWECiphertext { - #[inline] - pub fn builder() -> LWECiphertextBuilder { - LWECiphertextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl LWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: Zn) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: Zn = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(LWECiphertext { data, base2k, k }) - } -} - pub trait LWECiphertextToRef { fn to_ref(&self) -> LWECiphertext<&[u8]>; } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 775fb6f..1d8e30d 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -33,21 +33,16 @@ pub use lwe_pt::*; pub use lwe_sk::*; pub use lwe_to_glwe_ksk::*; -#[derive(Debug)] -pub enum BuildError { - MissingData, - MissingBase2K, - MissingK, - MissingDigits, - ZeroDegree, - NonPowerOfTwoDegree, - ZeroBase2K, - ZeroTorusPrecision, - ZeroCols, - ZeroLimbs, - ZeroRank, - ZeroDigits, - VecZnxColsNotOne, +use poulpy_hal::layouts::{Backend, Module}; + +pub trait GetDegree { + fn n(&self) -> Degree; +} + +impl GetDegree for Module { + fn n(&self) -> Degree { + Self::n(&self).into() + } } /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 32dd177..e590c21 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -3,9 +3,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, - GLWESwitchingKeyPreparedAllocBytes, GLWESwitchingKeyPreparedAllocBytesFromInfos, GLWESwitchingKeyPreparedAllocFromInfos, - GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, }, }; @@ -77,20 +76,9 @@ impl GGLWEInfos for AutomorphismKeyPrepared { } } -pub trait AutomorphismKeyPreparedAlloc { - fn automorphism_key_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKeyPrepared, B>; -} - -impl AutomorphismKeyPreparedAlloc for Module +pub trait AutomorphismKeyPreparedAlloc where - Module: GLWESwitchingKeyPreparedAlloc, + Self: GLWESwitchingKeyPreparedAlloc, { fn automorphism_key_prepared_alloc( &self, @@ -105,18 +93,7 @@ where p: 0, } } -} -pub trait AutomorphismKeyPreparedAllocFromInfos { - fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPreparedAllocFromInfos for Module -where - Module: GLWESwitchingKeyPreparedAllocFromInfos, -{ fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> where A: GGLWEInfos, @@ -126,28 +103,15 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - AutomorphismKeyPrepared { - key: self.glwe_switching_key_prepared_alloc_from_infos(infos), - p: 0, - } + self.automorphism_key_prepared_alloc( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } -} -pub trait AutomorphismKeyPreparedAllocBytes { - fn automorphism_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl AutomorphismKeyPreparedAllocBytes for Module -where - Module: GLWESwitchingKeyPreparedAllocBytes, -{ fn automorphism_key_prepared_alloc_bytes( &self, base2k: Base2K, @@ -158,18 +122,7 @@ where ) -> usize { self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize) } -} -pub trait AutomorphismKeyPreparedAllocBytesFromInfos { - fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPreparedAllocBytesFromInfos for Module -where - Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, -{ fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, @@ -179,15 +132,23 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - self.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + self.automorphism_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } +impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} + impl AutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAllocFromInfos, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_from_infos(infos) } @@ -202,28 +163,22 @@ impl AutomorphismKeyPrepared, B> { pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAllocBytesFromInfos, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_bytes_from_infos(infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: AutomorphismKeyPreparedAllocBytes, + Module: AutomorphismKeyPreparedAlloc, { module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) } } -pub trait AutomorphismKeyPrepareTmpBytes { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl AutomorphismKeyPrepareTmpBytes for Module +pub trait AutomorphismKeyPrepare where - Module: GLWESwitchingKeyPrepareTmpBytes, + Self: GLWESwitchingKeyPrepare, { fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -231,28 +186,7 @@ where { self.glwe_switching_key_prepare_tmp_bytes(infos) } -} -impl AutomorphismKeyPrepared { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize - where - Module: AutomorphismKeyPrepareTmpBytes, - { - module.automorphism_key_prepare_tmp_bytes(self) - } -} - -pub trait AutomorphismKeyPrepare { - fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement; -} - -impl AutomorphismKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, @@ -263,6 +197,27 @@ where } } +impl AutomorphismKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} + +impl AutomorphismKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: GLWESwitchingKeyPrepare, + { + module.automorphism_key_prepare_tmp_bytes(self) + } +} + +impl AutomorphismKeyPrepared { + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + Module: AutomorphismKeyPrepare, + { + module.automorphism_key_prepare(self, other, scratch); + } +} + pub trait AutomorphismKeyPreparedToMut { fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 077ad7c..6060e80 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -58,123 +57,9 @@ impl GGLWEInfos for GGLWEPrepared { } } -pub struct GGLWEPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGLWEPrepared { - #[inline] - pub fn builder() -> GGLWEPreparedBuilder { - GGLWEPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGLWEPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - B: VmpPMatAllocBytesImpl, - { - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWEPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWEPrepared { - data, - base2k, - k, - dsize, - }) - } -} - -pub trait GGLWEPreparedAlloc { - fn gglwe_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWEPrepared, B>; -} - -impl GGLWEPreparedAlloc for Module +pub trait GGLWEPreparedAlloc where - Module: VmpPMatAlloc, + Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { fn gglwe_prepared_alloc( &self, @@ -206,23 +91,12 @@ where dsize, } } -} -pub trait GGLWEPreparedAllocFromInfos { - fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> - where - A: GGLWEInfos; -} - -impl GGLWEPreparedAllocFromInfos for Module -where - Module: GGLWEPreparedAlloc, -{ fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> where A: GGLWEInfos, { - assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + assert_eq!(self.n(), infos.n()); self.gglwe_prepared_alloc( infos.base2k(), infos.k(), @@ -232,24 +106,7 @@ where infos.dsize(), ) } -} -pub trait GGLWEPreparedAllocBytes { - fn gglwe_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl GGLWEPreparedAllocBytes for Module -where - Module: VmpPMatAllocBytes, -{ fn gglwe_prepared_alloc_bytes( &self, base2k: Base2K, @@ -275,23 +132,12 @@ where self.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) } -} -pub trait GGLWEPreparedAllocBytesFromInfos { - fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GGLWEPreparedAllocBytesFromInfos for Module -where - Module: GGLWEPreparedAllocBytes, -{ fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(self.n() as u32, infos.n(), "module.n() != infos.n()"); + assert_eq!(self.n(), infos.n()); self.gglwe_prepared_alloc_bytes( infos.base2k(), infos.k(), @@ -303,11 +149,15 @@ where } } -impl GGLWEPrepared, B> { +impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} + +impl GGLWEPrepared, B> +where + Module: GGLWEPreparedAlloc, +{ pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: GGLWEPreparedAllocFromInfos, { module.gglwe_prepared_alloc_from_infos(infos) } @@ -320,17 +170,13 @@ impl GGLWEPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self - where - Module: GGLWEPreparedAlloc, - { + ) -> Self { module.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: GGLWEPreparedAllocBytesFromInfos, { module.gglwe_prepared_alloc_bytes_from_infos(infos) } @@ -343,23 +189,14 @@ impl GGLWEPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize - where - Module: GGLWEPreparedAllocBytes, - { + ) -> usize { module.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GGLWEPrepareTmpBytes { - fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GGLWEPrepareTmpBytes for Module +pub trait GGLWEPrepare where - Module: VmpPrepareTmpBytes, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -372,28 +209,7 @@ where infos.size(), ) } -} -impl GGLWEPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize - where - Module: GGLWEPrepareTmpBytes, - { - module.gglwe_prepare_tmp_bytes(self) - } -} - -pub trait GGLWEPrepare { - fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGLWEPreparedToMut, - O: GGLWEToRef; -} - -impl GGLWEPrepare for Module -where - Module: VmpPrepare, -{ fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut, @@ -402,8 +218,8 @@ where let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut(); let other: GGLWE<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.base2k, other.base2k); assert_eq!(res.k, other.k); assert_eq!(res.dsize, other.dsize); @@ -412,11 +228,13 @@ where } } +impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} + impl GGLWEPrepared where Module: GGLWEPrepare, { - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GGLWEToRef, { @@ -424,6 +242,15 @@ where } } +impl GGLWEPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: GGLWEPrepare, + { + module.gglwe_prepare_tmp_bytes(self) + } +} + pub trait GGLWEPreparedToMut { fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 7f3aa97..6d1e443 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, - prepared::{ - GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedAllocBytes, - GGLWEPreparedAllocBytesFromInfos, GGLWEPreparedAllocFromInfos, GGLWEPreparedToMut, GGLWEPreparedToRef, - }, + prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] @@ -78,21 +75,9 @@ impl GGLWEInfos for GLWESwitchingKeyPrepared { } } -pub trait GLWESwitchingKeyPreparedAlloc { - fn glwe_switching_key_prepared_alloc( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKeyPrepared, B>; -} - -impl GLWESwitchingKeyPreparedAlloc for Module +pub trait GLWESwitchingKeyPreparedAlloc where - Module: GGLWEPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn glwe_switching_key_prepared_alloc( &self, @@ -109,46 +94,21 @@ where sk_out_n: 0, } } -} -pub trait GLWESwitchingKeyPreparedAllocFromInfos { - fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPreparedAllocFromInfos for Module -where - Module: GGLWEPreparedAllocFromInfos, -{ fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { - GLWESwitchingKeyPrepared::, B> { - key: self.gglwe_prepared_alloc_from_infos(infos), - sk_in_n: 0, - sk_out_n: 0, - } + self.glwe_switching_key_prepared_alloc( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } -} -pub trait GLWESwitchingKeyPreparedAllocBytes { - fn glwe_switching_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize; -} - -impl GLWESwitchingKeyPreparedAllocBytes for Module -where - Module: GGLWEPreparedAllocBytes, -{ fn glwe_switching_key_prepared_alloc_bytes( &self, base2k: Base2K, @@ -160,31 +120,31 @@ where ) -> usize { self.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } -} -pub trait GLWESwitchingKeyPreparedAllocBytesFromInfos { - fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPreparedAllocBytesFromInfos for Module -where - Module: GGLWEPreparedAllocBytesFromInfos, -{ fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.gglwe_prepared_alloc_bytes_from_infos(infos) + self.glwe_switching_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } } -impl GLWESwitchingKeyPrepared, B> { +impl GLWESwitchingKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} + +impl GLWESwitchingKeyPrepared, B> +where + Module: GLWESwitchingKeyPreparedAlloc, +{ pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWESwitchingKeyPreparedAllocFromInfos, { module.glwe_switching_key_prepared_alloc_from_infos(infos) } @@ -197,17 +157,13 @@ impl GLWESwitchingKeyPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self - where - Module: GLWESwitchingKeyPreparedAlloc, - { + ) -> Self { module.glwe_switching_key_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) } pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWESwitchingKeyPreparedAllocBytesFromInfos, { module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) } @@ -220,23 +176,14 @@ impl GLWESwitchingKeyPrepared, B> { rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize - where - Module: GLWESwitchingKeyPreparedAllocBytes, - { + ) -> usize { module.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GLWESwitchingKeyPrepareTmpBytes { - fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; -} - -impl GLWESwitchingKeyPrepareTmpBytes for Module +pub trait GLWESwitchingKeyPrepare where - Module: GGLWEPrepareTmpBytes, + Self: GGLWEPrepare, { fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -244,28 +191,7 @@ where { self.gglwe_prepare_tmp_bytes(infos) } -} -impl GLWESwitchingKeyPrepared, B> -where - Module: GGLWEPrepareTmpBytes, -{ - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { - module.gglwe_prepare_tmp_bytes(self) - } -} - -pub trait GLWESwitchingKeyPrepare { - fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData; -} - -impl GLWESwitchingKeyPrepare for Module -where - Module: GGLWEPrepare, -{ fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, @@ -277,6 +203,8 @@ where } } +impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} + impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where @@ -287,6 +215,15 @@ impl GLWESwitchingKeyPrepared { } } +impl GLWESwitchingKeyPrepared, B> +where + Module: GLWESwitchingKeyPrepare, +{ + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { + module.gglwe_prepare_tmp_bytes(self) + } +} + pub trait GLWESwitchingKeyPreparedToMut { fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index c6cfc90..b4cd56d 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,13 +1,9 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, - compressed::TensorKeyCompressedToMut, prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; @@ -59,19 +55,36 @@ impl GGLWEInfos for TensorKeyPrepared { } } -impl TensorKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait TensorKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn tensor_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> TensorKeyPrepared, B> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyPrepared { + keys: (0..pairs) + .map(|_| self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn tensor_key_prepared_alloc_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - Self::alloc_with( - module, + self.tensor_key_prepared_alloc( infos.base2k(), infos.k(), infos.dnum(), @@ -80,56 +93,58 @@ impl TensorKeyPrepared, B> { ) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + fn tensor_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank, dnum, dsize) + } + + fn tensor_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where - Module: VmpPMatAlloc, + A: GGLWEInfos, { - let mut keys: Vec, B>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + self.tensor_key_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} + +impl TensorKeyPrepared, B> +where + Module: TensorKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.tensor_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + module.tensor_key_prepared_alloc(base2k, k, dnum, dsize, rank) } pub fn alloc_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKeyPrepared::alloc_bytes( - module, - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + module.tensor_key_prepared_alloc_bytes_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - Module: VmpPMatAllocBytes, - { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank, dnum, dsize) + pub fn alloc_bytes_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + module.tensor_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) } } @@ -155,22 +170,38 @@ impl TensorKeyPrepared { } } -pub trait TensorKeyPrepareTmpBytes { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize; -} - -impl TensorKeyPrepareTmpBytes for Module +pub trait TensorKeyPrepare where - Module: GLWESwitchingKeyPrepareTmpBytes, + Self: GLWESwitchingKeyPrepare, { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize { + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { self.glwe_switching_key_prepare_tmp_bytes(infos) } + + fn tensor_key_prepare(&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.glwe_switching_prepare(a, b, scratch); + } + } } +impl TensorKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} + impl TensorKeyPrepared, B> where - Module: TensorKeyPrepareTmpBytes, + Module: TensorKeyPrepare, { fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize where @@ -180,33 +211,6 @@ where } } -pub trait TensorKeyPrepare { - fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef; -} - -impl TensorKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef, - { - let res = res.to_mut(); - let other = other.to_ref(); - - assert_eq!(self.keys.len(), other.keys.len()); - - for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.glwe_switching_prepare(a, b, scratch); - } - } -} - impl TensorKeyPrepared where Module: TensorKeyPrepare, @@ -219,35 +223,6 @@ where } } -pub trait TensorKeyPrepareAlloc { - fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: TensorKeyToRef; -} - -impl TensorKeyPrepareAlloc for Module -where - Module: TensorKeyPrepare, -{ - fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: TensorKeyToRef, - { - let mut ct_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(self, other); - self.tensor_key_prepare(ct_prepared, other, scratch); - ct_prepared - } -} - -impl TensorKey { - pub fn prepare_alloc(&self, module: &Module, scratch: &Scratch) - where - Module: TensorKeyPrepareAlloc, - { - module.tensor_key_prepare_alloc(self, scratch); - } -} - pub trait TensorKeyPreparedToMut { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index 120c9ed..da9c5db 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -50,143 +49,18 @@ impl GGSWInfos for GGSWPrepared { } } -pub struct GGSWCiphertextPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSWPrepared { - #[inline] - pub fn builder() -> GGSWCiphertextPreparedBuilder { - GGSWCiphertextPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - B: VmpPMatAllocBytesImpl, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSWPrepared { - data, - base2k, - k, - dsize, - }) - } -} - -impl GGSWPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GGSWInfos, - Module: VmpPMatAlloc, - { - Self::alloc_with( - module, - infos.base2k(), - infos.k(), - infos.dnum(), - infos.dsize(), - infos.rank(), - ) - } - - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self - where - Module: VmpPMatAlloc, - { +pub trait GGSWPreparedAlloc +where + Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, +{ + fn ggsw_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> GGSWPrepared, B> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -201,8 +75,8 @@ impl GGSWPrepared, B> { dsize.0, ); - Self { - data: module.vmp_pmat_alloc( + GGSWPrepared { + data: self.vmp_pmat_alloc( dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -214,13 +88,12 @@ impl GGSWPrepared, B> { } } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn ggsw_prepared_alloc_from_infos(&self, infos: &A) -> GGSWPrepared, B> where A: GGSWInfos, - Module: VmpPMatAllocBytes, { - Self::alloc_bytes_with( - module, + assert_eq!(self.n(), infos.n()); + self.ggsw_prepared_alloc( infos.base2k(), infos.k(), infos.dnum(), @@ -229,10 +102,7 @@ impl GGSWPrepared, B> { ) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize - where - Module: VmpPMatAllocBytes, - { + fn ggsw_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -247,7 +117,57 @@ impl GGSWPrepared, B> { dsize.0, ); - module.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + } + + fn ggsw_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + assert_eq!(self.n(), infos.n()); + self.ggsw_prepared_alloc_bytes( + infos.base2k(), + infos.k(), + infos.dnum(), + infos.dsize(), + infos.rank(), + ) + } +} + +impl GGSWPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} + +impl GGSWPrepared, B> +where + Module: GGSWPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGSWInfos, + { + module.ggsw_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + module.ggsw_prepared_alloc(base2k, k, dnum, dsize, rank) + } + + pub fn alloc_bytes(module: &Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> usize { + module.ggsw_prepared_alloc_bytes(base2k, k, dnum, dsize, rank) } } @@ -257,20 +177,15 @@ impl GGSWPrepared { } } -pub trait GGSWPrepareTmpBytes { - fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize - where - A: GGSWInfos; -} - -impl GGSWPrepareTmpBytes for Module +pub trait GGSWPrepare where - Module: VmpPrepareTmpBytes, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { + assert_eq!(self.n(), infos.n()); self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), @@ -278,31 +193,6 @@ where infos.size(), ) } -} - -impl GGSWPrepared, B> -where - Module: GGSWPrepareTmpBytes, -{ - pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize - where - A: GGSWInfos, - { - module.ggsw_prepare_tmp_bytes(self) - } -} - -pub trait GGSWPrepare { - fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GGSWPreparedToMut, - O: GGSWToRef; -} - -impl GGSWPrepare for Module -where - Module: VmpPrepare, -{ fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGSWPreparedToMut, @@ -310,6 +200,8 @@ where { let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); let other: GGSW<&[u8]> = other.to_ref(); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.k, other.k); assert_eq!(res.base2k, other.base2k); assert_eq!(res.dsize, other.dsize); @@ -317,6 +209,20 @@ where } } +impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} + +impl GGSWPrepared, B> +where + Module: GGSWPrepare, +{ + pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepare_tmp_bytes(infos) + } +} + impl GGSWPrepared where Module: GGSWPrepare, @@ -329,35 +235,6 @@ where } } -pub trait GGSWPrepareAlloc { - fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GGSWToRef; -} - -impl GGSWPrepareAlloc for Module -where - Module: GGSWPrepare, -{ - fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GGSWToRef, - { - let mut ct_prepared: GGSWPrepared, B> = GGSWPrepared::alloc(self, other); - self.ggsw_prepare(&mut ct_prepared, other, scratch); - ct_prepared - } -} - -impl GGSW { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) - where - Module: GGSWPrepareAlloc, - { - module.ggsw_prepare_alloc(self, scratch); - } -} - pub trait GGSWPreparedToMut { fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; } @@ -383,7 +260,7 @@ impl GGSWCiphertextPreparedToRef for GGSWPrepared GLWEInfos for GLWEPublicKeyPrepared { } } -pub struct GLWEPublicKeyPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKeyPrepared { - #[inline] - pub fn builder() -> GLWEPublicKeyPreparedBuilder { - GLWEPublicKeyPreparedBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - B: VecZnxDftAllocBytesImpl, - { - self.data = Some(VecZnxDft::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyPreparedBuilder { - #[inline] - pub fn data(mut self, data: VecZnxDft) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnxDft = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKeyPrepared { - data, - base2k, - k, - dist: Distribution::NONE, - }) - } -} - -impl GLWEPublicKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - Module: VecZnxDftAlloc, - { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - Module: VecZnxDftAlloc, - { - Self { - data: module.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - dist: Distribution::NONE, - } - } - - pub fn alloc_bytes(module: &Module, infos: &A) -> usize - where - A: GLWEInfos, - Module: VecZnxDftAllocBytes, - { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_bytes_with(module, infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - Module: VecZnxDftAllocBytes, - { - module.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) - } -} - -pub trait GLWEPublicKeyPrepareTmpBytes { - fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos; -} - -impl GLWEPublicKeyPrepareTmpBytes for Module { - fn glwe_public_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos, - { - 0 - } -} - -impl GLWEPublicKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: GLWEPublicKeyPrepareTmpBytes, - { - module.glwe_public_key_prepare_tmp_bytes(infos); - } -} - -pub trait GLWEPublicKeyPrepare { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef; -} - -impl GLWEPublicKeyPrepare for Module +pub trait GLWEPublicKeyPreparedAlloc where - Module: VecZnxDftAlloc + VecZnxDftApply, + Self: GetDegree + VecZnxDftAlloc + VecZnxDftAllocBytes, { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn glwe_public_key_prepared_alloc(&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, + dist: Distribution::NONE, + } + } + + fn glwe_public_key_alloc_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> + where + A: GLWEInfos, + { + self.glwe_public_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank()) + } + + fn glwe_public_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + self.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + } + + fn glwe_public_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.glwe_public_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank()) + } +} + +impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftAllocBytes {} + +impl GLWEPublicKeyPrepared, B> +where + Module: GLWEPublicKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + { + module.glwe_public_key_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + module.glwe_public_key_prepared_alloc(base2k, k, rank) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + { + module.glwe_public_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank) + } +} + +pub trait GLWEPublicKeyPrepare +where + Self: GetDegree + VecZnxDftApply, +{ + fn glwe_public_key_prepare(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef, + O: GLWEPublicKeyToRef + GetDist, { { - let res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); + let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); let other: GLWEPublicKey<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.n(), self.n()); + assert_eq!(other.n(), self.n()); assert_eq!(res.size(), other.size()); assert_eq!(res.k(), other.k()); assert_eq!(res.base2k(), other.base2k()); - for i in 0..(self.rank() + 1).into() { - self.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); + for i in 0..(res.rank() + 1).into() { + self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i); } } - res.set_dist(other.dist); + res.set_dist(other.get_dist()); } } +impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} + impl GLWEPublicKeyPrepared where Module: GLWEPublicKeyPrepare, { - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &Module, other: &O) where - O: GLWEPublicKeyToRef, + O: GLWEPublicKeyToRef + GetDist, { - module.glwe_public_key_prepare(self, other, scratch); - } -} - -pub trait GLWEPublicKeyPrepareAlloc { - fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEPublicKeyToRef; -} - -impl GLWEPublicKeyPrepareAlloc for Module -where - Module: GLWEPublicKeyPrepare, -{ - fn glwe_public_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEPublicKeyToRef, - { - let mut ct_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(self, other); - self.glwe_public_key_prepare(&mut ct_prepared, ct_prepared, scratch); - ct_prepared - } -} - -impl GLWEPublicKey { - pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) - where - Module: GLWEPublicKeyPrepareAlloc, - { - module.glwe_public_key_prepare_alloc(self, scratch); + module.glwe_public_key_prepare(self, other); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index c75f425..4bac6c0 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -1,12 +1,12 @@ use poulpy_hal::{ api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, }; use crate::{ dist::Distribution, layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToMut, GLWESecretToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, prepared::SetDist, }, }; @@ -44,40 +44,63 @@ impl GLWEInfos for GLWESecretPrepared { Rank(self.data.cols() as u32) } } -impl GLWESecretPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - Module: SvpPPolAlloc, - { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_with(module, infos.rank()) - } - pub fn alloc_with(module: &Module, rank: Rank) -> Self - where - Module: SvpPPolAlloc, - { - Self { - data: module.svp_ppol_alloc(rank.into()), +pub trait GLWESecretPreparedAlloc +where + Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc, +{ + fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared, B> { + GLWESecretPrepared { + data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn glwe_secret_prepared_alloc_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> where A: GLWEInfos, - Module: SvpPPolAllocBytes, { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_bytes_with(module, infos.rank()) + assert_eq!(self.n(), infos.n()); + self.glwe_secret_prepared_alloc(infos.rank()) } - pub fn alloc_bytes_with(module: &Module, rank: Rank) -> usize + fn glwe_secret_alloc_bytes(&self, rank: Rank) -> usize { + self.svp_ppol_alloc_bytes(rank.into()) + } + fn glwe_secret_alloc_bytes_from_infos(&self, infos: &A) -> usize where - Module: SvpPPolAllocBytes, + A: GLWEInfos, { - module.svp_ppol_alloc_bytes(rank.into()) + assert_eq!(self.n(), infos.n()); + self.glwe_secret_alloc_bytes(infos.rank()) + } +} + +impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc {} + +impl GLWESecretPrepared, B> +where + Module: GLWESecretPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + { + module.glwe_secret_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, rank: Rank) -> Self { + module.glwe_secret_prepared_alloc(rank) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + { + module.glwe_secret_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, rank: Rank) -> usize { + module.glwe_secret_alloc_bytes(rank) } } @@ -91,81 +114,29 @@ impl GLWESecretPrepared { } } -pub trait GLWESecretPrepareTmpBytes { - fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos; -} - -impl GLWESecretPrepareTmpBytes for Module { - fn glwe_secret_prepare_tmp_bytes(&self, infos: &A) - where - A: GLWEInfos, - { - 0 - } -} - -impl GLWESecretPrepared, B> +pub trait GLWESecretPrepare where - Module: GLWESecretPrepareTmpBytes, + Self: SvpPrepare, { - fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize - where - A: GLWEInfos, - { - 0 - } -} - -pub trait GLWESecretPrepare { - fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn glwe_secret_prepare(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef; -} - -impl GLWESecretPrepare for Module -where - Module: SvpPrepare, -{ - fn glwe_secret_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) - where - R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef, + O: GLWESecretToRef + GetDist, { { - let res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); + let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); let other: GLWESecret<&[u8]> = other.to_ref(); - for i in 0..self.rank().into() { + for i in 0..res.rank().into() { self.svp_prepare(&mut res.data, i, &other.data, i); } } - res.set_dist(other.dist); + res.set_dist(other.get_dist()); } } -pub trait GLWESecretPrepareAlloc { - fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESecretToMut; -} - -impl GLWESecretPrepareAlloc for Module -where - Module: GLWESecretPrepare, -{ - fn glwe_secret_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWESecretToMut, - { - let mut ct_prep: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(self, self); - self.glwe_secret_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} +impl GLWESecretPrepare for Module where Self: SvpPrepare {} pub trait GLWESecretPreparedToRef { fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>; diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index e2a8fdc..5f10f66 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,13 +1,9 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToMut, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, prepared::{ - GLWESecretPrepareTmpBytes, GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, }, }; @@ -57,11 +53,22 @@ impl GGLWEInfos for GLWEToLWESwitchingKeyPrepared { } } -impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn glwe_to_lwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyPrepared, B> { + GLWEToLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + fn glwe_to_lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.rank_out().0, @@ -73,28 +80,22 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn glwe_to_lwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.rank_out().0, @@ -106,67 +107,73 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + self.glwe_to_lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } } -pub trait GLWEToLWESwitchingKeyPrepareTmpBytes { - fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} -impl GLWEToLWESwitchingKeyPrepareTmpBytes for Module +impl GLWEToLWESwitchingKeyPrepared, B> where - Module: GLWEToLWESwitchingKeyPrepareTmpBytes, + Module: GLWEToLWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.glwe_to_lwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} -impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: GLWEToLWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait GLWEToLWESwitchingKeyPrepare { - fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: GLWEToLWESwitchingKeyPreparedToMut, - O: GLWEToLWESwitchingKeyToRef; -} - -impl GLWEToLWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWEToLWESwitchingKeyPreparedToMut, O: GLWEToLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl GLWEToLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl GLWEToLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyPrepare, + { + module.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); } } impl GLWEToLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: GLWEToLWESwitchingKeyToRef, Module: GLWEToLWESwitchingKeyPrepare, @@ -175,26 +182,6 @@ impl GLWEToLWESwitchingKeyPrepared { } } -pub trait GLWEToLWESwitchingKeyPrepareAlloc { - fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEToLWESwitchingKeyToRef; -} - -impl GLWEToLWESwitchingKeyPrepareAlloc for Module -where - Module: GLWEToLWESwitchingKeyPrepare, -{ - fn glwe_to_lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: GLWEToLWESwitchingKeyToRef, - { - let mut ct_prep: GLWEToLWESwitchingKeyPrepared, B> = GLWEToLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.glwe_to_lwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait GLWEToLWESwitchingKeyPreparedToRef { fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index cf5f31b..6017f2f 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,11 +1,11 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] @@ -52,11 +52,22 @@ impl GGLWEInfos for LWESwitchingKeyPrepared { } } -impl LWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait LWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn lwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyPrepared, B> { + LWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.dsize().0, @@ -73,28 +84,16 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn lwe_switching_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.dsize().0, @@ -111,67 +110,72 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + self.lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.dnum()) } } -pub trait LWESwitchingKeyPrepareTmpBytes { - fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWESwitchingKeyPrepareTmpBytes for Module +impl LWESwitchingKeyPrepared, B> where - Module: LWESwitchingKeyPrepareTmpBytes, + Module: LWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.lwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + module.lwe_switching_key_prepared_alloc(base2k, k, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum) + } +} + +pub trait LWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.lwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} - -impl LWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: LWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait LWESwitchingKeyPrepare { - fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: LWESwitchingKeyPreparedToMut, - O: LWESwitchingKeyToRef; -} - -impl LWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWESwitchingKeyPreparedToMut, O: LWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl LWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl LWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: LWESwitchingKeyPrepare, + { + module.lwe_switching_key_prepare_tmp_bytes(infos); } } impl LWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: LWESwitchingKeyToRef, Module: LWESwitchingKeyPrepare, @@ -180,26 +184,6 @@ impl LWESwitchingKeyPrepared { } } -pub trait LWESwitchingKeyPrepareAlloc { - fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWESwitchingKeyToRef; -} - -impl LWESwitchingKeyPrepareAlloc for Module -where - Module: LWESwitchingKeyPrepare, -{ - fn lwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWESwitchingKeyToRef, - { - let mut ct_prep: LWESwitchingKeyPrepared, B> = LWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.lwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait LWESwitchingKeyPreparedToRef { fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 954553a..ed99a51 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,11 +1,11 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef}, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. @@ -54,11 +54,22 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyPrepared { } } -impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn lwe_to_glwe_switching_key_prepared_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyPrepared, B> { + LWEToGLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + fn lwe_to_glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { debug_assert_eq!( infos.rank_in().0, @@ -70,28 +81,22 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GLWESwitchingKeyPrepared::alloc_from_infos(module, infos)) + self.lwe_to_glwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - Module: VmpPMatAlloc, - { - Self(GLWESwitchingKeyPrepared::alloc( - module, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn lwe_to_glwe_switching_key_prepared_alloc_bytes( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> usize { + self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { debug_assert_eq!( infos.rank_in().0, @@ -103,67 +108,73 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKeyPrepared::alloc_bytes_from_infos(module, infos) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize - where - Module: VmpPMatAllocBytes, - { - GLWESwitchingKeyPrepared::alloc_bytes(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + self.lwe_to_glwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } } -pub trait LWEToGLWESwitchingKeyPrepareTmpBytes { - fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) - where - A: GGLWEInfos; -} +impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWEToGLWESwitchingKeyPrepareTmpBytes for Module +impl LWEToGLWESwitchingKeyPrepared, B> where - Module: LWEToGLWESwitchingKeyPrepareTmpBytes, + Module: LWEToGLWESwitchingKeyPreparedAlloc, +{ + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + { + module.lwe_to_glwe_switching_key_prepared_alloc_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) + } + + pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + } + + pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + module.lwe_to_glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_out, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyPrepare +where + Self: GLWESwitchingKeyPrepare, { fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { - self.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); + self.glwe_switching_key_prepare_tmp_bytes(infos); } -} -impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) - where - A: GLWEInfos, - Module: LWEToGLWESwitchingKeyPrepareTmpBytes, - { - module.glwe_secret_prepare_tmp_bytes(infos); - } -} - -pub trait LWEToGLWESwitchingKeyPrepare { - fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) - where - R: LWEToGLWESwitchingKeyPreparedToMut, - O: LWEToGLWESwitchingKeyToRef; -} - -impl LWEToGLWESwitchingKeyPrepare for Module -where - Module: GLWESwitchingKeyPrepare, -{ - fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWEToGLWESwitchingKeyPreparedToMut, O: LWEToGLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, other, scratch); + self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + } +} + +impl LWEToGLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl LWEToGLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyPrepare, + { + module.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); } } impl LWEToGLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &Scratch) + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: LWEToGLWESwitchingKeyToRef, Module: LWEToGLWESwitchingKeyPrepare, @@ -172,26 +183,6 @@ impl LWEToGLWESwitchingKeyPrepared { } } -pub trait LWEToGLWESwitchingKeyPrepareAlloc { - fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWEToGLWESwitchingKeyToRef; -} - -impl LWEToGLWESwitchingKeyPrepareAlloc for Module -where - Module: LWEToGLWESwitchingKeyPrepare, -{ - fn lwe_to_glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) - where - O: LWEToGLWESwitchingKeyToRef, - { - let mut ct_prep: LWEToGLWESwitchingKeyPrepared, B> = LWEToGLWESwitchingKeyPrepared::alloc(self, &other.to_ref()); - self.lwe_to_glwe_switching_key_prepare(&mut ct_prep, other, scratch); - ct_prep - } -} - pub trait LWEToGLWESwitchingKeyPreparedToRef { fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; } diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 0a94b39..01b260e 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -129,12 +129,11 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWECiphertext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWECiphertext { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -169,12 +168,11 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), 1, infos.size()); ( - GLWEPlaintext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPlaintext { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -196,13 +194,12 @@ where infos.size(), ); ( - GGLWE::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .dsize(infos.dsize()) - .data(data) - .build() - .unwrap(), + GGLWE { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -224,13 +221,12 @@ where infos.size(), ); ( - GGLWEPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGLWEPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -252,13 +248,12 @@ where infos.size(), ); ( - GGSW::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSW { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -280,13 +275,12 @@ where infos.size(), ); ( - GGSWPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSWPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } @@ -321,13 +315,12 @@ where { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKey::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .base2k(infos.base2k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKey { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } @@ -343,12 +336,12 @@ where { let (data, scratch) = self.take_vec_znx_dft(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKeyPrepared::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKeyPrepared { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } 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 5b066a1..b63d616 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -182,7 +182,7 @@ where AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -362,7 +362,7 @@ where AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 3213894..1af36d1 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -101,7 +101,7 @@ where { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) + .map(|_| GGSWPrepared::alloc(module, base2k, k, dnum, dsize, rank)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index a8fa39a..22e9bd4 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -122,7 +122,7 @@ where A: BlindRotationKeyInfos, { let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos))); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc_from_infos(module, infos))); Self { data, dist: Distribution::NONE, From 72dca47cbeadf66cd1c2815330c0cf60ed1c9823 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 14 Oct 2025 18:46:25 +0200 Subject: [PATCH 06/60] wip --- poulpy-backend/src/cpu_fft64_avx/scratch.rs | 14 +- poulpy-backend/src/cpu_fft64_avx/svp.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_big.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_avx/vmp.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/scratch.rs | 14 +- poulpy-backend/src/cpu_fft64_ref/svp.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_big.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/vmp.rs | 2 +- .../src/cpu_spqlios/fft64/scratch.rs | 14 +- .../src/cpu_spqlios/fft64/svp_ppol.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_dft.rs | 2 +- .../src/cpu_spqlios/fft64/vmp_pmat.rs | 2 +- .../src/cpu_spqlios/ntt120/svp_ppol.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_dft.rs | 2 +- .../benches/external_product_glwe_fft64.rs | 31 ++- poulpy-core/benches/keyswitch_glwe_fft64.rs | 34 +-- poulpy-core/examples/encryption.rs | 16 +- poulpy-core/src/automorphism/gglwe_atk.rs | 10 +- poulpy-core/src/automorphism/ggsw_ct.rs | 6 +- poulpy-core/src/automorphism/glwe_ct.rs | 14 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 21 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 19 +- poulpy-core/src/decryption/glwe_ct.rs | 8 +- poulpy-core/src/decryption/lwe_ct.rs | 4 +- .../src/encryption/compressed/gglwe_atk.rs | 3 +- .../src/encryption/compressed/gglwe_ct.rs | 16 +- .../src/encryption/compressed/gglwe_ksk.rs | 6 +- .../src/encryption/compressed/ggsw_ct.rs | 12 +- .../src/encryption/compressed/glwe_ct.rs | 16 +- poulpy-core/src/encryption/gglwe_atk.rs | 2 +- poulpy-core/src/encryption/gglwe_ct.rs | 10 +- poulpy-core/src/encryption/gglwe_ksk.rs | 6 +- poulpy-core/src/encryption/gglwe_tsk.rs | 10 +- poulpy-core/src/encryption/ggsw_ct.rs | 10 +- poulpy-core/src/encryption/glwe_ct.rs | 49 ++-- poulpy-core/src/encryption/glwe_pk.rs | 6 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 5 +- poulpy-core/src/encryption/lwe_ct.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 4 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/external_product/gglwe_ksk.rs | 6 +- poulpy-core/src/external_product/ggsw_ct.rs | 6 +- poulpy-core/src/external_product/glwe_ct.rs | 76 +++--- poulpy-core/src/external_product/mod.rs | 17 +- poulpy-core/src/glwe_packing.rs | 35 ++- poulpy-core/src/glwe_trace.rs | 12 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 6 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 18 +- poulpy-core/src/keyswitching/glwe_ct.rs | 28 +- poulpy-core/src/keyswitching/lwe_ct.rs | 27 +- .../src/layouts/compressed/gglwe_atk.rs | 135 ++++++++-- .../src/layouts/compressed/gglwe_ct.rs | 242 ++++++++++-------- .../src/layouts/compressed/gglwe_ksk.rs | 141 +++++++--- .../src/layouts/compressed/gglwe_tsk.rs | 170 +++++++----- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 201 ++++++++++----- poulpy-core/src/layouts/compressed/glwe_ct.rs | 200 ++++++++------- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 147 +++++++++-- poulpy-core/src/layouts/compressed/lwe_ct.rs | 153 ++++++++--- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 146 ++++++++--- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 148 ++++++++--- poulpy-core/src/layouts/compressed/mod.rs | 6 - poulpy-core/src/layouts/gglwe_atk.rs | 118 +++++++-- poulpy-core/src/layouts/gglwe_ct.rs | 127 ++++++--- poulpy-core/src/layouts/gglwe_ksk.rs | 113 ++++++-- poulpy-core/src/layouts/gglwe_tsk.rs | 112 +++++--- poulpy-core/src/layouts/ggsw_ct.rs | 102 +++++--- poulpy-core/src/layouts/glwe_ct.rs | 146 +++++++---- poulpy-core/src/layouts/glwe_pk.rs | 79 ++++-- poulpy-core/src/layouts/glwe_pt.rs | 88 +++++-- poulpy-core/src/layouts/glwe_sk.rs | 72 ++++-- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 111 +++++--- poulpy-core/src/layouts/lwe_ct.rs | 100 +++++--- poulpy-core/src/layouts/lwe_ksk.rs | 127 +++++---- poulpy-core/src/layouts/lwe_pt.rs | 37 ++- poulpy-core/src/layouts/lwe_sk.rs | 19 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 114 ++++++--- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 42 +-- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 34 +-- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 36 +-- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 47 ++-- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 33 +-- poulpy-core/src/layouts/prepared/glwe_pk.rs | 30 +-- poulpy-core/src/layouts/prepared/glwe_sk.rs | 28 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 28 +- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 28 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 22 +- poulpy-core/src/noise/gglwe_ct.rs | 6 +- poulpy-core/src/noise/ggsw_ct.rs | 14 +- poulpy-core/src/noise/glwe_ct.rs | 8 +- poulpy-core/src/operations/glwe.rs | 92 ++++--- poulpy-core/src/scratch.rs | 20 +- poulpy-core/src/tests/serialization.rs | 48 ++-- .../test_suite/automorphism/gglwe_atk.rs | 22 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 18 +- .../tests/test_suite/automorphism/glwe_ct.rs | 38 +-- .../src/tests/test_suite/conversion.rs | 39 ++- .../tests/test_suite/encryption/gglwe_atk.rs | 10 +- .../tests/test_suite/encryption/gglwe_ct.rs | 15 +- .../tests/test_suite/encryption/ggsw_ct.rs | 14 +- .../tests/test_suite/encryption/glwe_ct.rs | 61 +++-- .../tests/test_suite/encryption/glwe_tsk.rs | 18 +- .../test_suite/external_product/gglwe_ksk.rs | 18 +- .../test_suite/external_product/ggsw_ct.rs | 14 +- .../test_suite/external_product/glwe_ct.rs | 34 +-- .../tests/test_suite/keyswitch/gglwe_ct.rs | 22 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 22 +- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 38 +-- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 14 +- poulpy-core/src/tests/test_suite/packing.rs | 25 +- poulpy-core/src/tests/test_suite/trace.rs | 22 +- poulpy-hal/src/api/svp_ppol.rs | 2 +- poulpy-hal/src/api/vec_znx_big.rs | 2 +- poulpy-hal/src/api/vec_znx_dft.rs | 2 +- poulpy-hal/src/api/vmp_pmat.rs | 2 +- poulpy-hal/src/delegates/svp_ppol.rs | 4 +- poulpy-hal/src/delegates/vec_znx_big.rs | 4 +- poulpy-hal/src/delegates/vec_znx_dft.rs | 4 +- poulpy-hal/src/delegates/vmp_pmat.rs | 4 +- poulpy-hal/src/layouts/mat_znx.rs | 12 +- poulpy-hal/src/layouts/scalar_znx.rs | 6 +- poulpy-hal/src/layouts/svp_ppol.rs | 4 +- poulpy-hal/src/layouts/vec_znx.rs | 6 +- poulpy-hal/src/layouts/vec_znx_big.rs | 4 +- poulpy-hal/src/layouts/vec_znx_dft.rs | 4 +- poulpy-hal/src/layouts/vmp_pmat.rs | 6 +- poulpy-hal/src/layouts/zn.rs | 6 +- poulpy-hal/src/oep/svp_ppol.rs | 2 +- poulpy-hal/src/oep/vec_znx_big.rs | 2 +- poulpy-hal/src/oep/vec_znx_dft.rs | 2 +- poulpy-hal/src/oep/vmp_pmat.rs | 2 +- poulpy-hal/src/reference/fft64/vmp.rs | 2 +- .../benches/circuit_bootstrapping.rs | 10 +- .../examples/circuit_bootstrapping.rs | 20 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 6 +- .../ciphertexts/block_prepared.rs | 2 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 8 +- .../src/tfhe/bdd_arithmetic/eval.rs | 26 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 6 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 4 +- .../src/tfhe/bdd_arithmetic/test.rs | 6 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 43 ++-- .../src/tfhe/blind_rotation/cggi_key.rs | 10 +- .../src/tfhe/blind_rotation/key_compressed.rs | 4 +- poulpy-schemes/src/tfhe/blind_rotation/mod.rs | 6 +- .../tests/generic_blind_rotation.rs | 15 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 20 +- .../src/tfhe/circuit_bootstrapping/key.rs | 10 +- .../src/tfhe/circuit_bootstrapping/mod.rs | 6 +- .../tests/circuit_bootstrapping.rs | 30 +-- 153 files changed, 3099 insertions(+), 1956 deletions(-) diff --git a/poulpy-backend/src/cpu_fft64_avx/scratch.rs b/poulpy-backend/src/cpu_fft64_avx/scratch.rs index 922166b..2cdf85e 100644 --- a/poulpy-backend/src/cpu_fft64_avx/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_avx/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -82,7 +82,7 @@ where B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -95,7 +95,7 @@ where B: ScratchFromBytesImpl, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice), @@ -115,7 +115,7 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size), @@ -136,7 +136,7 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); ( @@ -204,7 +204,7 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), @@ -227,7 +227,7 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), diff --git a/poulpy-backend/src/cpu_fft64_avx/svp.rs b/poulpy-backend/src/cpu_fft64_avx/svp.rs index f505597..1c2c999 100644 --- a/poulpy-backend/src/cpu_fft64_avx/svp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Avx { } unsafe impl SvpPPolAllocBytesImpl for FFT64Avx { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs index 99a39fd..08ec98a 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Avx { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs index 862f623..063ee26 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Avx { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Avx { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vmp.rs b/poulpy-backend/src/cpu_fft64_avx/vmp.rs index 6b87ce1..fcb6236 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_avx::{FFT64Avx, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl for FFT64Avx { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/scratch.rs b/poulpy-backend/src/cpu_fft64_ref/scratch.rs index 80b228d..ad617ce 100644 --- a/poulpy-backend/src/cpu_fft64_ref/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_ref/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -82,7 +82,7 @@ where B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -95,7 +95,7 @@ where B: ScratchFromBytesImpl, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice), @@ -115,7 +115,7 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size), @@ -136,7 +136,7 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); ( @@ -204,7 +204,7 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), @@ -227,7 +227,7 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), diff --git a/poulpy-backend/src/cpu_fft64_ref/svp.rs b/poulpy-backend/src/cpu_fft64_ref/svp.rs index 06dad9e..37ca14a 100644 --- a/poulpy-backend/src/cpu_fft64_ref/svp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Ref { } unsafe impl SvpPPolAllocBytesImpl for FFT64Ref { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs index bb75c8f..348c9b6 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Ref { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs index a08b728..a2a743d 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Ref { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Ref { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vmp.rs b/poulpy-backend/src/cpu_fft64_ref/vmp.rs index 2286de5..34cbf07 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_ref::{FFT64Ref, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl for FFT64Ref { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs index 9bddcb3..2b56218 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -82,7 +82,7 @@ where B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice), @@ -95,7 +95,7 @@ where B: ScratchFromBytesImpl, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice), @@ -115,7 +115,7 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size), @@ -136,7 +136,7 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); ( @@ -204,7 +204,7 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), @@ -227,7 +227,7 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), diff --git a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs index b917400..f46b795 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs @@ -27,7 +27,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Spqlios { } unsafe impl SvpPPolAllocBytesImpl for FFT64Spqlios { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { FFT64Spqlios::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs index 8becaf6..5021f6b 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs @@ -22,7 +22,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Spqlios { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs index 461d327..3b67089 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs @@ -30,7 +30,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Spqlios { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Spqlios { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs index ca64992..ff1eaa2 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs @@ -16,7 +16,7 @@ use crate::cpu_spqlios::{ }; unsafe impl VmpPMatAllocBytesImpl for FFT64Spqlios { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs index c98237a..8c7fdcc 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs @@ -18,7 +18,7 @@ unsafe impl SvpPPolAllocImpl for NTT120 { } unsafe impl SvpPPolAllocBytesImpl for NTT120 { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs index 715b432..58ddf78 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs @@ -3,7 +3,7 @@ use poulpy_hal::{layouts::Backend, oep::VecZnxBigAllocBytesImpl}; use crate::cpu_spqlios::NTT120; unsafe impl VecZnxBigAllocBytesImpl for NTT120 { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs index 53dd24f..9e1666b 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::cpu_spqlios::NTT120; unsafe impl VecZnxDftAllocBytesImpl for NTT120 { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 5027f3b..333d68f 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,6 +1,5 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -48,36 +47,36 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_out, rank, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_in, rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) + | GLWE::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); let mut source_xs = Source::new([0u8; 32]); let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); @@ -156,28 +155,28 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe, rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) + | GLWE::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 8316949..a1b5ea2 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWECiphertextLayout, GLWESecret, - GLWESwitchingKey, GLWESwitchingKeyLayout, Rank, TorusPrecision, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + GLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration}; @@ -48,28 +48,28 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { dsize, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_in, rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_out, rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc(&gglwe_atk_layout); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWECiphertext::keyswitch_scratch_space( + | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) + | GLWE::keyswitch_scratch_space( &module, &glwe_out_layout, &glwe_in_layout, @@ -81,7 +81,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc(&glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); @@ -167,31 +167,31 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank_out: rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct, rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_layout); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) + | GLWE::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index a65b473..f59d5a1 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -2,8 +2,7 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ GLWEOperations, SIGMA, layouts::{ - Base2K, Degree, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, TorusPrecision, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -34,7 +33,7 @@ fn main() { // Instantiate Module (DFT Tables) let module: Module = Module::::new(n.0 as u64); - let glwe_ct_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_ct_infos: GLWELayout = GLWELayout { n, base2k, k: k_ct, @@ -44,9 +43,9 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -55,12 +54,11 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_ct_infos) - | GLWECiphertext::decrypt_scratch_space(&module, &glwe_ct_infos), + GLWE::encrypt_sk_scratch_space(&module, &glwe_ct_infos) | GLWE::decrypt_scratch_space(&module, &glwe_ct_infos), ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 57324e3..233f250 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::AutomorphismKeyPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWE, prepared::AutomorphismKeyPrepared}; impl AutomorphismKey> { pub fn automorphism_scratch_space( @@ -22,7 +22,7 @@ impl AutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space( + GLWE::keyswitch_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), @@ -103,8 +103,8 @@ impl AutomorphismKey { (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); - let lhs_ct: GLWECiphertext<&[u8]> = lhs.at(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); + let lhs_ct: GLWE<&[u8]> = lhs.at(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| { @@ -176,7 +176,7 @@ impl AutomorphismKey { (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 09e2f94..f1a84ad 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, + GGLWEInfos, GGSW, GGSWInfos, GLWE, prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, }; @@ -33,8 +33,8 @@ impl GGSW> { + VecZnxBigNormalizeTmpBytes, { let out_size: usize = out_infos.size(); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes((key_infos.rank_out() + 1).into(), out_size); - let ks_internal: usize = GLWECiphertext::keyswitch_scratch_space( + let ci_dft: usize = module.vec_znx_dft_bytes_of((key_infos.rank_out() + 1).into(), out_size); + let ks_internal: usize = GLWE::keyswitch_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index f94b9bc..800248a 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; -impl GLWECiphertext> { +impl GLWE> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -36,11 +36,11 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn automorphism( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -93,7 +93,7 @@ impl GLWECiphertext { pub fn automorphism_add( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -176,7 +176,7 @@ impl GLWECiphertext { pub fn automorphism_sub_ab( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -261,7 +261,7 @@ impl GLWECiphertext { pub fn automorphism_sub_negate( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 517da90..d3e32ae 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, Rank, - prepared::GLWEToLWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}, }; -impl LWECiphertext> { +impl LWE> { pub fn from_glwe_scratch_space( module: &Module, lwe_infos: &OUT, @@ -28,24 +25,24 @@ impl LWECiphertext> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: lwe_infos.base2k(), k: lwe_infos.k(), rank: Rank(1), }; - GLWECiphertext::alloc_bytes_with( + GLWE::bytes_of( module.n().into(), lwe_infos.base2k(), lwe_infos.k(), 1u32.into(), - ) + GLWECiphertext::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) + ) + GLWE::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) } } -impl LWECiphertext { - pub fn sample_extract(&mut self, a: &GLWECiphertext) { +impl LWE { + pub fn sample_extract(&mut self, a: &GLWE) { #[cfg(debug_assertions)] { assert!(self.n() <= a.n()); @@ -66,7 +63,7 @@ impl LWECiphertext { pub fn from_glwe( &mut self, module: &Module, - a: &GLWECiphertext, + a: &GLWE, ks: &GLWEToLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -92,7 +89,7 @@ impl LWECiphertext { assert!(self.n() <= module.n() as u32); } - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: self.base2k(), k: self.k(), diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index d3ae616..bb5480e 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, - prepared::LWEToGLWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}, }; -impl GLWECiphertext> { +impl GLWE> { pub fn from_lwe_scratch_space( module: &Module, glwe_infos: &OUT, @@ -28,27 +25,27 @@ impl GLWECiphertext> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let ct: usize = GLWECiphertext::alloc_bytes_with( + let ct: usize = GLWE::bytes_of( module.n().into(), key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), ); - let ks: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); + let ks: usize = GLWE::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); if lwe_infos.base2k() == key_infos.base2k() { ct + ks } else { - let a_conv = VecZnx::alloc_bytes(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); + let a_conv = VecZnx::bytes_of(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); ct + a_conv + ks } } } -impl GLWECiphertext { +impl GLWE { pub fn from_lwe( &mut self, module: &Module, - lwe: &LWECiphertext, + lwe: &LWE, ksk: &LWEToGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -74,7 +71,7 @@ impl GLWECiphertext { assert!(lwe.n() <= module.n() as u32); } - let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: ksk.base2k(), k: lwe.k(), diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 19b4a82..92b8c29 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -6,20 +6,20 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, }; -use crate::layouts::{GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext> { +impl GLWE> { pub fn decrypt_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size: usize = infos.size(); - (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size) + (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_bytes_of(1, size)) + module.vec_znx_dft_bytes_of(1, size) } } -impl GLWECiphertext { +impl GLWE { pub fn decrypt( &self, module: &Module, diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index 57abdc6..ade21e3 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,9 +4,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}; -impl LWECiphertext +impl LWE where DataSelf: DataRef, { diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 9665c19..7e717c6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -20,8 +20,7 @@ impl AutomorphismKeyCompressed> { Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 4939fe5..f5a136f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -12,12 +12,12 @@ use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, - compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, + compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGLWECiphertextCompressed { +impl GGLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -34,7 +34,7 @@ impl GGLWECiphertextCompressed { } } -impl GGLWECiphertextCompressed> { +impl GGLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -54,7 +54,7 @@ pub trait GGLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -78,11 +78,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); #[cfg(debug_assertions)] @@ -107,10 +107,10 @@ where assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res), + scratch.available() >= GGLWECompressed::encrypt_sk_scratch_space(self, res), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res) + GGLWECompressed::encrypt_sk_scratch_space(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index f9549a5..260c02e 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -23,9 +23,9 @@ impl GLWESwitchingKeyCompressed> { A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) - + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of(module, infos.rank_out()) } } diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index e9e5017..56f22ad 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -9,12 +9,12 @@ use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, - compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, + compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertextCompressed> { +impl GGSWCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, @@ -34,7 +34,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -53,11 +53,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGSWCiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); @@ -113,7 +113,7 @@ where } } -impl GGSWCiphertextCompressed { +impl GGSWCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 86f86cc..6fadd31 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -7,19 +7,19 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, - compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut}, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + compressed::{GLWECompressed, GLWECompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertextCompressed> { +impl GLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, infos) + GLWE::encrypt_sk_scratch_space(module, infos) } } @@ -33,7 +33,7 @@ pub trait GLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -51,11 +51,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); let mut source_xa: Source = Source::new(seed_xa); let cols: usize = (res.rank() + 1).into(); @@ -77,7 +77,7 @@ where } } -impl GLWECiphertextCompressed { +impl GLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index a269847..18caa37 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -27,7 +27,7 @@ impl AutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(&infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index a7a98ce..9beed56 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GGLWE, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -22,8 +22,8 @@ impl GGLWE> { A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + (GLWEPlaintext::alloc_bytes(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + + (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) } pub fn encrypt_pk_scratch_space(_module: &Module, _infos: &A) -> usize @@ -44,7 +44,7 @@ pub trait GGLWEEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -64,7 +64,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 5bea8d0..b0caa54 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -20,9 +20,9 @@ impl GLWESwitchingKey> { A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) - + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes_from_infos(module, &infos.glwe_layout()) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 330ca0c..4de505a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -23,11 +23,11 @@ impl TensorKey> { Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) - + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1) - + module.vec_znx_big_alloc_bytes(1, 1) - + module.vec_znx_dft_alloc_bytes(1, 1) - + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) + GLWESecretPrepared::bytes_of(module, infos.rank_out()) + + module.vec_znx_dft_bytes_of(infos.rank_out().into(), 1) + + module.vec_znx_big_bytes_of(1, 1) + + module.vec_znx_dft_bytes_of(1, 1) + + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index ad713b5..ea200d4 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ SIGMA, TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWECiphertext, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -20,10 +20,10 @@ impl GGSW> { Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size = infos.size(); - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + VecZnx::alloc_bytes(module.n(), (infos.rank() + 1).into(), size) - + VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes((infos.rank() + 1).into(), size) + GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + + VecZnx::bytes_of(module.n(), 1, size) + + module.vec_znx_dft_bytes_of((infos.rank() + 1).into(), size) } } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index d05ffc6..9fe8411 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -13,12 +13,12 @@ use crate::{ dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ - GLWECiphertext, GLWECiphertextToMut, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEToMut, LWEInfos, prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertext> { +impl GLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, @@ -26,9 +26,7 @@ impl GLWECiphertext> { { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() - + 2 * VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.vec_znx_dft_bytes_of(1, size) } pub fn encrypt_pk_scratch_space(module: &Module, infos: &A) -> usize where @@ -37,14 +35,13 @@ impl GLWECiphertext> { { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - ((module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_big_alloc_bytes(1, size)) - | ScalarZnx::alloc_bytes(module.n(), 1)) - + module.svp_ppol_alloc_bytes(1) + ((module.vec_znx_dft_bytes_of(1, size) + module.vec_znx_big_bytes_of(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) + + module.svp_ppol_bytes_of(1) + module.vec_znx_normalize_tmp_bytes() } } -impl GLWECiphertext { +impl GLWE { pub fn encrypt_sk( &mut self, module: &Module, @@ -116,7 +113,7 @@ pub trait GLWEEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -135,11 +132,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + let mut res: GLWE<&mut [u8]> = res.to_mut(); let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); #[cfg(debug_assertions)] @@ -150,10 +147,10 @@ where assert_eq!(sk.n(), self.n() as u32); assert_eq!(pt.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), + scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_scratch_space(self, &res) ) } @@ -183,7 +180,7 @@ pub trait GLWEEncryptZeroSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef; } @@ -200,10 +197,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef, { - let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + let mut res: GLWE<&mut [u8]> = res.to_mut(); #[cfg(debug_assertions)] { @@ -212,10 +209,10 @@ where assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), + scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_scratch_space(self, &res) ) } @@ -246,7 +243,7 @@ pub trait GLWEEncryptPk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; } @@ -264,7 +261,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, { @@ -281,7 +278,7 @@ pub trait GLWEEncryptZeroPk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef; } @@ -297,7 +294,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal( @@ -321,7 +318,7 @@ pub(crate) trait GLWEEncryptPkInternal { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; } @@ -345,11 +342,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref(); #[cfg(debug_assertions)] diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index c0de69b..d8f96c2 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ encryption::glwe_ct::GLWEEncryptZeroSk, layouts::{ - GLWECiphertext, GLWEPublicKey, GLWEPublicKeyToMut, + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -45,9 +45,9 @@ where } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(self, res)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_scratch_space(self, res)); - let mut tmp: GLWECiphertext> = GLWECiphertext::alloc(res); + let mut tmp: GLWE> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index ebbd0f1..56c92aa 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -22,9 +22,8 @@ impl GLWEToLWESwitchingKey> { A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::alloc_bytes(module, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) + GLWESecretPrepared::bytes_of(module, infos.rank_in()) + + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) } } diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 4dd09ac..a01d95f 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -7,10 +7,10 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}, }; -impl LWECiphertext { +impl LWE { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 4408e6e..23cea9b 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -38,8 +38,8 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GLWESecretPrepared::alloc_bytes(module, Rank(1)) + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + + GLWESecretPrepared::bytes_of(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 5cb9339..bf60c09 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -26,7 +26,7 @@ impl LWEToGLWESwitchingKey> { "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) + + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in()) } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index bfeb146..c09898e 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGSWInfos, GLWECiphertext, GLWESwitchingKey, prepared::GGSWPrepared}; +use crate::layouts::{GGLWEInfos, GGSWInfos, GLWE, GLWESwitchingKey, prepared::GGSWPrepared}; impl GLWESwitchingKey> { pub fn external_product_scratch_space( @@ -22,7 +22,7 @@ impl GLWESwitchingKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space( + GLWE::external_product_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), @@ -40,7 +40,7 @@ impl GLWESwitchingKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) + GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) } } diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 1610cef..8e5eccd 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, prepared::GGSWPrepared}; impl GGSW> { #[allow(clippy::too_many_arguments)] @@ -23,7 +23,7 @@ impl GGSW> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space( + GLWE::external_product_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), @@ -41,7 +41,7 @@ impl GGSW> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) + GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 5a72049..221d000 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -7,15 +7,12 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, }; -use crate::{ - GLWEExternalProduct, GLWEExternalProductInplace, - layouts::{ - GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, - prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, - }, +use crate::layouts::{ + GGSWInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }; -impl GLWECiphertext> { +impl GLWE> { #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, @@ -35,8 +32,8 @@ impl GLWECiphertext> { .div_ceil(apply_infos.dsize().into()) as usize; let out_size: usize = out_infos.size(); let ggsw_size: usize = apply_infos.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), in_size); + let res_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), in_size); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, @@ -50,7 +47,7 @@ impl GLWECiphertext> { if in_infos.base2k() == apply_infos.base2k() { res_dft + a_dft + (vmp | normalize_big) } else { - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (apply_infos.rank() + 1).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), (apply_infos.rank() + 1).into(), in_size); res_dft + ((a_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } @@ -69,11 +66,11 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn external_product( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &GGSWPrepared, scratch: &mut Scratch, ) where @@ -88,15 +85,15 @@ impl GLWECiphertext { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: GLWEExternalProductInplace, + Module: GLWEExternalProduct, { module.external_product_inplace(self, rhs, scratch); } } -impl GLWEExternalProductInplace for Module +pub trait GLWEExternalProduct where - Module: VecZnxDftAllocBytes + Self: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -107,12 +104,12 @@ where + VecZnxNormalize, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) + fn glwe_external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) where - R: GLWECiphertextToMut, + R: GLWEToMut, D: GGSWCiphertextPreparedToRef, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); let basek_in: usize = res.base2k().into(); @@ -124,7 +121,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_inplace_scratch_space(self, res, rhs)); + assert!(scratch.available() >= GLWE::external_product_inplace_scratch_space(self, res, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -213,29 +210,15 @@ where ); } } -} -impl GLWEExternalProduct for Module -where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, -{ - fn external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) + fn glwe_external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, + R: GLWEToMut, + A: GLWEToRef, D: GGSWCiphertextPreparedToRef, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let lhs: &GLWE<&[u8]> = &lhs.to_ref(); let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref(); @@ -251,7 +234,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); assert_eq!(lhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_scratch_space(self, res, lhs, rhs)); + assert!(scratch.available() >= GLWE::external_product_scratch_space(self, res, lhs, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -342,3 +325,18 @@ where }); } } + +impl GLWEExternalProduct for Module +where + Self: VecZnxDftAllocBytes + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxIdftApplyConsume + + VecZnxBigNormalize + + VecZnxNormalize, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +{ +} diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index f46b82c..7e6c5ae 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -1,23 +1,8 @@ use poulpy_hal::layouts::{Backend, Scratch}; -use crate::layouts::{GLWECiphertextToMut, GLWECiphertextToRef, prepared::GGSWCiphertextPreparedToRef}; +use crate::layouts::{GLWEToMut, GLWEToRef, prepared::GGSWCiphertextPreparedToRef}; mod gglwe_atk; mod gglwe_ksk; mod ggsw_ct; mod glwe_ct; - -pub trait GLWEExternalProduct { - fn external_product(&self, res: &mut R, a: &A, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, - D: GGSWCiphertextPreparedToRef; -} - -pub trait GLWEExternalProductInplace { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - D: GGSWCiphertextPreparedToRef; -} diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 927e098..07b61a4 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -29,7 +29,7 @@ pub struct GLWEPacker { /// [Accumulator] stores intermediate packing result. /// There are Log(N) such accumulators in a [GLWEPacker]. struct Accumulator { - data: GLWECiphertext>, + data: GLWE>, value: bool, // Implicit flag for zero ciphertext control: bool, // Can be combined with incoming value } @@ -48,7 +48,7 @@ impl Accumulator { A: GLWEInfos, { Self { - data: GLWECiphertext::alloc(infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, } @@ -100,7 +100,7 @@ impl GLWEPacker { } pub fn galois_elements(module: &Module) -> Vec { - GLWECiphertext::trace_galois_elements(module) + GLWE::trace_galois_elements(module) } /// Adds a GLWE ciphertext to the [GLWEPacker]. @@ -115,7 +115,7 @@ impl GLWEPacker { pub fn add( &mut self, module: &Module, - a: Option<&GLWECiphertext>, + a: Option<&GLWE>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -162,7 +162,7 @@ impl GLWEPacker { } /// Flush result to`res`. - pub fn flush(&mut self, module: &Module, res: &mut GLWECiphertext) + pub fn flush(&mut self, module: &Module, res: &mut GLWE) where Module: VecZnxCopy, { @@ -188,7 +188,7 @@ where fn pack_core( module: &Module, - a: Option<&GLWECiphertext>, + a: Option<&GLWE>, accumulators: &mut [Accumulator], i: usize, auto_keys: &HashMap>, @@ -258,7 +258,7 @@ fn pack_core( } else { pack_core( module, - None::<&GLWECiphertext>>, + None::<&GLWE>>, acc_next, i + 1, auto_keys, @@ -274,16 +274,15 @@ where KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::alloc_bytes(out_infos) - + (GLWECiphertext::rsh_scratch_space(module.n()) - | GLWECiphertext::automorphism_inplace_scratch_space(module, out_infos, key_infos)) + GLWE::bytes_of(out_infos) + + (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos)) } /// [combine] merges two ciphertexts together. fn combine( module: &Module, acc: &mut Accumulator, - b: Option<&GLWECiphertext>, + b: Option<&GLWE>, i: usize, auto_keys: &HashMap>, scratch: &mut Scratch, @@ -314,7 +313,7 @@ fn combine( Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECt, { let log_n: usize = acc.data.n().log2(); - let a: &mut GLWECiphertext> = &mut acc.data; + let a: &mut GLWE> = &mut acc.data; let gal_el: i64 = if i == 0 { -1 @@ -395,7 +394,7 @@ fn combine( /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] pub fn glwe_packing( module: &Module, - cts: &mut HashMap>, + cts: &mut HashMap>, log_gap_out: usize, auto_keys: &HashMap>, scratch: &mut Scratch, @@ -446,8 +445,8 @@ pub fn glwe_packing( }; (0..t).for_each(|j| { - let mut a: Option<&mut GLWECiphertext> = cts.remove(&j); - let mut b: Option<&mut GLWECiphertext> = cts.remove(&(j + t)); + let mut a: Option<&mut GLWE> = cts.remove(&j); + let mut b: Option<&mut GLWE> = cts.remove(&(j + t)); pack_internal(module, &mut a, &mut b, i, auto_key, scratch); @@ -463,8 +462,8 @@ pub fn glwe_packing( #[allow(clippy::too_many_arguments)] fn pack_internal( module: &Module, - a: &mut Option<&mut GLWECiphertext>, - b: &mut Option<&mut GLWECiphertext>, + a: &mut Option<&mut GLWE>, + b: &mut Option<&mut GLWE>, i: usize, auto_key: &AutomorphismKeyPrepared, scratch: &mut Scratch, diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 397a7e1..85f0db3 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -11,11 +11,11 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; -impl GLWECiphertext> { +impl GLWE> { pub fn trace_galois_elements(module: &Module) -> Vec { let mut gal_els: Vec = Vec::new(); (0..module.log_n()).for_each(|i| { @@ -42,7 +42,7 @@ impl GLWECiphertext> { { let trace: usize = Self::automorphism_inplace_scratch_space(module, out_infos, key_infos); if in_infos.base2k() != key_infos.base2k() { - let glwe_conv: usize = VecZnx::alloc_bytes( + let glwe_conv: usize = VecZnx::bytes_of( module.n(), (key_infos.rank_out() + 1).into(), out_infos.k().min(in_infos.k()).div_ceil(key_infos.base2k()) as usize, @@ -63,13 +63,13 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn trace( &mut self, module: &Module, start: usize, end: usize, - lhs: &GLWECiphertext, + lhs: &GLWE, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -135,7 +135,7 @@ impl GLWECiphertext { } if self.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: module.n().into(), base2k: basek_ksk, k: self.k(), diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index ee86cbc..589c391 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, + AutomorphismKey, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, }; @@ -98,7 +98,7 @@ impl GLWESwitchingKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) + GLWE::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) } pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize @@ -107,7 +107,7 @@ impl GLWESwitchingKey> { KEY: GGLWEInfos + GLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_inplace_scratch_space(module, out_infos, key_apply) + GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply) } } diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 3ec738e..eea54a2 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::{ layouts::{ - GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, LWEInfos, prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, operations::GLWEOperations, @@ -29,8 +29,8 @@ impl GGSW> { .div_ceil(tsk_infos.base2k()) .div_ceil(tsk_infos.dsize().into()) as usize; - let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes((tsk_infos.rank_out() + 1).into(), tsk_size); - let tmp_a: usize = module.vec_znx_dft_alloc_bytes(1, size_in); + let tmp_dft_i: usize = module.vec_znx_dft_bytes_of((tsk_infos.rank_out() + 1).into(), tsk_size); + let tmp_a: usize = module.vec_znx_dft_bytes_of(1, size_in); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( tsk_size, size_in, @@ -39,7 +39,7 @@ impl GGSW> { (tsk_infos.rank_out()).into(), // Verify if rank+1 tsk_size, ); - let tmp_idft: usize = module.vec_znx_big_alloc_bytes(1, tsk_size); + let tmp_idft: usize = module.vec_znx_big_bytes_of(1, tsk_size); let norm: usize = module.vec_znx_normalize_tmp_bytes(); tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) @@ -74,16 +74,16 @@ impl GGSW> { let rank: usize = apply_infos.rank_out().into(); let size_out: usize = out_infos.k().div_ceil(out_infos.base2k()) as usize; - let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); + let res_znx: usize = VecZnx::bytes_of(module.n(), rank + 1, size_out); + let ci_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); + let ks: usize = GLWE::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); - let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); + let res_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); if in_infos.base2k() == tsk_infos.base2k() { res_znx + ci_dft + (ks | expand_rows | res_dft) } else { - let a_conv: usize = VecZnx::alloc_bytes( + let a_conv: usize = VecZnx::bytes_of( module.n(), 1, out_infos.k().div_ceil(tsk_infos.base2k()) as usize, diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index e7afcbe..f785016 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; -impl GLWECiphertext> { +impl GLWE> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -28,8 +28,8 @@ impl GLWECiphertext> { .div_ceil(key_apply.dsize().into()) as usize; let out_size: usize = out_infos.size(); let ksk_size: usize = key_apply.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); + let res_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, @@ -37,17 +37,17 @@ impl GLWECiphertext> { (key_apply.rank_in()).into(), (key_apply.rank_out() + 1).into(), ksk_size, - ) + module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); + ) + module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); let normalize_big: usize = module.vec_znx_big_normalize_tmp_bytes(); if in_infos.base2k() == key_apply.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) } else if key_apply.dsize() == 1 { // In this case, we only need one column, temporary, that we can drop once a_dft is computed. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); res_dft + (((ai_dft + normalize_conv) | vmp) | normalize_big) } else { // Since we stride over a to get a_dft when dsize > 1, we need to store the full columns of a with in the base conversion. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (key_apply.rank_in()).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), (key_apply.rank_in()).into(), in_size); res_dft + ((ai_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } @@ -62,12 +62,12 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { #[allow(dead_code)] pub(crate) fn assert_keyswitch( &self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where @@ -93,7 +93,7 @@ impl GLWECiphertext { assert_eq!(rhs.n(), self.n()); assert_eq!(lhs.n(), self.n()); - let scrach_needed: usize = GLWECiphertext::keyswitch_scratch_space(module, self, lhs, rhs); + let scrach_needed: usize = GLWE::keyswitch_scratch_space(module, self, lhs, rhs); assert!( scratch.available() >= scrach_needed, @@ -134,7 +134,7 @@ impl GLWECiphertext { assert_eq!(rhs.n(), self.n()); - let scrach_needed: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, self, rhs); + let scrach_needed: usize = GLWE::keyswitch_inplace_scratch_space(module, self, rhs); assert!( scratch.available() >= scrach_needed, @@ -144,11 +144,11 @@ impl GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub fn keyswitch( &mut self, module: &Module, - glwe_in: &GLWECiphertext, + glwe_in: &GLWE, rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -232,7 +232,7 @@ impl GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub(crate) fn keyswitch_internal( &self, module: &Module, diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 7d9e08e..dd8c552 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, LWECiphertext, LWEInfos, Rank, TorusPrecision, - prepared::LWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}, }; -impl LWECiphertext> { +impl LWE> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -40,33 +37,33 @@ impl LWECiphertext> { { let max_k: TorusPrecision = in_infos.k().max(out_infos.k()); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: module.n().into(), base2k: in_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: module.n().into(), base2k: out_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_in: usize = GLWECiphertext::alloc_bytes(&glwe_in_infos); - let glwe_out: usize = GLWECiphertext::alloc_bytes(&glwe_out_infos); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); + let glwe_in: usize = GLWE::bytes_of(&glwe_in_infos); + let glwe_out: usize = GLWE::bytes_of(&glwe_out_infos); + let ks: usize = GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); glwe_in + glwe_out + ks } } -impl LWECiphertext { +impl LWE { pub fn keyswitch( &mut self, module: &Module, - a: &LWECiphertext, + a: &LWE, ksk: &LWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -90,14 +87,14 @@ impl LWECiphertext { { assert!(self.n() <= module.n() as u32); assert!(a.n() <= module.n() as u32); - assert!(scratch.available() >= LWECiphertext::keyswitch_scratch_space(module, self, a, ksk)); + assert!(scratch.available() >= LWE::keyswitch_scratch_space(module, self, a, ksk)); } let max_k: TorusPrecision = self.k().max(a.k()); let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: a.base2k(), k: max_k, @@ -105,7 +102,7 @@ impl LWECiphertext { }); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: self.base2k(), k: max_k, diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index a62e4a7..29cf49b 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -1,12 +1,15 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, + AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -76,35 +79,100 @@ impl fmt::Display for AutomorphismKeyCompressed { } } +pub trait AutomorphismKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyCompressed> { + AutomorphismKeyCompressed { + key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> + where + A: GGLWEInfos, + { + assert_eq!(infos.rank_in(), infos.rank_out()); + self.alloc_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.rank_in(), infos.rank_out()); + self.bytes_of_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + impl AutomorphismKeyCompressed> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: Module, infos: &A) -> Self where A: GGLWEInfos, + Module: AutomorphismKeyCompressedAlloc, { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - Self { - key: GLWESwitchingKeyCompressed::alloc(infos), - p: 0, - } + module.alloc_automorphism_key_compressed_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - Self { - key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: AutomorphismKeyCompressedAlloc, + { + module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize where A: GGLWEInfos, + Module: AutomorphismKeyCompressedAlloc, { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GLWESwitchingKeyCompressed::alloc_bytes(infos) + module.bytes_of_automorphism_key_compressed_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: AutomorphismKeyCompressedAlloc, + { + module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } } @@ -122,13 +190,32 @@ impl WriterTo for AutomorphismKeyCompressed { } } -impl Decompress> for AutomorphismKey +pub trait AutomorphismKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &AutomorphismKeyCompressed) { - self.key.decompress(module, &other.key); - self.p = other.p; + fn decompress_automorphism_key(&self, res: &mut R, other: &O) + where + R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + { + self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); + res.set_p(other.p()); + } +} + +impl AutomorphismKeyDecompress for Module where Self: AutomorphismKeyDecompress {} + +impl AutomorphismKey +where + Self: SetAutomorphismGaloisElement, +{ + pub fn decompressed(&mut self, module: &Module, other: &O) + where + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + Module: AutomorphismKeyDecompress, + { + module.decompress_automorphism_key(self, other); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index a75a9a8..51bb1fb 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,14 +7,14 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertextCompressed { +pub struct GGLWECompressed { pub(crate) data: MatZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -23,7 +23,7 @@ pub struct GGLWECiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGLWECiphertextCompressed { +impl LWEInfos for GGLWECompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -40,13 +40,13 @@ impl LWEInfos for GGLWECiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGLWECiphertextCompressed { +impl GLWEInfos for GGLWECompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextCompressed { +impl GGLWEInfos for GGLWECompressed { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -64,53 +64,41 @@ impl GGLWEInfos for GGLWECiphertextCompressed { } } -impl fmt::Debug for GGLWECiphertextCompressed { +impl fmt::Debug for GGLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertextCompressed { +impl FillUniform for GGLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertextCompressed { +impl fmt::Display for GGLWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGLWECiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGLWECompressed: base2k={} k={} dsize={}) {}", self.base2k.0, self.k.0, self.dsize.0, self.data ) } } -impl GGLWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWECompressedAlloc +where + Self: GetDegree, +{ + fn alloc_gglwe_compressed( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWECompressed> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -125,9 +113,9 @@ impl GGLWECiphertextCompressed> { dsize.0, ); - Self { + GGLWECompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), rank_in.into(), 1, @@ -141,21 +129,22 @@ impl GGLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_gglwe_compressed_from_infos(&self, infos: &A) -> GGLWECompressed> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + assert_eq!(infos.n(), self.n()); + self.alloc_gglwe_compressed( infos.base2k(), infos.k(), infos.rank_in(), + infos.rank_out(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -170,20 +159,83 @@ impl GGLWECiphertextCompressed> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), rank_in.into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.n(), self.n()); + self.bytes_of_gglwe_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + infos.dsize(), + ) + } } -impl GGLWECiphertextCompressed { - pub(crate) fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGLWECompressedAlloc for Module where Self: GetDegree {} + +impl GGLWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed_from_infos(infos) + } + + pub fn byte_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } +} + +impl GGLWECompressed { + pub(crate) fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k, @@ -193,10 +245,10 @@ impl GGLWECiphertextCompressed { } } -impl GGLWECiphertextCompressed { - pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGLWECompressed { + pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { k: self.k, base2k: self.base2k, rank: self.rank_out, @@ -206,7 +258,7 @@ impl GGLWECiphertextCompressed { } } -impl ReaderFrom for GGLWECiphertextCompressed { +impl ReaderFrom for GGLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -221,7 +273,7 @@ impl ReaderFrom for GGLWECiphertextCompressed { } } -impl WriterTo for GGLWECiphertextCompressed { +impl WriterTo for GGLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -235,70 +287,50 @@ impl WriterTo for GGLWECiphertextCompressed { } } -impl Decompress> for GGLWE +pub trait GGLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWECiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", - self.n(), - other.n() - ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank_in(), - other.rank_in(), - "invalid receiver: self.rank_in()={} != other.rank_in()={}", - self.rank_in(), - other.rank_in() - ); - assert_eq!( - self.rank_out(), - other.rank_out(), - "invalid receiver: self.rank_out()={} != other.rank_out()={}", - self.rank_out(), - other.rank_out() - ); + fn decompress_gglwe(&self, res: &mut R, other: &O) + where + R: GGLWEToMut, + O: GGLWECompressedToRef, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GGLWECompressed<&[u8]> = &other.to_ref(); - assert_eq!( - self.dnum(), - other.dnum(), - "invalid receiver: self.dnum()={} != other.dnum()={}", - self.dnum(), - other.dnum() - ); + assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + + let rank_in: usize = res.rank_in().into(); + let dnum: usize = res.dnum().into(); + + for row_i in 0..dnum { + for col_i in 0..rank_in { + self.decompress_glwe(&mut res.at_mut(row_i, col_i), &other.at(row_i, col_i)); + } } - - let rank_in: usize = self.rank_in().into(); - let dnum: usize = self.dnum().into(); - - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|row_i| { - self.at_mut(row_i, col_i) - .decompress(module, &other.at(row_i, col_i)); - }); - }); } } -pub trait GGLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]>; +impl GGLWEDecompress for Module where Self: VecZnxFillUniform + VecZnxCopy {} + +impl GGLWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GGLWECompressedToRef, + Module: GGLWEDecompress, + { + module.decompress_gglwe(self, other); + } } -impl GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> { - GGLWECiphertextCompressed { +pub trait GGLWECompressedToMut { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]>; +} + +impl GGLWECompressedToMut for GGLWECompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -309,13 +341,13 @@ impl GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed } } -pub trait GGLWECiphertextCompressedToRef { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; +pub trait GGLWECompressedToRef { + fn to_ref(&self) -> GGLWECompressed<&[u8]>; } -impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { - GGLWECiphertextCompressed { +impl GGLWECompressedToRef for GGLWECompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 76d60a7..450c5bc 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -1,19 +1,19 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, + LWEInfos, Rank, TorusPrecision, + compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKeyCompressed { - pub(crate) key: GGLWECiphertextCompressed, + pub(crate) key: GGLWECompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } @@ -81,43 +81,103 @@ impl fmt::Display for GLWESwitchingKeyCompressed { } } -impl GLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +pub trait GLWESwitchingKeyCompressedAlloc +where + Self: GGLWECompressedAlloc, +{ + fn alloc_glwe_switching_key_compressed( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GLWESwitchingKeyCompressed> { GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWESwitchingKeyCompressed> where A: GGLWEInfos, { - GGLWECiphertextCompressed::alloc_bytes(infos) + self.alloc_glwe_switching_key_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, dsize) + fn bytes_of_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe_compressed_from_infos(infos) + } +} + +impl GLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) } } @@ -137,14 +197,31 @@ impl WriterTo for GLWESwitchingKeyCompressed { } } -impl Decompress> for GLWESwitchingKey +pub trait GLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GGLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GLWESwitchingKeyCompressed) { - self.key.decompress(module, &other.key); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + fn decompress_glwe_switching_key(&self, res: &mut R, other: &O) + where + R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyCompressedToRef, + { + let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); + self.decompress_gglwe(&mut res.to_mut().key, &other.key); + res.set_sk_in_n(other.sk_in_n); + res.set_sk_out_n(other.sk_out_n); + } +} + +impl GLWESwitchingKeyDecompress for Module where Self: GGLWEDecompress {} + +impl GLWESwitchingKey { + pub fn decompress(&mut self, module: Module, other: &O) + where + O: GLWESwitchingKeyCompressedToRef, + Module: GGLWEDecompress, + { + module.decompress_glwe_switching_key(self, other); } } @@ -154,7 +231,7 @@ pub trait GLWESwitchingKeyCompressedToMut { impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed where - GGLWECiphertextCompressed: GGLWECiphertextCompressedToMut, + GGLWECompressed: GGLWECompressedToMut, { fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { GLWESwitchingKeyCompressed { @@ -171,7 +248,7 @@ pub trait GLWESwitchingKeyCompressedToRef { impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed where - GGLWECiphertextCompressed: GGLWECiphertextCompressedToRef, + GGLWECompressed: GGLWECompressedToRef, { fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { GLWESwitchingKeyCompressed { diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 7255594..4c51b6a 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -1,12 +1,14 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -80,8 +82,27 @@ impl fmt::Display for TensorKeyCompressed { } } -impl TensorKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait TensorKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn tensor_key_compressed_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> TensorKeyCompressed> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyCompressed { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn tensor_key_compressed_alloc_from_infos(&self, infos: &A) -> TensorKeyCompressed> where A: GGLWEInfos, { @@ -90,58 +111,70 @@ impl TensorKeyCompressed> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" ); - Self::alloc_with( - infos.n(), + self.tensor_key_compressed_alloc( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn tensor_key_compressed_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + fn tensor_key_compressed_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKeyCompressed::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.dnum(), - infos.dsize(), - ) + self.tensor_key_compressed_bytes_of( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl TensorKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of(base2k, k, rank, dnum, dsize) } } @@ -181,28 +214,41 @@ impl TensorKeyCompressed { } } -impl Decompress> for TensorKey +pub trait TensorKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &TensorKeyCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.keys.len(), - other.keys.len(), - "invalid receiver: self.keys.len()={} != other.keys.len()={}", - self.keys.len(), - other.keys.len() - ); - } + fn decompress_tensor_key(&self, res: &mut R, other: &O) + where + R: TensorKeyToMut, + O: TensorKeyCompressedToRef, + { + let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let other: &TensorKeyCompressed<&[u8]> = &other.to_ref(); - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.decompress(module, b); - }); + assert_eq!( + res.keys.len(), + other.keys.len(), + "invalid receiver: res.keys.len()={} != other.keys.len()={}", + res.keys.len(), + other.keys.len() + ); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.decompress_glwe_switching_key(a, b); + } + } +} + +impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl TensorKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: TensorKeyCompressedToRef, + Module: GLWESwitchingKeyDecompress, + { + module.decompress_tensor_key(self, other); } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index 3d15e3e..d417385 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -1,5 +1,4 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{ Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, }, @@ -7,14 +6,14 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertextCompressed { +pub struct GGSWCompressed { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, @@ -23,7 +22,7 @@ pub struct GGSWCiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGSWCiphertextCompressed { +impl LWEInfos for GGSWCompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -39,13 +38,13 @@ impl LWEInfos for GGSWCiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGSWCiphertextCompressed { +impl GLWEInfos for GGSWCompressed { fn rank(&self) -> Rank { self.rank } } -impl GGSWInfos for GGSWCiphertextCompressed { +impl GGSWInfos for GGSWCompressed { fn dsize(&self) -> Dsize { self.dsize } @@ -55,46 +54,42 @@ impl GGSWInfos for GGSWCiphertextCompressed { } } -impl fmt::Debug for GGSWCiphertextCompressed { +impl fmt::Debug for GGSWCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertextCompressed { +impl fmt::Display for GGSWCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGSWCiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGSWCompressed: base2k={} k={} dsize={}) {}", self.base2k, self.k, self.dsize, self.data ) } } -impl FillUniform for GGSWCiphertextCompressed { +impl FillUniform for GGSWCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWCompressedAlloc +where + Self: GetDegree, +{ + fn alloc_ggsw_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGSWCompressed> { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 @@ -107,9 +102,9 @@ impl GGSWCiphertextCompressed> { dsize.0, ); - Self { + GGSWCompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), 1, @@ -123,12 +118,11 @@ impl GGSWCiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_compressed_from_infos(&self, infos: &A) -> GGSWCompressed> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw_compressed( infos.base2k(), infos.k(), infos.rank(), @@ -137,9 +131,9 @@ impl GGSWCiphertextCompressed> { ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 @@ -152,20 +146,72 @@ impl GGSWCiphertextCompressed> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), (rank + 1).into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_ggsw_compressed_key_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } } -impl GGSWCiphertextCompressed { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGSWCompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGSWInfos, + Module: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGSWInfos, + Module: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed_key_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl GGSWCompressed { + pub fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k, @@ -175,10 +221,10 @@ impl GGSWCiphertextCompressed { } } -impl GGSWCiphertextCompressed { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGSWCompressed { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at_mut(row, col), k: self.k, base2k: self.base2k, @@ -188,7 +234,7 @@ impl GGSWCiphertextCompressed { } } -impl ReaderFrom for GGSWCiphertextCompressed { +impl ReaderFrom for GGSWCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -203,7 +249,7 @@ impl ReaderFrom for GGSWCiphertextCompressed { } } -impl WriterTo for GGSWCiphertextCompressed { +impl WriterTo for GGSWCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -217,34 +263,49 @@ impl WriterTo for GGSWCiphertextCompressed { } } -impl Decompress> for GGSW +pub trait GGSWDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGSWCiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), other.rank()) - } + fn decompress_ggsw(&self, res: &mut R, other: &O) + where + R: GGSWToMut, + O: GGSWCompressedToRef, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let other: &GGSWCompressed<&[u8]> = &other.to_ref(); - let dnum: usize = self.dnum().into(); - let rank: usize = self.rank().into(); - (0..dnum).for_each(|row_i| { - (0..rank + 1).for_each(|col_j| { - self.at_mut(row_i, col_j) - .decompress(module, &other.at(row_i, col_j)); - }); - }); + assert_eq!(res.rank(), other.rank()); + let dnum: usize = res.dnum().into(); + let rank: usize = res.rank().into(); + + for row_i in 0..dnum { + for col_j in 0..rank + 1 { + self.decompress_glwe(&mut res.at_mut(row_i, col_j), &other.at(row_i, col_j)); + } + } } } -pub trait GGSWCiphertextCompressedToMut { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]>; +impl GGSWDecompress for Module where Self: GGSWDecompress {} + +impl GGSW { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GGSWCompressedToRef, + Module: GGSWDecompress, + { + module.decompress_ggsw(self, other); + } } -impl GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> { - GGSWCiphertextCompressed { +pub trait GGSWCompressedToMut { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]>; +} + +impl GGSWCompressedToMut for GGSWCompressed { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -255,13 +316,13 @@ impl GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed { } } -pub trait GGSWCiphertextCompressedToRef { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>; +pub trait GGSWCompressedToRef { + fn to_ref(&self) -> GGSWCompressed<&[u8]>; } -impl GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> { - GGSWCiphertextCompressed { +impl GGSWCompressedToRef for GGSWCompressed { + fn to_ref(&self) -> GGSWCompressed<&[u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 93cb829..1ee976a 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertextCompressed { +pub struct GLWECompressed { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -19,7 +19,7 @@ pub struct GLWECiphertextCompressed { pub(crate) seed: [u8; 32], } -impl LWEInfos for GLWECiphertextCompressed { +impl LWEInfos for GLWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -36,23 +36,23 @@ impl LWEInfos for GLWECiphertextCompressed { Degree(self.data.n() as u32) } } -impl GLWEInfos for GLWECiphertextCompressed { +impl GLWEInfos for GLWECompressed { fn rank(&self) -> Rank { self.rank } } -impl fmt::Debug for GLWECiphertextCompressed { +impl fmt::Debug for GLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertextCompressed { +impl fmt::Display for GLWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertextCompressed: base2k={} k={} rank={} seed={:?}: {}", + "GLWECompressed: base2k={} k={} rank={} seed={:?}: {}", self.base2k(), self.k(), self.rank(), @@ -62,23 +62,19 @@ impl fmt::Display for GLWECiphertextCompressed { } } -impl FillUniform for GLWECiphertextCompressed { +impl FillUniform for GLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), +pub trait GLWECompressedAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { + GLWECompressed { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, rank, @@ -86,19 +82,62 @@ impl GLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_compressed_from_infos(&self, infos: &A) -> GLWECompressed> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + assert_eq!(self.n(), infos.n()); + self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::alloc_bytes(n.into(), 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_glwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + assert_eq!(self.n(), infos.n()); + self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) } } -impl ReaderFrom for GLWECiphertextCompressed { +impl GLWECompressedAlloc for Module where Self: GetDegree {} + +impl GLWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWECompressedAlloc, + { + module.alloc_glwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWECompressedAlloc, + { + module.alloc_glwe_compressed(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed(base2k, k) + } +} + +impl ReaderFrom for GLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -108,7 +147,7 @@ impl ReaderFrom for GLWECiphertextCompressed { } } -impl WriterTo for GLWECiphertextCompressed { +impl WriterTo for GLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -118,74 +157,61 @@ impl WriterTo for GLWECiphertextCompressed { } } -impl Decompress> for GLWECiphertext +pub trait GLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GetDegree + VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GLWECiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", - self.n(), - other.n() - ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank(), - other.rank(), - "invalid receiver: self.rank()={} != other.rank()={}", - self.rank(), - other.rank() - ); - } - - let mut source: Source = Source::new(other.seed); - self.decompress_internal(module, other, &mut source); - } -} - -impl GLWECiphertext { - pub(crate) fn decompress_internal( - &mut self, - module: &Module, - other: &GLWECiphertextCompressed, - source: &mut Source, - ) where - DataOther: DataRef, - Module: VecZnxCopy + VecZnxFillUniform, + fn decompress_glwe(&self, res: &mut R, other: &O) + where + R: GLWEToMut + SetGLWEInfos, + O: GLWECompressedToRef + GLWEInfos, { - #[cfg(debug_assertions)] { - assert_eq!(self.rank(), other.rank()); - debug_assert_eq!(self.size(), other.size()); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GLWECompressed<&[u8]> = &other.to_ref(); + assert_eq!( + res.n(), + self.n(), + "invalid receiver: res.n()={} != other.n()={}", + res.n(), + self.n() + ); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + assert_eq!(res.glwe_layout(), other.glwe_layout()); + + let mut source: Source = Source::new(other.seed); + + self.vec_znx_copy(&mut res.data, 0, &other.data, 0); + (1..(other.rank() + 1).into()).for_each(|i| { + self.vec_znx_fill_uniform(other.base2k.into(), &mut res.data, i, &mut source); + }); } - module.vec_znx_copy(&mut self.data, 0, &other.data, 0); - (1..(other.rank() + 1).into()).for_each(|i| { - module.vec_znx_fill_uniform(other.base2k.into(), &mut self.data, i, source); - }); - - self.base2k = other.base2k; - self.k = other.k; + res.set_base2k(other.base2k()); + res.set_k(other.k()); } } -pub trait GLWECiphertextCompressedToRef { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>; +impl GLWEDecompress for Module where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} + +impl GLWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GLWECompressedToRef + GLWEInfos, + Module: GLWEDecompress, + { + module.decompress_glwe(self, other); + } } -impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { - GLWECiphertextCompressed { +pub trait GLWECompressedToRef { + fn to_ref(&self) -> GLWECompressed<&[u8]>; +} + +impl GLWECompressedToRef for GLWECompressed { + fn to_ref(&self) -> GLWECompressed<&[u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k, @@ -195,13 +221,13 @@ impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { } } -pub trait GLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>; +pub trait GLWECompressedToMut { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]>; } -impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { - GLWECiphertextCompressed { +impl GLWECompressedToMut for GLWECompressed { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k, diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index c3969b2..9f038a3 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -1,12 +1,17 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; #[derive(PartialEq, Eq, Clone)] @@ -83,54 +88,146 @@ impl WriterTo for GLWEToLWESwitchingKeyCompressed { } } -impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyCompressed> { + GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } +} + +impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + +impl GLWEToLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize + where + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O) + where + R: GLWEToLWESwitchingKeyToMut, + O: GLWEToLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl GLWEToLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl GLWEToLWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GLWEToLWESwitchingKeyCompressedToRef, + Module: GLWEToLWESwitchingKeyDecompress, + { + module.decompress_glwe_to_lwe_switching_key(self, other); + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index e11b3f3..2696784 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -2,21 +2,24 @@ use std::fmt; use poulpy_hal::{ api::ZnFillUniform, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnxInfos, ZnxView, ZnxViewMut}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos, ZnxView, + ZnxViewMut, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, LWECiphertext, LWEInfos, TorusPrecision, compressed::Decompress}; +use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision}; #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertextCompressed { +pub struct LWECompressed { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) seed: [u8; 32], } -impl LWEInfos for LWECiphertextCompressed { +impl LWEInfos for LWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -34,17 +37,17 @@ impl LWEInfos for LWECiphertextCompressed { } } -impl fmt::Debug for LWECiphertextCompressed { +impl fmt::Debug for LWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertextCompressed { +impl fmt::Display for LWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "LWECiphertextCompressed: base2k={} k={} seed={:?}: {}", + "LWECompressed: base2k={} k={} seed={:?}: {}", self.base2k(), self.k(), self.seed, @@ -53,22 +56,15 @@ impl fmt::Display for LWECiphertextCompressed { } } -impl FillUniform for LWECiphertextCompressed { +impl FillUniform for LWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl LWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWECompressedAlloc { + fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed> { + LWECompressed { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, @@ -76,21 +72,60 @@ impl LWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_compressed_from_infos(&self, infos: &A) -> LWECompressed> where A: LWEInfos, { - Self::alloc_bytes_with(infos.base2k(), infos.k()) + self.alloc_lwe_compressed(infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes(1, 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_lwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: LWEInfos, + { + self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) + } +} + +impl LWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: LWEInfos, + Module: LWECompressedAlloc, + { + module.alloc_lwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: LWECompressedAlloc, + { + module.alloc_lwe_compressed(base2k, k) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: LWEInfos, + Module: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed(base2k, k) } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for LWECiphertextCompressed { +impl ReaderFrom for LWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -99,7 +134,7 @@ impl ReaderFrom for LWECiphertextCompressed { } } -impl WriterTo for LWECiphertextCompressed { +impl WriterTo for LWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -108,22 +143,72 @@ impl WriterTo for LWECiphertextCompressed { } } -impl Decompress> for LWECiphertext +pub trait LWEDecompress where - Module: ZnFillUniform, + Self: ZnFillUniform, { - fn decompress(&mut self, module: &Module, other: &LWECiphertextCompressed) { - debug_assert_eq!(self.size(), other.size()); + fn decompress_lwe(&self, res: &mut R, other: &O) + where + R: LWEToMut, + O: LWECompressedToRef, + { + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let other: &LWECompressed<&[u8]> = &other.to_ref(); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + let mut source: Source = Source::new(other.seed); - module.zn_fill_uniform( - self.n().into(), + self.zn_fill_uniform( + res.n().into(), other.base2k().into(), - &mut self.data, + &mut res.data, 0, &mut source, ); - (0..self.size()).for_each(|i| { - self.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; - }); + for i in 0..res.size() { + res.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; + } + } +} + +impl LWEDecompress for Module where Self: ZnFillUniform {} + +impl LWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWECompressedToRef, + Module: LWEDecompress, + { + module.decompress_lwe(self, other); + } +} + +pub trait LWECompressedToRef { + fn to_ref(&self) -> LWECompressed<&[u8]>; +} + +impl LWECompressedToRef for LWECompressed { + fn to_ref(&self) -> LWECompressed<&[u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_ref(), + } + } +} + +pub trait LWECompressedToMut { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]>; +} + +impl LWECompressedToMut for LWECompressed { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index d7ea93b..ba987ab 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -1,12 +1,14 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt; @@ -83,73 +85,147 @@ impl WriterTo for LWESwitchingKeyCompressed { } } -impl LWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait LWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyCompressed> { + LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, - "dsize > 1 is not supported for LWESwitchingKey" + "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, - "rank_in > 1 is not supported for LWESwitchingKey" + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, - "rank_out > 1 is not supported for LWESwitchingKey" + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) } } -impl Decompress> for LWESwitchingKey +impl LWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_switching_key(&self, res: &mut R, other: &O) + where + R: LWESwitchingKeyToMut, + O: LWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWESwitchingKeyCompressedToRef, + Module: LWESwitchingKeyDecompress, + { + module.decompress_lwe_switching_key(self, other); + } +} + +pub trait LWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; +} + +impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { + LWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { + LWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index c1000ff..ce4e889 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -1,12 +1,15 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt; @@ -83,63 +86,138 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } -impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_to_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyCompressed> { + LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } } -impl Decompress> for LWEToGLWESwitchingKey +impl LWEToGLWESwitchingKeyCompressed> { + pub fn alloc(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWEToGLWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O) + where + R: LWEToGLWESwitchingKeyToMut, + O: LWEToGLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl LWEToGLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWEToGLWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWEToGLWESwitchingKeyCompressedToRef, + Module: LWEToGLWESwitchingKeyDecompress, + { + module.decompress_lwe_to_glwe_switching_key(self, other); + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/mod.rs b/poulpy-core/src/layouts/compressed/mod.rs index c1fcacf..cd7c459 100644 --- a/poulpy-core/src/layouts/compressed/mod.rs +++ b/poulpy-core/src/layouts/compressed/mod.rs @@ -19,9 +19,3 @@ pub use glwe_to_lwe_ksk::*; pub use lwe_ct::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; - -use poulpy_hal::layouts::{Backend, Module}; - -pub trait Decompress { - fn decompress(&mut self, module: &Module, other: &C); -} diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index f9244e1..c197828 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -131,43 +131,105 @@ impl fmt::Display for AutomorphismKey { } } +impl AutomorphismKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +pub trait AutomorphismKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_automorphism_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKey> { + AutomorphismKey { + key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_from_infos(&self, infos: &A) -> AutomorphismKey> + where + A: GGLWEInfos, + { + self.alloc_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for AutomorphismKey" + ); + self.bytes_of_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + impl AutomorphismKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: AutomorphismKeyAlloc, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - AutomorphismKey { - key: GLWESwitchingKey::alloc(infos), - p: 0, - } + module.alloc_automorphism_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - AutomorphismKey { - key: GLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } + pub fn alloc_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: AutomorphismKeyAlloc, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_automorphism_key_from_infos(infos) } - pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) } } @@ -204,13 +266,13 @@ where } impl AutomorphismKey { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } impl AutomorphismKey { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 509e46e..97508c2 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -1,9 +1,11 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -16,7 +18,7 @@ where fn dsize(&self) -> Dsize; fn rank_in(&self) -> Rank; fn rank_out(&self) -> Rank; - fn layout(&self) -> GGLWECiphertextLayout { + fn gglwe_layout(&self) -> GGLWECiphertextLayout { GGLWECiphertextLayout { n: self.n(), base2k: self.base2k(), @@ -170,8 +172,8 @@ impl fmt::Display for GGLWE { } impl GGLWE { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at(row, col), @@ -180,8 +182,8 @@ impl GGLWE { } impl GGLWE { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at_mut(row, col), @@ -189,31 +191,19 @@ impl GGLWE { } } -impl GGLWE> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWEAlloc +where + Self: GetDegree, +{ + fn alloc_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWE> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -228,9 +218,9 @@ impl GGLWE> { dsize.0, ); - Self { + GGLWE { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -242,12 +232,11 @@ impl GGLWE> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GGLWE> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_gglwe( infos.base2k(), infos.k(), infos.rank_in(), @@ -257,8 +246,8 @@ impl GGLWE> { ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -280,21 +269,85 @@ impl GGLWE> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } } -pub trait GGLWECiphertextToMut { +impl GGLWEAlloc for Module where Self: GetDegree {} + +impl GGLWE> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GGLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GGLWEAlloc, + { + module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GGLWEAlloc, + { + module.bytes_of_gglwe_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGLWEAlloc, + { + module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } +} + +pub trait GGLWEToMut { fn to_mut(&mut self) -> GGLWE<&mut [u8]>; } -impl GGLWECiphertextToMut for GGLWE { +impl GGLWEToMut for GGLWE { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { GGLWE { k: self.k(), diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 7b74193..c818d84 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -163,43 +163,42 @@ impl FillUniform for GLWESwitchingKey { } } -impl GLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GLWESwitchingKey { - key: GGLWE::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +pub trait GLWESwitchingKeyAlloc +where + Self: GGLWEAlloc, +{ + fn alloc_glwe_switching_key( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GLWESwitchingKey> { GLWESwitchingKey { - key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_from_infos(&self, infos: &A) -> GLWESwitchingKey> where A: GGLWEInfos, { - GGLWE::alloc_bytes(infos) + self.alloc_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_glwe_switching_key( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -207,7 +206,69 @@ impl GLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> usize { - GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GLWESwitchingKey> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -217,7 +278,7 @@ pub trait GLWESwitchingKeyToMut { impl GLWESwitchingKeyToMut for GLWESwitchingKey where - GGLWE: GGLWECiphertextToMut, + GGLWE: GGLWEToMut, { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { GLWESwitchingKey { @@ -246,13 +307,13 @@ where } impl GLWESwitchingKey { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } impl GLWESwitchingKey { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index e6eae2d..2483868 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -130,8 +130,20 @@ impl fmt::Display for TensorKey { } } -impl TensorKey> { - pub fn alloc(infos: &A) -> Self +pub trait TensorKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey> { + let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); + TensorKey { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn alloc_tensor_key_from_infos(&self, infos: &A) -> TensorKey> where A: GGLWEInfos, { @@ -140,34 +152,21 @@ impl TensorKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - Self::alloc_with( - infos.n(), + self.alloc_tensor_key( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_tensor_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -176,23 +175,54 @@ impl TensorKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKey::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + self.bytes_of_tensor_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +impl TensorKey> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: TensorKeyAlloc, + { + module.alloc_tensor_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: TensorKeyAlloc, + { + module.alloc_tensor_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: TensorKeyAlloc, + { + module.bytes_of_tensor_key_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: TensorKeyAlloc, + { + module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) } } @@ -260,14 +290,14 @@ where } pub trait TensorKeyToMut { - fn to_ref(&mut self) -> TensorKey<&mut [u8]>; + fn to_mut(&mut self) -> TensorKey<&mut [u8]>; } impl TensorKeyToMut for TensorKey where GLWESwitchingKey: GLWESwitchingKeyToMut, { - fn to_ref(&mut self) -> TensorKey<&mut [u8]> { + fn to_mut(&mut self) -> TensorKey<&mut [u8]> { TensorKey { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index b1fa240..b427ecd 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -1,10 +1,12 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; use std::fmt; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -131,8 +133,8 @@ impl FillUniform for GGSW { } impl GGSW { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at(row, col), @@ -141,8 +143,8 @@ impl GGSW { } impl GGSW { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at_mut(row, col), @@ -150,22 +152,13 @@ impl GGSW { } } -impl GGSW> { - pub fn alloc(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } +impl GGSWAlloc for Module where Self: GetDegree {} - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWAlloc +where + Self: GetDegree, +{ + fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -180,9 +173,9 @@ impl GGSW> { dsize.0, ); - Self { + GGSW { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -194,12 +187,11 @@ impl GGSW> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_from_infos(&self, infos: &A) -> GGSW> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw( infos.base2k(), infos.k(), infos.rank(), @@ -208,7 +200,7 @@ impl GGSW> { ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -223,14 +215,66 @@ impl GGSW> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_ggsw_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGSW> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGSWInfos, + Module: GGSWAlloc, + { + module.alloc_ggsw_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: GGSWAlloc, + { + module.alloc_ggsw(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGSWInfos, + Module: GGSWAlloc, + { + module.bytes_of_ggsw_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGSWAlloc, + { + module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) + } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 7776662..5bdb2cb 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -1,11 +1,12 @@ use poulpy_hal::{ layouts::{ - Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, + WriterTo, ZnxInfos, }, source::Source, }; -use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -14,8 +15,8 @@ where Self: LWEInfos, { fn rank(&self) -> Rank; - fn glwe_layout(&self) -> GLWECiphertextLayout { - GLWECiphertextLayout { + fn glwe_layout(&self) -> GLWELayout { + GLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -24,20 +25,20 @@ where } } -pub trait GLWELayoutSet { +pub trait SetGLWEInfos { fn set_k(&mut self, k: TorusPrecision); - fn set_basek(&mut self, base2k: Base2K); + fn set_base2k(&mut self, base2k: Base2K); } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GLWECiphertextLayout { +pub struct GLWELayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } -impl LWEInfos for GLWECiphertextLayout { +impl LWEInfos for GLWELayout { fn n(&self) -> Degree { self.n } @@ -51,21 +52,21 @@ impl LWEInfos for GLWECiphertextLayout { } } -impl GLWEInfos for GLWECiphertextLayout { +impl GLWEInfos for GLWELayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertext { +pub struct GLWE { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, } -impl GLWELayoutSet for GLWECiphertext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWE { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -74,19 +75,19 @@ impl GLWELayoutSet for GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub fn data(&self) -> &VecZnx { &self.data } } -impl GLWECiphertext { +impl GLWE { pub fn data_mut(&mut self) -> &mut VecZnx { &mut self.data } } -impl LWEInfos for GLWECiphertext { +impl LWEInfos for GLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -104,16 +105,16 @@ impl LWEInfos for GLWECiphertext { } } -impl GLWEInfos for GLWECiphertext { +impl GLWEInfos for GLWE { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } -impl ToOwnedDeep for GLWECiphertext { - type Owned = GLWECiphertext>; +impl ToOwnedDeep for GLWE { + type Owned = GLWE>; fn to_owned_deep(&self) -> Self::Owned { - GLWECiphertext { + GLWE { data: self.data.to_owned_deep(), k: self.k, base2k: self.base2k, @@ -121,17 +122,17 @@ impl ToOwnedDeep for GLWECiphertext { } } -impl fmt::Debug for GLWECiphertext { +impl fmt::Debug for GLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertext { +impl fmt::Display for GLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertext: base2k={} k={}: {}", + "GLWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data @@ -139,41 +140,86 @@ impl fmt::Display for GLWECiphertext { } } -impl FillUniform for GLWECiphertext { +impl FillUniform for GLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEAlloc +where + Self: GetDegree, +{ + fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { + GLWE { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GLWE> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) } } -impl ReaderFrom for GLWECiphertext { +impl GLWEAlloc for Module where Self: GetDegree {} + +impl GLWE> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWEAlloc, + { + module.alloc_glwe(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEAlloc, + { + module.bytes_of_glwe_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module: GLWEAlloc, + { + module.bytes_of_glwe(base2k, k, rank) + } +} + +impl ReaderFrom for GLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -181,7 +227,7 @@ impl ReaderFrom for GLWECiphertext { } } -impl WriterTo for GLWECiphertext { +impl WriterTo for GLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; @@ -189,13 +235,13 @@ impl WriterTo for GLWECiphertext { } } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; +pub trait GLWEToRef { + fn to_ref(&self) -> GLWE<&[u8]>; } -impl GLWECiphertextToRef for GLWECiphertext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl GLWEToRef for GLWE { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -203,13 +249,13 @@ impl GLWECiphertextToRef for GLWECiphertext { } } -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; +pub trait GLWEToMut { + fn to_mut(&mut self) -> GLWE<&mut [u8]>; } -impl GLWECiphertextToMut for GLWECiphertext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl GLWEToMut for GLWE { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index b270b33..a370656 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -1,8 +1,10 @@ -use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; +use poulpy_hal::layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, +}; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -80,32 +82,75 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -impl GLWEPublicKey> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEPublicKeyAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { + GLWEPublicKey { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, dist: Distribution::NONE, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_public_key_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + } +} + +impl GLWEPublicKey> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key(base2k, k, rank) } } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index dfd4b8d..5f4e2df 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -1,10 +1,9 @@ use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -40,8 +39,8 @@ pub struct GLWEPlaintext { pub k: TorusPrecision, } -impl GLWELayoutSet for GLWEPlaintext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWEPlaintext { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -86,39 +85,72 @@ impl fmt::Display for GLWEPlaintext { } } -impl GLWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - debug_assert!(rank.0 == 0); - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEPlaintextAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { + GLWEPlaintext { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) + self.alloc_glwe_plaintext(infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - debug_assert!(rank.0 == 0); - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) } } -impl GLWECiphertextToRef for GLWEPlaintext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl GLWEPlaintext> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext(base2k, k) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext(base2k, k) + } +} + +impl GLWEToRef for GLWEPlaintext { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -126,9 +158,9 @@ impl GLWECiphertextToRef for GLWEPlaintext { } } -impl GLWECiphertextToMut for GLWEPlaintext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl GLWEToMut for GLWEPlaintext { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index e954c3b..c5be370 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -1,11 +1,14 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero}, + layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, + ZnxZero, + }, source::Source, }; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -67,30 +70,65 @@ impl GLWEInfos for GLWESecret { } } -impl GLWESecret> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.rank()) - } - - pub fn alloc_with(n: Degree, rank: Rank) -> Self { - Self { - data: ScalarZnx::alloc(n.into(), rank.into()), +pub trait GLWESecretAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { + GLWESecret { + data: ScalarZnx::alloc(self.n().into(), rank.into()), dist: Distribution::NONE, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_secret_from_infos(&self, infos: &A) -> GLWESecret> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.rank()) + self.alloc_glwe_secret(infos.rank()) } - pub fn alloc_bytes_with(n: Degree, rank: Rank) -> usize { - ScalarZnx::alloc_bytes(n.into(), rank.into()) + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + ScalarZnx::bytes_of(self.n().into(), rank.into()) + } + + fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_secret(infos.rank()) + } +} + +impl GLWESecret> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWESecretAlloc, + { + module.alloc_glwe_secret_from_infos(infos) + } + + pub fn alloc(module: Module, rank: Rank) -> Self + where + Module: GLWESecretAlloc, + { + module.alloc_glwe_secret(rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWESecretAlloc, + { + module.bytes_of_glwe_secret_from_infos(infos) + } + + pub fn bytes_of(module: Module, rank: Rank) -> usize + where + Module: GLWESecretAlloc, + { + module.bytes_of_glwe_secret(rank) } } diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index ec5a4a3..4020721 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use std::fmt; @@ -132,55 +132,88 @@ impl WriterTo for GLWEToLWESwitchingKey { } } +pub trait GLWEToLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_glwe_to_lwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKey> { + GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } + + fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + } + + fn bytes_of_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } +} + impl GLWEToLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_glwe_to_lwe_switching_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + where + Module: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 8c73a4d..26ee40b 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -1,7 +1,7 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, source::Source, }; @@ -53,13 +53,13 @@ impl LWEInfos for LWECiphertextLayout { } } #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertext { +pub struct LWE { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, } -impl LWEInfos for LWECiphertext { +impl LWEInfos for LWE { fn base2k(&self) -> Base2K { self.base2k } @@ -76,7 +76,7 @@ impl LWEInfos for LWECiphertext { } } -impl SetLWEInfos for LWECiphertext { +impl SetLWEInfos for LWE { fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -86,25 +86,25 @@ impl SetLWEInfos for LWECiphertext { } } -impl LWECiphertext { +impl LWE { pub fn data(&self) -> &Zn { &self.data } } -impl LWECiphertext { +impl LWE { pub fn data_mut(&mut self) -> &Zn { &mut self.data } } -impl fmt::Debug for LWECiphertext { +impl fmt::Debug for LWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertext { +impl fmt::Display for LWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -116,7 +116,7 @@ impl fmt::Display for LWECiphertext { } } -impl FillUniform for LWECiphertext +impl FillUniform for LWE where Zn: FillUniform, { @@ -125,41 +125,73 @@ where } } -impl LWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEAlloc { + fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { + LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_from_infos(&self, infos: &A) -> LWE> where A: LWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_lwe_from_infos(&self, infos: &A) -> usize + where + A: LWEInfos, + { + self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) + } +} + +impl LWE> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: LWEInfos, + Module: LWEAlloc, + { + module.alloc_lwe_from_infos(infos) + } + + pub fn alloc(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: LWEAlloc, + { + module.alloc_lwe(n, base2k, k) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: LWEInfos, + Module: LWEAlloc, + { + module.bytes_of_lwe_from_infos(infos) + } + + pub fn bytes_of(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: LWEAlloc, + { + module.bytes_of_lwe(n, base2k, k) } } pub trait LWECiphertextToRef { - fn to_ref(&self) -> LWECiphertext<&[u8]>; + fn to_ref(&self) -> LWE<&[u8]>; } -impl LWECiphertextToRef for LWECiphertext { - fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext { +impl LWECiphertextToRef for LWE { + fn to_ref(&self) -> LWE<&[u8]> { + LWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -167,14 +199,14 @@ impl LWECiphertextToRef for LWECiphertext { } } -pub trait LWECiphertextToMut { +pub trait LWEToMut { #[allow(dead_code)] - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]>; + fn to_mut(&mut self) -> LWE<&mut [u8]>; } -impl LWECiphertextToMut for LWECiphertext { - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext { +impl LWEToMut for LWE { + fn to_mut(&mut self) -> LWE<&mut [u8]> { + LWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), @@ -182,7 +214,7 @@ impl LWECiphertextToMut for LWECiphertext { } } -impl ReaderFrom for LWECiphertext { +impl ReaderFrom for LWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -190,7 +222,7 @@ impl ReaderFrom for LWECiphertext { } } -impl WriterTo for LWECiphertext { +impl WriterTo for LWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 2d4ba31..77a027a 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -101,65 +101,94 @@ impl GGLWEInfos for LWESwitchingKey { } } +pub trait LWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { + LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } + + fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + } + + fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } +} + +impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl LWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_lwe_switching_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module: LWESwitchingKeyAlloc, + { + module.alloc_lwe_switching_key(base2k, k, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_glwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWESwitchingKeyAlloc, + { + module.bytes_of_lwe_switching_key(base2k, k, dnum) } } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index e739722..fb23b18 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -1,6 +1,6 @@ use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; @@ -52,21 +52,36 @@ impl LWEInfos for LWEPlaintext { } } -impl LWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEPlaintextAlloc { + fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext> { + LWEPlaintext { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } + + fn alloc_lwe_plaintext_from_infos(&self, infos: &A) -> LWEPlaintext> + where + A: LWEInfos, + { + self.alloc_lwe_plaintext(infos.base2k(), infos.k()) + } +} + +impl LWEPlaintextAlloc for Module {} + +impl LWEPlaintext> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: LWEInfos, + { + module.alloc_lwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self { + module.alloc_lwe_plaintext(base2k, k) + } } impl fmt::Display for LWEPlaintext { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index f2ba869..4190a4b 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -13,15 +13,26 @@ pub struct LWESecret { pub(crate) dist: Distribution, } -impl LWESecret> { - pub fn alloc(n: Degree) -> Self { - Self { +pub trait LWESecretAlloc { + fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { + LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, } } } +impl LWESecretAlloc for Module {} + +impl LWESecret> { + pub fn alloc(module: &Module, n: Degree) -> Self + where + Module: LWESecretAlloc, + { + module.alloc_lwe_secret(n) + } +} + impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 947e79d..e9905ae 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -131,55 +131,91 @@ impl WriterTo for LWEToGLWESwitchingKey { } } +pub trait LWEToGLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_to_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKey> { + LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + + self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } + + fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + } + + fn bytes_of_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } +} + +impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl LWEToGLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_lwe_to_glwe_switching_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + where + Module: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index e590c21..853419b 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -80,7 +80,7 @@ pub trait AutomorphismKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn automorphism_key_prepared_alloc( + fn alloc_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -89,12 +89,12 @@ where dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, B> { - key: self.glwe_switching_key_prepared_alloc(base2k, k, rank, rank, dnum, dsize), + key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } - fn automorphism_key_prepared_alloc_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> + fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> where A: GGLWEInfos, { @@ -103,7 +103,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - self.automorphism_key_prepared_alloc( + self.alloc_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), @@ -112,7 +112,7 @@ where ) } - fn automorphism_key_prepared_alloc_bytes( + fn bytes_of_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -120,10 +120,10 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize) + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) } - fn automorphism_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -132,7 +132,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - self.automorphism_key_prepared_alloc_bytes( + self.bytes_of_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), @@ -150,29 +150,29 @@ impl AutomorphismKeyPrepared, B> { A: GGLWEInfos, Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc_from_infos(infos) + module.alloc_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize) + module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) + module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } } @@ -180,19 +180,19 @@ pub trait AutomorphismKeyPrepare where Self: GLWESwitchingKeyPrepare, { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos) + self.prepare_glwe_switching_key_tmp_bytes(infos) } - fn automorphism_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, { - self.glwe_switching_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_p(other.p()); } } @@ -204,7 +204,7 @@ impl AutomorphismKeyPrepared, B> { where Module: GLWESwitchingKeyPrepare, { - module.automorphism_key_prepare_tmp_bytes(self) + module.prepare_automorphism_key_tmp_bytes(self) } } @@ -214,7 +214,7 @@ impl AutomorphismKeyPrepared { O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, Module: AutomorphismKeyPrepare, { - module.automorphism_key_prepare(self, other, scratch); + module.prepare_automorphism_key(self, other, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 6060e80..2f7f6d2 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -61,7 +61,7 @@ pub trait GGLWEPreparedAlloc where Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { - fn gglwe_prepared_alloc( + fn alloc_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -92,12 +92,12 @@ where } } - fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> + fn alloc_gglwe_prepared_from_infos(&self, infos: &A) -> GGLWEPrepared, B> where A: GGLWEInfos, { assert_eq!(self.n(), infos.n()); - self.gglwe_prepared_alloc( + self.alloc_gglwe_prepared( infos.base2k(), infos.k(), infos.rank_in(), @@ -107,7 +107,7 @@ where ) } - fn gglwe_prepared_alloc_bytes( + fn bytes_of_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -130,15 +130,15 @@ where dsize.0, ); - self.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + self.vmp_pmat_bytes_of(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) } - fn gglwe_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_gglwe_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!(self.n(), infos.n()); - self.gglwe_prepared_alloc_bytes( + self.bytes_of_gglwe_prepared( infos.base2k(), infos.k(), infos.rank_in(), @@ -159,7 +159,7 @@ where where A: GGLWEInfos, { - module.gglwe_prepared_alloc_from_infos(infos) + module.alloc_gglwe_prepared_from_infos(infos) } pub fn alloc( @@ -171,17 +171,17 @@ where dnum: Dnum, dsize: Dsize, ) -> Self { - module.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) + module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.gglwe_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_gglwe_prepared_from_infos(infos) } - pub fn alloc_bytes( + pub fn bytes_of( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -190,7 +190,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - module.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -198,7 +198,7 @@ pub trait GGLWEPrepare where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { - fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -210,7 +210,7 @@ where ) } - fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_gglwe(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut, O: GGLWEToRef, @@ -238,7 +238,7 @@ where where O: GGLWEToRef, { - module.gglwe_prepare(self, other, scratch); + module.prepare_gglwe(self, other, scratch); } } @@ -247,7 +247,7 @@ impl GGLWEPrepared, B> { where Module: GGLWEPrepare, { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 6d1e443..43eccc8 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -79,7 +79,7 @@ pub trait GLWESwitchingKeyPreparedAlloc where Self: GGLWEPreparedAlloc, { - fn glwe_switching_key_prepared_alloc( + fn alloc_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -89,7 +89,7 @@ where dsize: Dsize, ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, B> { - key: self.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } @@ -99,7 +99,7 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_prepared_alloc( + self.alloc_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(), @@ -109,7 +109,7 @@ where ) } - fn glwe_switching_key_prepared_alloc_bytes( + fn bytes_of_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -118,14 +118,14 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - fn glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn glwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepared_alloc_bytes( + self.bytes_of_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(), @@ -158,17 +158,17 @@ where dnum: Dnum, dsize: Dsize, ) -> Self { - module.glwe_switching_key_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) + module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.glwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes( + pub fn bytes_of( module: &Module, base2k: Base2K, k: TorusPrecision, @@ -177,7 +177,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - module.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -185,19 +185,19 @@ pub trait GLWESwitchingKeyPrepare where Self: GGLWEPrepare, { - fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_glwe_switching_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.gglwe_prepare_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } - fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, { - self.gglwe_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_sk_in_n(other.sk_in_n()); res.set_sk_out_n(other.sk_out_n()); } @@ -211,7 +211,7 @@ impl GLWESwitchingKeyPrepared { O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, Module: GLWESwitchingKeyPrepare, { - module.glwe_switching_prepare(self, other, scratch); + module.prepare_glwe_switching(self, other, scratch); } } @@ -220,7 +220,7 @@ where Module: GLWESwitchingKeyPrepare, { pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index b4cd56d..27b1699 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -59,7 +59,7 @@ pub trait TensorKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn tensor_key_prepared_alloc( + fn alloc_tensor_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -70,12 +70,12 @@ where let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyPrepared { keys: (0..pairs) - .map(|_| self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn tensor_key_prepared_alloc_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> + fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, { @@ -84,7 +84,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - self.tensor_key_prepared_alloc( + self.alloc_tensor_key_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -93,16 +93,16 @@ where ) } - fn tensor_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank, dnum, dsize) + pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) } - fn tensor_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_tensor_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.tensor_key_prepared_alloc_bytes( + self.bytes_of_tensor_key_prepared( infos.base2k(), infos.k(), infos.rank(), @@ -122,29 +122,22 @@ where where A: GGLWEInfos, { - module.tensor_key_prepared_alloc_from_infos(infos) + module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { - module.tensor_key_prepared_alloc(base2k, k, dnum, dsize, rank) + module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.tensor_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_tensor_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - module.tensor_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } @@ -174,14 +167,14 @@ pub trait TensorKeyPrepare where Self: GLWESwitchingKeyPrepare, { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos) + self.prepare_glwe_switching_key_tmp_bytes(infos) } - fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: TensorKeyPreparedToMut, O: TensorKeyToRef, @@ -192,7 +185,7 @@ where assert_eq!(res.keys.len(), other.keys.len()); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.glwe_switching_prepare(a, b, scratch); + self.prepare_glwe_switching(a, b, scratch); } } } @@ -207,7 +200,7 @@ where where A: GGLWEInfos, { - module.tensor_key_prepare_tmp_bytes(infos) + module.prepare_tensor_key_tmp_bytes(infos) } } @@ -219,7 +212,7 @@ where where O: TensorKeyToRef, { - module.tensor_key_prepare(self, other, scratch); + module.prepare_tensor_key(self, other, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index da9c5db..8f3b6c1 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -53,7 +53,7 @@ pub trait GGSWPreparedAlloc where Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { - fn ggsw_prepared_alloc( + fn alloc_ggsw_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -88,12 +88,12 @@ where } } - fn ggsw_prepared_alloc_from_infos(&self, infos: &A) -> GGSWPrepared, B> + fn alloc_ggsw_prepared_from_infos(&self, infos: &A) -> GGSWPrepared, B> where A: GGSWInfos, { assert_eq!(self.n(), infos.n()); - self.ggsw_prepared_alloc( + self.alloc_ggsw_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -102,7 +102,7 @@ where ) } - fn ggsw_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + fn bytes_of_ggsw_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -117,15 +117,15 @@ where dsize.0, ); - self.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.vmp_pmat_bytes_of(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) } - fn ggsw_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_ggsw_prepared_from_infos(&self, infos: &A) -> usize where A: GGSWInfos, { assert_eq!(self.n(), infos.n()); - self.ggsw_prepared_alloc_bytes( + self.bytes_of_ggsw_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -145,29 +145,22 @@ where where A: GGSWInfos, { - module.ggsw_prepared_alloc_from_infos(infos) + module.alloc_ggsw_prepared_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { - module.ggsw_prepared_alloc(base2k, k, dnum, dsize, rank) + module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGSWInfos, { - module.ggsw_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_ggsw_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - dsize: Dsize, - rank: Rank, - ) -> usize { - module.ggsw_prepared_alloc_bytes(base2k, k, dnum, dsize, rank) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 4ad2be6..36d1e81 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -54,7 +54,7 @@ pub trait GLWEPublicKeyPreparedAlloc where Self: GetDegree + VecZnxDftAlloc + VecZnxDftAllocBytes, { - fn glwe_public_key_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { + 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, @@ -63,22 +63,22 @@ where } } - fn glwe_public_key_alloc_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> + fn alloc_glwe_public_key_prepared_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> where A: GLWEInfos, { - self.glwe_public_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } - fn glwe_public_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - self.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + self.vec_znx_dft_bytes_of((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } - fn glwe_public_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_public_key_prepared_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { - self.glwe_public_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank()) + self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } } @@ -92,22 +92,22 @@ where where A: GLWEInfos, { - module.glwe_public_key_alloc_from_infos(infos) + module.alloc_glwe_public_key_prepared_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - module.glwe_public_key_prepared_alloc(base2k, k, rank) + module.alloc_glwe_public_key_prepared(base2k, k, rank) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GLWEInfos, { - module.glwe_public_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_glwe_public_key_prepared_from_infos(infos) } - pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } } @@ -115,7 +115,7 @@ pub trait GLWEPublicKeyPrepare where Self: GetDegree + VecZnxDftApply, { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O) + fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut + SetDist, O: GLWEPublicKeyToRef + GetDist, @@ -149,7 +149,7 @@ where where O: GLWEPublicKeyToRef + GetDist, { - module.glwe_public_key_prepare(self, other); + module.prepare_glwe_public_key(self, other); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 4bac6c0..4fc9ba3 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -49,29 +49,29 @@ pub trait GLWESecretPreparedAlloc where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc, { - fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared, B> { + fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - fn glwe_secret_prepared_alloc_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> + fn alloc_glwe_secret_prepared_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> where A: GLWEInfos, { assert_eq!(self.n(), infos.n()); - self.glwe_secret_prepared_alloc(infos.rank()) + self.alloc_glwe_secret_prepared(infos.rank()) } - fn glwe_secret_alloc_bytes(&self, rank: Rank) -> usize { - self.svp_ppol_alloc_bytes(rank.into()) + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + self.svp_ppol_bytes_of(rank.into()) } - fn glwe_secret_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { assert_eq!(self.n(), infos.n()); - self.glwe_secret_alloc_bytes(infos.rank()) + self.bytes_of_glwe_secret(infos.rank()) } } @@ -85,22 +85,22 @@ where where A: GLWEInfos, { - module.glwe_secret_prepared_alloc_from_infos(infos) + module.alloc_glwe_secret_prepared_from_infos(infos) } pub fn alloc(module: &Module, rank: Rank) -> Self { - module.glwe_secret_prepared_alloc(rank) + module.alloc_glwe_secret_prepared(rank) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GLWEInfos, { - module.glwe_secret_alloc_bytes_from_infos(infos) + module.bytes_of_glwe_secret_from_infos(infos) } - pub fn alloc_bytes(module: &Module, rank: Rank) -> usize { - module.glwe_secret_alloc_bytes(rank) + pub fn bytes_of(module: &Module, rank: Rank) -> usize { + module.bytes_of_glwe_secret(rank) } } @@ -118,7 +118,7 @@ pub trait GLWESecretPrepare where Self: SvpPrepare, { - fn glwe_secret_prepare(&self, res: &mut R, other: &O) + fn prepare_glwe_secret(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut + SetDist, O: GLWESecretToRef + GetDist, diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 5f10f66..620bf85 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -64,7 +64,7 @@ where rank_in: Rank, dnum: Dnum, ) -> GLWEToLWESwitchingKeyPrepared, B> { - GLWEToLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) } fn glwe_to_lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> where @@ -83,17 +83,11 @@ where self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - fn glwe_to_lwe_switching_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + fn glwe_to_lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - fn glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -107,7 +101,7 @@ where 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - self.glwe_to_lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + self.glwe_to_lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } } @@ -128,15 +122,15 @@ where module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + module.glwe_to_lwe_switching_key_prepared_bytes_of(base2k, k, rank_in, dnum) } } @@ -148,7 +142,7 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -156,7 +150,7 @@ where R: GLWEToLWESwitchingKeyPreparedToMut, O: GLWEToLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 6017f2f..ffb5bf9 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -56,13 +56,13 @@ pub trait LWESwitchingKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn lwe_switching_key_prepared_alloc( + fn alloc_lwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, ) -> LWESwitchingKeyPrepared, B> { - LWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) } fn lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> @@ -84,14 +84,14 @@ where 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.dnum()) + self.alloc_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) } - fn lwe_switching_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + fn lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - fn lwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -110,7 +110,7 @@ where 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.dnum()) + self.lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.dnum()) } } @@ -128,18 +128,18 @@ where } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - module.lwe_switching_key_prepared_alloc(base2k, k, dnum) + module.alloc_lwe_switching_key_prepared(base2k, k, dnum) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.lwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + module.lwe_switching_key_prepared_bytes_of(base2k, k, dnum) } } @@ -151,14 +151,14 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWESwitchingKeyPreparedToMut, O: LWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index ed99a51..b2e7383 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -65,7 +65,7 @@ where rank_out: Rank, dnum: Dnum, ) -> LWEToGLWESwitchingKeyPrepared, B> { - LWEToGLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) } fn lwe_to_glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> where @@ -84,17 +84,17 @@ where self.lwe_to_glwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - fn lwe_to_glwe_switching_key_prepared_alloc_bytes( + fn lwe_to_glwe_switching_key_prepared_bytes_of( &self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum, ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - fn lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(&self, infos: &A) -> usize + fn lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -108,7 +108,7 @@ where 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - self.lwe_to_glwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + self.lwe_to_glwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } } @@ -129,15 +129,15 @@ where module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { - module.lwe_to_glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_out, dnum) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + module.lwe_to_glwe_switching_key_prepared_bytes_of(base2k, k, rank_out, dnum) } } @@ -149,7 +149,7 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -157,7 +157,7 @@ where R: LWEToGLWESwitchingKeyPreparedToMut, O: LWEToGLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 70a8ef3..4582ad2 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGLWE, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGLWE { pub fn assert_noise( @@ -35,8 +35,8 @@ impl GGLWE { let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 84a2f87..0527b14 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGSW { pub fn assert_noise( @@ -42,13 +42,13 @@ impl GGSW { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { @@ -114,13 +114,13 @@ impl GGSW { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index f7af2a1..c1fcd2f 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext { +impl GLWE { pub fn noise( &self, module: &Module, @@ -32,7 +32,7 @@ impl GLWECiphertext { + VecZnxBigNormalize, Scratch: TakeVecZnxDft + TakeVecZnxBig, { - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); self.decrypt(module, &mut pt_have, sk_prepared, scratch); module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); @@ -61,7 +61,7 @@ impl GLWECiphertext { + VecZnxNormalizeInplace, B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index b0ee4f6..ea4cf9d 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -7,24 +7,22 @@ use poulpy_hal::{ layouts::{Backend, DataMut, Module, Scratch, VecZnx, ZnxZero}, }; -use crate::layouts::{ - GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, GLWEPlaintext, LWEInfos, TorusPrecision, -}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}; impl GLWEOperations for GLWEPlaintext where D: DataMut, - GLWEPlaintext: GLWECiphertextToMut + GLWEInfos, + GLWEPlaintext: GLWEToMut + GLWEInfos, { } -impl GLWEOperations for GLWECiphertext where GLWECiphertext: GLWECiphertextToMut + GLWEInfos {} +impl GLWEOperations for GLWE where GLWE: GLWEToMut + GLWEInfos {} -pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Sized { +pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { fn add(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxAdd + VecZnxCopy, { #[cfg(debug_assertions)] @@ -39,9 +37,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_add(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); @@ -64,13 +62,13 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn add_inplace(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxAddInplace, { #[cfg(debug_assertions)] @@ -80,8 +78,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_add_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -92,8 +90,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, { #[cfg(debug_assertions)] @@ -108,9 +106,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_sub(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); @@ -134,13 +132,13 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn sub_inplace_ab(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubInplace, { #[cfg(debug_assertions)] @@ -150,8 +148,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -162,7 +160,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub_inplace_ba(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubNegateInplace, { #[cfg(debug_assertions)] @@ -172,8 +170,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_negate_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -184,7 +182,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn rotate(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxRotate, { #[cfg(debug_assertions)] @@ -193,14 +191,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) } @@ -208,7 +206,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxRotateInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate_inplace(k, &mut self_mut.data, i, scratch); @@ -217,7 +215,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn mul_xp_minus_one(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxMulXpMinusOne, { #[cfg(debug_assertions)] @@ -226,14 +224,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) } @@ -241,7 +239,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxMulXpMinusOneInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one_inplace(k, &mut self_mut.data, i, scratch); @@ -250,7 +248,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn copy(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxCopy, { #[cfg(debug_assertions)] @@ -259,15 +257,15 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); }); self.set_k(a.k().min(self.max_k())); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); } fn rsh(&mut self, module: &Module, k: usize, scratch: &mut Scratch) @@ -282,7 +280,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn normalize(&mut self, module: &Module, a: &A, scratch: &mut Scratch) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxNormalize, { #[cfg(debug_assertions)] @@ -291,8 +289,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize( @@ -305,7 +303,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size scratch, ); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(a.k().min(self.k())); } @@ -313,14 +311,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxNormalizeInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize_inplace(self_mut.base2k().into(), &mut self_mut.data, i, scratch); }); } } -impl GLWECiphertext> { +impl GLWE> { pub fn rsh_scratch_space(n: usize) -> usize { VecZnx::rsh_scratch_space(n) } diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 01b260e..a125d70 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, - GLWESecret, GLWESwitchingKey, Rank, TensorKey, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, + GLWESwitchingKey, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, @@ -16,13 +16,13 @@ use crate::{ }; pub trait TakeGLWECt { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos; } pub trait TakeGLWECtSlice { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos; } @@ -123,13 +123,13 @@ impl TakeGLWECt for Scratch where Scratch: TakeVecZnx, { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos, { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWECiphertext { + GLWE { k: infos.k(), base2k: infos.base2k(), data, @@ -143,12 +143,12 @@ impl TakeGLWECtSlice for Scratch where Scratch: TakeVecZnx, { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, { let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { let (ct, new_scratch) = scratch.take_glwe_ct(infos); scratch = new_scratch; @@ -463,7 +463,7 @@ where let mut scratch: &mut Scratch = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { @@ -499,7 +499,7 @@ where let mut scratch: &mut Scratch = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 50dc0d6..3c4e564 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,12 +1,12 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey, - LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, + LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, - GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, - LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed, + AutomorphismKeyCompressed, GGLWECompressed, GGSWCompressed, GLWECompressed, GLWESwitchingKeyCompressed, + GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + TensorKeyCompressed, }, }; @@ -20,51 +20,50 @@ const DSIZE: Dsize = Dsize(1); #[test] fn glwe_serialization() { - let original: GLWECiphertext> = GLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWE> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let original: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let original: LWECiphertext> = LWECiphertext::alloc_with(N_LWE, BASE2K, K); + let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let original: LWECiphertextCompressed> = LWECiphertextCompressed::alloc_with(BASE2K, K); + let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let original: GGLWE> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let original: GGLWECiphertextCompressed> = - GGLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let original: GLWESwitchingKey> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { let original: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -76,39 +75,38 @@ fn test_automorphism_key_serialization() { #[test] fn test_automorphism_key_compressed_serialization() { - let original: AutomorphismKeyCompressed> = - AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: TensorKey> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { let original: GLWEToLWESwitchingKeyCompressed> = - GLWEToLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } @@ -121,24 +119,24 @@ fn lwe_to_glwe_switching_key_compressed_serialization() { #[test] fn lwe_switching_key_serialization() { - let original: LWESwitchingKey> = LWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let original: GGSW> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let original: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed> = GGSWCompressed::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 b388e1c..b1acd01 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -130,7 +130,7 @@ where ), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key_in); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key_in); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 @@ -166,9 +166,9 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&auto_key_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key_out_infos); + let mut sk_auto: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key_out_infos); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { module.vec_znx_automorphism( @@ -311,8 +311,8 @@ where rank: rank.into(), }; - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -324,7 +324,7 @@ where | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 @@ -355,9 +355,9 @@ where // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow()); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&auto_key); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk_auto: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { 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 b63d616..e49fb0f 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -128,10 +128,10 @@ where rank: rank.into(), }; - let mut ct_in: GGSW> = GGSW::alloc(&ggsw_in_layout); - let mut ct_out: GGSW> = GGSW::alloc(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut ct_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_layout); + let mut ct_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -147,7 +147,7 @@ where let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -309,9 +309,9 @@ where rank: rank.into(), }; - let mut ct: GGSW> = GGSW::alloc(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -327,7 +327,7 @@ where let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); 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 5affb70..4d6218a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -18,7 +18,7 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, @@ -77,14 +77,14 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let ct_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -100,10 +100,10 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&ct_in_infos); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&ct_in_infos); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -113,12 +113,12 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWECiphertext::decrypt_scratch_space(module, &ct_out) - | GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in) - | GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), + | GLWE::decrypt_scratch_space(module, &ct_out) + | GLWE::encrypt_sk_scratch_space(module, &ct_in) + | GLWE::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -219,7 +219,7 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -235,9 +235,9 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -247,12 +247,12 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWECiphertext::decrypt_scratch_space(module, &ct) - | GLWECiphertext::encrypt_sk_scratch_space(module, &ct) - | GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey), + | GLWE::decrypt_scratch_space(module, &ct) + | GLWE::encrypt_sk_scratch_space(module, &ct) + | GLWE::automorphism_inplace_scratch_space(module, &ct, &autokey), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 7e3beba..d1384ee 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -16,9 +16,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, - GLWEToLWESwitchingKey, LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, - LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, + LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, }; @@ -83,7 +82,7 @@ where rank_out: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -98,11 +97,11 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, &lwe_to_glwe_infos) - | GLWECiphertext::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + | GLWE::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -112,13 +111,13 @@ where let data: i64 = 17; - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_pt.encode_i64(data, k_lwe_pt); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe); - let mut ksk: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(&lwe_to_glwe_infos); + let mut ksk: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos); ksk.encrypt_sk( module, @@ -129,13 +128,13 @@ where scratch.borrow(), ); - let mut glwe_ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow()); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); @@ -196,7 +195,7 @@ where rank_in: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -215,11 +214,11 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) - | LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + | LWE::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -228,10 +227,10 @@ where sk_lwe.fill_ternary_prob(0.5, &mut source_xs); let data: i64 = 17; - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); - let mut glwe_ct = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt, @@ -252,13 +251,13 @@ where scratch.borrow(), ); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); let ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); 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 476abd1..b08de6b 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -84,7 +84,7 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -94,7 +94,7 @@ where module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -187,7 +187,7 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -196,7 +196,7 @@ where module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -217,7 +217,7 @@ where }); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key 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 b0a8a8b..95a0f7e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -81,7 +81,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -92,10 +92,10 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -173,7 +173,8 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GLWESwitchingKeyCompressed> = GLWESwitchingKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed> = + GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -183,10 +184,10 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -201,7 +202,7 @@ where scratch.borrow(), ); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key 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 13ed1de..2aa8f1a 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -18,7 +18,7 @@ use crate::{ encryption::SIGMA, layouts::{ GGSW, GGSWCiphertextLayout, GLWESecret, - compressed::{Decompress, GGSWCiphertextCompressed}, + compressed::{Decompress, GGSWCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -45,7 +45,7 @@ where rank: rank.into(), }; - let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -57,7 +57,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -135,7 +135,7 @@ where rank: rank.into(), }; - let mut ct_compressed: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc(&ggsw_infos); + let mut ct_compressed: GGSWCompressed> = GGSWCompressed::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -144,12 +144,12 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertextCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_scratch_space( module, &ggsw_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -166,7 +166,7 @@ where let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); 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 35938c3..841ed6e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, - compressed::{Decompress, GLWECiphertextCompressed}, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, + compressed::{Decompress, GLWECompressed}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, operations::GLWEOperations, @@ -64,7 +64,7 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), @@ -77,20 +77,19 @@ where k: k_pt.into(), }; - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -157,7 +156,7 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), @@ -170,21 +169,20 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc(&glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertextCompressed::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWECompressed::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -201,7 +199,7 @@ where scratch.borrow(), ); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); @@ -258,29 +256,28 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos), + GLWE::decrypt_scratch_space(module, &glwe_infos) | GLWE::encrypt_sk_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module, @@ -336,16 +333,16 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -353,16 +350,16 @@ where let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_pk_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_scratch_space(module, &glwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos) + | GLWE::encrypt_pk_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc(&glwe_infos); + let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(&glwe_infos); pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); 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 2944d59..7b34e63 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -80,7 +80,7 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -91,7 +91,7 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -103,11 +103,11 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { @@ -199,7 +199,7 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -209,7 +209,7 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -217,14 +217,14 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { 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 15ac048..c4cb4aa 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 @@ -110,9 +110,9 @@ where rank: rank_out.into(), }; - let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -137,10 +137,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -282,8 +282,8 @@ where rank: rank_out.into(), }; - let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -303,10 +303,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); 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 32f15e9..e7f8d85 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 @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -133,7 +133,7 @@ where | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -265,8 +265,8 @@ where rank: rank.into(), }; - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -287,7 +287,7 @@ where | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); 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 efd5af0..cf1880a 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 @@ -17,7 +17,7 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, @@ -73,14 +73,14 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -96,11 +96,11 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -117,11 +117,11 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), + | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) + | GLWE::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -224,7 +224,7 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -240,10 +240,10 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -260,11 +260,11 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), + | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); 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 dfde51e..41fb701 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -110,9 +110,9 @@ where rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -130,13 +130,13 @@ where &gglwe_s1s2_infos, )); - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in_s0s1.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in_s0s1.into()); sk0.fill_ternary_prob(0.5, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s0s1.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s0s1.into()); sk1.fill_ternary_prob(0.5, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s1s2.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s1s2.into()); sk2.fill_ternary_prob(0.5, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); @@ -266,8 +266,8 @@ where rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -285,13 +285,13 @@ where let var_xs: f64 = 0.5; - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk0.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk1.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk2.fill_ternary_prob(var_xs, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); 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 91114a7..55f1b7f 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -121,10 +121,10 @@ where rank_out: rank.into(), }; - let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -146,11 +146,11 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -300,9 +300,9 @@ where rank_out: rank.into(), }; - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -318,11 +318,11 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); 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 7c8c1ec..6654fe9 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -18,7 +18,7 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, @@ -77,14 +77,14 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank_in.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -101,10 +101,10 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -114,15 +114,15 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), + | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) + | GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -217,7 +217,7 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -234,9 +234,9 @@ where rank_out: rank.into(), }; - let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -246,15 +246,15 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), + | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), ); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); 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 1c29c70..b20361a 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, + LWE, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, }; @@ -100,7 +100,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( LWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | LWECiphertext::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), + | LWE::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), ); let mut sk_lwe_in: LWESecret> = LWESecret::alloc(n_lwe_in.into()); @@ -111,10 +111,10 @@ where let data: i64 = 17; - let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); lwe_pt_in.encode_i64(data, k_lwe_pt.into()); - let mut lwe_ct_in: LWECiphertext> = LWECiphertext::alloc(&lwe_in_infos); + let mut lwe_ct_in: LWE> = LWE::alloc_from_infos(&lwe_in_infos); lwe_ct_in.encrypt_sk( module, &lwe_pt_in, @@ -123,7 +123,7 @@ where &mut source_xe, ); - let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc(&key_apply_infos); + let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc_from_infos(&key_apply_infos); ksk.encrypt_sk( module, @@ -134,13 +134,13 @@ where scratch.borrow(), ); - let mut lwe_ct_out: LWECiphertext> = LWECiphertext::alloc(&lwe_out_infos); + let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); let ksk_prepared: LWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); - let mut lwe_pt_out: LWEPlaintext> = LWEPlaintext::alloc(&lwe_out_infos); + let mut lwe_pt_out: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_out_infos); lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out); assert_eq!(lwe_pt_in.data.at(0, 0)[0], lwe_pt_out.data.at(0, 0)[0]); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index 354738b..005a556 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -21,7 +21,7 @@ use poulpy_hal::{ use crate::{ GLWEOperations, GLWEPacker, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; @@ -88,7 +88,7 @@ where let dnum: usize = k_ct.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), @@ -105,16 +105,16 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) + GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { *x = i as i64; @@ -125,7 +125,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap, B>> = HashMap::new(); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -143,7 +143,7 @@ where let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); ct.encrypt_sk( module, @@ -171,19 +171,14 @@ where if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); } else { - packer.add( - module, - None::<&GLWECiphertext>>, - &auto_keys, - scratch.borrow(), - ) + packer.add(module, None::<&GLWE>>, &auto_keys, scratch.borrow()) } }); - let mut res: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); packer.flush(module, &mut res); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { if i.is_multiple_of(5) { diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index d3a8976..73ce3cb 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -21,7 +21,7 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWEInfos, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWEInfos, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product, @@ -82,7 +82,7 @@ where let dsize: usize = 1; let dnum: usize = k.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -98,22 +98,22 @@ where dnum: dnum.into(), }; - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) + GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::decrypt_scratch_space(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), + | GLWE::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -135,8 +135,8 @@ where ); let mut auto_keys: HashMap, B>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); + 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| { tmp.encrypt_sk( module, diff --git a/poulpy-hal/src/api/svp_ppol.rs b/poulpy-hal/src/api/svp_ppol.rs index 5a72367..b1733cc 100644 --- a/poulpy-hal/src/api/svp_ppol.rs +++ b/poulpy-hal/src/api/svp_ppol.rs @@ -9,7 +9,7 @@ pub trait SvpPPolAlloc { /// Returns the size in bytes to allocate a [crate::layouts::SvpPPol]. pub trait SvpPPolAllocBytes { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize; + fn svp_ppol_bytes_of(&self, cols: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::MatZnx]. diff --git a/poulpy-hal/src/api/vec_znx_big.rs b/poulpy-hal/src/api/vec_znx_big.rs index 08159bb..52048a5 100644 --- a/poulpy-hal/src/api/vec_znx_big.rs +++ b/poulpy-hal/src/api/vec_znx_big.rs @@ -17,7 +17,7 @@ pub trait VecZnxBigAlloc { /// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig]. pub trait VecZnxBigAllocBytes { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize; + fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::VecZnxBig]. diff --git a/poulpy-hal/src/api/vec_znx_dft.rs b/poulpy-hal/src/api/vec_znx_dft.rs index 58589c3..621860c 100644 --- a/poulpy-hal/src/api/vec_znx_dft.rs +++ b/poulpy-hal/src/api/vec_znx_dft.rs @@ -11,7 +11,7 @@ pub trait VecZnxDftFromBytes { } pub trait VecZnxDftAllocBytes { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize; + fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize; } pub trait VecZnxDftApply { diff --git a/poulpy-hal/src/api/vmp_pmat.rs b/poulpy-hal/src/api/vmp_pmat.rs index 3d0e248..be749de 100644 --- a/poulpy-hal/src/api/vmp_pmat.rs +++ b/poulpy-hal/src/api/vmp_pmat.rs @@ -7,7 +7,7 @@ pub trait VmpPMatAlloc { } pub trait VmpPMatAllocBytes { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPMatFromBytes { diff --git a/poulpy-hal/src/delegates/svp_ppol.rs b/poulpy-hal/src/delegates/svp_ppol.rs index 54a99b2..aaa3954 100644 --- a/poulpy-hal/src/delegates/svp_ppol.rs +++ b/poulpy-hal/src/delegates/svp_ppol.rs @@ -34,8 +34,8 @@ impl SvpPPolAllocBytes for Module where B: Backend + SvpPPolAllocBytesImpl, { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize { - B::svp_ppol_alloc_bytes_impl(self.n(), cols) + fn svp_ppol_bytes_of(&self, cols: usize) -> usize { + B::svp_ppol_bytes_of_impl(self.n(), cols) } } diff --git a/poulpy-hal/src/delegates/vec_znx_big.rs b/poulpy-hal/src/delegates/vec_znx_big.rs index 1556a87..d5c06f5 100644 --- a/poulpy-hal/src/delegates/vec_znx_big.rs +++ b/poulpy-hal/src/delegates/vec_znx_big.rs @@ -53,8 +53,8 @@ impl VecZnxBigAllocBytes for Module where B: Backend + VecZnxBigAllocBytesImpl, { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_big_alloc_bytes_impl(self.n(), cols, size) + fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize { + B::vec_znx_big_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vec_znx_dft.rs b/poulpy-hal/src/delegates/vec_znx_dft.rs index 3736e34..650e657 100644 --- a/poulpy-hal/src/delegates/vec_znx_dft.rs +++ b/poulpy-hal/src/delegates/vec_znx_dft.rs @@ -28,8 +28,8 @@ impl VecZnxDftAllocBytes for Module where B: Backend + VecZnxDftAllocBytesImpl, { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_dft_alloc_bytes_impl(self.n(), cols, size) + fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize { + B::vec_znx_dft_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vmp_pmat.rs b/poulpy-hal/src/delegates/vmp_pmat.rs index a875a40..2df0d4a 100644 --- a/poulpy-hal/src/delegates/vmp_pmat.rs +++ b/poulpy-hal/src/delegates/vmp_pmat.rs @@ -27,8 +27,8 @@ impl VmpPMatAllocBytes for Module where B: Backend + VmpPMatAllocBytesImpl, { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - B::vmp_pmat_alloc_bytes_impl(self.n(), rows, cols_in, cols_out, size) + fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + B::vmp_pmat_bytes_of_impl(self.n(), rows, cols_in, cols_out, size) } } diff --git a/poulpy-hal/src/layouts/mat_znx.rs b/poulpy-hal/src/layouts/mat_znx.rs index 01be1a1..5ccb860 100644 --- a/poulpy-hal/src/layouts/mat_znx.rs +++ b/poulpy-hal/src/layouts/mat_znx.rs @@ -114,12 +114,12 @@ impl MatZnx { } impl MatZnx> { - pub fn alloc_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - rows * cols_in * VecZnx::>::alloc_bytes(n, cols_out, size) + pub fn bytes_of(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + rows * cols_in * VecZnx::>::bytes_of(n, cols_out, size) } pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec = alloc_aligned(Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + let data: Vec = alloc_aligned(Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n, @@ -132,7 +132,7 @@ impl MatZnx> { pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + assert!(data.len() == Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n, @@ -153,7 +153,7 @@ impl MatZnx { } let self_ref: MatZnx<&[u8]> = self.to_ref(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(self.n, self.cols_out, self.size); + let nb_bytes: usize = VecZnx::>::bytes_of(self.n, self.cols_out, self.size); let start: usize = nb_bytes * self.cols() * row + col * nb_bytes; let end: usize = start + nb_bytes; @@ -181,7 +181,7 @@ impl MatZnx { let size: usize = self.size(); let self_ref: MatZnx<&mut [u8]> = self.to_mut(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(n, cols_out, size); + let nb_bytes: usize = VecZnx::>::bytes_of(n, cols_out, size); let start: usize = nb_bytes * cols_in * row + col * nb_bytes; let end: usize = start + nb_bytes; diff --git a/poulpy-hal/src/layouts/scalar_znx.rs b/poulpy-hal/src/layouts/scalar_znx.rs index 296071b..bdf2159 100644 --- a/poulpy-hal/src/layouts/scalar_znx.rs +++ b/poulpy-hal/src/layouts/scalar_znx.rs @@ -132,18 +132,18 @@ impl ScalarZnx { } impl ScalarZnx> { - pub fn alloc_bytes(n: usize, cols: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize) -> usize { n * cols * size_of::() } pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols)); Self { data, n, cols } } pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data, n, cols } } } diff --git a/poulpy-hal/src/layouts/svp_ppol.rs b/poulpy-hal/src/layouts/svp_ppol.rs index 50523fc..234ebca 100644 --- a/poulpy-hal/src/layouts/svp_ppol.rs +++ b/poulpy-hal/src/layouts/svp_ppol.rs @@ -77,7 +77,7 @@ where B: SvpPPolAllocBytesImpl, { pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(B::svp_ppol_alloc_bytes_impl(n, cols)); + let data: Vec = alloc_aligned::(B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n, @@ -88,7 +88,7 @@ where pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::svp_ppol_alloc_bytes_impl(n, cols)); + assert!(data.len() == B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vec_znx.rs b/poulpy-hal/src/layouts/vec_znx.rs index d40ef4b..c96de28 100644 --- a/poulpy-hal/src/layouts/vec_znx.rs +++ b/poulpy-hal/src/layouts/vec_znx.rs @@ -125,12 +125,12 @@ impl ZnxZero for VecZnx { } impl VecZnx> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -142,7 +142,7 @@ impl VecZnx> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/layouts/vec_znx_big.rs b/poulpy-hal/src/layouts/vec_znx_big.rs index c50cf66..73a3e0f 100644 --- a/poulpy-hal/src/layouts/vec_znx_big.rs +++ b/poulpy-hal/src/layouts/vec_znx_big.rs @@ -96,7 +96,7 @@ where B: VecZnxBigAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data = alloc_aligned::(B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + let data = alloc_aligned::(B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -109,7 +109,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vec_znx_dft.rs b/poulpy-hal/src/layouts/vec_znx_dft.rs index 3dc92d5..19d28e1 100644 --- a/poulpy-hal/src/layouts/vec_znx_dft.rs +++ b/poulpy-hal/src/layouts/vec_znx_dft.rs @@ -116,7 +116,7 @@ where B: VecZnxDftAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let data: Vec = alloc_aligned::(B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -129,7 +129,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vmp_pmat.rs b/poulpy-hal/src/layouts/vmp_pmat.rs index ce83458..bd469ec 100644 --- a/poulpy-hal/src/layouts/vmp_pmat.rs +++ b/poulpy-hal/src/layouts/vmp_pmat.rs @@ -88,9 +88,7 @@ where B: VmpPMatAllocBytesImpl, { pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec = alloc_aligned(B::vmp_pmat_alloc_bytes_impl( - n, rows, cols_in, cols_out, size, - )); + let data: Vec = alloc_aligned(B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n, @@ -104,7 +102,7 @@ where pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size)); + assert!(data.len() == B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/zn.rs b/poulpy-hal/src/layouts/zn.rs index 40f5622..eaba4b4 100644 --- a/poulpy-hal/src/layouts/zn.rs +++ b/poulpy-hal/src/layouts/zn.rs @@ -113,12 +113,12 @@ impl ZnxZero for Zn { } impl Zn> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -130,7 +130,7 @@ impl Zn> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/oep/svp_ppol.rs b/poulpy-hal/src/oep/svp_ppol.rs index 6550b6f..42c50ea 100644 --- a/poulpy-hal/src/oep/svp_ppol.rs +++ b/poulpy-hal/src/oep/svp_ppol.rs @@ -23,7 +23,7 @@ pub unsafe trait SvpPPolAllocImpl { /// * See [crate::api::SvpPPolAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocBytesImpl { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize; + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/oep/vec_znx_big.rs b/poulpy-hal/src/oep/vec_znx_big.rs index b2bd4c8..4c12e6a 100644 --- a/poulpy-hal/src/oep/vec_znx_big.rs +++ b/poulpy-hal/src/oep/vec_znx_big.rs @@ -35,7 +35,7 @@ pub unsafe trait VecZnxBigFromBytesImpl { /// * See [crate::api::VecZnxBigAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocBytesImpl { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } #[allow(clippy::too_many_arguments)] diff --git a/poulpy-hal/src/oep/vec_znx_dft.rs b/poulpy-hal/src/oep/vec_znx_dft.rs index e5a2bcb..0f9288b 100644 --- a/poulpy-hal/src/oep/vec_znx_dft.rs +++ b/poulpy-hal/src/oep/vec_znx_dft.rs @@ -42,7 +42,7 @@ pub unsafe trait VecZnxDftApplyImpl { /// * See [crate::api::VecZnxDftAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocBytesImpl { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/oep/vmp_pmat.rs b/poulpy-hal/src/oep/vmp_pmat.rs index e399f00..bdca416 100644 --- a/poulpy-hal/src/oep/vmp_pmat.rs +++ b/poulpy-hal/src/oep/vmp_pmat.rs @@ -15,7 +15,7 @@ pub unsafe trait VmpPMatAllocImpl { /// * See [crate::api::VmpPMatAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocBytesImpl { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/reference/fft64/vmp.rs b/poulpy-hal/src/reference/fft64/vmp.rs index f6fb73c..07e1a8d 100644 --- a/poulpy-hal/src/reference/fft64/vmp.rs +++ b/poulpy-hal/src/reference/fft64/vmp.rs @@ -140,7 +140,7 @@ where assert!(a.cols() <= cols); } - let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_bytes_of_impl(n, cols, size)); let mut a_dft: VecZnxDft<&mut [u8], BE> = VecZnxDft::from_data(cast_mut(data), n, cols, size); diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 12a44fd..cd86fa3 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,8 +3,8 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret, - TensorKeyLayout, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWE, LWECiphertextLayout, LWESecret, TensorKeyLayout, + prepared::PrepareAlloc, }; use poulpy_hal::{ api::{ @@ -202,10 +202,10 @@ where sk_lwe.fill_binary_block(params.block_size, &mut source_xs); sk_lwe.fill_zero(); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe, rank); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let ct_lwe: LWECiphertext> = LWECiphertext::alloc(¶ms.lwe_infos); + let ct_lwe: LWE> = LWE::alloc_from_infos(¶ms.lwe_infos); // Circuit bootstrapping evaluation key let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( @@ -218,7 +218,7 @@ where scratch.borrow(), ); - let mut res: GGSW> = GGSW::alloc(¶ms.ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(¶ms.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || { diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 2ac8eb2..73435a9 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,8 +1,8 @@ use poulpy_core::{ GLWEOperations, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, + AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, + LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; @@ -140,7 +140,7 @@ fn main() { sk_lwe.fill_zero(); // GLWE secret - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); // sk_glwe.fill_zero(); @@ -151,7 +151,7 @@ fn main() { let data: i64 = 1 % (1 << k_lwe_pt); // LWE plaintext - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); // LWE plaintext(data * 2^{- (k_lwe_pt - 1)}) pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); // +1 for padding bit @@ -167,7 +167,7 @@ fn main() { println!("pt_lwe: {pt_lwe}"); // LWE ciphertext - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); // Encrypt LWE Plaintext ct_lwe.encrypt_sk(&module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); @@ -187,7 +187,7 @@ fn main() { println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = @@ -214,7 +214,7 @@ fn main() { // Tests RLWE(1) * GGSW(data) - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: (k_ggsw_res - base2k).into(), @@ -222,11 +222,11 @@ fn main() { }; // GLWE ciphertext modulus - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_infos); // Some GLWE plaintext with signed data let k_glwe_pt: usize = 3; - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut data_vec: Vec = vec![0i64; n_glwe]; data_vec .iter_mut() @@ -255,7 +255,7 @@ fn main() { ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); // Decrypt - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); ct_glwe.decrypt(&module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); println!("pt_res: {:?}", &pt_res.data.at(0, 0)[..64]); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 145ce6b..d579db2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{Base2K, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; +use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared}; use poulpy_hal::api::VecZnxBigAllocBytes; @@ -24,7 +24,7 @@ use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocks { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -62,7 +62,7 @@ impl FheUintBlocks, T> { pub(crate) fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GLWECiphertext::alloc_with(module.n().into(), base2k, k, rank)) + .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 1af36d1..2571896 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -60,7 +60,7 @@ impl FheUintBlocksPrepDebug, T> { ) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) + .map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index b09b8a5..5216611 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -2,7 +2,7 @@ use itertools::Itertools; use poulpy_core::{ GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, + GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; @@ -24,14 +24,14 @@ use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// A FHE ciphertext encrypting a [UnsignedInteger]. -pub struct FheUintWord(pub(crate) GLWECiphertext, pub(crate) PhantomData); +pub struct FheUintWord(pub(crate) GLWE, pub(crate) PhantomData); impl FheUintWord { #[allow(dead_code)] fn post_process( &mut self, module: &Module, - mut tmp_res: Vec>, + mut tmp_res: Vec>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -67,7 +67,7 @@ impl FheUintWord { // Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; - let mut cts: HashMap> = HashMap::new(); + let mut cts: HashMap> = HashMap::new(); for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 13f34e1..6eb72e7 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -2,7 +2,7 @@ use itertools::Itertools; use poulpy_core::{ GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, layouts::{ - GLWECiphertext, GLWECiphertextToMut, LWEInfos, + GLWE, GLWEToMut, LWEInfos, prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, }; @@ -38,7 +38,7 @@ where fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], + out: &mut [GLWE], inputs: &[&dyn GGSWCiphertextPreparedToRef], scratch: &mut Scratch, ) where @@ -54,7 +54,7 @@ where fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], + out: &mut [GLWE], inputs: &[&dyn GGSWCiphertextPreparedToRef], scratch: &mut Scratch, ) where @@ -159,14 +159,8 @@ impl Node { } pub trait Cmux { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, @@ -177,14 +171,8 @@ impl Cmux for Module where Module: GLWEExternalProductInplace + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index 322a2e2..260e8ba 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -11,7 +11,7 @@ use crate::tfhe::{ use poulpy_core::{ TakeGGSW, TakeGLWECt, layouts::{ - GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, }; @@ -182,7 +182,7 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out); for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch_1); @@ -231,7 +231,7 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch); self.cbt diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 5a7ae97..40d91c2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,6 +1,6 @@ #[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, TensorKeyLayout, TorusPrecision, }; @@ -25,7 +25,7 @@ pub(crate) const TEST_BLOCK_SIZE: u32 = 7; pub(crate) const TEST_RANK: u32 = 2; #[cfg(test)] -pub(crate) static TEST_GLWE_INFOS: GLWECiphertextLayout = GLWECiphertextLayout { +pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GLWE), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 1889e02..7b469c8 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs @@ -4,7 +4,7 @@ use poulpy_backend::FFT64Ref; use poulpy_core::{ TakeGGSW, TakeGLWEPt, layouts::{ - GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret, + GGSWCiphertextLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -107,7 +107,7 @@ where BlindRotationKeyPrepared, BRA, BE>: BlincRotationExecute, BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { - let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS; + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS; let n_glwe: usize = glwe_infos.n().into(); @@ -120,7 +120,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prep: GLWESecretPrepared, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 03f36e9..ca9415b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use poulpy_core::{ Distribution, GLWEOperations, TakeGLWECt, - layouts::{GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWEInfos, LWECiphertext, LWECiphertextToRef, LWEInfos}, + layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWECiphertextToRef, LWEInfos}, }; use crate::tfhe::blind_rotation::{ @@ -43,14 +43,14 @@ where if block_size > 1 { let cols: usize = (brk_infos.rank() + 1).into(); let dnum: usize = brk_infos.dnum().into(); - let acc_dft: usize = module.vec_znx_dft_alloc_bytes(cols, dnum) * extension_factor; - let acc_big: usize = module.vec_znx_big_alloc_bytes(1, brk_size); - let vmp_res: usize = module.vec_znx_dft_alloc_bytes(cols, brk_size) * extension_factor; - let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(1, brk_size); + let acc_dft: usize = module.vec_znx_dft_bytes_of(cols, dnum) * extension_factor; + let acc_big: usize = module.vec_znx_big_bytes_of(1, brk_size); + let vmp_res: usize = module.vec_znx_dft_bytes_of(cols, brk_size) * extension_factor; + let vmp_xai: usize = module.vec_znx_dft_bytes_of(1, brk_size); let acc_dft_add: usize = vmp_res; let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) let acc: usize = if extension_factor > 1 { - VecZnx::alloc_bytes(module.n(), cols, glwe_infos.size()) * extension_factor + VecZnx::bytes_of(module.n(), cols, glwe_infos.size()) * extension_factor } else { 0 }; @@ -61,8 +61,7 @@ where + vmp_xai + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes()))) } else { - GLWECiphertext::alloc_bytes(glwe_infos) - + GLWECiphertext::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) + GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) } } @@ -99,8 +98,8 @@ where fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ) { @@ -121,8 +120,8 @@ where fn execute_block_binary_extended( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -179,7 +178,7 @@ fn execute_block_binary_extended( } let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).as_usize()]; // TODO: from scratch space - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = 2 * n_glwe; let two_n_ext: usize = 2 * lut.domain_size(); @@ -288,8 +287,8 @@ fn execute_block_binary_extended( fn execute_block_binary( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -324,8 +323,8 @@ fn execute_block_binary( { let n_glwe: usize = brk.n_glwe().into(); let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = n_glwe << 1; let base2k: usize = brk.base2k().into(); let dnum: usize = brk.dnum().into(); @@ -410,8 +409,8 @@ fn execute_block_binary( fn execute_standard( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -480,8 +479,8 @@ fn execute_standard( } let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); mod_switch_2n( 2 * lut.domain_size(), @@ -519,7 +518,7 @@ fn execute_standard( out_mut.normalize_inplace(module, scratch_1); } -pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8]>, rot_dir: LookUpTableRotationDirection) { +pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) { let base2k: usize = lwe.base2k().into(); let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index 22e9bd4..f0a0962 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -15,7 +15,7 @@ use poulpy_core::{ Distribution, layouts::{ GGSW, GGSWInfos, LWESecret, - compressed::GGSWCiphertextCompressed, + compressed::GGSWCompressed, prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; @@ -32,7 +32,7 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { { let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSW::alloc(infos)); + data.push(GGSW::alloc_from_infos(infos)); } Self { @@ -137,8 +137,8 @@ impl BlindRotationKeyCompressed, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos))); + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos))); Self { keys: data, dist: Distribution::NONE, @@ -151,7 +151,7 @@ impl BlindRotationKeyCompressed, CGGI> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos) + GGSWCompressed::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs index 51ff139..784d332 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -8,14 +8,14 @@ use std::{fmt, marker::PhantomData}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, - layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCiphertextCompressed}, + layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; #[derive(Clone)] pub struct BlindRotationKeyCompressed { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs index bd83a08..8cc262b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs @@ -14,7 +14,7 @@ pub use lut::*; pub mod tests; -use poulpy_core::layouts::{GLWECiphertext, LWECiphertext}; +use poulpy_core::layouts::{GLWE, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; pub trait BlindRotationAlgo {} @@ -27,8 +27,8 @@ pub trait BlincRotationExecute { fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 190adff..b09f975 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -23,8 +23,7 @@ use crate::tfhe::blind_rotation::{ }; use poulpy_core::layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWECiphertextToRef, - LWEInfos, LWEPlaintext, LWESecret, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, LWECiphertextToRef, LWEInfos, LWEPlaintext, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }; @@ -111,7 +110,7 @@ where rank: rank.into(), }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: k_res.into(), @@ -128,7 +127,7 @@ where module, &brk_infos, )); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_dft: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -154,9 +153,9 @@ where scratch.borrow(), ); - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); let x: i64 = 15 % (message_modulus as i64); @@ -175,13 +174,13 @@ where let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); lut.set(module, &f_vec, log_message_modulus + 1); - let mut res: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let brk_prepared: BlindRotationKeyPrepared, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 22e7a7a..9e419fe 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, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -75,7 +75,7 @@ where &self, module: &Module, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -98,7 +98,7 @@ where module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -123,7 +123,7 @@ pub fn circuit_bootstrap_core( module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, key: &CircuitBootstrappingKeyPrepared, @@ -233,7 +233,7 @@ pub fn circuit_bootstrap_core( let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _; (0..dnum).for_each(|i| { - let mut tmp_glwe: GLWECiphertext<&mut [u8]> = tmp_gglwe.at_mut(i, 0); + let mut tmp_glwe: GLWE<&mut [u8]> = tmp_gglwe.at_mut(i, 0); if to_exponent { // Isolates i-th LUT and moves coefficients according to requested gap. @@ -263,8 +263,8 @@ pub fn circuit_bootstrap_core( #[allow(clippy::too_many_arguments)] fn post_process( module: &Module, - res: &mut GLWECiphertext, - a: &GLWECiphertext, + res: &mut GLWE, + a: &GLWE, log_gap_in: usize, log_gap_out: usize, log_domain: usize, @@ -303,7 +303,7 @@ fn post_process( { let log_n: usize = module.log_n(); - let mut cts: HashMap>> = HashMap::new(); + let mut cts: HashMap>> = HashMap::new(); // First partial trace, vanishes all coefficients which are not multiples of gap_in // [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0] @@ -322,7 +322,7 @@ fn post_process( let steps: usize = 1 << log_domain; // TODO: from Scratch - let mut cts_vec: Vec>> = Vec::new(); + let mut cts_vec: Vec>> = Vec::new(); for i in 0..steps { if i != 0 { @@ -336,7 +336,7 @@ fn post_process( } glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); - let packed: &mut GLWECiphertext> = cts.remove(&0).unwrap(); + let packed: &mut GLWE> = cts.remove(&0).unwrap(); res.trace( module, log_n - log_gap_out, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 3d91943..0f1e468 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, - TensorKey, TensorKeyLayout, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, + TensorKeyLayout, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap; @@ -122,9 +122,9 @@ where let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); + let gal_els: Vec = GLWE::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); auto_keys.insert(*gal_el, key); }); @@ -141,7 +141,7 @@ where scratch, ); - let mut tsk: TensorKey> = TensorKey::alloc(&trk_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self { diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index e316d34..5835765 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -5,7 +5,7 @@ pub mod tests; pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSW, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; @@ -14,7 +14,7 @@ pub trait CirtuitBootstrappingExecute { &self, module: &Module, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -26,7 +26,7 @@ pub trait CirtuitBootstrappingExecute { module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index a0dbb42..d94ac0c 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -36,7 +36,7 @@ use poulpy_core::layouts::{ }; use poulpy_core::layouts::{ - GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, + GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, prepared::{GGSWPrepared, GLWESecretPrepared}, }; @@ -179,19 +179,19 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); @@ -206,7 +206,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; @@ -236,8 +236,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - 2); ct_glwe.encrypt_sk( @@ -253,7 +253,7 @@ where ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. @@ -401,19 +401,19 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); @@ -428,7 +428,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); @@ -449,8 +449,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - k_lwe_pt - 1); ct_glwe.encrypt_sk( @@ -466,7 +466,7 @@ where ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. From 779e02acc40a05681b4ebe914c462bd663f4c865 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 14 Oct 2025 23:39:16 +0200 Subject: [PATCH 07/60] wip --- poulpy-backend/src/cpu_fft64_avx/scratch.rs | 181 +--------------- poulpy-backend/src/cpu_fft64_ref/scratch.rs | 181 +--------------- .../src/cpu_spqlios/fft64/scratch.rs | 181 +--------------- poulpy-core/src/automorphism/gglwe_atk.rs | 10 +- poulpy-core/src/automorphism/ggsw_ct.rs | 30 +-- poulpy-core/src/automorphism/glwe_ct.rs | 22 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 10 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 10 +- poulpy-core/src/decryption/glwe_ct.rs | 6 +- .../src/encryption/compressed/gglwe_atk.rs | 7 +- .../src/encryption/compressed/gglwe_ct.rs | 10 +- .../src/encryption/compressed/gglwe_ksk.rs | 9 +- .../src/encryption/compressed/gglwe_tsk.rs | 7 +- .../src/encryption/compressed/ggsw_ct.rs | 8 +- .../src/encryption/compressed/glwe_ct.rs | 4 +- poulpy-core/src/encryption/gglwe_atk.rs | 12 +- poulpy-core/src/encryption/gglwe_ct.rs | 9 +- poulpy-core/src/encryption/gglwe_ksk.rs | 10 +- poulpy-core/src/encryption/gglwe_tsk.rs | 19 +- poulpy-core/src/encryption/ggsw_ct.rs | 10 +- poulpy-core/src/encryption/glwe_ct.rs | 20 +- poulpy-core/src/encryption/glwe_pk.rs | 4 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 12 +- poulpy-core/src/encryption/lwe_ksk.rs | 12 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 12 +- poulpy-core/src/external_product/gglwe_atk.rs | 10 +- poulpy-core/src/external_product/gglwe_ksk.rs | 10 +- poulpy-core/src/external_product/ggsw_ct.rs | 10 +- poulpy-core/src/external_product/glwe_ct.rs | 155 +++++++------ poulpy-core/src/external_product/mod.rs | 6 +- poulpy-core/src/glwe_packing.rs | 22 +- poulpy-core/src/glwe_trace.rs | 12 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 18 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 50 ++--- poulpy-core/src/keyswitching/glwe_ct.rs | 26 +-- poulpy-core/src/keyswitching/lwe_ct.rs | 8 +- .../src/layouts/compressed/gglwe_atk.rs | 2 +- .../src/layouts/compressed/gglwe_tsk.rs | 20 +- poulpy-core/src/layouts/gglwe_ksk.rs | 2 + poulpy-core/src/layouts/prepared/gglwe_atk.rs | 6 +- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 8 +- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 8 +- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 8 +- poulpy-core/src/layouts/prepared/glwe_pk.rs | 8 +- poulpy-core/src/layouts/prepared/glwe_sk.rs | 8 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 28 +-- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 22 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 28 +-- poulpy-core/src/noise/gglwe_ct.rs | 8 +- poulpy-core/src/noise/ggsw_ct.rs | 14 +- poulpy-core/src/noise/glwe_ct.rs | 8 +- poulpy-core/src/scratch.rs | 205 +----------------- .../test_suite/automorphism/gglwe_atk.rs | 22 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 18 +- .../tests/test_suite/automorphism/glwe_ct.rs | 18 +- .../src/tests/test_suite/conversion.rs | 18 +- .../tests/test_suite/encryption/gglwe_atk.rs | 18 +- .../tests/test_suite/encryption/gglwe_ct.rs | 16 +- .../tests/test_suite/encryption/ggsw_ct.rs | 14 +- .../tests/test_suite/encryption/glwe_ct.rs | 31 ++- .../tests/test_suite/encryption/glwe_tsk.rs | 16 +- .../test_suite/external_product/gglwe_ksk.rs | 16 +- .../test_suite/external_product/ggsw_ct.rs | 22 +- .../test_suite/external_product/glwe_ct.rs | 16 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 22 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 18 +- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 21 +- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 12 +- poulpy-core/src/tests/test_suite/packing.rs | 12 +- poulpy-core/src/tests/test_suite/trace.rs | 18 +- poulpy-hal/src/api/scratch.rs | 147 +++++++++---- poulpy-hal/src/api/svp_ppol.rs | 4 +- poulpy-hal/src/api/vec_znx_big.rs | 4 +- poulpy-hal/src/api/vec_znx_dft.rs | 4 +- poulpy-hal/src/api/vmp_pmat.rs | 4 +- poulpy-hal/src/delegates/scratch.rs | 114 +--------- poulpy-hal/src/delegates/svp_ppol.rs | 6 +- poulpy-hal/src/delegates/vec_znx_big.rs | 6 +- poulpy-hal/src/delegates/vec_znx_dft.rs | 10 +- poulpy-hal/src/delegates/vmp_pmat.rs | 6 +- poulpy-hal/src/oep/scratch.rs | 110 +--------- .../benches/circuit_bootstrapping.rs | 24 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 20 +- .../ciphertexts/block_prepared.rs | 10 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 14 +- .../src/tfhe/bdd_arithmetic/eval.rs | 4 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 18 +- .../src/tfhe/bdd_arithmetic/test.rs | 18 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 44 ++-- .../src/tfhe/blind_rotation/cggi_key.rs | 13 +- .../tests/generic_blind_rotation.rs | 12 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 16 +- .../src/tfhe/circuit_bootstrapping/key.rs | 8 +- .../tests/circuit_bootstrapping.rs | 22 +- 94 files changed, 784 insertions(+), 1688 deletions(-) diff --git a/poulpy-backend/src/cpu_fft64_avx/scratch.rs b/poulpy-backend/src/cpu_fft64_avx/scratch.rs index 2cdf85e..f6595f9 100644 --- a/poulpy-backend/src/cpu_fft64_avx/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_avx/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_fft64_avx::FFT64Avx; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Avx -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Avx -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_bytes_of_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Avx -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_bytes_of_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Avx -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Avx -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Avx -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::bytes_of(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-backend/src/cpu_fft64_ref/scratch.rs b/poulpy-backend/src/cpu_fft64_ref/scratch.rs index ad617ce..1593370 100644 --- a/poulpy-backend/src/cpu_fft64_ref/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_ref/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_fft64_ref::FFT64Ref; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Ref -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Ref -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_bytes_of_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Ref -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_bytes_of_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Ref -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Ref -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Ref -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::bytes_of(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs index 2b56218..d32b9f4 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_spqlios::FFT64Spqlios; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Spqlios -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Spqlios -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_bytes_of_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Spqlios -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_bytes_of_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Spqlios -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Spqlios -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::bytes_of(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 233f250..fc49c52 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, @@ -20,7 +20,7 @@ impl AutomorphismKey> { OUT: GGLWEInfos, IN: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::keyswitch_scratch_space( module, @@ -34,7 +34,7 @@ impl AutomorphismKey> { where OUT: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { AutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) } @@ -48,7 +48,7 @@ impl AutomorphismKey { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -136,7 +136,7 @@ impl AutomorphismKey { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index f1a84ad..41acde9 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; @@ -26,14 +26,11 @@ impl GGSW> { IN: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { let out_size: usize = out_infos.size(); - let ci_dft: usize = module.vec_znx_dft_bytes_of((key_infos.rank_out() + 1).into(), out_size); + let ci_dft: usize = module.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), out_size); let ks_internal: usize = GLWE::keyswitch_scratch_space( module, &out_infos.glwe_layout(), @@ -54,11 +51,8 @@ impl GGSW> { OUT: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { GGSW::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) } @@ -73,7 +67,7 @@ impl GGSW { tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -83,7 +77,7 @@ impl GGSW { + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace @@ -141,7 +135,7 @@ impl GGSW { tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -151,7 +145,7 @@ impl GGSW { + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 800248a..8cdaa91 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -2,7 +2,7 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, - VecZnxBigSubSmallNegateInplace, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigSubSmallNegateInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, @@ -21,7 +21,7 @@ impl GLWE> { OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { Self::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) } @@ -30,7 +30,7 @@ impl GLWE> { where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { Self::keyswitch_inplace_scratch_space(module, out_infos, key_infos) } @@ -44,7 +44,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -70,7 +70,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -97,7 +97,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -138,7 +138,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -180,7 +180,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -222,7 +222,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -265,7 +265,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -307,7 +307,7 @@ impl GLWE { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index d3e32ae..ef18123 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -1,14 +1,14 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; use crate::{ - TakeGLWECt, + TakeGLWE, layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}, }; @@ -23,7 +23,7 @@ impl LWE> { OUT: LWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), @@ -69,7 +69,7 @@ impl LWE { ) where DGlwe: DataRef, DKs: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -80,7 +80,7 @@ impl LWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index bb5480e..de40d8f 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -1,14 +1,14 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, ZnxView, ZnxViewMut, ZnxZero}, }; use crate::{ - TakeGLWECt, + TakeGLWE, layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}, }; @@ -23,7 +23,7 @@ impl GLWE> { OUT: GLWEInfos, IN: LWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { let ct: usize = GLWE::bytes_of( module.n().into(), @@ -51,7 +51,7 @@ impl GLWE { ) where DLwe: DataRef, DKsk: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -62,7 +62,7 @@ impl GLWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 92b8c29..89c6591 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, }; @@ -12,10 +12,10 @@ impl GLWE> { pub fn decrypt_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size: usize = infos.size(); - (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_bytes_of(1, size)) + module.vec_znx_dft_bytes_of(1, size) + (module.vec_znx_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_dft(1, size) } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 7e717c6..9091e74 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{ScratchAvailable, SvpPPolAllocBytes, VecZnxAutomorphism, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, + api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; @@ -17,7 +17,7 @@ impl AutomorphismKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) @@ -40,8 +40,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk { impl GGLWEAutomorphismKeyCompressedEncryptSk for Module where - Module: - GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxAutomorphism, + Module: GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxAutomorphism, Scratch: TakeGLWESecret + ScratchAvailable, { fn gglwe_automorphism_key_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index f5a136f..bd0f3b3 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, + ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, ZnNormalizeInplace, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, @@ -8,7 +8,7 @@ use poulpy_hal::{ }; use crate::{ - TakeGLWEPt, + TakeGLWEPlaintext, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, @@ -38,7 +38,7 @@ impl GGLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GGLWE::encrypt_sk_scratch_space(module, infos) } @@ -64,10 +64,10 @@ where Module: GLWEEncryptSkInternal + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxAddScalarInplace + ZnNormalizeInplace, - Scratch: TakeGLWEPt + ScratchAvailable, + Scratch: TakeGLWEPlaintext + ScratchAvailable, { fn gglwe_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 260c02e..800a37e 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,7 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes, - VecZnxSwitchRing, + ScratchAvailable, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, @@ -21,7 +20,7 @@ impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) @@ -64,9 +63,9 @@ pub trait GGLWEKeyCompressedEncryptSk { impl GGLWEKeyCompressedEncryptSk for Module where Module: GGLWECompressedEncryptSk - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxNormalizeTmpBytes - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxSwitchRing + SvpPrepare, Scratch: ScratchAvailable + TakeScalarZnx + TakeGLWESecretPrepared, diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 65012d6..0c88f67 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDft, SvpPPolAllocBytes, SvpPrepare, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAllocBytes, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, @@ -20,8 +20,7 @@ impl TensorKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: - SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { TensorKey::encrypt_sk_scratch_space(module, infos) } diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 56f22ad..31efd53 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, + TakeGLWEPlaintext, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, @@ -18,7 +18,7 @@ impl GGSWCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { GGSW::encrypt_sk_scratch_space(module, infos) } @@ -42,7 +42,7 @@ pub trait GGSWCompressedEncryptSk { impl GGSWCompressedEncryptSk for Module where Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPt, + Scratch: TakeGLWEPlaintext, { fn ggsw_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 6fadd31..1c8efe6 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, + api::{VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; @@ -17,7 +17,7 @@ impl GLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { GLWE::encrypt_sk_scratch_space(module, infos) } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 18caa37..2fa90d4 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, + VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, Module, Scratch}, @@ -20,7 +20,7 @@ impl AutomorphismKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { assert_eq!( infos.rank_in(), @@ -80,7 +80,7 @@ where impl GGLWEAutomorphismKeyEncryptSk for Module where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -95,7 +95,7 @@ where + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxAutomorphism, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 9beed56..a4bff60 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, @@ -8,7 +8,7 @@ use poulpy_hal::{ }; use crate::{ - TakeGLWEPt, + TakeGLWEPlaintext, encryption::glwe_ct::GLWEEncryptSk, layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, @@ -20,7 +20,7 @@ impl GGLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) @@ -51,8 +51,7 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: - GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { fn gglwe_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index b0caa54..64d9e9e 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, + VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, @@ -18,7 +18,7 @@ impl GLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) @@ -45,7 +45,7 @@ impl GLWESwitchingKey { scratch: &mut Scratch, ) where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -60,7 +60,7 @@ impl GLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, + + SvpPPolBytesOf, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { #[cfg(debug_assertions)] diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 4de505a..c2762a0 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, - TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, + TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, @@ -20,13 +20,12 @@ impl TensorKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: - SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { GLWESecretPrepared::bytes_of(module, infos.rank_out()) - + module.vec_znx_dft_bytes_of(infos.rank_out().into(), 1) - + module.vec_znx_big_bytes_of(1, 1) - + module.vec_znx_dft_bytes_of(1, 1) + + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + module.bytes_of_vec_znx_big(1, 1) + + module.bytes_of_vec_znx_dft(1, 1) + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } @@ -44,7 +43,7 @@ impl TensorKey { Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -59,7 +58,7 @@ impl TensorKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, + + SvpPPolBytesOf, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared + TakeVecZnxBig, { diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index ea200d4..6def5d3 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{VecZnxAddScalarInplace, VecZnxDftAllocBytes, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, source::Source, }; use crate::{ - SIGMA, TakeGLWEPt, + SIGMA, TakeGLWEPlaintext, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, @@ -17,13 +17,13 @@ impl GGSW> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size = infos.size(); GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + VecZnx::bytes_of(module.n(), 1, size) - + module.vec_znx_dft_bytes_of((infos.rank() + 1).into(), size) + + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) } } @@ -45,7 +45,7 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPt, + Scratch: TakeGLWEPlaintext, { fn ggsw_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 9fe8411..afd0881 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSvpPPol, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, @@ -22,21 +22,21 @@ impl GLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.vec_znx_dft_bytes_of(1, size) + module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) } pub fn encrypt_pk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - ((module.vec_znx_dft_bytes_of(1, size) + module.vec_znx_big_bytes_of(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) - + module.svp_ppol_bytes_of(1) + ((module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) + + module.bytes_of_svp_ppol(1) + module.vec_znx_normalize_tmp_bytes() } } @@ -120,7 +120,7 @@ pub trait GLWEEncryptSk { impl GLWEEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, Scratch: ScratchAvailable, { fn glwe_encrypt_sk( @@ -186,7 +186,7 @@ pub trait GLWEEncryptZeroSk { impl GLWEEncryptZeroSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, Scratch: ScratchAvailable, { fn glwe_encrypt_zero_sk( @@ -440,7 +440,7 @@ pub(crate) trait GLWEEncryptSkInternal { impl GLWEEncryptSkInternal for Module where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index d8f96c2..6312b13 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, source::Source, }; @@ -21,7 +21,7 @@ pub trait GLWEPublicKeyGenerate { impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 56c92aa..585bb12 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, + VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, @@ -20,7 +20,7 @@ impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GLWESecretPrepared::bytes_of(module, infos.rank_in()) + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) @@ -42,7 +42,7 @@ impl GLWEToLWESwitchingKey { DGlwe: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -57,7 +57,7 @@ impl GLWEToLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, + + SvpPPolBytesOf, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { #[cfg(debug_assertions)] diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 23cea9b..28eab5f 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, + VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, @@ -21,7 +21,7 @@ impl LWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { debug_assert_eq!( infos.dsize().0, @@ -59,7 +59,7 @@ impl LWESwitchingKey { DOut: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -74,7 +74,7 @@ impl LWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, + + SvpPPolBytesOf, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { #[cfg(debug_assertions)] diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index bf60c09..0f8ea56 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, + VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, @@ -18,7 +18,7 @@ impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { debug_assert_eq!( infos.rank_in(), @@ -45,7 +45,7 @@ impl LWEToGLWESwitchingKey { DGlwe: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -60,7 +60,7 @@ impl LWEToGLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, + + SvpPPolBytesOf, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, { #[cfg(debug_assertions)] diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index 7228c28..742af02 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, @@ -20,7 +20,7 @@ impl AutomorphismKey> { OUT: GGLWEInfos, IN: GGLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) } @@ -33,7 +33,7 @@ impl AutomorphismKey> { where OUT: GGLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) } @@ -47,7 +47,7 @@ impl AutomorphismKey { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -67,7 +67,7 @@ impl AutomorphismKey { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index c09898e..5877d40 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, @@ -20,7 +20,7 @@ impl GLWESwitchingKey> { OUT: GGLWEInfos, IN: GGLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::external_product_scratch_space( module, @@ -38,7 +38,7 @@ impl GLWESwitchingKey> { where OUT: GGLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) } @@ -52,7 +52,7 @@ impl GLWESwitchingKey { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -110,7 +110,7 @@ impl GLWESwitchingKey { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 8e5eccd..387fdee 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, @@ -21,7 +21,7 @@ impl GGSW> { OUT: GGSWInfos, IN: GGSWInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::external_product_scratch_space( module, @@ -39,7 +39,7 @@ impl GGSW> { where OUT: GGSWInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) } @@ -53,7 +53,7 @@ impl GGSW { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -108,7 +108,7 @@ impl GGSW { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 221d000..7c08cb6 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -1,21 +1,22 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, }; -use crate::layouts::{ - GGSWInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, +use crate::{ + ScratchTakeCore, + layouts::{ + GGSWInfos, GGSWToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, + }, }; -impl GLWE> { - #[allow(clippy::too_many_arguments)] - pub fn external_product_scratch_space( - module: &Module, +impl GLWE { + pub fn external_product_scratch_space( + module: Module, out_infos: &OUT, in_infos: &IN, apply_infos: &GGSW, @@ -24,76 +25,35 @@ impl GLWE> { OUT: GLWEInfos, IN: GLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + Module: GLWEExternalProduct, { - let in_size: usize = in_infos - .k() - .div_ceil(apply_infos.base2k()) - .div_ceil(apply_infos.dsize().into()) as usize; - let out_size: usize = out_infos.size(); - let ggsw_size: usize = apply_infos.size(); - let res_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), in_size); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( - out_size, - in_size, - in_size, // rows - (apply_infos.rank() + 1).into(), // cols in - (apply_infos.rank() + 1).into(), // cols out - ggsw_size, - ); - let normalize_big: usize = module.vec_znx_normalize_tmp_bytes(); - - if in_infos.base2k() == apply_infos.base2k() { - res_dft + a_dft + (vmp | normalize_big) - } else { - let normalize_conv: usize = VecZnx::bytes_of(module.n(), (apply_infos.rank() + 1).into(), in_size); - res_dft + ((a_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) - } + module.glwe_external_product_scratch_space(out_infos, in_infos, apply_infos) } - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - apply_infos: &GGSW, - ) -> usize + pub fn external_product(&mut self, module: &Module, lhs: &L, rhs: &R, scratch: &mut Scratch) where - OUT: GLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, - { - Self::external_product_scratch_space(module, out_infos, out_infos, apply_infos) - } -} - -impl GLWE { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GLWE, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where + L: GLWEToRef, + R: GGSWToRef, Module: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.external_product(self, lhs, rhs, scratch); + module.glwe_external_product(self, lhs, rhs, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where + pub fn external_product_inplace(&mut self, module: &Module, rhs: &R, scratch: &mut Scratch) + where + R: GGSWToRef, Module: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.external_product_inplace(self, rhs, scratch); + module.glwe_external_product_inplace(self, rhs, scratch); } } pub trait GLWEExternalProduct where - Self: VecZnxDftAllocBytes + Self: GetDegree + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -101,13 +61,49 @@ where + VmpApplyDftToDftAdd + VecZnxIdftApplyConsume + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + + VecZnxNormalize + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes, { + #[allow(clippy::too_many_arguments)] + fn glwe_external_product_scratch_space(&self, out_infos: &OUT, in_infos: &IN, apply_infos: &GGSW) -> usize + where + OUT: GLWEInfos, + IN: GLWEInfos, + GGSW: GGSWInfos, + { + let in_size: usize = in_infos + .k() + .div_ceil(apply_infos.base2k()) + .div_ceil(apply_infos.dsize().into()) as usize; + let out_size: usize = out_infos.size(); + let ggsw_size: usize = apply_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((apply_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = self.bytes_of_vec_znx_dft((apply_infos.rank() + 1).into(), in_size); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( + out_size, + in_size, + in_size, // rows + (apply_infos.rank() + 1).into(), // cols in + (apply_infos.rank() + 1).into(), // cols out + ggsw_size, + ); + let normalize_big: usize = self.vec_znx_normalize_tmp_bytes(); + + if in_infos.base2k() == apply_infos.base2k() { + res_dft + a_dft + (vmp | normalize_big) + } else { + let normalize_conv: usize = VecZnx::bytes_of(self.n().into(), (apply_infos.rank() + 1).into(), in_size); + res_dft + ((a_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) + } + } + fn glwe_external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) where R: GLWEToMut, D: GGSWCiphertextPreparedToRef, + Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); @@ -121,7 +117,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); - assert!(scratch.available() >= GLWE::external_product_inplace_scratch_space(self, res, rhs)); + assert!(scratch.available() >= self.glwe_external_product_scratch_space(res, res, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -157,7 +153,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n().into(), cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -216,6 +212,7 @@ where R: GLWEToMut, A: GLWEToRef, D: GGSWCiphertextPreparedToRef, + Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let lhs: &GLWE<&[u8]> = &lhs.to_ref(); @@ -234,7 +231,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); assert_eq!(lhs.n(), res.n()); - assert!(scratch.available() >= GLWE::external_product_scratch_space(self, res, lhs, rhs)); + assert!(scratch.available() >= self.glwe_external_product_scratch_space(res, lhs, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -242,8 +239,8 @@ where let a_size: usize = (lhs.size() * basek_in).div_ceil(basek_ggsw); - let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n(), cols, rhs.size()); // Todo optimise - let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self.n(), cols, a_size.div_ceil(dsize)); + let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), cols, rhs.size()); // Todo optimise + let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self.n().into(), cols, a_size.div_ceil(dsize)); a_dft.data_mut().fill(0); if basek_in == basek_ggsw { @@ -271,7 +268,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n().into(), cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -326,9 +323,9 @@ where } } -impl GLWEExternalProduct for Module -where - Self: VecZnxDftAllocBytes +impl GLWEExternalProduct for Module where + Self: GetDegree + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -336,7 +333,9 @@ where + VmpApplyDftToDftAdd + VecZnxIdftApplyConsume + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + + VecZnxNormalize + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes { } diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index 7e6c5ae..ad59fe9 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -1,8 +1,6 @@ -use poulpy_hal::layouts::{Backend, Scratch}; - -use crate::layouts::{GLWEToMut, GLWEToRef, prepared::GGSWCiphertextPreparedToRef}; - mod gglwe_atk; mod gglwe_ksk; mod ggsw_ct; mod glwe_ct; + +pub use glwe_ct::*; diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 07b61a4..c5a2129 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, @@ -12,7 +12,7 @@ use poulpy_hal::{ }; use crate::{ - GLWEOperations, TakeGLWECt, + GLWEOperations, TakeGLWE, layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; @@ -94,7 +94,7 @@ impl GLWEPacker { where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { pack_core_scratch_space(module, out_infos, key_infos) } @@ -119,7 +119,7 @@ impl GLWEPacker { auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -181,7 +181,7 @@ fn pack_core_scratch_space(module: &Module, out_infos: where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { combine_scratch_space(module, out_infos, key_infos) } @@ -194,7 +194,7 @@ fn pack_core( auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -272,7 +272,7 @@ fn combine_scratch_space(module: &Module, out_infos: &O where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::bytes_of(out_infos) + (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos)) @@ -287,7 +287,7 @@ fn combine( auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -310,7 +310,7 @@ fn combine( + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECt, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWE, { let log_n: usize = acc.data.n().log2(); let a: &mut GLWE> = &mut acc.data; @@ -413,7 +413,7 @@ pub fn glwe_packing( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -480,7 +480,7 @@ fn pack_internal( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 85f0db3..5158361 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -3,14 +3,14 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx}, }; use crate::{ - TakeGLWECt, + TakeGLWE, layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; @@ -38,7 +38,7 @@ impl GLWE> { OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { let trace: usize = Self::automorphism_inplace_scratch_space(module, out_infos, key_infos); if in_infos.base2k() != key_infos.base2k() { @@ -57,7 +57,7 @@ impl GLWE> { where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { Self::trace_scratch_space(module, out_infos, out_infos, key_infos) } @@ -73,7 +73,7 @@ impl GLWE { auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -101,7 +101,7 @@ impl GLWE { auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 589c391..9b6f947 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, @@ -23,7 +23,7 @@ impl AutomorphismKey> { OUT: GGLWEInfos, IN: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) } @@ -32,7 +32,7 @@ impl AutomorphismKey> { where OUT: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) } @@ -46,7 +46,7 @@ impl AutomorphismKey { rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -68,7 +68,7 @@ impl AutomorphismKey { rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -96,7 +96,7 @@ impl GLWESwitchingKey> { OUT: GGLWEInfos, IN: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) } @@ -105,7 +105,7 @@ impl GLWESwitchingKey> { where OUT: GGLWEInfos + GLWEInfos, KEY: GGLWEInfos + GLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply) } @@ -119,7 +119,7 @@ impl GLWESwitchingKey { rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -190,7 +190,7 @@ impl GLWESwitchingKey { rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index eea54a2..42aa925 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, @@ -21,7 +21,7 @@ impl GGSW> { where OUT: GGSWInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize; let size_in: usize = out_infos @@ -29,8 +29,8 @@ impl GGSW> { .div_ceil(tsk_infos.base2k()) .div_ceil(tsk_infos.dsize().into()) as usize; - let tmp_dft_i: usize = module.vec_znx_dft_bytes_of((tsk_infos.rank_out() + 1).into(), tsk_size); - let tmp_a: usize = module.vec_znx_dft_bytes_of(1, size_in); + let tmp_dft_i: usize = module.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size); + let tmp_a: usize = module.bytes_of_vec_znx_dft(1, size_in); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( tsk_size, size_in, @@ -39,7 +39,7 @@ impl GGSW> { (tsk_infos.rank_out()).into(), // Verify if rank+1 tsk_size, ); - let tmp_idft: usize = module.vec_znx_big_bytes_of(1, tsk_size); + let tmp_idft: usize = module.bytes_of_vec_znx_big(1, tsk_size); let norm: usize = module.vec_znx_normalize_tmp_bytes(); tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) @@ -58,11 +58,8 @@ impl GGSW> { IN: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { #[cfg(debug_assertions)] { @@ -75,10 +72,10 @@ impl GGSW> { let size_out: usize = out_infos.k().div_ceil(out_infos.base2k()) as usize; let res_znx: usize = VecZnx::bytes_of(module.n(), rank + 1, size_out); - let ci_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); + let ci_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, size_out); let ks: usize = GLWE::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); - let res_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); + let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, size_out); if in_infos.base2k() == tsk_infos.base2k() { res_znx + ci_dft + (ks | expand_rows | res_dft) @@ -103,11 +100,8 @@ impl GGSW> { OUT: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { GGSW::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) } @@ -124,9 +118,9 @@ impl GGSW { DataA: DataRef, DataTsk: DataRef, Module: VecZnxCopy - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftApply + VecZnxDftCopy @@ -162,7 +156,7 @@ impl GGSW { tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -171,8 +165,8 @@ impl GGSW { + VecZnxIdftApplyConsume + VecZnxBigAddSmallInplace + VecZnxBigNormalize - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes + + VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace @@ -196,7 +190,7 @@ impl GGSW { tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -205,8 +199,8 @@ impl GGSW { + VecZnxIdftApplyConsume + VecZnxBigAddSmallInplace + VecZnxBigNormalize - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes + + VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace @@ -229,9 +223,9 @@ impl GGSW { tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftApply + VecZnxDftCopy diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index f785016..1f3e10c 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, @@ -20,7 +20,7 @@ impl GLWE> { OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { let in_size: usize = in_infos .k() @@ -28,8 +28,8 @@ impl GLWE> { .div_ceil(key_apply.dsize().into()) as usize; let out_size: usize = out_infos.size(); let ksk_size: usize = key_apply.size(); - let res_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); + let res_dft: usize = module.bytes_of_vec_znx_dft((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = module.bytes_of_vec_znx_dft((key_apply.rank_in()).into(), in_size); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, @@ -37,7 +37,7 @@ impl GLWE> { (key_apply.rank_in()).into(), (key_apply.rank_out() + 1).into(), ksk_size, - ) + module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); + ) + module.bytes_of_vec_znx_dft((key_apply.rank_in()).into(), in_size); let normalize_big: usize = module.vec_znx_big_normalize_tmp_bytes(); if in_infos.base2k() == key_apply.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) @@ -56,7 +56,7 @@ impl GLWE> { where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { Self::keyswitch_scratch_space(module, out_infos, out_infos, key_apply) } @@ -73,7 +73,7 @@ impl GLWE { ) where DataLhs: DataRef, DataRhs: DataRef, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, Scratch: ScratchAvailable, { assert_eq!( @@ -121,7 +121,7 @@ impl GLWE { scratch: &Scratch, ) where DataRhs: DataRef, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, Scratch: ScratchAvailable, { assert_eq!( @@ -152,7 +152,7 @@ impl GLWE { rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -194,7 +194,7 @@ impl GLWE { rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDftTmpBytes @@ -243,7 +243,7 @@ impl GLWE { where DataRes: DataMut, DataKey: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDftTmpBytes @@ -294,7 +294,7 @@ where DataRes: DataMut, DataIn: DataRef, DataVmp: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxDftApply + VmpApplyDftToDft + VecZnxIdftApplyConsume @@ -340,7 +340,7 @@ where DataRes: DataMut, DataIn: DataRef, DataVmp: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxDftApply + VmpApplyDftToDft + VmpApplyDftToDftAdd diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index dd8c552..9a0c3bc 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -1,14 +1,14 @@ use poulpy_hal::{ api::{ ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; use crate::{ - TakeGLWECt, + TakeGLWE, layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}, }; @@ -23,7 +23,7 @@ impl LWE> { OUT: LWEInfos, IN: LWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDftTmpBytes @@ -69,7 +69,7 @@ impl LWE { ) where A: DataRef, DKs: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 29cf49b..04a51c0 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -210,7 +210,7 @@ impl AutomorphismKey where Self: SetAutomorphismGaloisElement, { - pub fn decompressed(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &Module, other: &O) where O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, Module: AutomorphismKeyDecompress, diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 4c51b6a..730336c 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -86,7 +86,7 @@ pub trait TensorKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, { - fn tensor_key_compressed_alloc( + fn alloc_tensor_key_compressed( &self, base2k: Base2K, k: TorusPrecision, @@ -102,7 +102,7 @@ where } } - fn tensor_key_compressed_alloc_from_infos(&self, infos: &A) -> TensorKeyCompressed> + fn alloc_tensor_key_compressed_from_infos(&self, infos: &A) -> TensorKeyCompressed> where A: GGLWEInfos, { @@ -111,7 +111,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" ); - self.tensor_key_compressed_alloc( + self.alloc_tensor_key_compressed( infos.base2k(), infos.k(), infos.rank(), @@ -120,16 +120,16 @@ where ) } - fn tensor_key_compressed_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) } - fn tensor_key_compressed_bytes_of_from_infos(&self, infos: &A) -> usize + fn bytes_of_tensor_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.tensor_key_compressed_bytes_of( + self.bytes_of_tensor_key_compressed( infos.base2k(), infos.k(), infos.rank(), @@ -145,14 +145,14 @@ impl TensorKeyCompressed> { A: GGLWEInfos, Module: TensorKeyCompressedAlloc, { - module.tensor_key_compressed_alloc_from_infos(infos) + module.alloc_tensor_key_compressed_from_infos(infos) } pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where Module: TensorKeyCompressedAlloc, { - module.tensor_key_compressed_alloc(base2k, k, rank, dnum, dsize) + module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize) } pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize @@ -160,7 +160,7 @@ impl TensorKeyCompressed> { A: GGLWEInfos, Module: TensorKeyCompressedAlloc, { - module.tensor_key_compressed_bytes_of_from_infos(infos) + module.bytes_of_tensor_key_compressed_from_infos(infos) } pub fn bytes_of( @@ -174,7 +174,7 @@ impl TensorKeyCompressed> { where Module: TensorKeyCompressedAlloc, { - module.tensor_key_compressed_bytes_of(base2k, k, rank, dnum, dsize) + module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index c818d84..1384ccc 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -224,6 +224,8 @@ where } } +impl GLWESwitchingKeyAlloc for Module where Self: GGLWEAlloc {} + impl GLWESwitchingKey> { pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 853419b..e9f907c 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -176,7 +176,7 @@ impl AutomorphismKeyPrepared, B> { } } -pub trait AutomorphismKeyPrepare +pub trait PrepareAutomorphismKey where Self: GLWESwitchingKeyPrepare, { @@ -197,7 +197,7 @@ where } } -impl AutomorphismKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} +impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &Module) -> usize @@ -212,7 +212,7 @@ impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) where O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - Module: AutomorphismKeyPrepare, + Module: PrepareAutomorphismKey, { module.prepare_automorphism_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 2f7f6d2..a428474 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, + api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, }; @@ -59,7 +59,7 @@ impl GGLWEInfos for GGLWEPrepared { pub trait GGLWEPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_gglwe_prepared( &self, @@ -130,7 +130,7 @@ where dsize.0, ); - self.vmp_pmat_bytes_of(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + self.bytes_of_vmp_pmat(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) } fn bytes_of_gglwe_prepared_from_infos(&self, infos: &A) -> usize @@ -149,7 +149,7 @@ where } } -impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} +impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGLWEPrepared, B> where diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 43eccc8..2bd20f3 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -95,7 +95,7 @@ where } } - fn glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> + fn alloc_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { @@ -121,7 +121,7 @@ where self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - fn glwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -146,7 +146,7 @@ where where A: GGLWEInfos, { - module.glwe_switching_key_prepared_alloc_from_infos(infos) + module.alloc_glwe_switching_key_prepared_from_infos(infos) } pub fn alloc( @@ -165,7 +165,7 @@ where where A: GGLWEInfos, { - module.glwe_switching_key_prepared_bytes_of_from_infos(infos) + module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of( diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index 8f3b6c1..fe7f218 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, + api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, }; @@ -51,7 +51,7 @@ impl GGSWInfos for GGSWPrepared { pub trait GGSWPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_ggsw_prepared( &self, @@ -117,7 +117,7 @@ where dsize.0, ); - self.vmp_pmat_bytes_of(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.bytes_of_vmp_pmat(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) } fn bytes_of_ggsw_prepared_from_infos(&self, infos: &A) -> usize @@ -135,7 +135,7 @@ where } } -impl GGSWPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes {} +impl GGSWPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGSWPrepared, B> where diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 36d1e81..b6f2a0c 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply}, + api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf}, layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, }; @@ -52,7 +52,7 @@ impl GLWEInfos for GLWEPublicKeyPrepared { pub trait GLWEPublicKeyPreparedAlloc where - Self: GetDegree + VecZnxDftAlloc + VecZnxDftAllocBytes, + Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf, { fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { @@ -71,7 +71,7 @@ where } fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - self.vec_znx_dft_bytes_of((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } fn bytes_of_glwe_public_key_prepared_from_infos(&self, infos: &A) -> usize @@ -82,7 +82,7 @@ where } } -impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftAllocBytes {} +impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf {} impl GLWEPublicKeyPrepared, B> where diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 4fc9ba3..6028698 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare}, + api::{SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare}, layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, }; @@ -47,7 +47,7 @@ impl GLWEInfos for GLWESecretPrepared { pub trait GLWESecretPreparedAlloc where - Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc, + Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc, { fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { @@ -64,7 +64,7 @@ where } fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { - self.svp_ppol_bytes_of(rank.into()) + self.bytes_of_svp_ppol(rank.into()) } fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize where @@ -75,7 +75,7 @@ where } } -impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc {} +impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc {} impl GLWESecretPrepared, B> where diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 620bf85..d4b8e85 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -57,7 +57,7 @@ pub trait GLWEToLWESwitchingKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn glwe_to_lwe_switching_key_prepared_alloc( + fn alloc_glwe_to_lwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -66,7 +66,7 @@ where ) -> GLWEToLWESwitchingKeyPrepared, B> { GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) } - fn glwe_to_lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> + fn alloc_glwe_to_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { @@ -80,14 +80,14 @@ where 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + self.alloc_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - fn glwe_to_lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + fn bytes_of_glwe_to_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -101,7 +101,7 @@ where 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - self.glwe_to_lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } } @@ -115,22 +115,22 @@ where where A: GGLWEInfos, { - module.glwe_to_lwe_switching_key_prepared_alloc_from_infos(infos) + module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) + module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(infos) + module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - module.glwe_to_lwe_switching_key_prepared_bytes_of(base2k, k, rank_in, dnum) + module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } } @@ -138,14 +138,14 @@ pub trait GLWEToLWESwitchingKeyPrepare where Self: GLWESwitchingKeyPrepare, { - fn glwe_to_lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + fn prepare_glwe_to_lwe_switching_key_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { self.prepare_glwe_switching_key_tmp_bytes(infos); } - fn glwe_to_lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWEToLWESwitchingKeyPreparedToMut, O: GLWEToLWESwitchingKeyToRef, @@ -162,7 +162,7 @@ impl GLWEToLWESwitchingKeyPrepared, B> { A: GGLWEInfos, Module: GLWEToLWESwitchingKeyPrepare, { - module.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos); + module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos); } } @@ -172,7 +172,7 @@ impl GLWEToLWESwitchingKeyPrepared { O: GLWEToLWESwitchingKeyToRef, Module: GLWEToLWESwitchingKeyPrepare, { - module.glwe_to_lwe_switching_key_prepare(self, other, scratch); + 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_ksk.rs index ffb5bf9..b5b3a35 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -65,7 +65,7 @@ where LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) } - fn lwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> + fn alloc_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> where A: GGLWEInfos, { @@ -87,11 +87,11 @@ where self.alloc_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) } - fn lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + fn bytes_of_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - fn lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize + fn bytes_of_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -110,7 +110,7 @@ where 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.dnum()) + self.bytes_of_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) } } @@ -124,7 +124,7 @@ where where A: GGLWEInfos, { - module.lwe_switching_key_prepared_alloc_from_infos(infos) + module.alloc_lwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { @@ -135,11 +135,11 @@ where where A: GGLWEInfos, { - module.lwe_switching_key_prepared_bytes_of_from_infos(infos) + module.bytes_of_lwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - module.lwe_switching_key_prepared_bytes_of(base2k, k, dnum) + module.bytes_of_lwe_switching_key_prepared(base2k, k, dnum) } } @@ -147,13 +147,13 @@ pub trait LWESwitchingKeyPrepare where Self: GLWESwitchingKeyPrepare, { - fn lwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + fn prepare_lwe_switching_key_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { self.prepare_glwe_switching_key_tmp_bytes(infos); } - fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWESwitchingKeyPreparedToMut, O: LWESwitchingKeyToRef, @@ -170,7 +170,7 @@ impl LWESwitchingKeyPrepared, B> { A: GGLWEInfos, Module: LWESwitchingKeyPrepare, { - module.lwe_switching_key_prepare_tmp_bytes(infos); + module.prepare_lwe_switching_key_tmp_bytes(infos); } } @@ -180,7 +180,7 @@ impl LWESwitchingKeyPrepared { O: LWESwitchingKeyToRef, Module: LWESwitchingKeyPrepare, { - module.lwe_switching_key_prepare(self, other, scratch); + 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_ksk.rs index b2e7383..53867de 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -58,7 +58,7 @@ pub trait LWEToGLWESwitchingKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn lwe_to_glwe_switching_key_prepared_alloc( + fn alloc_lwe_to_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -67,7 +67,7 @@ where ) -> LWEToGLWESwitchingKeyPrepared, B> { LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) } - fn lwe_to_glwe_switching_key_prepared_alloc_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> + fn alloc_lwe_to_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> where A: GGLWEInfos, { @@ -81,10 +81,10 @@ where 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - self.lwe_to_glwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + self.alloc_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - fn lwe_to_glwe_switching_key_prepared_bytes_of( + fn bytes_of_lwe_to_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -94,7 +94,7 @@ where self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - fn lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -108,7 +108,7 @@ where 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - self.lwe_to_glwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + self.bytes_of_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } } @@ -122,22 +122,22 @@ where where A: GGLWEInfos, { - module.lwe_to_glwe_switching_key_prepared_alloc_from_infos(infos) + module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) + module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, { - module.lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(infos) + module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { - module.lwe_to_glwe_switching_key_prepared_bytes_of(base2k, k, rank_out, dnum) + module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } } @@ -145,14 +145,14 @@ pub trait LWEToGLWESwitchingKeyPrepare where Self: GLWESwitchingKeyPrepare, { - fn lwe_to_glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) + fn prepare_lwe_to_glwe_switching_key_tmp_bytes(&self, infos: &A) where A: GGLWEInfos, { self.prepare_glwe_switching_key_tmp_bytes(infos); } - fn lwe_to_glwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWEToGLWESwitchingKeyPreparedToMut, O: LWEToGLWESwitchingKeyToRef, @@ -169,7 +169,7 @@ impl LWEToGLWESwitchingKeyPrepared, B> { A: GGLWEInfos, Module: LWEToGLWESwitchingKeyPrepare, { - module.lwe_to_glwe_switching_key_prepare_tmp_bytes(infos); + module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos); } } @@ -179,7 +179,7 @@ impl LWEToGLWESwitchingKeyPrepared { O: LWEToGLWESwitchingKeyToRef, Module: LWEToGLWESwitchingKeyPrepare, { - module.lwe_to_glwe_switching_key_prepare(self, other, scratch); + module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } } diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 4582ad2..ae40bf4 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, - VecZnxNormalizeTmpBytes, VecZnxSubScalarInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + VecZnxSubScalarInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, @@ -20,8 +20,8 @@ impl GGLWE { ) where DataSk: DataRef, DataWant: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 0527b14..93302e3 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalizeTmpBytes, VecZnxSubInplace, + VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, @@ -21,8 +21,8 @@ impl GGSW { ) where DataSk: DataRef, DataScalar: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -94,8 +94,8 @@ impl GGSW { ) where DataSk: DataRef, DataScalar: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index c1fcd2f..76eaa19 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, @@ -48,8 +48,8 @@ impl GLWE { ) where DataSk: DataRef, DataPt: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index a125d70..6cfc130 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -1,13 +1,13 @@ use poulpy_hal::{ - api::{TakeMatZnx, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxDft, TakeVmpPMat}, - layouts::{Backend, Scratch}, + api::{ScratchAvailable, ScratchTakeBasic}, + layouts::{Backend, Module, Scratch}, }; use crate::{ dist::Distribution, layouts::{ AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, - GLWESwitchingKey, Rank, TensorKey, + GLWESwitchingKey, GetDegree, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, @@ -15,119 +15,15 @@ use crate::{ }, }; -pub trait TakeGLWECt { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWECtSlice { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWEPt { - fn take_glwe_pt(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGGLWE { - fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEPrepared { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGSW { - fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGGSWPrepared { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGGSWPreparedSlice { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGLWESecret { - fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self); -} - -pub trait TakeGLWESecretPrepared { - fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self); -} - -pub trait TakeGLWEPk { - fn take_glwe_pk(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWEPkPrepared { - fn take_glwe_pk_prepared(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWESwitchingKey { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWESwitchingKeyPrepared { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeTensorKey { - fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWETensorKeyPrepared { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEAutomorphismKey { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEAutomorphismKeyPrepared { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -impl TakeGLWECt for Scratch +pub trait ScratchTakeCore where - Scratch: TakeVecZnx, + Self: ScratchTakeBasic + ScratchAvailable, { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, module: &Module, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size()); ( GLWE { k: infos.k(), @@ -137,12 +33,7 @@ where scratch, ) } -} -impl TakeGLWECtSlice for Scratch -where - Scratch: TakeVecZnx, -{ fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, @@ -156,12 +47,7 @@ where } (cts, scratch) } -} -impl TakeGLWEPt for Scratch -where - Scratch: TakeVecZnx, -{ fn take_glwe_pt(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) where A: GLWEInfos, @@ -176,12 +62,7 @@ where scratch, ) } -} -impl TakeGGLWE for Scratch -where - Scratch: TakeMatZnx, -{ fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, @@ -203,12 +84,7 @@ where scratch, ) } -} -impl TakeGGLWEPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, @@ -230,12 +106,7 @@ where scratch, ) } -} -impl TakeGGSW for Scratch -where - Scratch: TakeMatZnx, -{ fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, @@ -257,12 +128,7 @@ where scratch, ) } -} -impl TakeGGSWPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos, @@ -284,12 +150,7 @@ where scratch, ) } -} -impl TakeGGSWPreparedSlice for Scratch -where - Scratch: TakeGGSWPrepared, -{ fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GGSWInfos, @@ -303,12 +164,7 @@ where } (cts, scratch) } -} -impl TakeGLWEPk for Scratch -where - Scratch: TakeVecZnx, -{ fn take_glwe_pk(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) where A: GLWEInfos, @@ -324,12 +180,7 @@ where scratch, ) } -} -impl TakeGLWEPkPrepared for Scratch -where - Scratch: TakeVecZnxDft, -{ fn take_glwe_pk_prepared(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) where A: GLWEInfos, @@ -345,12 +196,7 @@ where scratch, ) } -} -impl TakeGLWESecret for Scratch -where - Scratch: TakeScalarZnx, -{ fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) { let (data, scratch) = self.take_scalar_znx(n.into(), rank.into()); ( @@ -361,12 +207,7 @@ where scratch, ) } -} -impl TakeGLWESecretPrepared for Scratch -where - Scratch: TakeSvpPPol, -{ fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) { let (data, scratch) = self.take_svp_ppol(n.into(), rank.into()); ( @@ -377,12 +218,7 @@ where scratch, ) } -} -impl TakeGLWESwitchingKey for Scratch -where - Scratch: TakeMatZnx, -{ fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, @@ -397,12 +233,7 @@ where scratch, ) } -} -impl TakeGGLWESwitchingKeyPrepared for Scratch -where - Scratch: TakeGGLWEPrepared, -{ fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, @@ -417,12 +248,7 @@ where scratch, ) } -} -impl TakeGGLWEAutomorphismKey for Scratch -where - Scratch: TakeMatZnx, -{ fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, @@ -430,12 +256,7 @@ where let (data, scratch) = self.take_glwe_switching_key(infos); (AutomorphismKey { key: data, p: 0 }, scratch) } -} -impl TakeGGLWEAutomorphismKeyPrepared for Scratch -where - Scratch: TakeGGLWESwitchingKeyPrepared, -{ fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, @@ -443,12 +264,7 @@ where let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } -} -impl TakeTensorKey for Scratch -where - Scratch: TakeMatZnx, -{ fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, @@ -478,12 +294,7 @@ where } (TensorKey { keys }, scratch) } -} -impl TakeGGLWETensorKeyPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, @@ -515,3 +326,5 @@ where (TensorKeyPrepared { keys }, scratch) } } + +impl ScratchTakeCore for Scratch where Self: ScratchTakeBasic + ScratchAvailable {} 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 b1acd01..b98bfaf 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -38,8 +38,8 @@ where + VecZnxBigNormalize + VecZnxAutomorphism + VecZnxAutomorphismInplace - + SvpPPolAllocBytes - + VecZnxDftAllocBytes + + SvpPPolBytesOf + + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VmpPMatAlloc + VmpPrepare @@ -224,7 +224,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -238,9 +238,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDftTmpBytes @@ -255,8 +255,8 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxAutomorphismInplace - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes + + VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume 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 e49fb0f..17f182f 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -27,8 +27,8 @@ use crate::{ pub fn test_ggsw_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -41,7 +41,7 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpA + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAddScalarInplace + VecZnxCopy @@ -219,8 +219,8 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -233,7 +233,7 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpA + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAddScalarInplace + VecZnxCopy 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 4d6218a..49643fe 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -26,7 +26,7 @@ use crate::{ pub fn test_glwe_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,9 +40,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VmpApplyDftToDftTmpBytes @@ -169,7 +169,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_glwe_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -183,9 +183,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VmpApplyDftToDftTmpBytes diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index d1384ee..94d922e 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, }, @@ -23,7 +23,7 @@ use crate::layouts::{ pub fn test_lwe_to_glwe(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -36,9 +36,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -142,7 +142,7 @@ where pub fn test_glwe_to_lwe(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -155,9 +155,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes 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 b08de6b..68fca13 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -26,7 +26,7 @@ use crate::{ pub fn test_gglwe_automorphisk_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,7 +40,7 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes @@ -51,7 +51,7 @@ where + VecZnxSwitchRing + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -129,7 +129,7 @@ where pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -143,7 +143,7 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes @@ -154,7 +154,7 @@ where + VecZnxSwitchRing + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy 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 95a0f7e..e3450f9 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, }, @@ -25,7 +25,7 @@ use crate::{ pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,12 +39,12 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxSwitchRing + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -117,7 +117,7 @@ where pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -131,12 +131,12 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxSwitchRing + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy 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 2aa8f1a..c419bb4 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{ @@ -79,7 +79,7 @@ where pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -93,11 +93,11 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VmpPMatAlloc 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 841ed6e..9094c33 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -1,10 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -26,8 +25,8 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -36,7 +35,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -117,8 +116,8 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -127,7 +126,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -219,8 +218,8 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -229,7 +228,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -294,7 +293,7 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -308,10 +307,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc 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 7b34e63..1f369db 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, }, @@ -25,7 +25,7 @@ use crate::{ pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,10 +39,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc @@ -145,7 +145,7 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -159,10 +159,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc 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 c4cb4aa..f10643b 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 @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -41,9 +41,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxSwitchRing @@ -209,7 +209,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -223,9 +223,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxSwitchRing 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 e7f8d85..8d62b19 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 @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, + VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, oep::{ @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_ggsw_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -41,9 +41,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace @@ -192,7 +192,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -206,9 +206,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace 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 cf1880a..08f9695 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 @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -26,7 +26,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -178,7 +178,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_glwe_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -191,9 +191,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes 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 41fb701..90c179b 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, + VmpPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -26,7 +26,7 @@ use crate::{ pub fn test_gglwe_switching_key_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -196,7 +196,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -209,9 +209,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes 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 55f1b7f..6d74811 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAlloc, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAlloc, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_ggsw_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,9 +40,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -216,7 +216,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -229,9 +229,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes 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 6654fe9..7f629b8 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -27,7 +26,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -169,7 +168,7 @@ where pub fn test_glwe_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -182,9 +181,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes 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 b20361a..b833209 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, }, @@ -22,7 +22,7 @@ use crate::layouts::{ pub fn test_lwe_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -35,9 +35,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index 005a556..ba6d250 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -2,10 +2,10 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -28,7 +28,7 @@ use crate::{ pub fn test_glwe_packing(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxAutomorphism + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallNegateInplace @@ -48,9 +48,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 73ce3cb..2f5b5a5 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, - VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, + VmpPrepare, }, layouts::{Backend, Module, ScratchOwned, ZnxView, ZnxViewMut}, oep::{ @@ -29,7 +29,7 @@ use crate::{ pub fn test_glwe_trace_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxAutomorphism + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallNegateInplace @@ -47,9 +47,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index 38901bf..c1bd869 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -1,4 +1,7 @@ -use crate::layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}; +use crate::{ + api::{SvpPPolBytesOf, VecZnxBigBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, + layouts::{Backend, MatZnx, Module, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, +}; /// Allocates a new [crate::layouts::ScratchOwned] of `size` aligned bytes. pub trait ScratchOwnedAlloc { @@ -25,76 +28,124 @@ pub trait TakeSlice { fn take_slice(&mut self, len: usize) -> (&mut [T], &mut Self); } -/// Take a slice of bytes from a [Scratch], wraps it into a [ScalarZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeScalarZnx { - fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self); -} +pub trait ScratchTakeBasic +where + Self: TakeSlice, +{ + fn take_scalar_znx(&mut self, module: &Module, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { + let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(module.n(), cols)); + ( + ScalarZnx::from_data(take_slice, module.n(), cols), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [SvpPPol] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeSvpPPol { - fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self); -} + fn take_svp_ppol(&mut self, module: &Module, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) + where + Module: SvpPPolBytesOf, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_svp_ppol(cols)); + (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnx { - fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self); -} + fn take_vec_znx(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) { + let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(module.n(), cols, size)); + ( + VecZnx::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnx] aand returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxSlice { - fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec>, &mut Self); -} + fn take_vec_znx_big(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) + where + Module: VecZnxBigBytesOf, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_big(cols, size)); + ( + VecZnxBig::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxBig] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxBig { - fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self); -} + fn take_vec_znx_dft(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) + where + Module: VecZnxDftBytesOf, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_dft(cols, size)); -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxDft] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxDft { - fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self); -} + ( + VecZnxDft::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnxDft] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxDftSlice { fn take_vec_znx_dft_slice( &mut self, + module: &Module, len: usize, - n: usize, cols: usize, size: usize, - ) -> (Vec>, &mut Self); -} + ) -> (Vec>, &mut Self) + where + Module: VecZnxDftBytesOf, + { + let mut scratch: &mut Self = self; + let mut slice: Vec> = Vec::with_capacity(len); + for _ in 0..len { + let (znx, new_scratch) = scratch.take_vec_znx_dft(module, cols, size); + scratch = new_scratch; + slice.push(znx); + } + (slice, scratch) + } + + fn take_vec_znx_slice( + &mut self, + module: &Module, + len: usize, + cols: usize, + size: usize, + ) -> (Vec>, &mut Self) { + let mut scratch: &mut Self = self; + let mut slice: Vec> = Vec::with_capacity(len); + for _ in 0..len { + let (znx, new_scratch) = scratch.take_vec_znx(module, cols, size); + scratch = new_scratch; + slice.push(znx); + } + (slice, scratch) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VmpPMat] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVmpPMat { fn take_vmp_pmat( &mut self, - n: usize, + module: &Module, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Self); -} + ) -> (VmpPMat<&mut [u8], B>, &mut Self) + where + Module: VmpPMatBytesOf, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vmp_pmat(rows, cols_in, cols_out, size)); + ( + VmpPMat::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [MatZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeMatZnx { fn take_mat_znx( &mut self, - n: usize, + module: &Module, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self); + ) -> (MatZnx<&mut [u8]>, &mut Self) { + let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(module.n(), rows, cols_in, cols_out, size)); + ( + MatZnx::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), + rem_slice, + ) + } } diff --git a/poulpy-hal/src/api/svp_ppol.rs b/poulpy-hal/src/api/svp_ppol.rs index b1733cc..6678584 100644 --- a/poulpy-hal/src/api/svp_ppol.rs +++ b/poulpy-hal/src/api/svp_ppol.rs @@ -8,8 +8,8 @@ pub trait SvpPPolAlloc { } /// Returns the size in bytes to allocate a [crate::layouts::SvpPPol]. -pub trait SvpPPolAllocBytes { - fn svp_ppol_bytes_of(&self, cols: usize) -> usize; +pub trait SvpPPolBytesOf { + fn bytes_of_svp_ppol(&self, cols: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::MatZnx]. diff --git a/poulpy-hal/src/api/vec_znx_big.rs b/poulpy-hal/src/api/vec_znx_big.rs index 52048a5..8cb5105 100644 --- a/poulpy-hal/src/api/vec_znx_big.rs +++ b/poulpy-hal/src/api/vec_znx_big.rs @@ -16,8 +16,8 @@ pub trait VecZnxBigAlloc { } /// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig]. -pub trait VecZnxBigAllocBytes { - fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize; +pub trait VecZnxBigBytesOf { + fn bytes_of_vec_znx_big(&self, cols: usize, size: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::VecZnxBig]. diff --git a/poulpy-hal/src/api/vec_znx_dft.rs b/poulpy-hal/src/api/vec_znx_dft.rs index 621860c..3a003a9 100644 --- a/poulpy-hal/src/api/vec_znx_dft.rs +++ b/poulpy-hal/src/api/vec_znx_dft.rs @@ -10,8 +10,8 @@ pub trait VecZnxDftFromBytes { fn vec_znx_dft_from_bytes(&self, cols: usize, size: usize, bytes: Vec) -> VecZnxDftOwned; } -pub trait VecZnxDftAllocBytes { - fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize; +pub trait VecZnxDftBytesOf { + fn bytes_of_vec_znx_dft(&self, cols: usize, size: usize) -> usize; } pub trait VecZnxDftApply { diff --git a/poulpy-hal/src/api/vmp_pmat.rs b/poulpy-hal/src/api/vmp_pmat.rs index be749de..de3433a 100644 --- a/poulpy-hal/src/api/vmp_pmat.rs +++ b/poulpy-hal/src/api/vmp_pmat.rs @@ -6,8 +6,8 @@ pub trait VmpPMatAlloc { fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned; } -pub trait VmpPMatAllocBytes { - fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; +pub trait VmpPMatBytesOf { + fn bytes_of_vmp_pmat(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPMatFromBytes { diff --git a/poulpy-hal/src/delegates/scratch.rs b/poulpy-hal/src/delegates/scratch.rs index ac022e3..b91afbb 100644 --- a/poulpy-hal/src/delegates/scratch.rs +++ b/poulpy-hal/src/delegates/scratch.rs @@ -1,14 +1,7 @@ use crate::{ - api::{ - ScratchAvailable, ScratchFromBytes, ScratchOwnedAlloc, ScratchOwnedBorrow, TakeMatZnx, TakeScalarZnx, TakeSlice, - TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, TakeVmpPMat, - }, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, - TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, - TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, - }, + api::{ScratchAvailable, ScratchFromBytes, ScratchOwnedAlloc, ScratchOwnedBorrow, TakeSlice}, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; impl ScratchOwnedAlloc for ScratchOwned @@ -55,104 +48,3 @@ where B::take_slice_impl(self, len) } } - -impl TakeScalarZnx for Scratch -where - B: Backend + TakeScalarZnxImpl, -{ - fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { - B::take_scalar_znx_impl(self, n, cols) - } -} - -impl TakeSvpPPol for Scratch -where - B: Backend + TakeSvpPPolImpl, -{ - fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) { - B::take_svp_ppol_impl(self, n, cols) - } -} - -impl TakeVecZnx for Scratch -where - B: Backend + TakeVecZnxImpl, -{ - fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) { - B::take_vec_znx_impl(self, n, cols, size) - } -} - -impl TakeVecZnxSlice for Scratch -where - B: Backend + TakeVecZnxSliceImpl, -{ - fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec>, &mut Self) { - B::take_vec_znx_slice_impl(self, len, n, cols, size) - } -} - -impl TakeVecZnxBig for Scratch -where - B: Backend + TakeVecZnxBigImpl, -{ - fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) { - B::take_vec_znx_big_impl(self, n, cols, size) - } -} - -impl TakeVecZnxDft for Scratch -where - B: Backend + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) { - B::take_vec_znx_dft_impl(self, n, cols, size) - } -} - -impl TakeVecZnxDftSlice for Scratch -where - B: Backend + TakeVecZnxDftSliceImpl, -{ - fn take_vec_znx_dft_slice( - &mut self, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Self) { - B::take_vec_znx_dft_slice_impl(self, len, n, cols, size) - } -} - -impl TakeVmpPMat for Scratch -where - B: Backend + TakeVmpPMatImpl, -{ - fn take_vmp_pmat( - &mut self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Self) { - B::take_vmp_pmat_impl(self, n, rows, cols_in, cols_out, size) - } -} - -impl TakeMatZnx for Scratch -where - B: Backend + TakeMatZnxImpl, -{ - fn take_mat_znx( - &mut self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self) { - B::take_mat_znx_impl(self, n, rows, cols_in, cols_out, size) - } -} diff --git a/poulpy-hal/src/delegates/svp_ppol.rs b/poulpy-hal/src/delegates/svp_ppol.rs index aaa3954..de36fd0 100644 --- a/poulpy-hal/src/delegates/svp_ppol.rs +++ b/poulpy-hal/src/delegates/svp_ppol.rs @@ -1,6 +1,6 @@ use crate::{ api::{ - SvpApplyDft, SvpApplyDftToDft, SvpApplyDftToDftAdd, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + SvpApplyDft, SvpApplyDftToDft, SvpApplyDftToDftAdd, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPPolFromBytes, SvpPrepare, }, layouts::{ @@ -30,11 +30,11 @@ where } } -impl SvpPPolAllocBytes for Module +impl SvpPPolBytesOf for Module where B: Backend + SvpPPolAllocBytesImpl, { - fn svp_ppol_bytes_of(&self, cols: usize) -> usize { + fn bytes_of_svp_ppol(&self, cols: usize) -> usize { B::svp_ppol_bytes_of_impl(self.n(), cols) } } diff --git a/poulpy-hal/src/delegates/vec_znx_big.rs b/poulpy-hal/src/delegates/vec_znx_big.rs index d5c06f5..a1cc307 100644 --- a/poulpy-hal/src/delegates/vec_znx_big.rs +++ b/poulpy-hal/src/delegates/vec_znx_big.rs @@ -1,7 +1,7 @@ use crate::{ api::{ VecZnxBigAdd, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmall, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace, VecZnxBigAutomorphismInplaceTmpBytes, + VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace, VecZnxBigAutomorphismInplaceTmpBytes, VecZnxBigBytesOf, VecZnxBigFromBytes, VecZnxBigFromSmall, VecZnxBigNegate, VecZnxBigNegateInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSub, VecZnxBigSubInplace, VecZnxBigSubNegateInplace, VecZnxBigSubSmallA, VecZnxBigSubSmallB, VecZnxBigSubSmallInplace, VecZnxBigSubSmallNegateInplace, @@ -49,11 +49,11 @@ where } } -impl VecZnxBigAllocBytes for Module +impl VecZnxBigBytesOf for Module where B: Backend + VecZnxBigAllocBytesImpl, { - fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize { + fn bytes_of_vec_znx_big(&self, cols: usize, size: usize) -> usize { B::vec_znx_big_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vec_znx_dft.rs b/poulpy-hal/src/delegates/vec_znx_dft.rs index 650e657..16a583f 100644 --- a/poulpy-hal/src/delegates/vec_znx_dft.rs +++ b/poulpy-hal/src/delegates/vec_znx_dft.rs @@ -1,8 +1,8 @@ use crate::{ api::{ - VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, - VecZnxDftFromBytes, VecZnxDftSub, VecZnxDftSubInplace, VecZnxDftSubNegateInplace, VecZnxDftZero, VecZnxIdftApply, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxIdftApplyTmpBytes, + VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxDftFromBytes, + VecZnxDftSub, VecZnxDftSubInplace, VecZnxDftSubNegateInplace, VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxIdftApplyTmpBytes, }, layouts::{ Backend, Data, Module, Scratch, VecZnxBig, VecZnxBigToMut, VecZnxDft, VecZnxDftOwned, VecZnxDftToMut, VecZnxDftToRef, @@ -24,11 +24,11 @@ where } } -impl VecZnxDftAllocBytes for Module +impl VecZnxDftBytesOf for Module where B: Backend + VecZnxDftAllocBytesImpl, { - fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize { + fn bytes_of_vec_znx_dft(&self, cols: usize, size: usize) -> usize { B::vec_znx_dft_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vmp_pmat.rs b/poulpy-hal/src/delegates/vmp_pmat.rs index 2df0d4a..2c65508 100644 --- a/poulpy-hal/src/delegates/vmp_pmat.rs +++ b/poulpy-hal/src/delegates/vmp_pmat.rs @@ -1,7 +1,7 @@ use crate::{ api::{ VmpApplyDft, VmpApplyDftTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftAddTmpBytes, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatFromBytes, VmpPrepare, VmpPrepareTmpBytes, + VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPMatBytesOf, VmpPMatFromBytes, VmpPrepare, VmpPrepareTmpBytes, }, layouts::{ Backend, MatZnxToRef, Module, Scratch, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef, VmpPMatOwned, VmpPMatToMut, @@ -23,11 +23,11 @@ where } } -impl VmpPMatAllocBytes for Module +impl VmpPMatBytesOf for Module where B: Backend + VmpPMatAllocBytesImpl, { - fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn bytes_of_vmp_pmat(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { B::vmp_pmat_bytes_of_impl(self.n(), rows, cols_in, cols_out, size) } } diff --git a/poulpy-hal/src/oep/scratch.rs b/poulpy-hal/src/oep/scratch.rs index 51a9c56..c973b04 100644 --- a/poulpy-hal/src/oep/scratch.rs +++ b/poulpy-hal/src/oep/scratch.rs @@ -1,4 +1,4 @@ -use crate::layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}; +use crate::layouts::{Backend, Scratch, ScratchOwned}; /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. @@ -39,111 +39,3 @@ pub unsafe trait ScratchAvailableImpl { pub unsafe trait TakeSliceImpl { fn take_slice_impl(scratch: &mut Scratch, len: usize) -> (&mut [T], &mut Scratch); } - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeScalarZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeScalarZnxImpl { - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeSvpPPol] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeSvpPPolImpl { - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxImpl { - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxSlice] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxSliceImpl { - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxBig] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxBigImpl { - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxDft] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxDftImpl { - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxDftSlice] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxDftSliceImpl { - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVmpPMat] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVmpPMatImpl { - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeMatZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeMatZnxImpl { - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch); -} diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index cd86fa3..9005309 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -9,10 +9,10 @@ use poulpy_core::layouts::{ use poulpy_hal::{ api::{ ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, - SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, + SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, + VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, + VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, @@ -42,7 +42,7 @@ where + VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -55,7 +55,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -70,7 +70,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -80,7 +80,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -124,7 +124,7 @@ where + VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -137,7 +137,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -152,7 +152,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -162,7 +162,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index d579db2..70bd910 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -2,8 +2,8 @@ use std::marker::PhantomData; use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; -use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared}; -use poulpy_hal::api::VecZnxBigAllocBytes; +use poulpy_core::{TakeGLWEPlaintext, layouts::prepared::GLWESecretPrepared}; +use poulpy_hal::api::VecZnxBigBytesOf; #[cfg(test)] use poulpy_hal::api::{ ScratchAvailable, TakeVecZnx, VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, @@ -12,8 +12,8 @@ use poulpy_hal::api::{ use poulpy_hal::source::Source; use poulpy_hal::{ api::{ - TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; @@ -83,7 +83,7 @@ impl FheUintBlocks { scratch: &mut Scratch, ) where S: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -96,7 +96,7 @@ impl FheUintBlocks { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPt, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, { use poulpy_core::layouts::GLWEPlaintextLayout; @@ -136,7 +136,7 @@ impl FheUintBlocks { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { @@ -175,8 +175,8 @@ impl FheUintBlocks { scratch: &mut Scratch, ) -> Vec where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -186,7 +186,7 @@ impl FheUintBlocks { + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPt + TakeVecZnxDft + TakeVecZnxBig, + Scratch: TakeGLWEPlaintext + TakeVecZnxDft + TakeVecZnxBig, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 2571896..c0cd09f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -14,8 +14,8 @@ use poulpy_hal::{ use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPrepare, }, @@ -123,7 +123,7 @@ impl FheUintBlocksPrep: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -190,8 +190,8 @@ impl FheUintBlocksPrepDebug { #[allow(dead_code)] pub(crate) fn noise(&self, module: &Module, sk: &GLWESecretPrepared, want: T) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index 5216611..a3a779a 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -1,6 +1,6 @@ use itertools::Itertools; use poulpy_core::{ - GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, + GLWEOperations, TakeGLWEPlaintext, TakeGLWESlice, glwe_packing, layouts::{ GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, @@ -11,7 +11,7 @@ use poulpy_hal::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -39,7 +39,7 @@ impl FheUintWord { Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxAddInplace + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes @@ -62,7 +62,7 @@ impl FheUintWord { + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotate, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECtSlice, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWESlice, { // Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; @@ -109,7 +109,7 @@ impl FheUintWord { scratch: &mut Scratch, ) where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -122,7 +122,7 @@ impl FheUintWord { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPt, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { @@ -167,7 +167,7 @@ impl FheUintWord { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 6eb72e7..7706237 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -1,6 +1,6 @@ use itertools::Itertools; use poulpy_core::{ - GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, + GLWEExternalProductInplace, GLWEOperations, TakeGLWESlice, layouts::{ GLWE, GLWEToMut, LWEInfos, prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, @@ -49,7 +49,7 @@ impl Circuit where Self: GetBitCircuitInfo, Module: Cmux + VecZnxCopy, - Scratch: TakeGLWECtSlice, + Scratch: TakeGLWESlice, { fn execute( &self, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index 260e8ba..fdcf5b3 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -9,7 +9,7 @@ use crate::tfhe::{ }, }; use poulpy_core::{ - TakeGGSW, TakeGLWECt, + TakeGGSW, TakeGLWE, layouts::{ GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, @@ -17,10 +17,10 @@ use poulpy_core::{ }; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPrepare, }, @@ -77,7 +77,7 @@ impl BDDKey, Vec, BRA> { Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -92,7 +92,7 @@ impl BDDKey, Vec, BRA> { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAutomorphism + VecZnxAutomorphismInplace, @@ -157,7 +157,7 @@ where BE: Backend, Module: VmpPrepare + VecZnxRotate - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -168,7 +168,7 @@ where + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx + TakeGGSW, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, { fn prepare( @@ -206,7 +206,7 @@ where BE: Backend, Module: VmpPrepare + VecZnxRotate - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -217,7 +217,7 @@ where + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx + TakeGGSW, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, { fn prepare( diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 7b469c8..428ba95 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs @@ -2,7 +2,7 @@ use std::time::Instant; use poulpy_backend::FFT64Ref; use poulpy_core::{ - TakeGGSW, TakeGLWEPt, + TakeGGSW, TakeGLWEPlaintext, layouts::{ GGSWCiphertextLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, @@ -11,11 +11,11 @@ use poulpy_core::{ use poulpy_hal::{ api::{ ModuleNew, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, - SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, + SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, @@ -51,7 +51,7 @@ where Module: ModuleNew + SvpPPolAlloc + SvpPrepare + VmpPMatAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -68,16 +68,16 @@ where Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx + TakeSlice, Module: VecZnxCopy + VecZnxNegateInplace + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd, Module: VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, Module: VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAlloc + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -85,7 +85,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index ca9415b..1c2ebff 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -1,9 +1,9 @@ use itertools::izip; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpPPolAllocBytes, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, - TakeVecZnxSlice, VecZnxAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, SvpApplyDftToDft, SvpPPolBytesOf, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, + TakeVecZnxSlice, VecZnxAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -12,7 +12,7 @@ use poulpy_hal::{ }; use poulpy_core::{ - Distribution, GLWEOperations, TakeGLWECt, + Distribution, GLWEOperations, TakeGLWE, layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWECiphertextToRef, LWEInfos}, }; @@ -31,10 +31,10 @@ pub fn cggi_blind_rotate_scratch_space( where OUT: GLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { @@ -43,10 +43,10 @@ where if block_size > 1 { let cols: usize = (brk_infos.rank() + 1).into(); let dnum: usize = brk_infos.dnum().into(); - let acc_dft: usize = module.vec_znx_dft_bytes_of(cols, dnum) * extension_factor; - let acc_big: usize = module.vec_znx_big_bytes_of(1, brk_size); - let vmp_res: usize = module.vec_znx_dft_bytes_of(cols, brk_size) * extension_factor; - let vmp_xai: usize = module.vec_znx_dft_bytes_of(1, brk_size); + let acc_dft: usize = module.bytes_of_vec_znx_dft(cols, dnum) * extension_factor; + let acc_big: usize = module.bytes_of_vec_znx_big(1, brk_size); + let vmp_res: usize = module.bytes_of_vec_znx_dft(cols, brk_size) * extension_factor; + let vmp_xai: usize = module.bytes_of_vec_znx_dft(1, brk_size); let acc_dft_add: usize = vmp_res; let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) let acc: usize = if extension_factor > 1 { @@ -67,9 +67,9 @@ where impl BlincRotationExecute for BlindRotationKeyPrepared where - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -129,9 +129,9 @@ fn execute_block_binary_extended( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -296,9 +296,9 @@ fn execute_block_binary( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -418,9 +418,9 @@ fn execute_standard( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index f0a0962..26c9721 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -1,9 +1,8 @@ use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VmpPMatAlloc, VmpPrepare, + VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, source::Source, @@ -47,7 +46,7 @@ impl BlindRotationKey, CGGI> { pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { GGSW::encrypt_sk_scratch_space(module, infos) } @@ -56,7 +55,7 @@ impl BlindRotationKey, CGGI> { impl BlindRotationKeyEncryptSk for BlindRotationKey where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -149,7 +148,7 @@ impl BlindRotationKeyCompressed, CGGI> { pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { GGSWCompressed::encrypt_sk_scratch_space(module, infos) } @@ -169,7 +168,7 @@ impl BlindRotationKeyCompressed { DataSkGLWE: DataRef, DataSkLWE: DataRef, Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index b09f975..9148df3 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftSubInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, @@ -29,9 +29,9 @@ use poulpy_core::layouts::{ pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) where - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 9e419fe..0704dff 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ ScratchAvailable, TakeMatZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, - VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -15,7 +15,7 @@ use poulpy_hal::{ }; use poulpy_core::{ - GLWEOperations, TakeGGLWE, TakeGLWECt, + GLWEOperations, TakeGGLWE, TakeGLWE, layouts::{Dsize, GGLWECiphertextLayout, GGSWInfos, GLWEInfos, LWEInfos}, }; @@ -44,7 +44,7 @@ where + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -56,7 +56,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + VecZnxNormalize, @@ -145,7 +145,7 @@ pub fn circuit_bootstrap_core( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -156,7 +156,7 @@ pub fn circuit_bootstrap_core( + VecZnxBigNormalize + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotateInplaceTmpBytes + VecZnxRotate @@ -286,7 +286,7 @@ fn post_process( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 0f1e468..1e52a76 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -7,9 +7,9 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, }, @@ -78,7 +78,7 @@ where Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -93,7 +93,7 @@ where + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAutomorphism, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index d94ac0c..c68e907 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -2,11 +2,11 @@ use std::time::Instant; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, @@ -45,7 +45,7 @@ where Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -58,7 +58,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -73,7 +73,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -83,7 +83,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -267,7 +267,7 @@ where Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -280,7 +280,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -295,7 +295,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRotateInplaceTmpBytes @@ -305,7 +305,7 @@ where + VecZnxCopy + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform From a5df85170d0bda9be5a4c9321621d351c38a3151 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 14 Oct 2025 23:45:00 +0200 Subject: [PATCH 08/60] wip --- poulpy-core/src/external_product/ggsw_ct.rs | 8 +++---- poulpy-core/src/external_product/glwe_ct.rs | 24 ++++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 387fdee..6535076 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -15,7 +15,7 @@ impl GGSW> { module: &Module, out_infos: &OUT, in_infos: &IN, - apply_infos: &GGSW, + ggsw_infos: &GGSW, ) -> usize where OUT: GGSWInfos, @@ -27,21 +27,21 @@ impl GGSW> { module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), - apply_infos, + ggsw_infos, ) } pub fn external_product_inplace_scratch_space( module: &Module, out_infos: &OUT, - apply_infos: &GGSW, + ggsw_infos: &GGSW, ) -> usize where OUT: GGSWInfos, GGSW: GGSWInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) + GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 7c08cb6..6ee5796 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -19,7 +19,7 @@ impl GLWE { module: Module, out_infos: &OUT, in_infos: &IN, - apply_infos: &GGSW, + ggsw_infos: &GGSW, ) -> usize where OUT: GLWEInfos, @@ -27,7 +27,7 @@ impl GLWE { GGSW: GGSWInfos, Module: GLWEExternalProduct, { - module.glwe_external_product_scratch_space(out_infos, in_infos, apply_infos) + module.glwe_external_product_scratch_space(out_infos, in_infos, ggsw_infos) } pub fn external_product(&mut self, module: &Module, lhs: &L, rhs: &R, scratch: &mut Scratch) @@ -67,7 +67,7 @@ where + VecZnxNormalizeTmpBytes, { #[allow(clippy::too_many_arguments)] - fn glwe_external_product_scratch_space(&self, out_infos: &OUT, in_infos: &IN, apply_infos: &GGSW) -> usize + fn glwe_external_product_scratch_space(&self, out_infos: &OUT, in_infos: &IN, ggsw_infos: &GGSW) -> usize where OUT: GLWEInfos, IN: GLWEInfos, @@ -75,26 +75,26 @@ where { let in_size: usize = in_infos .k() - .div_ceil(apply_infos.base2k()) - .div_ceil(apply_infos.dsize().into()) as usize; + .div_ceil(ggsw_infos.base2k()) + .div_ceil(ggsw_infos.dsize().into()) as usize; let out_size: usize = out_infos.size(); - let ggsw_size: usize = apply_infos.size(); - let res_dft: usize = self.bytes_of_vec_znx_dft((apply_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = self.bytes_of_vec_znx_dft((apply_infos.rank() + 1).into(), in_size); + let ggsw_size: usize = ggsw_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((ggsw_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = self.bytes_of_vec_znx_dft((ggsw_infos.rank() + 1).into(), in_size); let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, in_size, // rows - (apply_infos.rank() + 1).into(), // cols in - (apply_infos.rank() + 1).into(), // cols out + (ggsw_infos.rank() + 1).into(), // cols in + (ggsw_infos.rank() + 1).into(), // cols out ggsw_size, ); let normalize_big: usize = self.vec_znx_normalize_tmp_bytes(); - if in_infos.base2k() == apply_infos.base2k() { + if in_infos.base2k() == ggsw_infos.base2k() { res_dft + a_dft + (vmp | normalize_big) } else { - let normalize_conv: usize = VecZnx::bytes_of(self.n().into(), (apply_infos.rank() + 1).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(self.n().into(), (ggsw_infos.rank() + 1).into(), in_size); res_dft + ((a_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } From 008b800c015e52797273c5f85d26676190db4c4b Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 10:48:14 +0200 Subject: [PATCH 09/60] wip --- poulpy-core/src/automorphism/gglwe_atk.rs | 10 +- poulpy-core/src/automorphism/ggsw_ct.rs | 12 +- poulpy-core/src/automorphism/glwe_ct.rs | 24 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 13 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 11 +- poulpy-core/src/decryption/glwe_ct.rs | 6 +- .../src/encryption/compressed/gglwe_atk.rs | 3 +- .../src/encryption/compressed/gglwe_ct.rs | 3 +- .../src/encryption/compressed/gglwe_ksk.rs | 7 +- .../src/encryption/compressed/gglwe_tsk.rs | 7 +- .../src/encryption/compressed/ggsw_ct.rs | 3 +- poulpy-core/src/encryption/gglwe_atk.rs | 19 +- poulpy-core/src/encryption/gglwe_ct.rs | 10 +- poulpy-core/src/encryption/gglwe_ksk.rs | 14 +- poulpy-core/src/encryption/gglwe_tsk.rs | 18 +- poulpy-core/src/encryption/ggsw_ct.rs | 4 +- poulpy-core/src/encryption/glwe_ct.rs | 12 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 17 +- poulpy-core/src/encryption/lwe_ksk.rs | 18 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 15 +- poulpy-core/src/external_product/gglwe_atk.rs | 9 +- poulpy-core/src/external_product/gglwe_ksk.rs | 9 +- poulpy-core/src/external_product/ggsw_ct.rs | 215 +++++++++--------- poulpy-core/src/external_product/glwe_ct.rs | 113 +++++---- poulpy-core/src/glwe_packing.rs | 32 +-- poulpy-core/src/glwe_trace.rs | 11 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 12 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 15 +- poulpy-core/src/keyswitching/glwe_ct.rs | 14 +- poulpy-core/src/keyswitching/lwe_ct.rs | 17 +- .../src/layouts/compressed/gglwe_atk.rs | 22 +- .../src/layouts/compressed/gglwe_ct.rs | 24 +- .../src/layouts/compressed/gglwe_ksk.rs | 24 +- .../src/layouts/compressed/gglwe_tsk.rs | 22 +- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 22 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 20 +- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 20 +- poulpy-core/src/layouts/compressed/lwe_ct.rs | 22 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 22 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 20 +- poulpy-core/src/layouts/gglwe_atk.rs | 30 +-- poulpy-core/src/layouts/gglwe_ct.rs | 26 +-- poulpy-core/src/layouts/gglwe_ksk.rs | 20 +- poulpy-core/src/layouts/gglwe_tsk.rs | 23 +- poulpy-core/src/layouts/ggsw_ct.rs | 23 +- poulpy-core/src/layouts/glwe_ct.rs | 16 +- poulpy-core/src/layouts/glwe_pk.rs | 18 +- poulpy-core/src/layouts/glwe_pt.rs | 18 +- poulpy-core/src/layouts/glwe_sk.rs | 18 +- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 18 +- poulpy-core/src/layouts/lwe_ct.rs | 18 +- poulpy-core/src/layouts/lwe_ksk.rs | 16 +- poulpy-core/src/layouts/lwe_pt.rs | 8 +- poulpy-core/src/layouts/lwe_sk.rs | 4 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 16 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 24 +- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 41 ++-- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 47 ++-- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 39 ++-- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 43 ++-- poulpy-core/src/layouts/prepared/glwe_pk.rs | 29 +-- poulpy-core/src/layouts/prepared/glwe_sk.rs | 31 ++- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 31 +-- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 29 ++- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 23 +- poulpy-core/src/lib.rs | 2 +- poulpy-core/src/noise/gglwe_ct.rs | 6 +- poulpy-core/src/noise/ggsw_ct.rs | 14 +- poulpy-core/src/noise/glwe_ct.rs | 14 +- poulpy-core/src/scratch.rs | 143 ++++++++---- poulpy-hal/src/api/module.rs | 4 + poulpy-hal/src/api/scratch.rs | 60 ++--- .../src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs | 12 +- .../src/tfhe/bdd_arithmetic/eval.rs | 6 +- 74 files changed, 890 insertions(+), 871 deletions(-) diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index fc49c52..e822b63 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; @@ -61,7 +61,7 @@ impl AutomorphismKey { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -149,7 +149,7 @@ impl AutomorphismKey { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 41acde9..e99b3d5 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; @@ -83,7 +83,7 @@ impl GGSW { + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -151,7 +151,7 @@ impl GGSW { + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { // Keyswitch the j-th row of the col 0 (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 8cdaa91..5d30917 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, - VecZnxBigSubSmallNegateInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, VecZnxBigSubSmallNegateInplace, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; @@ -56,7 +56,7 @@ impl GLWE { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.keyswitch(module, lhs, &rhs.key, scratch); (0..(self.rank() + 1).into()).for_each(|i| { @@ -82,7 +82,7 @@ impl GLWE { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.keyswitch_inplace(module, &rhs.key, scratch); (0..(self.rank() + 1).into()).for_each(|i| { @@ -109,7 +109,7 @@ impl GLWE { + VecZnxBigAutomorphismInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -150,7 +150,7 @@ impl GLWE { + VecZnxBigAutomorphismInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -193,7 +193,7 @@ impl GLWE { + VecZnxBigSubSmallInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -235,7 +235,7 @@ impl GLWE { + VecZnxBigSubSmallInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -278,7 +278,7 @@ impl GLWE { + VecZnxBigSubSmallNegateInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -320,7 +320,7 @@ impl GLWE { + VecZnxBigSubSmallNegateInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index ef18123..c023a43 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -1,16 +1,13 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::{ - TakeGLWE, - layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}, -}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}; impl LWE> { pub fn from_glwe_scratch_space( @@ -80,7 +77,7 @@ impl LWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index de40d8f..e3ba833 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -1,16 +1,13 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::{ - TakeGLWE, - layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}, -}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}; impl GLWE> { pub fn from_lwe_scratch_space( @@ -62,7 +59,7 @@ impl GLWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 89c6591..fdc040a 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, }; @@ -33,7 +33,7 @@ impl GLWE { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 9091e74..17cd17f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -5,7 +5,6 @@ use poulpy_hal::{ }; use crate::{ - TakeGLWESecret, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, @@ -41,7 +40,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk { impl GGLWEAutomorphismKeyCompressedEncryptSk for Module where Module: GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxAutomorphism, - Scratch: TakeGLWESecret + ScratchAvailable, + Scratch: ScratchAvailable, { fn gglwe_automorphism_key_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index bd0f3b3..7757e95 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -8,7 +8,6 @@ use poulpy_hal::{ }; use crate::{ - TakeGLWEPlaintext, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, @@ -67,7 +66,7 @@ where + VecZnxDftBytesOf + VecZnxAddScalarInplace + ZnNormalizeInplace, - Scratch: TakeGLWEPlaintext + ScratchAvailable, + Scratch: ScratchAvailable, { fn gglwe_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 800a37e..2c0266a 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,13 +1,10 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing, - }, + api::{ScratchAvailable, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ - TakeGLWESecretPrepared, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, @@ -68,7 +65,7 @@ where + VecZnxDftBytesOf + VecZnxSwitchRing + SvpPrepare, - Scratch: ScratchAvailable + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch: ScratchAvailable, { fn gglwe_key_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 0c88f67..6115fdd 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,14 +1,13 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, @@ -47,7 +46,7 @@ where + VecZnxIdftApplyTmpA + VecZnxBigNormalize + SvpPrepare, - Scratch: TakeGLWESecretPrepared + TakeVecZnxDft + TakeVecZnxBig + TakeGLWESecret, + Scratch:, { fn gglwe_tensor_key_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 31efd53..8a8949b 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -5,7 +5,6 @@ use poulpy_hal::{ }; use crate::{ - TakeGLWEPlaintext, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, @@ -42,7 +41,7 @@ pub trait GGSWCompressedEncryptSk { impl GGSWCompressedEncryptSk for Module where Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPlaintext, + Scratch:, { fn ggsw_compressed_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 2fa90d4..8e0ecb7 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,19 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, }, layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, - }, +use crate::layouts::{ + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, }; impl AutomorphismKey> { @@ -27,7 +24,7 @@ impl AutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize @@ -97,7 +94,7 @@ where + VecZnxSwitchRing + SvpPPolBytesOf + VecZnxAutomorphism, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch: ScratchAvailable, { fn gglwe_automorphism_key_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index a4bff60..d99ca5f 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,14 +1,10 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, - }, + api::{ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPlaintext, encryption::glwe_ct::GLWEEncryptSk, layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, @@ -23,7 +19,7 @@ impl GGLWE> { Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + + (GLWEPlaintext::bytes_of_from_infos(module, &infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) } pub fn encrypt_pk_scratch_space(_module: &Module, _infos: &A) -> usize @@ -52,7 +48,7 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { fn gglwe_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 64d9e9e..1210aa7 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,17 +1,15 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecretPrepared, - layouts::{Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared}, +use crate::layouts::{ + Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared, }; impl GLWESwitchingKey> { @@ -61,7 +59,7 @@ impl GLWESwitchingKey { + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index c2762a0..125cdf5 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,19 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, - TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, - }, +use crate::layouts::{ + Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, }; impl TensorKey> { @@ -59,8 +56,7 @@ impl TensorKey { + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf, - Scratch: - TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared + TakeVecZnxBig, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 6def5d3..23443a0 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ }; use crate::{ - SIGMA, TakeGLWEPlaintext, + SIGMA, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, @@ -45,7 +45,7 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPlaintext, + Scratch:, { fn ggsw_encrypt_sk( &self, diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index afd0881..c5ed129 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, - TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, + VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, @@ -331,7 +331,7 @@ where + VecZnxBigAddNormal + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + Scratch:, { fn glwe_encrypt_pk_internal( &self, @@ -453,7 +453,7 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { fn glwe_encrypt_sk_internal( &self, diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 585bb12..6658480 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,19 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, - }, +use crate::layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, }; impl GLWEToLWESwitchingKey> { @@ -58,7 +55,7 @@ impl GLWEToLWESwitchingKey { + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 28eab5f..04b52aa 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,20 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, + VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, - prepared::GLWESecretPrepared, - }, +use crate::layouts::{ + Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::GLWESecretPrepared, }; impl LWESwitchingKey> { @@ -75,7 +71,7 @@ impl LWESwitchingKey { + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 0f8ea56..b50a2db 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,18 +1,15 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, -}; +use crate::layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}; impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize @@ -61,7 +58,7 @@ impl LWEToGLWESwitchingKey { + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index 742af02..0eba897 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -1,8 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; @@ -56,7 +55,7 @@ impl AutomorphismKey { + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.key.external_product(module, &lhs.key, rhs, scratch); } @@ -76,7 +75,7 @@ impl AutomorphismKey { + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.key.external_product_inplace(module, rhs, scratch); } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 5877d40..c9b7299 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -1,8 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; @@ -61,7 +60,7 @@ impl GLWESwitchingKey { + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -119,7 +118,7 @@ impl GLWESwitchingKey { + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 6535076..5257a49 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -1,47 +1,116 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, + api::ScratchAvailable, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, prepared::GGSWPrepared}; +use crate::{ + GLWEExternalProduct, ScratchTakeCore, + layouts::{ + GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, + }, +}; -impl GGSW> { - #[allow(clippy::too_many_arguments)] - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, - ) -> usize +pub trait GGSWExternalProduct +where + Self: GLWEExternalProduct, +{ + fn ggsw_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GGSWInfos, - IN: GGSWInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + B: GGSWInfos, { - GLWE::external_product_scratch_space( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - ggsw_infos, - ) + self.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) } - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - ggsw_infos: &GGSW, + fn ggsw_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGSWToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + let b: &GGSWPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank(), + a.rank(), + "res rank: {} != a rank: {}", + res.rank(), + a.rank() + ); + assert_eq!( + res.rank(), + b.rank(), + "res rank: {} != b rank: {}", + res.rank(), + b.rank() + ); + + assert!(scratch.available() >= self.ggsw_external_product_tmp_bytes(res, a, b)); + + let min_dnum: usize = res.dnum().min(a.dnum()).into(); + + for row in 0..min_dnum { + for col in 0..(res.rank() + 1).into() { + self.glwe_external_product(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } + } + + for row in min_dnum..res.dnum().into() { + for col in 0..(res.rank() + 1).into() { + res.at_mut(row, col).data.zero(); + } + } + } + + fn ggsw_external_product_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSWPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!( + res.rank(), + a.rank(), + "res rank: {} != a rank: {}", + res.rank(), + a.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..(res.rank() + 1).into() { + self.glwe_external_product_inplace(&mut res.at_mut(row, col), a, scratch); + } + } + } +} + +impl GGSWExternalProduct for Module where Self: GLWEExternalProduct {} + +impl GGSW> { + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, ) -> usize where - OUT: GGSWInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + B: GGSWInfos, + M: GGSWExternalProduct, { - GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) + module.ggsw_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } @@ -52,54 +121,7 @@ impl GGSW { lhs: &GGSW, rhs: &GGSWPrepared, scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; - - assert_eq!(lhs.n(), self.n()); - assert_eq!(rhs.n(), self.n()); - - assert_eq!( - self.rank(), - lhs.rank(), - "ggsw_out rank: {} != ggsw_in rank: {}", - self.rank(), - lhs.rank() - ); - assert_eq!( - self.rank(), - rhs.rank(), - "ggsw_in rank: {} != ggsw_apply rank: {}", - self.rank(), - rhs.rank() - ); - - assert!(scratch.available() >= GGSW::external_product_scratch_space(module, self, lhs, rhs)) - } - - let min_dnum: usize = self.dnum().min(lhs.dnum()).into(); - - (0..(self.rank() + 1).into()).for_each(|col_i| { - (0..min_dnum).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - (min_dnum..self.dnum().into()).for_each(|row_i| { - self.at_mut(row_i, col_i).data.zero(); - }); - }); + ) { } pub fn external_product_inplace( @@ -107,37 +129,6 @@ impl GGSW { module: &Module, rhs: &GGSWPrepared, scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; - - assert_eq!(rhs.n(), self.n()); - assert_eq!( - self.rank(), - rhs.rank(), - "ggsw_out rank: {} != ggsw_apply: {}", - self.rank(), - rhs.rank() - ); - } - - (0..(self.rank() + 1).into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product_inplace(module, rhs, scratch); - }); - }); + ) { } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 6ee5796..06de868 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -1,58 +1,59 @@ use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + ModuleN, ScratchTakeBasic, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, - layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, + layouts::{Backend, DataMut, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, }; use crate::{ ScratchTakeCore, layouts::{ - GGSWInfos, GGSWToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, - prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, + GGSWInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; impl GLWE { - pub fn external_product_scratch_space( - module: Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, + pub fn external_product_scratch_space( + module: Module, + res_infos: &R, + a_infos: &A, + b_infos: &B, ) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - GGSW: GGSWInfos, - Module: GLWEExternalProduct, + R: GLWEInfos, + A: GLWEInfos, + B: GGSWInfos, + Module: GLWEExternalProduct, { - module.glwe_external_product_scratch_space(out_infos, in_infos, ggsw_infos) + module.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) } - pub fn external_product(&mut self, module: &Module, lhs: &L, rhs: &R, scratch: &mut Scratch) + pub fn external_product(&mut self, module: &Module, a: &A, b: &B, scratch: &mut Scratch) where - L: GLWEToRef, - R: GGSWToRef, - Module: GLWEExternalProduct, - Scratch: ScratchTakeCore, + A: GLWEToRef, + B: GGSWPreparedToRef, + Module: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.glwe_external_product(self, lhs, rhs, scratch); + module.glwe_external_product(self, a, b, scratch); } - pub fn external_product_inplace(&mut self, module: &Module, rhs: &R, scratch: &mut Scratch) + pub fn external_product_inplace(&mut self, module: &Module, a: &A, scratch: &mut Scratch) where - R: GGSWToRef, - Module: GLWEExternalProduct, - Scratch: ScratchTakeCore, + A: GGSWPreparedToRef, + Module: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.glwe_external_product_inplace(self, rhs, scratch); + module.glwe_external_product_inplace(self, a, scratch); } } pub trait GLWEExternalProduct where - Self: GetDegree + Self: Sized + + ModuleN + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes @@ -61,52 +62,48 @@ where + VmpApplyDftToDftAdd + VecZnxIdftApplyConsume + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes, + + VecZnxNormalize, { - #[allow(clippy::too_many_arguments)] - fn glwe_external_product_scratch_space(&self, out_infos: &OUT, in_infos: &IN, ggsw_infos: &GGSW) -> usize + fn glwe_external_product_scratch_space(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - GGSW: GGSWInfos, + R: GLWEInfos, + A: GLWEInfos, + B: GGSWInfos, { - let in_size: usize = in_infos + let in_size: usize = a_infos .k() - .div_ceil(ggsw_infos.base2k()) - .div_ceil(ggsw_infos.dsize().into()) as usize; - let out_size: usize = out_infos.size(); - let ggsw_size: usize = ggsw_infos.size(); - let res_dft: usize = self.bytes_of_vec_znx_dft((ggsw_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = self.bytes_of_vec_znx_dft((ggsw_infos.rank() + 1).into(), in_size); + .div_ceil(b_infos.base2k()) + .div_ceil(b_infos.dsize().into()) as usize; + let out_size: usize = res_infos.size(); + let ggsw_size: usize = b_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank() + 1).into(), in_size); let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, - in_size, // rows - (ggsw_infos.rank() + 1).into(), // cols in - (ggsw_infos.rank() + 1).into(), // cols out + in_size, // rows + (b_infos.rank() + 1).into(), // cols in + (b_infos.rank() + 1).into(), // cols out ggsw_size, ); let normalize_big: usize = self.vec_znx_normalize_tmp_bytes(); - if in_infos.base2k() == ggsw_infos.base2k() { + if a_infos.base2k() == b_infos.base2k() { res_dft + a_dft + (vmp | normalize_big) } else { - let normalize_conv: usize = VecZnx::bytes_of(self.n().into(), (ggsw_infos.rank() + 1).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(self.n(), (b_infos.rank() + 1).into(), in_size); res_dft + ((a_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } - fn glwe_external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) + fn glwe_external_product_inplace(&self, res: &mut R, a: &D, scratch: &mut Scratch) where R: GLWEToMut, - D: GGSWCiphertextPreparedToRef, + D: GGSWPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &a.to_ref(); let basek_in: usize = res.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); @@ -124,8 +121,8 @@ where let dsize: usize = rhs.dsize().into(); let a_size: usize = (res.size() * basek_in).div_ceil(basek_ggsw); - let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(res.n().into(), cols, rhs.size()); // Todo optimise - let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(res.n().into(), cols, a_size.div_ceil(dsize)); + let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, rhs.size()); // Todo optimise + let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, a_size.div_ceil(dsize)); a_dft.data_mut().fill(0); if basek_in == basek_ggsw { @@ -153,7 +150,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n().into(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -211,7 +208,7 @@ where where R: GLWEToMut, A: GLWEToRef, - D: GGSWCiphertextPreparedToRef, + D: GGSWPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -239,8 +236,8 @@ where let a_size: usize = (lhs.size() * basek_in).div_ceil(basek_ggsw); - let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), cols, rhs.size()); // Todo optimise - let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self.n().into(), cols, a_size.div_ceil(dsize)); + let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, rhs.size()); // Todo optimise + let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, a_size.div_ceil(dsize)); a_dft.data_mut().fill(0); if basek_in == basek_ggsw { @@ -268,7 +265,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n().into(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -324,7 +321,7 @@ where } impl GLWEExternalProduct for Module where - Self: GetDegree + Self: ModuleN + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index c5a2129..05a2965 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -2,17 +2,17 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, - VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; use crate::{ - GLWEOperations, TakeGLWE, + GLWEOperations, layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; @@ -43,12 +43,12 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(infos: &A) -> Self + pub fn alloc(module: &Module, infos: &A) -> Self where A: GLWEInfos, { Self { - data: GLWE::alloc_from_infos(infos), + data: GLWE::alloc_from_infos(module, infos), value: false, control: false, } @@ -66,13 +66,13 @@ impl GLWEPacker { /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn new(infos: &A, log_batch: usize) -> Self + pub fn new(module: Module, infos: &A, log_batch: usize) -> Self where A: GLWEInfos, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); - (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos))); + (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(module, infos))); Self { accumulators, log_batch, @@ -142,7 +142,7 @@ impl GLWEPacker { + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { assert!( (self.counter as u32) < self.accumulators[0].data.n(), @@ -217,7 +217,7 @@ fn pack_core( + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { let log_n: usize = module.log_n(); @@ -274,7 +274,7 @@ where KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWE::bytes_of(out_infos) + GLWE::bytes_of_from_infos(module, out_infos) + (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos)) } @@ -310,7 +310,7 @@ fn combine( + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWE, + Scratch: ScratchAvailable, { let log_n: usize = acc.data.n().log2(); let a: &mut GLWE> = &mut acc.data; @@ -426,7 +426,7 @@ pub fn glwe_packing( + VecZnxBigSubSmallNegateInplace + VecZnxRotate + VecZnxNormalize, - Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -493,7 +493,7 @@ fn pack_internal( + VecZnxBigSubSmallNegateInplace + VecZnxRotate + VecZnxNormalize, - Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, + Scratch: ScratchAvailable, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) // We also use the identity: AUTO(a * X^t, g) = -X^t * AUTO(a, g) diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 5158361..1d7a6e6 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -2,15 +2,14 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, + VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx}, }; use crate::{ - TakeGLWE, layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; @@ -87,7 +86,7 @@ impl GLWE { + VecZnxCopy + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.copy(module, lhs); self.trace_inplace(module, start, end, auto_keys, scratch); @@ -114,7 +113,7 @@ impl GLWE { + VecZnxRshInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { let basek_ksk: Base2K = auto_keys .get(auto_keys.keys().next().unwrap()) diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 9b6f947..b1fd8f4 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, @@ -57,7 +57,7 @@ impl AutomorphismKey { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.key.keyswitch(module, &lhs.key, rhs, scratch); } @@ -79,7 +79,7 @@ impl AutomorphismKey { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.key.keyswitch_inplace(module, &rhs.key, scratch); } @@ -130,7 +130,7 @@ impl GLWESwitchingKey { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -201,7 +201,7 @@ impl GLWESwitchingKey { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 42aa925..4f89a41 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -1,9 +1,8 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, + VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, VmpPMat, ZnxInfos}, }; @@ -130,7 +129,7 @@ impl GGSW { + VecZnxBigNormalize + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -172,7 +171,7 @@ impl GGSW { + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { (0..lhs.dnum().into()).for_each(|row_i| { // Key-switch column 0, i.e. @@ -206,7 +205,7 @@ impl GGSW { + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { (0..self.dnum().into()).for_each(|row_i| { // Key-switch column 0, i.e. @@ -235,7 +234,7 @@ impl GGSW { + VecZnxBigNormalize + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { let basek_in: usize = self.base2k().into(); let basek_tsk: usize = tsk.base2k().into(); diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index 1f3e10c..ebd0243 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, @@ -163,7 +163,7 @@ impl GLWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -206,7 +206,7 @@ impl GLWE { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -254,7 +254,7 @@ impl GLWE { + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, + Scratch:, { if rhs.dsize() == 1 { return keyswitch_vmp_one_digit( @@ -300,7 +300,7 @@ where + VecZnxIdftApplyConsume + VecZnxBigAddSmallInplace + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, + Scratch:, { let cols: usize = a.cols(); @@ -347,7 +347,7 @@ where + VecZnxIdftApplyConsume + VecZnxBigAddSmallInplace + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, + Scratch:, { let cols: usize = a.cols(); let a_size: usize = (a.size() * basek_in).div_ceil(basek_ksk); diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 9a0c3bc..938c732 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -1,16 +1,13 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::{ - TakeGLWE, - layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}, -}; +use crate::layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}; impl LWE> { pub fn keyswitch_scratch_space( @@ -51,8 +48,8 @@ impl LWE> { rank: Rank(1), }; - let glwe_in: usize = GLWE::bytes_of(&glwe_in_infos); - let glwe_out: usize = GLWE::bytes_of(&glwe_out_infos); + let glwe_in: usize = GLWE::bytes_of_from_infos(module, &glwe_in_infos); + let glwe_out: usize = GLWE::bytes_of_from_infos(module, &glwe_out_infos); let ks: usize = GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); glwe_in + glwe_out + ks @@ -81,7 +78,7 @@ impl LWE { + VecZnxNormalize + VecZnxNormalizeTmpBytes + VecZnxCopy, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 04a51c0..5b675a5 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -138,31 +138,31 @@ where } impl AutomorphismKeyCompressed> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: AutomorphismKeyCompressedAlloc, + M: AutomorphismKeyCompressedAlloc, { module.alloc_automorphism_key_compressed_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: AutomorphismKeyCompressedAlloc, + M: AutomorphismKeyCompressedAlloc, { module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: AutomorphismKeyCompressedAlloc, + M: AutomorphismKeyCompressedAlloc, { module.bytes_of_automorphism_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, @@ -170,7 +170,7 @@ impl AutomorphismKeyCompressed> { dsize: Dsize, ) -> usize where - Module: AutomorphismKeyCompressedAlloc, + M: AutomorphismKeyCompressedAlloc, { module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } @@ -210,10 +210,10 @@ impl AutomorphismKey where Self: SetAutomorphismGaloisElement, { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, - Module: AutomorphismKeyDecompress, + M: AutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 51bb1fb..4accd8f 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -186,16 +186,16 @@ where impl GGLWECompressedAlloc for Module where Self: GetDegree {} impl GGLWECompressed> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GGLWECompressedAlloc, + M: GGLWECompressedAlloc, { module.alloc_gglwe_compressed_from_infos(infos) } - pub fn alloc( - module: &Module, + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -204,21 +204,21 @@ impl GGLWECompressed> { dsize: Dsize, ) -> Self where - Module: GGLWECompressedAlloc, + M: GGLWECompressedAlloc, { module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GGLWECompressedAlloc, + M: GGLWECompressedAlloc, { module.bytes_of_gglwe_compressed_from_infos(infos) } - pub fn byte_of( - module: &Module, + pub fn byte_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -226,7 +226,7 @@ impl GGLWECompressed> { dsize: Dsize, ) -> usize where - Module: GGLWECompressedAlloc, + M: GGLWECompressedAlloc, { module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) } @@ -315,10 +315,10 @@ where impl GGLWEDecompress for Module where Self: VecZnxFillUniform + VecZnxCopy {} impl GGLWE { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: GGLWECompressedToRef, - Module: GGLWEDecompress, + M: GGLWEDecompress, { module.decompress_gglwe(self, other); } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 450c5bc..53f57e2 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -135,16 +135,16 @@ where } impl GLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWESwitchingKeyCompressedAlloc, + M: GLWESwitchingKeyCompressedAlloc, { module.alloc_glwe_switching_key_compressed_from_infos(infos) } - pub fn alloc( - module: &Module, + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -153,21 +153,21 @@ impl GLWESwitchingKeyCompressed> { dsize: Dsize, ) -> Self where - Module: GLWESwitchingKeyCompressedAlloc, + M: GLWESwitchingKeyCompressedAlloc, { module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWESwitchingKeyCompressedAlloc, + M: GLWESwitchingKeyCompressedAlloc, { module.bytes_of_glwe_switching_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -175,7 +175,7 @@ impl GLWESwitchingKeyCompressed> { dsize: Dsize, ) -> usize where - Module: GLWESwitchingKeyCompressedAlloc, + M: GLWESwitchingKeyCompressedAlloc, { module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) } @@ -216,10 +216,10 @@ where impl GLWESwitchingKeyDecompress for Module where Self: GGLWEDecompress {} impl GLWESwitchingKey { - pub fn decompress(&mut self, module: Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: GLWESwitchingKeyCompressedToRef, - Module: GGLWEDecompress, + M: GLWESwitchingKeyDecompress, { module.decompress_glwe_switching_key(self, other); } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 730336c..173d065 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -140,31 +140,31 @@ where } impl TensorKeyCompressed> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: TensorKeyCompressedAlloc, + M: TensorKeyCompressedAlloc, { module.alloc_tensor_key_compressed_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: TensorKeyCompressedAlloc, + M: TensorKeyCompressedAlloc, { module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: TensorKeyCompressedAlloc, + M: TensorKeyCompressedAlloc, { module.bytes_of_tensor_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, @@ -172,7 +172,7 @@ impl TensorKeyCompressed> { dsize: Dsize, ) -> usize where - Module: TensorKeyCompressedAlloc, + M: TensorKeyCompressedAlloc, { module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize) } @@ -243,10 +243,10 @@ where impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl TensorKey { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: TensorKeyCompressedToRef, - Module: GLWESwitchingKeyDecompress, + M: TensorKeyDecompress, { module.decompress_tensor_key(self, other); } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index d417385..aba734f 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -170,31 +170,31 @@ where } impl GGSWCompressed> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGSWInfos, - Module: GGSWCompressedAlloc, + M: GGSWCompressedAlloc, { module.alloc_ggsw_compressed_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: GGSWCompressedAlloc, + M: GGSWCompressedAlloc, { module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGSWInfos, - Module: GGSWCompressedAlloc, + M: GGSWCompressedAlloc, { module.bytes_of_ggsw_compressed_key_from_infos(infos) } - pub fn bytes_of( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, @@ -202,7 +202,7 @@ impl GGSWCompressed> { dsize: Dsize, ) -> usize where - Module: GGSWCompressedAlloc, + M: GGSWCompressedAlloc, { module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) } @@ -290,10 +290,10 @@ where impl GGSWDecompress for Module where Self: GGSWDecompress {} impl GGSW { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: GGSWCompressedToRef, - Module: GGSWDecompress, + M: GGSWDecompress, { module.decompress_ggsw(self, other); } diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 1ee976a..8a5193f 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -106,32 +106,32 @@ where impl GLWECompressedAlloc for Module where Self: GetDegree {} impl GLWECompressed> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: GLWECompressedAlloc, + M: GLWECompressedAlloc, { module.alloc_glwe_compressed_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - Module: GLWECompressedAlloc, + M: GLWECompressedAlloc, { module.alloc_glwe_compressed(base2k, k, rank) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: GLWECompressedAlloc, + M: GLWECompressedAlloc, { module.bytes_of_glwe_compressed_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize where - Module: GLWECompressedAlloc, + M: GLWECompressedAlloc, { module.bytes_of_glwe_compressed(base2k, k) } @@ -196,10 +196,10 @@ where impl GLWEDecompress for Module where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} impl GLWE { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: GLWECompressedToRef + GLWEInfos, - Module: GLWEDecompress, + M: GLWEDecompress, { module.decompress_glwe(self, other); } diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 9f038a3..afdb493 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -150,32 +150,32 @@ where impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWEToLWESwitchingKeyCompressedAlloc, + M: GLWEToLWESwitchingKeyCompressedAlloc, { module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where - Module: GLWEToLWESwitchingKeyCompressedAlloc, + M: GLWEToLWESwitchingKeyCompressedAlloc, { module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWEToLWESwitchingKeyCompressedAlloc, + M: GLWEToLWESwitchingKeyCompressedAlloc, { module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) } - pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize where - Module: GLWEToLWESwitchingKeyCompressedAlloc, + M: GLWEToLWESwitchingKeyCompressedAlloc, { module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) } @@ -197,10 +197,10 @@ where impl GLWEToLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl GLWEToLWESwitchingKey { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: GLWEToLWESwitchingKeyCompressedToRef, - Module: GLWEToLWESwitchingKeyDecompress, + M: GLWEToLWESwitchingKeyDecompress, { module.decompress_glwe_to_lwe_switching_key(self, other); } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index 2696784..c1d613e 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -91,33 +91,35 @@ pub trait LWECompressedAlloc { } } +impl LWECompressedAlloc for Module{} + impl LWECompressed> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: LWEInfos, - Module: LWECompressedAlloc, + M: LWECompressedAlloc, { module.alloc_lwe_compressed_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self where - Module: LWECompressedAlloc, + M: LWECompressedAlloc, { module.alloc_lwe_compressed(base2k, k) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: LWEInfos, - Module: LWECompressedAlloc, + M: LWECompressedAlloc, { module.bytes_of_lwe_compressed_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize where - Module: LWECompressedAlloc, + M: LWECompressedAlloc, { module.bytes_of_lwe_compressed(base2k, k) } @@ -174,10 +176,10 @@ where impl LWEDecompress for Module where Self: ZnFillUniform {} impl LWE { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: LWECompressedToRef, - Module: LWEDecompress, + M: LWEDecompress, { module.decompress_lwe(self, other); } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index ba987ab..1f5a50a 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -147,33 +147,35 @@ where } } +impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc{} + impl LWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: LWESwitchingKeyCompressedAlloc, + M: LWESwitchingKeyCompressedAlloc, { module.alloc_lwe_switching_key_compressed_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where - Module: LWESwitchingKeyCompressedAlloc, + M: LWESwitchingKeyCompressedAlloc, { module.alloc_lwe_switching_key_compressed(base2k, k, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: LWESwitchingKeyCompressedAlloc, + M: LWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_switching_key_compressed_from_infos(infos) } - pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where - Module: LWESwitchingKeyCompressedAlloc, + M: LWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) } @@ -195,10 +197,10 @@ where impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl LWESwitchingKey { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: LWESwitchingKeyCompressedToRef, - Module: LWESwitchingKeyDecompress, + M: LWESwitchingKeyDecompress, { module.decompress_lwe_switching_key(self, other); } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index ce4e889..5545518 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -140,32 +140,32 @@ where } impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: LWEToGLWESwitchingKeyCompressedAlloc, + M: LWEToGLWESwitchingKeyCompressedAlloc, { module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where - Module: LWEToGLWESwitchingKeyCompressedAlloc, + M: LWEToGLWESwitchingKeyCompressedAlloc, { module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: LWEToGLWESwitchingKeyCompressedAlloc, + M: LWEToGLWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where - Module: LWEToGLWESwitchingKeyCompressedAlloc, + M: LWEToGLWESwitchingKeyCompressedAlloc, { module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) } @@ -187,10 +187,10 @@ where impl LWEToGLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl LWEToGLWESwitchingKey { - pub fn decompress(&mut self, module: &Module, other: &O) + pub fn decompress(&mut self, module: &M, other: &O) where O: LWEToGLWESwitchingKeyCompressedToRef, - Module: LWEToGLWESwitchingKeyDecompress, + M: LWEToGLWESwitchingKeyDecompress, { module.decompress_lwe_to_glwe_switching_key(self, other); } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index c197828..2c99bd2 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -188,46 +188,32 @@ where } impl AutomorphismKey> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: AutomorphismKeyAlloc, + M: AutomorphismKeyAlloc, { module.alloc_automorphism_key_from_infos(infos) } - pub fn alloc_with( - module: &Module, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: AutomorphismKeyAlloc, + M: AutomorphismKeyAlloc, { module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: AutomorphismKeyAlloc, + M: AutomorphismKeyAlloc, { module.bytes_of_automorphism_key_from_infos(infos) } - pub fn bytes_of( - module: &Module, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: AutomorphismKeyAlloc, + M: AutomorphismKeyAlloc, { module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 97508c2..9c49c43 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -296,39 +296,31 @@ where impl GGLWEAlloc for Module where Self: GetDegree {} impl GGLWE> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GGLWEAlloc, + M: GGLWEAlloc, { module.alloc_glwe_from_infos(infos) } - pub fn alloc( - module: Module, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: GGLWEAlloc, + M: GGLWEAlloc, { module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GGLWEAlloc, + M: GGLWEAlloc, { module.bytes_of_gglwe_from_infos(infos) } - pub fn bytes_of( - module: Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -337,7 +329,7 @@ impl GGLWE> { dsize: Dsize, ) -> usize where - Module: GGLWEAlloc, + M: GGLWEAlloc, { module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) } diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 1384ccc..b261230 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -227,16 +227,16 @@ where impl GLWESwitchingKeyAlloc for Module where Self: GGLWEAlloc {} impl GLWESwitchingKey> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWESwitchingKeyAlloc, + M: GLWESwitchingKeyAlloc, { module.alloc_glwe_switching_key_from_infos(infos) } - pub fn alloc( - module: &Module, + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -245,21 +245,21 @@ impl GLWESwitchingKey> { dsize: Dsize, ) -> Self where - Module: GLWESwitchingKeyAlloc, + M: GLWESwitchingKeyAlloc, { module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWESwitchingKeyAlloc, + M: GLWESwitchingKeyAlloc, { module.bytes_of_glwe_switching_key_from_infos(infos) } - pub fn bytes_of( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -268,7 +268,7 @@ impl GLWESwitchingKey> { dsize: Dsize, ) -> usize where - Module: GLWESwitchingKeyAlloc, + M: GLWESwitchingKeyAlloc, { module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 2483868..10523b1 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -188,39 +188,32 @@ where impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} impl TensorKey> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: TensorKeyAlloc, + M: TensorKeyAlloc, { module.alloc_tensor_key_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: TensorKeyAlloc, + M: TensorKeyAlloc, { module.alloc_tensor_key(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: TensorKeyAlloc, + M: TensorKeyAlloc, { module.bytes_of_tensor_key_from_infos(infos) } - pub fn bytes_of( - module: Module, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: TensorKeyAlloc, + M: TensorKeyAlloc, { module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index b427ecd..a4fffd3 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -239,39 +239,32 @@ where } impl GGSW> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGSWInfos, - Module: GGSWAlloc, + M: GGSWAlloc, { module.alloc_ggsw_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: GGSWAlloc, + M: GGSWAlloc, { module.alloc_ggsw(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGSWInfos, - Module: GGSWAlloc, + M: GGSWAlloc, { module.bytes_of_ggsw_from_infos(infos) } - pub fn bytes_of( - module: Module, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: GGSWAlloc, + M: GGSWAlloc, { module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) } diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 5bdb2cb..328b1ff 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -188,32 +188,32 @@ where impl GLWEAlloc for Module where Self: GetDegree {} impl GLWE> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: GLWEAlloc, + M: GLWEAlloc, { module.alloc_glwe_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - Module: GLWEAlloc, + M: GLWEAlloc, { module.alloc_glwe(base2k, k, rank) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: GLWEAlloc, + M: GLWEAlloc, { module.bytes_of_glwe_from_infos(infos) } - pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where - Module: GLWEAlloc, + M: GLWEAlloc, { module.bytes_of_glwe(base2k, k, rank) } diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index a370656..0b0d058 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -122,33 +122,35 @@ where } } +impl GLWEPublicKeyAlloc for Module where Self: GetDegree {} + impl GLWEPublicKey> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: GLWEPublicKeyAlloc, + M: GLWEPublicKeyAlloc, { module.alloc_glwe_public_key_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - Module: GLWEPublicKeyAlloc, + M: GLWEPublicKeyAlloc, { module.alloc_glwe_public_key(base2k, k, rank) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: GLWEPublicKeyAlloc, + M: GLWEPublicKeyAlloc, { module.bytes_of_glwe_public_key_from_infos(infos) } - pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where - Module: GLWEPublicKeyAlloc, + M: GLWEPublicKeyAlloc, { module.bytes_of_glwe_public_key(base2k, k, rank) } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index 5f4e2df..e7ae403 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -116,33 +116,35 @@ where } } +impl GLWEPlaintextAlloc for Module where Self: GetDegree {} + impl GLWEPlaintext> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: GLWEPlaintextAlloc, + M: GLWEPlaintextAlloc, { module.alloc_glwe_plaintext_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self where - Module: GLWEPlaintextAlloc, + M: GLWEPlaintextAlloc, { module.alloc_glwe_plaintext(base2k, k) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: GLWEPlaintextAlloc, + M: GLWEPlaintextAlloc, { module.bytes_of_glwe_plaintext_from_infos(infos) } - pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize where - Module: GLWEPlaintextAlloc, + M: GLWEPlaintextAlloc, { module.bytes_of_glwe_plaintext(base2k, k) } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index c5be370..65caa70 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -100,33 +100,35 @@ where } } +impl GLWESecretAlloc for Module where Self: GetDegree {} + impl GLWESecret> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: GLWESecretAlloc, + M: GLWESecretAlloc, { module.alloc_glwe_secret_from_infos(infos) } - pub fn alloc(module: Module, rank: Rank) -> Self + pub fn alloc(module: &M, rank: Rank) -> Self where - Module: GLWESecretAlloc, + M: GLWESecretAlloc, { module.alloc_glwe_secret(rank) } - pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: GLWESecretAlloc, + M: GLWESecretAlloc, { module.bytes_of_glwe_secret_from_infos(infos) } - pub fn bytes_of(module: Module, rank: Rank) -> usize + pub fn bytes_of(module: &M, rank: Rank) -> usize where - Module: GLWESecretAlloc, + M: GLWESecretAlloc, { module.bytes_of_glwe_secret(rank) } diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 4020721..76e0ebc 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -185,33 +185,35 @@ where } } +impl GLWEToLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl GLWEToLWESwitchingKey> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: GLWEToLWESwitchingKeyAlloc, + M: GLWEToLWESwitchingKeyAlloc, { module.alloc_glwe_to_lwe_switching_key_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where - Module: GLWEToLWESwitchingKeyAlloc, + M: GLWEToLWESwitchingKeyAlloc, { module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: GLWEToLWESwitchingKeyAlloc, + M: GLWEToLWESwitchingKeyAlloc, { module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where - Module: GLWEToLWESwitchingKeyAlloc, + M: GLWEToLWESwitchingKeyAlloc, { module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 26ee40b..e5b218d 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -153,33 +153,35 @@ pub trait LWEAlloc { } } +impl LWEAlloc for Module {} + impl LWE> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: LWEInfos, - Module: LWEAlloc, + M: LWEAlloc, { module.alloc_lwe_from_infos(infos) } - pub fn alloc(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self + pub fn alloc(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self where - Module: LWEAlloc, + M: LWEAlloc, { module.alloc_lwe(n, base2k, k) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: LWEInfos, - Module: LWEAlloc, + M: LWEAlloc, { module.bytes_of_lwe_from_infos(infos) } - pub fn bytes_of(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize where - Module: LWEAlloc, + M: LWEAlloc, { module.bytes_of_lwe(n, base2k, k) } diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 77a027a..e449da6 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -161,32 +161,32 @@ where impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} impl LWESwitchingKey> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: LWESwitchingKeyAlloc, + M: LWESwitchingKeyAlloc, { module.alloc_lwe_switching_key_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where - Module: LWESwitchingKeyAlloc, + M: LWESwitchingKeyAlloc, { module.alloc_lwe_switching_key(base2k, k, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: LWESwitchingKeyAlloc, + M: LWESwitchingKeyAlloc, { module.bytes_of_glwe_switching_key_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where - Module: LWESwitchingKeyAlloc, + M: LWESwitchingKeyAlloc, { module.bytes_of_lwe_switching_key(base2k, k, dnum) } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index fb23b18..01ae19b 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -72,14 +72,18 @@ pub trait LWEPlaintextAlloc { impl LWEPlaintextAlloc for Module {} impl LWEPlaintext> { - pub fn alloc_from_infos(module: Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: LWEInfos, + M: LWEPlaintextAlloc, { module.alloc_lwe_plaintext_from_infos(infos) } - pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self + where + M: LWEPlaintextAlloc, + { module.alloc_lwe_plaintext(base2k, k) } } diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index 4190a4b..b5f019f 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -25,9 +25,9 @@ pub trait LWESecretAlloc { impl LWESecretAlloc for Module {} impl LWESecret> { - pub fn alloc(module: &Module, n: Degree) -> Self + pub fn alloc(module: &M, n: Degree) -> Self where - Module: LWESecretAlloc, + M: LWESecretAlloc, { module.alloc_lwe_secret(n) } diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index e9905ae..1427447 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -188,32 +188,32 @@ where impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} impl LWEToGLWESwitchingKey> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: LWEToGLWESwitchingKeyAlloc, + M: LWEToGLWESwitchingKeyAlloc, { module.alloc_lwe_to_glwe_switching_key_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where - Module: LWEToGLWESwitchingKeyAlloc, + M: LWEToGLWESwitchingKeyAlloc, { module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: LWEToGLWESwitchingKeyAlloc, + M: LWEToGLWESwitchingKeyAlloc, { module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize where - Module: LWEToGLWESwitchingKeyAlloc, + M: LWEToGLWESwitchingKeyAlloc, { module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index e9f907c..d09b2e6 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -145,32 +145,32 @@ where impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} impl AutomorphismKeyPrepared, B> { - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAlloc, + M: AutomorphismKeyPreparedAlloc, { module.alloc_automorphism_key_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: AutomorphismKeyPreparedAlloc, + M: AutomorphismKeyPreparedAlloc, { module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: AutomorphismKeyPreparedAlloc, + M: AutomorphismKeyPreparedAlloc, { module.bytes_of_automorphism_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - Module: AutomorphismKeyPreparedAlloc, + M: AutomorphismKeyPreparedAlloc, { module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } @@ -200,19 +200,19 @@ where impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} impl AutomorphismKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - Module: GLWESwitchingKeyPrepare, + M: PrepareAutomorphismKey, { module.prepare_automorphism_key_tmp_bytes(self) } } impl AutomorphismKeyPrepared { - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - Module: PrepareAutomorphismKey, + M: PrepareAutomorphismKey, { module.prepare_automorphism_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index a428474..180393a 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -151,45 +151,50 @@ where impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} -impl GGLWEPrepared, B> -where - Module: GGLWEPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl GGLWEPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: GGLWEPreparedAlloc, { module.alloc_gglwe_prepared_from_infos(infos) } - pub fn alloc( - module: &Module, + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> Self + where + M: GGLWEPreparedAlloc, + { module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GGLWEPreparedAlloc, { module.bytes_of_gglwe_prepared_from_infos(infos) } - pub fn bytes_of( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize { + ) -> usize + where + M: GGLWEPreparedAlloc, + { module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -230,22 +235,20 @@ where impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} -impl GGLWEPrepared -where - Module: GGLWEPrepare, -{ - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) +impl GGLWEPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef, + M: GGLWEPrepare, { module.prepare_gglwe(self, other, scratch); } } impl GGLWEPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - Module: GGLWEPrepare, + M: GGLWEPrepare, { module.prepare_gglwe_tmp_bytes(self) } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 2bd20f3..d8ab0ca 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -136,47 +136,52 @@ where } } -impl GLWESwitchingKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} +impl GLWESwitchingKeyPreparedAlloc for Module where Self: GGLWEPreparedAlloc {} -impl GLWESwitchingKeyPrepared, B> -where - Module: GLWESwitchingKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl GLWESwitchingKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: GLWESwitchingKeyPreparedAlloc, { module.alloc_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc( - module: &Module, + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> Self + where + M: GLWESwitchingKeyPreparedAlloc, + { module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GLWESwitchingKeyPreparedAlloc, { module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } - pub fn bytes_of( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize { + ) -> usize + where + M: GLWESwitchingKeyPreparedAlloc, + { module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -206,21 +211,21 @@ where impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} impl GLWESwitchingKeyPrepared { - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, - Module: GLWESwitchingKeyPrepare, + M: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching(self, other, scratch); } } -impl GLWESwitchingKeyPrepared, B> -where - Module: GLWESwitchingKeyPrepare, -{ - pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { - module.prepare_gglwe_tmp_bytes(self) +impl GLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M) -> usize + where + M: GLWESwitchingKeyPrepare, + { + 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/gglwe_tsk.rs index 27b1699..8357e40 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -114,29 +114,34 @@ where impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} -impl TensorKeyPrepared, B> -where - Module: TensorKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl TensorKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: TensorKeyPreparedAlloc, { module.alloc_tensor_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + where + M: TensorKeyPreparedAlloc, + { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: TensorKeyPreparedAlloc, { module.bytes_of_tensor_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: TensorKeyPreparedAlloc, + { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } @@ -190,27 +195,23 @@ where } } -impl TensorKeyPrepare for Module where Module: GLWESwitchingKeyPrepare {} +impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} -impl TensorKeyPrepared, B> -where - Module: TensorKeyPrepare, -{ - fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize +impl TensorKeyPrepared, B> { + fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: TensorKeyPrepare, { module.prepare_tensor_key_tmp_bytes(infos) } } -impl TensorKeyPrepared -where - Module: TensorKeyPrepare, -{ - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) +impl TensorKeyPrepared { + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, + M: TensorKeyPrepare, { module.prepare_tensor_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index fe7f218..f8f85c9 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -135,31 +135,36 @@ where } } -impl GGSWPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGSWPreparedAlloc for Module where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} -impl GGSWPrepared, B> -where - Module: GGSWPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl GGSWPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGSWInfos, + M: GGSWPreparedAlloc, { module.alloc_ggsw_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + where + M: GGSWPreparedAlloc, + { module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGSWInfos, + M: GGSWPreparedAlloc, { module.bytes_of_ggsw_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize + where + M: GGSWPreparedAlloc, + { module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) } } @@ -204,25 +209,21 @@ where impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} -impl GGSWPrepared, B> -where - Module: GGSWPrepare, -{ - pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize +impl GGSWPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGSWInfos, + M: GGSWPrepare, { module.ggsw_prepare_tmp_bytes(infos) } } -impl GGSWPrepared -where - Module: GGSWPrepare, -{ - pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) +impl GGSWPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGSWToRef, + M: GGSWPrepare, { module.ggsw_prepare(self, other, scratch); } @@ -243,11 +244,11 @@ impl GGSWPreparedToMut for GGSWPrepared { } } -pub trait GGSWCiphertextPreparedToRef { +pub trait GGSWPreparedToRef { fn to_ref(&self) -> GGSWPrepared<&[u8], B>; } -impl GGSWCiphertextPreparedToRef for GGSWPrepared { +impl GGSWPreparedToRef for GGSWPrepared { fn to_ref(&self) -> GGSWPrepared<&[u8], B> { GGSWPrepared { base2k: self.base2k, diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index b6f2a0c..8aa0a59 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -84,29 +84,34 @@ where impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf {} -impl GLWEPublicKeyPrepared, B> -where - Module: GLWEPublicKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl GLWEPublicKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, + M: GLWEPublicKeyPreparedAlloc, { module.alloc_glwe_public_key_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + M: GLWEPublicKeyPreparedAlloc, + { module.alloc_glwe_public_key_prepared(base2k, k, rank) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, + M: GLWEPublicKeyPreparedAlloc, { module.bytes_of_glwe_public_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + M: GLWEPublicKeyPreparedAlloc, + { module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } } @@ -141,13 +146,11 @@ where impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} -impl GLWEPublicKeyPrepared -where - Module: GLWEPublicKeyPrepare, -{ - pub fn prepare(&mut self, module: &Module, other: &O) +impl GLWEPublicKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &O) where O: GLWEPublicKeyToRef + GetDist, + M: GLWEPublicKeyPrepare, { module.prepare_glwe_public_key(self, other); } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 6028698..9e3f2ef 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -77,29 +77,34 @@ where impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc {} -impl GLWESecretPrepared, B> -where - Module: GLWESecretPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +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: &Module, rank: Rank) -> Self { + pub fn alloc(module: &M, rank: Rank) -> Self + where + M: GLWESecretPreparedAlloc, + { module.alloc_glwe_secret_prepared(rank) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, + M: GLWESecretPreparedAlloc, { module.bytes_of_glwe_secret_from_infos(infos) } - pub fn bytes_of(module: &Module, rank: Rank) -> usize { + pub fn bytes_of(module: &M, rank: Rank) -> usize + where + M: GLWESecretPreparedAlloc, + { module.bytes_of_glwe_secret(rank) } } @@ -138,6 +143,16 @@ where impl GLWESecretPrepare for Module where Self: SvpPrepare {} +impl GLWESecretPrepared { + pub fn prepare(&mut self, module: &M, other: &O) + where + M: GLWESecretPrepare, + O: GLWESecretToRef + GetDist, + { + module.prepare_glwe_secret(self, other); + } +} + pub trait GLWESecretPreparedToRef { fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index d4b8e85..63dec0b 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -105,31 +105,36 @@ where } } -impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} +impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl GLWEToLWESwitchingKeyPrepared, B> -where - Module: GLWEToLWESwitchingKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl GLWEToLWESwitchingKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: GLWEToLWESwitchingKeyPreparedAlloc, { module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + M: GLWEToLWESwitchingKeyPreparedAlloc, + { module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GLWEToLWESwitchingKeyPreparedAlloc, { module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + where + M: GLWEToLWESwitchingKeyPreparedAlloc, + { module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } } @@ -157,20 +162,20 @@ where impl GLWEToLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - Module: GLWEToLWESwitchingKeyPrepare, + M: GLWEToLWESwitchingKeyPrepare, { module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos); } } impl GLWEToLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GLWEToLWESwitchingKeyToRef, - Module: GLWEToLWESwitchingKeyPrepare, + M: GLWEToLWESwitchingKeyPrepare, { 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_ksk.rs index b5b3a35..49914f2 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -116,29 +116,34 @@ where impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWESwitchingKeyPrepared, B> -where - Module: LWESwitchingKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl LWESwitchingKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: LWESwitchingKeyPreparedAlloc, { module.alloc_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + M: LWESwitchingKeyPreparedAlloc, + { module.alloc_lwe_switching_key_prepared(base2k, k, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: LWESwitchingKeyPreparedAlloc, { module.bytes_of_lwe_switching_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + M: LWESwitchingKeyPreparedAlloc, + { module.bytes_of_lwe_switching_key_prepared(base2k, k, dnum) } } @@ -165,20 +170,20 @@ where impl LWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl LWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - Module: LWESwitchingKeyPrepare, + M: LWESwitchingKeyPrepare, { module.prepare_lwe_switching_key_tmp_bytes(infos); } } impl LWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWESwitchingKeyToRef, - Module: LWESwitchingKeyPrepare, + M: LWESwitchingKeyPrepare, { 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_ksk.rs index 53867de..e7922dc 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -114,29 +114,28 @@ where impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWEToGLWESwitchingKeyPrepared, B> -where - Module: LWEToGLWESwitchingKeyPreparedAlloc, -{ - pub fn alloc_from_infos(module: &Module, infos: &A) -> Self +impl LWEToGLWESwitchingKeyPrepared, B>{ + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: LWEToGLWESwitchingKeyPreparedAlloc, { module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where M: LWEToGLWESwitchingKeyPreparedAlloc, { module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } - pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: LWEToGLWESwitchingKeyPreparedAlloc, { module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize where M: LWEToGLWESwitchingKeyPreparedAlloc,{ module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } } @@ -164,20 +163,20 @@ where impl LWEToGLWESwitchingKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn prepare_tmp_bytes(&self, module: &Module, infos: &A) + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - Module: LWEToGLWESwitchingKeyPrepare, + M: LWEToGLWESwitchingKeyPrepare, { module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos); } } impl LWEToGLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWEToGLWESwitchingKeyToRef, - Module: LWEToGLWESwitchingKeyPrepare, + M: LWEToGLWESwitchingKeyPrepare, { module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 70035af..b0c5a38 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -22,4 +22,4 @@ pub use encryption::SIGMA; pub use scratch::*; -pub mod tests; +// pub mod tests; diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index ae40bf4..c924192 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ VecZnxSubScalarInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; @@ -30,13 +30,13 @@ impl GGLWE { + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + VecZnxSubScalarInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 93302e3..4f4c4f4 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; @@ -36,14 +36,14 @@ impl GGSW { + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, F: Fn(usize) -> f64, { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); @@ -109,13 +109,13 @@ impl GGSW { + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index 76eaa19..3242d91 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; @@ -30,9 +30,9 @@ impl GLWE { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig, + Scratch:, { - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); self.decrypt(module, &mut pt_have, sk_prepared, scratch); module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); @@ -59,7 +59,7 @@ impl GLWE { + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 6cfc130..8a9e78e 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -1,13 +1,13 @@ use poulpy_hal::{ - api::{ScratchAvailable, ScratchTakeBasic}, - layouts::{Backend, Module, Scratch}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, + layouts::{Backend, Scratch}, }; use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, - GLWESwitchingKey, GetDegree, Rank, TensorKey, + AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, + GLWESwitchingKey, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, @@ -17,12 +17,14 @@ use crate::{ pub trait ScratchTakeCore where - Self: ScratchTakeBasic + ScratchAvailable, + Self: ScratchTakeBasic + ScratchAvailable, { - fn take_glwe_ct(&mut self, module: &Module, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, module: &M, infos: &A) -> (GLWE<&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()); ( GLWE { @@ -34,25 +36,28 @@ where ) } - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, module: &M, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let mut scratch: &mut Scratch = self; + 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(infos); + let (ct, new_scratch) = scratch.take_glwe_ct(module, infos); scratch = new_scratch; cts.push(ct); } (cts, scratch) } - fn take_glwe_pt(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) + fn take_glwe_pt(&mut self, module: &M, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), 1, infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx(module, 1, infos.size()); ( GLWEPlaintext { k: infos.k(), @@ -63,12 +68,14 @@ where ) } - fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, module: &M, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - infos.n().into(), + module, infos.dnum().0.div_ceil(infos.dsize().0) as usize, infos.rank_in().into(), (infos.rank_out() + 1).into(), @@ -85,12 +92,14 @@ where ) } - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, module: &M, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_vmp_pmat( - infos.n().into(), + module, infos.dnum().into(), infos.rank_in().into(), (infos.rank_out() + 1).into(), @@ -107,12 +116,14 @@ where ) } - fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, module: &M, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - infos.n().into(), + module, infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), @@ -129,12 +140,14 @@ where ) } - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, module: &M, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_vmp_pmat( - infos.n().into(), + module, infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), @@ -151,25 +164,33 @@ where ) } - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice( + &mut self, + module: &M, + size: usize, + infos: &A, + ) -> (Vec>, &mut Self) where A: GGSWInfos, + M: ModuleN + VmpPMatBytesOf, { - let mut scratch: &mut Scratch = self; + let mut scratch: &mut Self = self; let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { - let (ct, new_scratch) = scratch.take_ggsw_prepared(infos); + let (ct, new_scratch) = scratch.take_ggsw_prepared(module, infos); scratch = new_scratch; cts.push(ct) } (cts, scratch) } - fn take_glwe_pk(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) + fn take_glwe_pk(&mut self, module: &M, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size()); ( GLWEPublicKey { k: infos.k(), @@ -181,11 +202,13 @@ where ) } - fn take_glwe_pk_prepared(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_pk_prepared(&mut self, module: &M, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) where A: GLWEInfos, + M: ModuleN + VecZnxDftBytesOf, { - let (data, scratch) = self.take_vec_znx_dft(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx_dft(module, (infos.rank() + 1).into(), infos.size()); ( GLWEPublicKeyPrepared { k: infos.k(), @@ -197,8 +220,11 @@ where ) } - fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) { - let (data, scratch) = self.take_scalar_znx(n.into(), rank.into()); + fn take_glwe_secret(&mut self, module: &M, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) + where + M: ModuleN, + { + let (data, scratch) = self.take_scalar_znx(module, rank.into()); ( GLWESecret { data, @@ -208,8 +234,11 @@ where ) } - fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) { - let (data, scratch) = self.take_svp_ppol(n.into(), rank.into()); + fn take_glwe_secret_prepared(&mut self, module: &M, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) + where + M: ModuleN + SvpPPolBytesOf, + { + let (data, scratch) = self.take_svp_ppol(module, rank.into()); ( GLWESecretPrepared { data, @@ -219,11 +248,13 @@ where ) } - fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, module: &M, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_gglwe(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe(module, infos); ( GLWESwitchingKey { key: data, @@ -234,11 +265,17 @@ where ) } - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared( + &mut self, + module: &M, + infos: &A, + ) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { - let (data, scratch) = self.take_gglwe_prepared(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe_prepared(module, infos); ( GLWESwitchingKeyPrepared { key: data, @@ -249,26 +286,36 @@ where ) } - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, module: &M, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_glwe_switching_key(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_glwe_switching_key(module, infos); (AutomorphismKey { key: data, p: 0 }, scratch) } - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared( + &mut self, + module: &M, + infos: &A, + ) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { - let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe_switching_key_prepared(module, infos); (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } - fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, module: &M, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); assert_eq!( infos.rank_in(), infos.rank_out(), @@ -277,28 +324,30 @@ where let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; - let mut scratch: &mut Scratch = self; + let mut scratch: &mut Self = self; let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_glwe_switching_key(&ksk_infos); + let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_glwe_switching_key(&ksk_infos); + let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); scratch = s; keys.push(gglwe); } (TensorKey { keys }, scratch) } - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); assert_eq!( infos.rank_in(), infos.rank_out(), @@ -308,18 +357,18 @@ where let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; - let mut scratch: &mut Scratch = self; + let mut scratch: &mut Self = self; let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(&ksk_infos); + let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(&ksk_infos); + let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } @@ -327,4 +376,4 @@ where } } -impl ScratchTakeCore for Scratch where Self: ScratchTakeBasic + ScratchAvailable {} +impl ScratchTakeCore for Scratch where Self: ScratchTakeBasic + ScratchAvailable {} diff --git a/poulpy-hal/src/api/module.rs b/poulpy-hal/src/api/module.rs index 6e5faed..3dd6176 100644 --- a/poulpy-hal/src/api/module.rs +++ b/poulpy-hal/src/api/module.rs @@ -4,3 +4,7 @@ use crate::layouts::Backend; pub trait ModuleNew { fn new(n: u64) -> Self; } + +pub trait ModuleN { + fn n(&self) -> usize; +} diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index c1bd869..ee4a080 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -1,6 +1,6 @@ use crate::{ - api::{SvpPPolBytesOf, VecZnxBigBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, - layouts::{Backend, MatZnx, Module, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, + api::{ModuleN, SvpPPolBytesOf, VecZnxBigBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, + layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, }; /// Allocates a new [crate::layouts::ScratchOwned] of `size` aligned bytes. @@ -28,11 +28,14 @@ pub trait TakeSlice { fn take_slice(&mut self, len: usize) -> (&mut [T], &mut Self); } -pub trait ScratchTakeBasic +pub trait ScratchTakeBasic where Self: TakeSlice, { - fn take_scalar_znx(&mut self, module: &Module, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { + fn take_scalar_znx(&mut self, module: &M, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(module.n(), cols)); ( ScalarZnx::from_data(take_slice, module.n(), cols), @@ -40,15 +43,18 @@ where ) } - fn take_svp_ppol(&mut self, module: &Module, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) + fn take_svp_ppol(&mut self, module: &M, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) where - Module: SvpPPolBytesOf, + M: SvpPPolBytesOf + ModuleN, { let (take_slice, rem_slice) = self.take_slice(module.bytes_of_svp_ppol(cols)); (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice) } - fn take_vec_znx(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) { + fn take_vec_znx(&mut self, module: &M, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(module.n(), cols, size)); ( VecZnx::from_data(take_slice, module.n(), cols, size), @@ -56,9 +62,9 @@ where ) } - fn take_vec_znx_big(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) + fn take_vec_znx_big(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) where - Module: VecZnxBigBytesOf, + M: VecZnxBigBytesOf + ModuleN, { let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_big(cols, size)); ( @@ -67,9 +73,9 @@ where ) } - fn take_vec_znx_dft(&mut self, module: &Module, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) + fn take_vec_znx_dft(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) where - Module: VecZnxDftBytesOf, + M: VecZnxDftBytesOf + ModuleN, { let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_dft(cols, size)); @@ -79,15 +85,15 @@ where ) } - fn take_vec_znx_dft_slice( + fn take_vec_znx_dft_slice( &mut self, - module: &Module, + module: &M, len: usize, cols: usize, size: usize, ) -> (Vec>, &mut Self) where - Module: VecZnxDftBytesOf, + M: VecZnxDftBytesOf + ModuleN, { let mut scratch: &mut Self = self; let mut slice: Vec> = Vec::with_capacity(len); @@ -99,13 +105,10 @@ where (slice, scratch) } - fn take_vec_znx_slice( - &mut self, - module: &Module, - len: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Self) { + fn take_vec_znx_slice(&mut self, module: &M, len: usize, cols: usize, size: usize) -> (Vec>, &mut Self) + where + M: ModuleN, + { let mut scratch: &mut Self = self; let mut slice: Vec> = Vec::with_capacity(len); for _ in 0..len { @@ -116,16 +119,16 @@ where (slice, scratch) } - fn take_vmp_pmat( + fn take_vmp_pmat( &mut self, - module: &Module, + module: &M, rows: usize, cols_in: usize, cols_out: usize, size: usize, ) -> (VmpPMat<&mut [u8], B>, &mut Self) where - Module: VmpPMatBytesOf, + M: VmpPMatBytesOf + ModuleN, { let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vmp_pmat(rows, cols_in, cols_out, size)); ( @@ -134,14 +137,17 @@ where ) } - fn take_mat_znx( + fn take_mat_znx( &mut self, - module: &Module, + module: &M, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self) { + ) -> (MatZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(module.n(), rows, cols_in, cols_out, size)); ( MatZnx::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs index 938c45e..ba0d38f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs @@ -1,5 +1,5 @@ use itertools::Itertools; -use poulpy_core::layouts::prepared::GGSWCiphertextPreparedToRef; +use poulpy_core::layouts::prepared::GGSWPreparedToRef; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use crate::tfhe::bdd_arithmetic::{ @@ -60,15 +60,11 @@ pub fn eval_bdd_2w_to_1w> = a + let inputs: Vec<&dyn GGSWPreparedToRef> = a .blocks .iter() - .map(|x| x as &dyn GGSWCiphertextPreparedToRef) - .chain( - b.blocks - .iter() - .map(|x| x as &dyn GGSWCiphertextPreparedToRef), - ) + .map(|x| x as &dyn GGSWPreparedToRef) + .chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef)) .collect_vec(); // Evaluates out[i] = circuit[i](a, b) diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 7706237..245fe03 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -3,7 +3,7 @@ use poulpy_core::{ GLWEExternalProductInplace, GLWEOperations, TakeGLWESlice, layouts::{ GLWE, GLWEToMut, LWEInfos, - prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; use poulpy_hal::{ @@ -39,7 +39,7 @@ where &self, module: &Module, out: &mut [GLWE], - inputs: &[&dyn GGSWCiphertextPreparedToRef], + inputs: &[&dyn GGSWPreparedToRef], scratch: &mut Scratch, ) where O: DataMut; @@ -55,7 +55,7 @@ where &self, module: &Module, out: &mut [GLWE], - inputs: &[&dyn GGSWCiphertextPreparedToRef], + inputs: &[&dyn GGSWPreparedToRef], scratch: &mut Scratch, ) where O: DataMut, From c604676f2e2dc8370ebbcb1616147b9a0892bd6c Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 11:11:57 +0200 Subject: [PATCH 10/60] wip --- poulpy-core/src/external_product/gglwe_atk.rs | 96 +++---- poulpy-core/src/external_product/gglwe_ksk.rs | 237 +++++++++--------- poulpy-core/src/external_product/ggsw_ct.rs | 30 +-- poulpy-core/src/external_product/glwe_ct.rs | 17 +- .../src/layouts/compressed/gglwe_atk.rs | 9 +- .../src/layouts/compressed/gglwe_ct.rs | 9 +- .../src/layouts/compressed/gglwe_ksk.rs | 9 +- .../src/layouts/compressed/gglwe_tsk.rs | 9 +- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 9 +- poulpy-core/src/layouts/compressed/lwe_ct.rs | 2 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 2 +- poulpy-core/src/layouts/gglwe_ct.rs | 10 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 12 +- 13 files changed, 191 insertions(+), 260 deletions(-) diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index 0eba897..871eab8 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -1,82 +1,46 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, +use poulpy_hal::layouts::{Backend, DataMut, Scratch}; + +use crate::{ + ScratchTakeCore, + external_product::gglwe_ksk::GGLWEExternalProduct, + layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef}, }; -use crate::layouts::{AutomorphismKey, GGLWEInfos, GGSWInfos, GLWESwitchingKey, prepared::GGSWPrepared}; - impl AutomorphismKey> { - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, ) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, + M: GGLWEExternalProduct, { - GLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) - } - - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - ggsw_infos: &GGSW, - ) -> usize - where - OUT: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, - { - GLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) + module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } impl AutomorphismKey { - pub fn external_product( - &mut self, - module: &Module, - lhs: &AutomorphismKey, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: AutomorphismKeyToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - self.key.external_product(module, &lhs.key, rhs, scratch); + module.gglwe_external_product(&mut self.key.key, &a.to_ref().key.key, b, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - self.key.external_product_inplace(module, rhs, scratch); + module.gglwe_external_product_inplace(&mut self.key.key, a, scratch); } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index c9b7299..b7bd4af 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -1,143 +1,134 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; + +use crate::{ + GLWEExternalProduct, ScratchTakeCore, + layouts::{ + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGSWInfos, GLWE, GLWESwitchingKey, prepared::GGSWPrepared}; - -impl GLWESwitchingKey> { - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, - ) -> usize +pub trait GGLWEExternalProduct +where + Self: GLWEExternalProduct, +{ + fn gglwe_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, { - GLWE::external_product_scratch_space( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - ggsw_infos, - ) + self.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) } - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - ggsw_infos: &GGSW, + fn gglwe_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GGLWEToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let b: &GGSWPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank_in(), + a.rank_in(), + "res input rank_in: {} != a input rank_in: {}", + res.rank_in(), + a.rank_in() + ); + assert_eq!( + a.rank_out(), + b.rank(), + "a output rank_out: {} != b rank: {}", + a.rank_out(), + b.rank() + ); + assert_eq!( + res.rank_out(), + b.rank(), + "res output rank_out: {} != b rank: {}", + res.rank_out(), + b.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_external_product(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } + } + + for row in res.dnum().min(a.dnum()).into()..res.dnum().into() { + for col in 0..res.rank_in().into() { + res.at_mut(row, col).data_mut().zero(); + } + } + } + + fn gglwe_external_product_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGSWPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!( + res.rank_out(), + a.rank(), + "res output rank: {} != a rank: {}", + res.rank_out(), + a.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_external_product_inplace(&mut res.at_mut(row, col), a, scratch); + } + } + } +} + +impl GGLWEExternalProduct for Module where Self: GLWEExternalProduct {} + +impl GLWESwitchingKey> { + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, ) -> usize where - OUT: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, + M: GGLWEExternalProduct, { - GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) + module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } impl GLWESwitchingKey { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GLWESwitchingKey, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GLWESwitchingKeyToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - use crate::layouts::GLWEInfos; - - assert_eq!( - self.rank_in(), - lhs.rank_in(), - "ksk_out input rank: {} != ksk_in input rank: {}", - self.rank_in(), - lhs.rank_in() - ); - assert_eq!( - lhs.rank_out(), - rhs.rank(), - "ksk_in output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - assert_eq!( - self.rank_out(), - rhs.rank(), - "ksk_out output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - }); - - (self.dnum().min(lhs.dnum()).into()..self.dnum().into()).for_each(|row_i| { - (0..self.rank_in().into()).for_each(|col_j| { - self.at_mut(row_i, col_j).data.zero(); - }); - }); + module.gglwe_external_product(&mut self.key, &a.to_ref().key, b, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - use crate::layouts::GLWEInfos; - - assert_eq!( - self.rank_out(), - rhs.rank(), - "ksk_out output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product_inplace(module, rhs, scratch); - }); - }); + module.gglwe_external_product_inplace(&mut self.key, a, scratch); } } diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 5257a49..0b38ebf 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::ScratchAvailable, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, Scratch, ZnxZero}, }; use crate::{ @@ -115,20 +115,22 @@ impl GGSW> { } impl GGSW { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GGSW, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGSWExternalProduct, + A: GGSWToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_external_product(self, a, b, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWPrepared, - scratch: &mut Scratch, - ) { + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGSWExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 06de868..4abac52 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -15,35 +15,30 @@ use crate::{ }; impl GLWE { - pub fn external_product_scratch_space( - module: Module, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_scratch_space(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GLWEInfos, A: GLWEInfos, B: GGSWInfos, - Module: GLWEExternalProduct, + M: GLWEExternalProduct, { module.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) } - pub fn external_product(&mut self, module: &Module, a: &A, b: &B, scratch: &mut Scratch) + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GLWEToRef, B: GGSWPreparedToRef, - Module: GLWEExternalProduct, + M: GLWEExternalProduct, Scratch: ScratchTakeCore, { module.glwe_external_product(self, a, b, scratch); } - pub fn external_product_inplace(&mut self, module: &Module, a: &A, scratch: &mut Scratch) + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where A: GGSWPreparedToRef, - Module: GLWEExternalProduct, + M: GLWEExternalProduct, Scratch: ScratchTakeCore, { module.glwe_external_product_inplace(self, a, scratch); diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 5b675a5..629e3f3 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -161,14 +161,7 @@ impl AutomorphismKeyCompressed> { module.bytes_of_automorphism_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: AutomorphismKeyCompressedAlloc, { diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 4accd8f..f54f759 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -217,14 +217,7 @@ impl GGLWECompressed> { module.bytes_of_gglwe_compressed_from_infos(infos) } - pub fn byte_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn byte_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: GGLWECompressedAlloc, { diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 53f57e2..f59587b 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -166,14 +166,7 @@ impl GLWESwitchingKeyCompressed> { module.bytes_of_glwe_switching_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: GLWESwitchingKeyCompressedAlloc, { diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 173d065..7c20bba 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -163,14 +163,7 @@ impl TensorKeyCompressed> { module.bytes_of_tensor_key_compressed_from_infos(infos) } - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: TensorKeyCompressedAlloc, { diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index aba734f..fa1c46b 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -193,14 +193,7 @@ impl GGSWCompressed> { module.bytes_of_ggsw_compressed_key_from_infos(infos) } - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where M: GGSWCompressedAlloc, { diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index c1d613e..c21a6dd 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -91,7 +91,7 @@ pub trait LWECompressedAlloc { } } -impl LWECompressedAlloc for Module{} +impl LWECompressedAlloc for Module {} impl LWECompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 1f5a50a..7e1b0cf 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -147,7 +147,7 @@ where } } -impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc{} +impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} impl LWESwitchingKeyCompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 9c49c43..3ef1758 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -304,7 +304,15 @@ impl GGLWE> { module.alloc_glwe_from_infos(infos) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self where M: GGLWEAlloc, { diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index e7922dc..44b5af5 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -114,7 +114,7 @@ where impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} -impl LWEToGLWESwitchingKeyPrepared, B>{ +impl LWEToGLWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, @@ -123,7 +123,10 @@ impl LWEToGLWESwitchingKeyPrepared, B>{ module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where M: LWEToGLWESwitchingKeyPreparedAlloc, { + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + M: LWEToGLWESwitchingKeyPreparedAlloc, + { module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } @@ -135,7 +138,10 @@ impl LWEToGLWESwitchingKeyPrepared, B>{ module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize where M: LWEToGLWESwitchingKeyPreparedAlloc,{ + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize + where + M: LWEToGLWESwitchingKeyPreparedAlloc, + { module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } } From 9f64f691cae9d4352f8a34cafea50c6dd608440d Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 11:13:03 +0200 Subject: [PATCH 11/60] wip --- poulpy-core/src/external_product/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index ad59fe9..52d24ba 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -4,3 +4,5 @@ mod ggsw_ct; mod glwe_ct; pub use glwe_ct::*; +pub use gglwe_ksk::*; +pub use ggsw_ct::*; \ No newline at end of file From 58bee4805e254675e2d3275c9b627ee7c0fc8171 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 11:18:52 +0200 Subject: [PATCH 12/60] wip --- poulpy-core/src/external_product/glwe_ct.rs | 4 +++- poulpy-core/src/external_product/mod.rs | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 4abac52..4735ff9 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -14,7 +14,7 @@ use crate::{ }, }; -impl GLWE { +impl GLWE> { pub fn external_product_scratch_space(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GLWEInfos, @@ -24,7 +24,9 @@ impl GLWE { { module.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) } +} +impl GLWE { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GLWEToRef, diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index 52d24ba..532406b 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -3,6 +3,6 @@ mod gglwe_ksk; mod ggsw_ct; mod glwe_ct; -pub use glwe_ct::*; pub use gglwe_ksk::*; -pub use ggsw_ct::*; \ No newline at end of file +pub use ggsw_ct::*; +pub use glwe_ct::*; From 15ee02e21ddaaad90b0ec488f5ad2ac3bc41c264 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 15:39:52 +0200 Subject: [PATCH 13/60] wip --- poulpy-core/README.md | 4 +- .../benches/external_product_glwe_fft64.rs | 12 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 12 +- poulpy-core/examples/encryption.rs | 2 +- poulpy-core/src/automorphism/gglwe_atk.rs | 8 +- poulpy-core/src/automorphism/ggsw_ct.rs | 12 +- poulpy-core/src/automorphism/glwe_ct.rs | 8 +- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 279 ++++++++ poulpy-core/src/conversion/glwe_to_lwe.rs | 4 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 4 +- poulpy-core/src/conversion/mod.rs | 3 + poulpy-core/src/decryption/glwe_ct.rs | 2 +- .../src/encryption/compressed/gglwe_atk.rs | 10 +- .../src/encryption/compressed/gglwe_ct.rs | 10 +- .../src/encryption/compressed/gglwe_ksk.rs | 10 +- .../src/encryption/compressed/gglwe_tsk.rs | 4 +- .../src/encryption/compressed/ggsw_ct.rs | 4 +- .../src/encryption/compressed/glwe_ct.rs | 4 +- poulpy-core/src/encryption/gglwe_atk.rs | 14 +- poulpy-core/src/encryption/gglwe_ct.rs | 12 +- poulpy-core/src/encryption/gglwe_ksk.rs | 14 +- poulpy-core/src/encryption/gglwe_tsk.rs | 4 +- poulpy-core/src/encryption/ggsw_ct.rs | 4 +- poulpy-core/src/encryption/glwe_ct.rs | 16 +- poulpy-core/src/encryption/glwe_pk.rs | 2 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 4 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 5 +- poulpy-core/src/external_product/gglwe_ksk.rs | 2 +- poulpy-core/src/external_product/ggsw_ct.rs | 2 +- poulpy-core/src/external_product/glwe_ct.rs | 10 +- poulpy-core/src/glwe_packing.rs | 14 +- poulpy-core/src/glwe_trace.rs | 13 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 16 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 438 +++--------- poulpy-core/src/keyswitching/glwe_ct.rs | 658 ++++++++---------- poulpy-core/src/keyswitching/lwe_ct.rs | 6 +- poulpy-core/src/lib.rs | 1 + poulpy-core/src/noise/gglwe_ct.rs | 2 +- poulpy-core/src/noise/ggsw_ct.rs | 4 +- poulpy-core/src/noise/glwe_ct.rs | 2 +- poulpy-core/src/operations/glwe.rs | 8 +- .../test_suite/automorphism/gglwe_atk.rs | 12 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 16 +- .../tests/test_suite/automorphism/glwe_ct.rs | 16 +- .../src/tests/test_suite/conversion.rs | 12 +- .../tests/test_suite/encryption/gglwe_atk.rs | 4 +- .../tests/test_suite/encryption/gglwe_ct.rs | 4 +- .../tests/test_suite/encryption/ggsw_ct.rs | 4 +- .../tests/test_suite/encryption/glwe_ct.rs | 12 +- .../tests/test_suite/encryption/glwe_tsk.rs | 4 +- .../test_suite/external_product/gglwe_ksk.rs | 12 +- .../test_suite/external_product/ggsw_ct.rs | 12 +- .../test_suite/external_product/glwe_ct.rs | 12 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 14 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 16 +- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 12 +- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 4 +- poulpy-core/src/tests/test_suite/packing.rs | 6 +- poulpy-core/src/tests/test_suite/trace.rs | 8 +- poulpy-hal/src/layouts/vec_znx.rs | 2 +- poulpy-hal/src/layouts/zn.rs | 2 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 4 +- .../src/tfhe/blind_rotation/cggi_key.rs | 8 +- .../tests/generic_blind_rotation.rs | 6 +- 65 files changed, 927 insertions(+), 922 deletions(-) create mode 100644 poulpy-core/src/conversion/gglwe_to_ggsw.rs diff --git a/poulpy-core/README.md b/poulpy-core/README.md index 07d5304..259988e 100644 --- a/poulpy-core/README.md +++ b/poulpy-core/README.md @@ -52,8 +52,8 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, n, base2k, ct.k()) - | GLWECiphertext::decrypt_scratch_space(&module, n, base2k, ct.k()), + GLWECiphertext::encrypt_sk_tmp_bytes(&module, n, base2k, ct.k()) + | GLWECiphertext::decrypt_tmp_bytes(&module, n, base2k, ct.k()), ); // Generate secret-key diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 333d68f..4af5d1f 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -67,9 +67,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWE::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), + GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_in_layout) + | GLWE::external_product_tmp_bytes(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); let mut source_xs = Source::new([0u8; 32]); @@ -167,9 +167,9 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWE::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), + GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) + | GLWE::external_product_inplace_tmp_bytes(&module, &glwe_layout, &ggsw_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index a1b5ea2..736b53c 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -67,9 +67,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) - | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWE::keyswitch_scratch_space( + GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_in_layout) + | GLWE::keyswitch_tmp_bytes( &module, &glwe_out_layout, &glwe_in_layout, @@ -178,9 +178,9 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) - | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWE::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), + GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) + | GLWE::keyswitch_inplace_tmp_bytes(&module, &glwe_layout, &gglwe_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index f59d5a1..d4b17b9 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -54,7 +54,7 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_scratch_space(&module, &glwe_ct_infos) | GLWE::decrypt_scratch_space(&module, &glwe_ct_infos), + GLWE::encrypt_sk_tmp_bytes(&module, &glwe_ct_infos) | GLWE::decrypt_tmp_bytes(&module, &glwe_ct_infos), ); // Generate secret-key diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index e822b63..9650aa2 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWE, prepared::AutomorphismKeyPrepared}; impl AutomorphismKey> { - pub fn automorphism_scratch_space( + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -22,7 +22,7 @@ impl AutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWE::keyswitch_scratch_space( + GLWE::keyswitch_tmp_bytes( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), @@ -30,13 +30,13 @@ impl AutomorphismKey> { ) } - pub fn automorphism_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GGLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - AutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) + AutomorphismKey::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos) } } diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index e99b3d5..a3cef86 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -14,7 +14,7 @@ use crate::layouts::{ }; impl GGSW> { - pub fn automorphism_scratch_space( + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -31,17 +31,17 @@ impl GGSW> { { let out_size: usize = out_infos.size(); let ci_dft: usize = module.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), out_size); - let ks_internal: usize = GLWE::keyswitch_scratch_space( + let ks_internal: usize = GLWE::keyswitch_tmp_bytes( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), key_infos, ); - let expand: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand: usize = GGSW::expand_row_tmp_bytes(module, out_infos, tsk_infos); ci_dft + (ks_internal | expand) } - pub fn automorphism_inplace_scratch_space( + pub fn automorphism_inplace_tmp_bytes( module: &Module, out_infos: &OUT, key_infos: &KEY, @@ -54,7 +54,7 @@ impl GGSW> { Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSW::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) + GGSW::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos, tsk_infos) } } @@ -115,7 +115,7 @@ impl GGSW { self.rank(), tensor_key.rank_out() ); - assert!(scratch.available() >= GGSW::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) + assert!(scratch.available() >= GGSW::automorphism_tmp_bytes(module, self, lhs, auto_key, tensor_key)) }; // Keyswitch the j-th row of the col 0 diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 5d30917..0c8b581 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -11,7 +11,7 @@ use poulpy_hal::{ use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; impl GLWE> { - pub fn automorphism_scratch_space( + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -23,16 +23,16 @@ impl GLWE> { KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) + Self::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) } - pub fn automorphism_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + Self::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) } } diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs new file mode 100644 index 0000000..a7b86fa --- /dev/null +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -0,0 +1,279 @@ +use poulpy_hal::{ + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAddInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, + VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + }, + layouts::{Backend, DataMut, Module, Scratch, VmpPMat, ZnxInfos}, +}; + +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, + prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + }, + operations::GLWEOperations, +}; + +impl GGLWE> { + pub fn from_gglw_tmp_bytes(module: &M, res_infos: &R, tsk_infos: &A) -> usize + where + M: GGSWFromGGLWE, + R: GGSWInfos, + A: GGLWEInfos, + { + module.ggsw_from_gglwe_tmp_bytes(res_infos, tsk_infos) + } +} + +impl GGSW { + pub fn from_gglwe(&mut self, module: &M, gglwe: &G, tsk: &T, scratch: &mut Scratch) + where + M: GGSWFromGGLWE, + G: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_from_gglwe(self, gglwe, tsk, scratch); + } +} + +impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + VecZnxCopy {} + +pub trait GGSWFromGGLWE +where + Self: GGSWExpandRows + VecZnxCopy, +{ + fn ggsw_from_gglwe_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos, + { + self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos) + } + + fn ggsw_from_gglwe(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + assert_eq!(res.rank(), a.rank_out()); + assert_eq!(res.dnum(), a.dnum()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(tsk.n(), self.n() as u32); + + for row in 0..res.dnum().into() { + res.at_mut(row, 0).copy(self, &a.at(row, 0)); + } + + self.ggsw_expand_row(res, tsk, scratch); + } +} + +impl GGSWExpandRows for Module where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VecZnxDftCopy + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftAddInplace + + VecZnxBigNormalize + + VecZnxIdftApplyTmpA + + VecZnxNormalize +{ +} + +pub(crate) trait GGSWExpandRows +where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VecZnxDftCopy + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftAddInplace + + VecZnxBigNormalize + + VecZnxIdftApplyTmpA + + VecZnxNormalize, +{ + fn ggsw_expand_rows_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos, + { + let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize; + let size_in: usize = res_infos + .k() + .div_ceil(tsk_infos.base2k()) + .div_ceil(tsk_infos.dsize().into()) as usize; + + let tmp_dft_i: usize = self.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size); + let tmp_a: usize = self.bytes_of_vec_znx_dft(1, size_in); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( + tsk_size, + size_in, + size_in, + (tsk_infos.rank_in()).into(), // Verify if rank+1 + (tsk_infos.rank_out()).into(), // Verify if rank+1 + tsk_size, + ); + let tmp_idft: usize = self.bytes_of_vec_znx_big(1, tsk_size); + let norm: usize = self.vec_znx_normalize_tmp_bytes(); + + tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) + } + + fn ggsw_expand_row(&self, res: &mut R, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + let basek_in: usize = res.base2k().into(); + let basek_tsk: usize = tsk.base2k().into(); + + assert!(scratch.available() >= self.ggsw_expand_rows_tmp_bytes(res, tsk)); + + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + + let a_size: usize = (res.size() * basek_in).div_ceil(basek_tsk); + + // Keyswitch the j-th row of the col 0 + for row_i in 0..res.dnum().into() { + let a = &res.at(row_i, 0).data; + + // Pre-compute DFT of (a0, a1, a2) + let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, a_size); + + if basek_in == basek_tsk { + for i in 0..cols { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i); + } + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, a_size); + for i in 0..cols { + self.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2); + self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0); + } + } + + for col_j in 1..cols { + // Example for rank 3: + // + // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is + // actually composed of that many dnum and we focus on a specific row here + // implicitely given ci_dft. + // + // # Input + // + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) + // col 1: (0, 0, 0, 0) + // col 2: (0, 0, 0, 0) + // col 3: (0, 0, 0, 0) + // + // # Output + // + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) + // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 ) + // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 ) + // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i]) + + let dsize: usize = tsk.dsize().into(); + + let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, tsk.size()); + let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, ci_dft.size().div_ceil(dsize)); + + { + // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2 + // + // # Example for col=1 + // + // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2) + // + + // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2) + // + + // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2) + // = + // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) + for col_i in 1..cols { + let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) + + // Extracts a[i] and multipies with Enc(s[i]s[j]) + for di in 0..dsize { + tmp_a.set_size((ci_dft.size() + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize); + + self.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i); + if di == 0 && col_i == 1 { + self.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3); + } else { + self.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3); + } + } + } + } + + // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i + // + // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2) + // + + // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0) + // = + // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2) + // = + // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2) + self.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0); + let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(self, 1, tsk.size()); + for i in 0..cols { + self.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i); + self.vec_znx_big_normalize( + basek_in, + &mut res.at_mut(row_i, col_j).data, + i, + basek_tsk, + &tmp_idft, + 0, + scratch_3, + ); + } + } + } + } +} diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index c023a43..b6c6ed1 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}; impl LWE> { - pub fn from_glwe_scratch_space( + pub fn from_glwe_tmp_bytes( module: &Module, lwe_infos: &OUT, glwe_infos: &IN, @@ -34,7 +34,7 @@ impl LWE> { lwe_infos.base2k(), lwe_infos.k(), 1u32.into(), - ) + GLWE::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) + ) + GLWE::keyswitch_tmp_bytes(module, &glwe_layout, glwe_infos, key_infos) } } diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index e3ba833..c4a3b88 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}; impl GLWE> { - pub fn from_lwe_scratch_space( + pub fn from_lwe_tmp_bytes( module: &Module, glwe_infos: &OUT, lwe_infos: &IN, @@ -28,7 +28,7 @@ impl GLWE> { lwe_infos.k().max(glwe_infos.k()), 1u32.into(), ); - let ks: usize = GLWE::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); + let ks: usize = GLWE::keyswitch_inplace_tmp_bytes(module, glwe_infos, key_infos); if lwe_infos.base2k() == key_infos.base2k() { ct + ks } else { diff --git a/poulpy-core/src/conversion/mod.rs b/poulpy-core/src/conversion/mod.rs index 090208b..9771531 100644 --- a/poulpy-core/src/conversion/mod.rs +++ b/poulpy-core/src/conversion/mod.rs @@ -1,2 +1,5 @@ +mod gglwe_to_ggsw; mod glwe_to_lwe; mod lwe_to_glwe; + +pub use gglwe_to_ggsw::*; diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index fdc040a..4306d33 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GLWE> { - pub fn decrypt_scratch_space(module: &Module, infos: &A) -> usize + pub fn decrypt_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 17cd17f..f0afcae 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -13,13 +13,13 @@ use crate::{ }; impl AutomorphismKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) } } @@ -63,10 +63,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", + scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", scratch.available(), - AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) + AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) ) } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 7757e95..b67dc88 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -34,12 +34,12 @@ impl GGLWECompressed { } impl GGLWECompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - GGLWE::encrypt_sk_scratch_space(module, infos) + GGLWE::encrypt_sk_tmp_bytes(module, infos) } } @@ -106,10 +106,10 @@ where assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECompressed::encrypt_sk_scratch_space(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GGLWECompressed::encrypt_sk_scratch_space(self, res) + GGLWECompressed::encrypt_sk_tmp_bytes(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 2c0266a..5c2c8c2 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -14,12 +14,12 @@ use crate::{ }; impl GLWESwitchingKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { - (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + GLWESecretPrepared::bytes_of(module, infos.rank_out()) } @@ -91,10 +91,10 @@ where assert!(sk_in.n().0 <= self.n() as u32); assert!(sk_out.n().0 <= self.n() as u32); assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", + scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", scratch.available(), - GLWESwitchingKey::encrypt_sk_scratch_space(self, res) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) ) } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 6115fdd..2beaa4b 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -16,12 +16,12 @@ use crate::{ }; impl TensorKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { - TensorKey::encrypt_sk_scratch_space(module, infos) + TensorKey::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 8a8949b..567f04f 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -14,12 +14,12 @@ use crate::{ }; impl GGSWCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSW::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 1c8efe6..f04a07a 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -14,12 +14,12 @@ use crate::{ }; impl GLWECompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GLWE::encrypt_sk_scratch_space(module, infos) + GLWE::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 8e0ecb7..6536c7e 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -14,7 +14,7 @@ use crate::layouts::{ }; impl AutomorphismKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, @@ -24,10 +24,10 @@ impl AutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { @@ -36,7 +36,7 @@ impl AutomorphismKey> { _infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) + GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) } } @@ -119,10 +119,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= AutomorphismKey::encrypt_sk_scratch_space(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", + scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), - AutomorphismKey::encrypt_sk_scratch_space(self, res) + AutomorphismKey::encrypt_sk_tmp_bytes(self, res) ) } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index d99ca5f..d333892 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -13,16 +13,16 @@ use crate::{ }; impl GGLWE> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) + (GLWEPlaintext::bytes_of_from_infos(module, &infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) } - pub fn encrypt_pk_scratch_space(_module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(_module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { @@ -88,12 +88,12 @@ where assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWE::encrypt_sk_scratch_space(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}", + scratch.available() >= GGLWE::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", scratch.available(), res.rank_out(), res.size(), - GGLWE::encrypt_sk_scratch_space(self, res) + GGLWE::encrypt_sk_tmp_bytes(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 1210aa7..8c3a70f 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -13,21 +13,21 @@ use crate::layouts::{ }; impl GLWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { - GGLWE::encrypt_pk_scratch_space(module, _infos) + GGLWE::encrypt_pk_tmp_bytes(module, _infos) } } @@ -66,10 +66,10 @@ impl GLWESwitchingKey { assert!(sk_in.n().0 <= module.n() as u32); assert!(sk_out.n().0 <= module.n() as u32); assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(module, self), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", + scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", scratch.available(), - GLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) ) } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 125cdf5..672808b 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -14,7 +14,7 @@ use crate::layouts::{ }; impl TensorKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, @@ -24,7 +24,7 @@ impl TensorKey> { + module.bytes_of_vec_znx_big(1, 1) + module.bytes_of_vec_znx_dft(1, 1) + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) - + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 23443a0..b044ae3 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -14,13 +14,13 @@ use crate::{ }; impl GGSW> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size = infos.size(); - GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index c5ed129..16bbadf 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -19,7 +19,7 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, @@ -28,7 +28,7 @@ impl GLWE> { assert_eq!(module.n() as u32, infos.n()); module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) } - pub fn encrypt_pk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, @@ -147,10 +147,10 @@ where assert_eq!(sk.n(), self.n() as u32); assert_eq!(pt.n(), self.n() as u32); assert!( - scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GLWE::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_tmp_bytes(self, &res) ) } @@ -209,10 +209,10 @@ where assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); assert!( - scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GLWE::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_tmp_bytes(self, &res) ) } diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index 6312b13..d89f515 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -45,7 +45,7 @@ where } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_scratch_space(self, res)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); let mut tmp: GLWE> = GLWE::alloc_from_infos(res); diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 6658480..971766e 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -14,13 +14,13 @@ use crate::layouts::{ }; impl GLWEToLWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GLWESecretPrepared::bytes_of(module, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) } } diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 04b52aa..1f1820a 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -14,7 +14,7 @@ use crate::layouts::{ }; impl LWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, @@ -36,7 +36,7 @@ impl LWESwitchingKey> { ); GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + GLWESecretPrepared::bytes_of(module, Rank(1)) - + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index b50a2db..56a6701 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -12,7 +12,7 @@ use poulpy_hal::{ use crate::layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}; impl LWEToGLWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, @@ -22,8 +22,7 @@ impl LWEToGLWESwitchingKey> { Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in()) } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index b7bd4af..5bb4557 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -18,7 +18,7 @@ where A: GGLWEInfos, B: GGSWInfos, { - self.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) + self.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } fn gglwe_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 0b38ebf..d3a59a6 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -21,7 +21,7 @@ where A: GGSWInfos, B: GGSWInfos, { - self.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) + self.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } fn ggsw_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 4735ff9..ab9968c 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -15,14 +15,14 @@ use crate::{ }; impl GLWE> { - pub fn external_product_scratch_space(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GLWEInfos, A: GLWEInfos, B: GGSWInfos, M: GLWEExternalProduct, { - module.glwe_external_product_scratch_space(res_infos, a_infos, b_infos) + module.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } @@ -61,7 +61,7 @@ where + VecZnxBigNormalize + VecZnxNormalize, { - fn glwe_external_product_scratch_space(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + fn glwe_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GLWEInfos, A: GLWEInfos, @@ -111,7 +111,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); - assert!(scratch.available() >= self.glwe_external_product_scratch_space(res, res, rhs)); + assert!(scratch.available() >= self.glwe_external_product_tmp_bytes(res, res, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -225,7 +225,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); assert_eq!(lhs.n(), res.n()); - assert!(scratch.available() >= self.glwe_external_product_scratch_space(res, lhs, rhs)); + assert!(scratch.available() >= self.glwe_external_product_tmp_bytes(res, lhs, rhs)); } let cols: usize = (rhs.rank() + 1).into(); diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 05a2965..7dacb97 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -90,13 +90,13 @@ impl GLWEPacker { } /// Number of scratch space bytes required to call [Self::add]. - pub fn scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - pack_core_scratch_space(module, out_infos, key_infos) + pack_core_tmp_bytes(module, out_infos, key_infos) } pub fn galois_elements(module: &Module) -> Vec { @@ -111,7 +111,7 @@ impl GLWEPacker { /// of packed ciphertexts reaches N/2^log_batch is a result written. /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. - /// * `scratch`: scratch space of size at least [Self::scratch_space]. + /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. pub fn add( &mut self, module: &Module, @@ -177,13 +177,13 @@ impl GLWEPacker { } } -fn pack_core_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn pack_core_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - combine_scratch_space(module, out_infos, key_infos) + combine_tmp_bytes(module, out_infos, key_infos) } fn pack_core( @@ -268,14 +268,14 @@ fn pack_core( } } -fn combine_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn combine_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { GLWE::bytes_of_from_infos(module, out_infos) - + (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos)) + + (GLWE::rsh_tmp_bytes(module.n()) | GLWE::automorphism_inplace_tmp_bytes(module, out_infos, key_infos)) } /// [combine] merges two ciphertexts together. diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 1d7a6e6..36aabb9 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -27,19 +27,14 @@ impl GLWE> { gal_els } - pub fn trace_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize + pub fn trace_tmp_bytes(module: &Module, out_infos: &OUT, in_infos: &IN, key_infos: &KEY) -> usize where OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let trace: usize = Self::automorphism_inplace_scratch_space(module, out_infos, key_infos); + let trace: usize = Self::automorphism_inplace_tmp_bytes(module, out_infos, key_infos); if in_infos.base2k() != key_infos.base2k() { let glwe_conv: usize = VecZnx::bytes_of( module.n(), @@ -52,13 +47,13 @@ impl GLWE> { trace } - pub fn trace_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn trace_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::trace_scratch_space(module, out_infos, out_infos, key_infos) + Self::trace_tmp_bytes(module, out_infos, out_infos, key_infos) } } diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index b1fd8f4..a65266f 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -13,7 +13,7 @@ use crate::layouts::{ }; impl AutomorphismKey> { - pub fn keyswitch_scratch_space( + pub fn keyswitch_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -25,16 +25,16 @@ impl AutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) + GLWESwitchingKey::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) } - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn keyswitch_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GGLWEInfos, KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + GLWESwitchingKey::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) } } @@ -86,7 +86,7 @@ impl AutomorphismKey { } impl GLWESwitchingKey> { - pub fn keyswitch_scratch_space( + pub fn keyswitch_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -98,16 +98,16 @@ impl GLWESwitchingKey> { KEY: GGLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWE::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) + GLWE::keyswitch_tmp_bytes(module, out_infos, in_infos, key_apply) } - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize + pub fn keyswitch_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize where OUT: GGLWEInfos + GLWEInfos, KEY: GGLWEInfos + GLWEInfos, Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply) + GLWE::keyswitch_inplace_tmp_bytes(module, out_infos, key_apply) } } diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 4f89a41..078739b 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -1,359 +1,131 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, - VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, VmpPMat, ZnxInfos}, -}; +use poulpy_hal::layouts::{Backend, DataMut, Scratch, VecZnx}; use crate::{ + GGSWExpandRows, ScratchTakeCore, + keyswitching::glwe_ct::GLWEKeySwitching, layouts::{ - GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, LWEInfos, - prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared}, + GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, + prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, }, - operations::GLWEOperations, }; impl GGSW> { - pub(crate) fn expand_row_scratch_space(module: &Module, out_infos: &OUT, tsk_infos: &TSK) -> usize - where - OUT: GGSWInfos, - TSK: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, - { - let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize; - let size_in: usize = out_infos - .k() - .div_ceil(tsk_infos.base2k()) - .div_ceil(tsk_infos.dsize().into()) as usize; - - let tmp_dft_i: usize = module.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size); - let tmp_a: usize = module.bytes_of_vec_znx_dft(1, size_in); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( - tsk_size, - size_in, - size_in, - (tsk_infos.rank_in()).into(), // Verify if rank+1 - (tsk_infos.rank_out()).into(), // Verify if rank+1 - tsk_size, - ); - let tmp_idft: usize = module.bytes_of_vec_znx_big(1, tsk_size); - let norm: usize = module.vec_znx_normalize_tmp_bytes(); - - tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) - } - - #[allow(clippy::too_many_arguments)] - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - apply_infos: &KEY, - tsk_infos: &TSK, + pub fn keyswitch_tmp_bytes( + module: &M, + res_infos: &R, + a_infos: &A, + key_infos: &K, + tsk_infos: &T, ) -> usize where - OUT: GGSWInfos, - IN: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: - VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + M: GGSWKeySwitch, { - #[cfg(debug_assertions)] - { - assert_eq!(apply_infos.rank_in(), apply_infos.rank_out()); - assert_eq!(tsk_infos.rank_in(), tsk_infos.rank_out()); - assert_eq!(apply_infos.rank_in(), tsk_infos.rank_in()); - } + module.ggsw_keyswitch_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos) + } +} - let rank: usize = apply_infos.rank_out().into(); +impl GGSW { + pub fn keyswitch(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) + where + A: GGSWToRef, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWKeySwitch, + { + module.ggsw_keyswitch(self, a, key, tsk, scratch); + } - let size_out: usize = out_infos.k().div_ceil(out_infos.base2k()) as usize; - let res_znx: usize = VecZnx::bytes_of(module.n(), rank + 1, size_out); - let ci_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, size_out); - let ks: usize = GLWE::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); - let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); - let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, size_out); + pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) + where + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWKeySwitch, + { + module.ggsw_keyswitch_inplace(self, key, tsk, scratch); + } +} - if in_infos.base2k() == tsk_infos.base2k() { +pub trait GGSWKeySwitch +where + Self: GLWEKeySwitching + GGSWExpandRows, +{ + fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize + where + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + { + assert_eq!(key_infos.rank_in(), key_infos.rank_out()); + assert_eq!(tsk_infos.rank_in(), tsk_infos.rank_out()); + assert_eq!(key_infos.rank_in(), tsk_infos.rank_in()); + + let rank: usize = key_infos.rank_out().into(); + + let size_out: usize = res_infos.k().div_ceil(res_infos.base2k()) as usize; + let res_znx: usize = VecZnx::bytes_of(self.n(), rank + 1, size_out); + let ci_dft: usize = self.bytes_of_vec_znx_dft(rank + 1, size_out); + let ks: usize = self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos); + let expand_rows: usize = self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos); + let res_dft: usize = self.bytes_of_vec_znx_dft(rank + 1, size_out); + + if a_infos.base2k() == tsk_infos.base2k() { res_znx + ci_dft + (ks | expand_rows | res_dft) } else { let a_conv: usize = VecZnx::bytes_of( - module.n(), + self.n(), 1, - out_infos.k().div_ceil(tsk_infos.base2k()) as usize, - ) + module.vec_znx_normalize_tmp_bytes(); + res_infos.k().div_ceil(tsk_infos.base2k()) as usize, + ) + self.vec_znx_normalize_tmp_bytes(); res_znx + ci_dft + (a_conv | ks | expand_rows | res_dft) } } - #[allow(clippy::too_many_arguments)] - pub fn keyswitch_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - apply_infos: &KEY, - tsk_infos: &TSK, - ) -> usize + fn ggsw_keyswitch(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where - OUT: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: - VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, + R: GGSWToMut, + A: GGSWToRef, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, { - GGSW::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + + assert_eq!(res.ggsw_layout(), a.ggsw_layout()); + + for row in 0..a.dnum().into() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) + self.glwe_keyswitch(&mut res.at_mut(row, 0), &a.at(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); + } + + fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + + for row in 0..res.dnum().into() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) + self.glwe_keyswitch_inplace(&mut res.at_mut(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); } } -impl GGSW { - pub fn from_gglwe( - &mut self, - module: &Module, - a: &GGLWE, - tsk: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - DataA: DataRef, - DataTsk: DataRef, - Module: VecZnxCopy - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VecZnxDftCopy - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftAddInplace - + VecZnxBigNormalize - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, - { - #[cfg(debug_assertions)] - { - use crate::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(self.rank(), a.rank_out()); - assert_eq!(self.dnum(), a.dnum()); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(a.n(), module.n() as u32); - assert_eq!(tsk.n(), module.n() as u32); - } - (0..self.dnum().into()).for_each(|row_i| { - self.at_mut(row_i, 0).copy(module, &a.at(row_i, 0)); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &GGSW, - ksk: &GLWESwitchingKeyPrepared, - tsk: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, - { - (0..lhs.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .keyswitch(module, &lhs.at(row_i, 0), ksk, scratch); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn keyswitch_inplace( - &mut self, - module: &Module, - ksk: &GLWESwitchingKeyPrepared, - tsk: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, - { - (0..self.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .keyswitch_inplace(module, ksk, scratch); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn expand_row( - &mut self, - module: &Module, - tsk: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VecZnxDftCopy - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftAddInplace - + VecZnxBigNormalize - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, - { - let basek_in: usize = self.base2k().into(); - let basek_tsk: usize = tsk.base2k().into(); - - assert!(scratch.available() >= GGSW::expand_row_scratch_space(module, self, tsk)); - - let n: usize = self.n().into(); - let rank: usize = self.rank().into(); - let cols: usize = rank + 1; - - let a_size: usize = (self.size() * basek_in).div_ceil(basek_tsk); - - // Keyswitch the j-th row of the col 0 - for row_i in 0..self.dnum().into() { - let a = &self.at(row_i, 0).data; - - // Pre-compute DFT of (a0, a1, a2) - let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(n, cols, a_size); - - if basek_in == basek_tsk { - for i in 0..cols { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i); - } - } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(n, 1, a_size); - for i in 0..cols { - module.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2); - module.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0); - } - } - - for col_j in 1..cols { - // Example for rank 3: - // - // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is - // actually composed of that many dnum and we focus on a specific row here - // implicitely given ci_dft. - // - // # Input - // - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) - // col 1: (0, 0, 0, 0) - // col 2: (0, 0, 0, 0) - // col 3: (0, 0, 0, 0) - // - // # Output - // - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) - // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 ) - // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 ) - // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i]) - - let dsize: usize = tsk.dsize().into(); - - let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(n, cols, tsk.size()); - let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(n, 1, ci_dft.size().div_ceil(dsize)); - - { - // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2 - // - // # Example for col=1 - // - // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2) - // + - // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2) - // + - // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2) - // = - // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) - for col_i in 1..cols { - let pmat: &VmpPMat = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) - - // Extracts a[i] and multipies with Enc(s[i]s[j]) - for di in 0..dsize { - tmp_a.set_size((ci_dft.size() + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize); - - module.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i); - if di == 0 && col_i == 1 { - module.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3); - } else { - module.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3); - } - } - } - } - - // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i - // - // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2) - // + - // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0) - // = - // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2) - // = - // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2) - module.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0); - let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(n, 1, tsk.size()); - for i in 0..cols { - module.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i); - module.vec_znx_big_normalize( - basek_in, - &mut self.at_mut(row_i, col_j).data, - i, - basek_tsk, - &tmp_idft, - 0, - scratch_3, - ); - } - } - } - } -} +impl GGSW {} diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index ebd0243..ac4c7b8 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -1,186 +1,179 @@ use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, + }, +}; impl GLWE> { - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_apply: &KEY, - ) -> usize + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: GLWEInfos, + B: GGLWEInfos, + M: GLWEKeySwitching, { - let in_size: usize = in_infos + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) + } +} + +impl GLWE { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + M: GLWEKeySwitching, + Scratch: ScratchTakeCore, + { + module.glwe_keyswitch(self, a, b, scratch); + } + + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + M: GLWEKeySwitching, + Scratch: ScratchTakeCore, + { + module.glwe_keyswitch_inplace(self, a, scratch); + } +} + +impl GLWEKeySwitching for Module where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize + + VecZnxNormalizeTmpBytes +{ +} + +pub trait GLWEKeySwitching +where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize + + VecZnxNormalizeTmpBytes, +{ + fn glwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + where + R: GLWEInfos, + A: GLWEInfos, + B: GGLWEInfos, + { + let in_size: usize = a_infos .k() - .div_ceil(key_apply.base2k()) - .div_ceil(key_apply.dsize().into()) as usize; - let out_size: usize = out_infos.size(); - let ksk_size: usize = key_apply.size(); - let res_dft: usize = module.bytes_of_vec_znx_dft((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.bytes_of_vec_znx_dft((key_apply.rank_in()).into(), in_size); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( + .div_ceil(b_infos.base2k()) + .div_ceil(b_infos.dsize().into()) as usize; + let out_size: usize = res_infos.size(); + let ksk_size: usize = b_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, in_size, - (key_apply.rank_in()).into(), - (key_apply.rank_out() + 1).into(), + (b_infos.rank_in()).into(), + (b_infos.rank_out() + 1).into(), ksk_size, - ) + module.bytes_of_vec_znx_dft((key_apply.rank_in()).into(), in_size); - let normalize_big: usize = module.vec_znx_big_normalize_tmp_bytes(); - if in_infos.base2k() == key_apply.base2k() { + ) + self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + let normalize_big: usize = self.vec_znx_big_normalize_tmp_bytes(); + if a_infos.base2k() == b_infos.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) - } else if key_apply.dsize() == 1 { + } else if b_infos.dsize() == 1 { // In this case, we only need one column, temporary, that we can drop once a_dft is computed. - let normalize_conv: usize = VecZnx::bytes_of(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); + let normalize_conv: usize = VecZnx::bytes_of(self.n(), 1, in_size) + self.vec_znx_normalize_tmp_bytes(); res_dft + (((ai_dft + normalize_conv) | vmp) | normalize_big) } else { // Since we stride over a to get a_dft when dsize > 1, we need to store the full columns of a with in the base conversion. - let normalize_conv: usize = VecZnx::bytes_of(module.n(), (key_apply.rank_in()).into(), in_size); - res_dft + ((ai_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) + let normalize_conv: usize = VecZnx::bytes_of(self.n(), (b_infos.rank_in()).into(), in_size); + res_dft + ((ai_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize + fn glwe_keyswitch(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEToMut, + A: GLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - Self::keyswitch_scratch_space(module, out_infos, out_infos, key_apply) - } -} + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); -impl GLWE { - #[allow(dead_code)] - pub(crate) fn assert_keyswitch( - &self, - module: &Module, - lhs: &GLWE, - rhs: &GLWESwitchingKeyPrepared, - scratch: &Scratch, - ) where - DataLhs: DataRef, - DataRhs: DataRef, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { assert_eq!( - lhs.rank(), - rhs.rank_in(), - "lhs.rank(): {} != rhs.rank_in(): {}", - lhs.rank(), - rhs.rank_in() + a.rank(), + b.rank_in(), + "a.rank(): {} != b.rank_in(): {}", + a.rank(), + b.rank_in() ); assert_eq!( - self.rank(), - rhs.rank_out(), - "self.rank(): {} != rhs.rank_out(): {}", - self.rank(), - rhs.rank_out() + res.rank(), + b.rank_out(), + "res.rank(): {} != b.rank_out(): {}", + res.rank(), + b.rank_out() ); - assert_eq!(rhs.n(), self.n()); - assert_eq!(lhs.n(), self.n()); - let scrach_needed: usize = GLWE::keyswitch_scratch_space(module, self, lhs, rhs); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(b.n(), self.n() as u32); + + let scrach_needed: usize = self.glwe_keyswitch_tmp_bytes(res, a, b); assert!( scratch.available() >= scrach_needed, - "scratch.available()={} < GLWECiphertext::keyswitch_scratch_space( - module, - self.base2k(), - self.k(), - lhs.base2k(), - lhs.k(), - rhs.base2k(), - rhs.k(), - rhs.dsize(), - rhs.rank_in(), - rhs.rank_out(), - )={scrach_needed}", + "scratch.available()={} < glwe_keyswitch_tmp_bytes={scrach_needed}", scratch.available(), ); - } - #[allow(dead_code)] - pub(crate) fn assert_keyswitch_inplace( - &self, - module: &Module, - rhs: &GLWESwitchingKeyPrepared, - scratch: &Scratch, - ) where - DataRhs: DataRef, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { - assert_eq!( - self.rank(), - rhs.rank_out(), - "self.rank(): {} != rhs.rank_out(): {}", - self.rank(), - rhs.rank_out() - ); + let basek_out: usize = res.base2k().into(); + let base2k_out: usize = b.base2k().into(); - assert_eq!(rhs.n(), self.n()); - - let scrach_needed: usize = GLWE::keyswitch_inplace_scratch_space(module, self, rhs); - - assert!( - scratch.available() >= scrach_needed, - "scratch.available()={} < GLWECiphertext::keyswitch_scratch_space()={scrach_needed}", - scratch.available(), - ); - } -} - -impl GLWE { - pub fn keyswitch( - &mut self, - module: &Module, - glwe_in: &GLWE, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { - #[cfg(debug_assertions)] - { - self.assert_keyswitch(module, glwe_in, rhs, scratch); - } - - let basek_out: usize = self.base2k().into(); - let basek_ksk: usize = rhs.base2k().into(); - - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // Todo optimise - let res_big: VecZnxBig<_, B> = glwe_in.keyswitch_internal(module, res_dft, rhs, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_normalize( + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), b.size()); // Todo optimise + let res_big: VecZnxBig<&mut [u8], BE> = keyswitch_internal(self, res_dft, a, b, scratch_1); + (0..(res.rank() + 1).into()).for_each(|i| { + self.vec_znx_big_normalize( basek_out, - &mut self.data, + &mut res.data, i, - basek_ksk, + base2k_out, &res_big, i, scratch_1, @@ -188,227 +181,190 @@ impl GLWE { }) } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { - #[cfg(debug_assertions)] - { - self.assert_keyswitch_inplace(module, rhs, scratch); - } - - let basek_in: usize = self.base2k().into(); - let basek_ksk: usize = rhs.base2k().into(); - - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // Todo optimise - let res_big: VecZnxBig<_, B> = self.keyswitch_internal(module, res_dft, rhs, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_normalize( - basek_in, - &mut self.data, - i, - basek_ksk, - &res_big, - i, - scratch_1, - ); - }) - } -} - -impl GLWE { - pub(crate) fn keyswitch_internal( - &self, - module: &Module, - res_dft: VecZnxDft, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) -> VecZnxBig + fn glwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) where - DataRes: DataMut, - DataKey: DataRef, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch:, + R: GLWEToMut, + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - if rhs.dsize() == 1 { - return keyswitch_vmp_one_digit( - module, - self.base2k().into(), - rhs.base2k().into(), - res_dft, - &self.data, - &rhs.key.data, - scratch, + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!( + res.rank(), + a.rank_in(), + "res.rank(): {} != a.rank_in(): {}", + res.rank(), + a.rank_in() + ); + assert_eq!( + res.rank(), + a.rank_out(), + "res.rank(): {} != b.rank_out(): {}", + res.rank(), + a.rank_out() + ); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + + let scrach_needed: usize = self.glwe_keyswitch_tmp_bytes(res, res, a); + + assert!( + scratch.available() >= scrach_needed, + "scratch.available()={} < glwe_keyswitch_tmp_bytes={scrach_needed}", + scratch.available(), + ); + + let base2k_in: usize = res.base2k().into(); + let base2k_out: usize = a.base2k().into(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), a.size()); // Todo optimise + let res_big: VecZnxBig<&mut [u8], BE> = keyswitch_internal(self, res_dft, res, a, scratch_1); + (0..(res.rank() + 1).into()).for_each(|i| { + self.vec_znx_big_normalize( + base2k_in, + &mut res.data, + i, + base2k_out, + &res_big, + i, + scratch_1, ); - } - - keyswitch_vmp_multiple_digits( - module, - self.base2k().into(), - rhs.base2k().into(), - res_dft, - &self.data, - &rhs.key.data, - rhs.dsize().into(), - scratch, - ) + }) } } -fn keyswitch_vmp_one_digit( - module: &Module, - basek_in: usize, - basek_ksk: usize, - mut res_dft: VecZnxDft, - a: &VecZnx, - mat: &VmpPMat, - scratch: &mut Scratch, -) -> VecZnxBig +impl GLWE> {} + +impl GLWE {} + +fn keyswitch_internal( + module: &M, + mut res: VecZnxDft, + a: &GLWE, + b: &GLWESwitchingKeyPrepared, + scratch: &mut Scratch, +) -> VecZnxBig where - DataRes: DataMut, - DataIn: DataRef, - DataVmp: DataRef, - Module: VecZnxDftBytesOf - + VecZnxDftApply - + VmpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxNormalize, - Scratch:, + DR: DataMut, + DA: DataRef, + DB: DataRef, + M: ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDftTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize, + Scratch: ScratchTakeCore, { - let cols: usize = a.cols(); + let base2k_in: usize = a.base2k().into(); + let base2k_out: usize = b.base2k().into(); + let cols: usize = (a.rank() + 1).into(); + let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); + let pmat: &VmpPMat = &b.key.data; - let a_size: usize = (a.size() * basek_in).div_ceil(basek_ksk); - let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(a.n(), cols - 1, a.size()); + if b.dsize() == 1 { + let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); - if basek_in == basek_ksk { - (0..cols - 1).for_each(|col_i| { - module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, a, col_i + 1); - }); + if base2k_in == base2k_out { + (0..cols - 1).for_each(|col_i| { + module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, a.data(), col_i + 1); + }); + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, 1, a_size); + (0..cols - 1).for_each(|col_i| { + module.vec_znx_normalize( + base2k_out, + &mut a_conv, + 0, + base2k_in, + a.data(), + col_i + 1, + scratch_2, + ); + module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, &a_conv, 0); + }); + } + + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_1); } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(a.n(), 1, a_size); - (0..cols - 1).for_each(|col_i| { - module.vec_znx_normalize(basek_ksk, &mut a_conv, 0, basek_in, a, col_i + 1, scratch_2); - module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, &a_conv, 0); - }); + let dsize: usize = b.dsize().into(); + + let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a_size.div_ceil(dsize)); + ai_dft.data_mut().fill(0); + + if base2k_in == base2k_out { + for di in 0..dsize { + ai_dft.set_size((a_size + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + res.set_size(pmat.size() - ((dsize - di) as isize - 2).max(0) as usize); + + for j in 0..cols - 1 { + module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, a.data(), j + 1); + } + + if di == 0 { + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_1); + } else { + module.vmp_apply_dft_to_dft_add(&mut res, &ai_dft, pmat, di, scratch_1); + } + } + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, cols - 1, a_size); + for j in 0..cols - 1 { + module.vec_znx_normalize( + base2k_out, + &mut a_conv, + j, + base2k_in, + a.data(), + j + 1, + scratch_2, + ); + } + + for di in 0..dsize { + ai_dft.set_size((a_size + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + res.set_size(pmat.size() - ((dsize - di) as isize - 2).max(0) as usize); + + for j in 0..cols - 1 { + module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, &a_conv, j); + } + + if di == 0 { + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_2); + } else { + module.vmp_apply_dft_to_dft_add(&mut res, &ai_dft, pmat, di, scratch_2); + } + } + } + + res.set_size(res.max_size()); } - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_1); - let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res_dft); - module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0); - res_big -} - -#[allow(clippy::too_many_arguments)] -fn keyswitch_vmp_multiple_digits( - module: &Module, - basek_in: usize, - basek_ksk: usize, - mut res_dft: VecZnxDft, - a: &VecZnx, - mat: &VmpPMat, - dsize: usize, - scratch: &mut Scratch, -) -> VecZnxBig -where - DataRes: DataMut, - DataIn: DataRef, - DataVmp: DataRef, - Module: VecZnxDftBytesOf - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxNormalize, - Scratch:, -{ - let cols: usize = a.cols(); - let a_size: usize = (a.size() * basek_in).div_ceil(basek_ksk); - let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(a.n(), cols - 1, a_size.div_ceil(dsize)); - ai_dft.data_mut().fill(0); - - if basek_in == basek_ksk { - for di in 0..dsize { - ai_dft.set_size((a_size + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - res_dft.set_size(mat.size() - ((dsize - di) as isize - 2).max(0) as usize); - - for j in 0..cols - 1 { - module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, a, j + 1); - } - - if di == 0 { - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_1); - } else { - module.vmp_apply_dft_to_dft_add(&mut res_dft, &ai_dft, mat, di, scratch_1); - } - } - } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(a.n(), cols - 1, a_size); - for j in 0..cols - 1 { - module.vec_znx_normalize(basek_ksk, &mut a_conv, j, basek_in, a, j + 1, scratch_2); - } - - for di in 0..dsize { - ai_dft.set_size((a_size + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - res_dft.set_size(mat.size() - ((dsize - di) as isize - 2).max(0) as usize); - - for j in 0..cols - 1 { - module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, &a_conv, j); - } - - if di == 0 { - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_2); - } else { - module.vmp_apply_dft_to_dft_add(&mut res_dft, &ai_dft, mat, di, scratch_2); - } - } - } - - res_dft.set_size(res_dft.max_size()); - let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res_dft); - module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0); + let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res); + module.vec_znx_big_add_small_inplace(&mut res_big, 0, a.data(), 0); res_big } diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 938c732..e39259f 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}; impl LWE> { - pub fn keyswitch_scratch_space( + pub fn keyswitch_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -50,7 +50,7 @@ impl LWE> { let glwe_in: usize = GLWE::bytes_of_from_infos(module, &glwe_in_infos); let glwe_out: usize = GLWE::bytes_of_from_infos(module, &glwe_out_infos); - let ks: usize = GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); + let ks: usize = GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, key_infos); glwe_in + glwe_out + ks } @@ -84,7 +84,7 @@ impl LWE { { assert!(self.n() <= module.n() as u32); assert!(a.n() <= module.n() as u32); - assert!(scratch.available() >= LWE::keyswitch_scratch_space(module, self, a, ksk)); + assert!(scratch.available() >= LWE::keyswitch_tmp_bytes(module, self, a, ksk)); } let max_k: TorusPrecision = self.k().max(a.k()); diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index b0c5a38..15e6c76 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -14,6 +14,7 @@ mod utils; pub use operations::*; pub mod layouts; +pub use conversion::*; pub use dist::*; pub use external_product::*; pub use glwe_packing::*; diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index c924192..516a210 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -35,7 +35,7 @@ impl GGLWE { let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); (0..self.rank_in().into()).for_each(|col_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 4f4c4f4..92f806b 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -48,7 +48,7 @@ impl GGSW { let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { @@ -120,7 +120,7 @@ impl GGSW { let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index 3242d91..40b86d9 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -61,7 +61,7 @@ impl GLWE { + VecZnxNormalizeInplace, B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index ea4cf9d..b8b32ce 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -246,10 +246,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { }); } - fn copy(&mut self, module: &Module, a: &A) + fn copy(&mut self, module: &M, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxCopy, + M: VecZnxCopy, { #[cfg(debug_assertions)] { @@ -319,8 +319,8 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { } impl GLWE> { - pub fn rsh_scratch_space(n: usize) -> usize { - VecZnx::rsh_scratch_space(n) + pub fn rsh_tmp_bytes(n: usize) -> usize { + VecZnx::rsh_tmp_bytes(n) } } 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 b98bfaf..37b5403 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -120,9 +120,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) - | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) - | AutomorphismKey::automorphism_scratch_space( + AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) + | AutomorphismKey::automorphism_tmp_bytes( module, &auto_key_out_infos, &auto_key_in_infos, @@ -319,9 +319,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) - | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) + | AutomorphismKey::automorphism_inplace_tmp_bytes(module, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); 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 17f182f..7c2a427 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -139,10 +139,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ct_in) - | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSW::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), + GGSW::encrypt_sk_tmp_bytes(module, &ct_in) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; @@ -319,10 +319,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ct) - | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSW::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), + GGSW::encrypt_sk_tmp_bytes(module, &ct) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GGSW::automorphism_inplace_tmp_bytes(module, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; 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 49643fe..02afcb8 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -112,10 +112,10 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWE::decrypt_scratch_space(module, &ct_out) - | GLWE::encrypt_sk_scratch_space(module, &ct_in) - | GLWE::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + | GLWE::decrypt_tmp_bytes(module, &ct_out) + | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) + | GLWE::automorphism_tmp_bytes(module, &ct_out, &ct_in, &autokey), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); @@ -246,10 +246,10 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWE::decrypt_scratch_space(module, &ct) - | GLWE::encrypt_sk_scratch_space(module, &ct) - | GLWE::automorphism_inplace_scratch_space(module, &ct, &autokey), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + | GLWE::decrypt_tmp_bytes(module, &ct) + | GLWE::encrypt_sk_tmp_bytes(module, &ct) + | GLWE::automorphism_inplace_tmp_bytes(module, &ct, &autokey), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 94d922e..972e0c6 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -96,9 +96,9 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, &lwe_to_glwe_infos) - | GLWE::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) - | GLWE::decrypt_scratch_space(module, &glwe_infos), + LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos) + | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); @@ -213,9 +213,9 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) - | LWE::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) - | GLWE::decrypt_scratch_space(module, &glwe_infos), + GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos) + | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); 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 68fca13..bd164dc 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -90,7 +90,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); @@ -192,7 +192,7 @@ where let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::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 e3450f9..5e09781 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -87,7 +87,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); @@ -179,7 +179,7 @@ where let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); 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 c419bb4..7620709 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -55,7 +55,7 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -144,7 +144,7 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes( module, &ggsw_infos, )); 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 9094c33..ed2c1e3 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -85,7 +85,7 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); @@ -178,7 +178,7 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECompressed::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), + GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); @@ -269,7 +269,7 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::decrypt_scratch_space(module, &glwe_infos) | GLWE::encrypt_sk_scratch_space(module, &glwe_infos), + GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); @@ -349,9 +349,9 @@ where let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWE::decrypt_scratch_space(module, &glwe_infos) - | GLWE::encrypt_pk_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos) + | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); 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 1f369db..da05ba1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -86,7 +86,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); @@ -204,7 +204,7 @@ where 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_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::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 f10643b..a369978 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 @@ -121,14 +121,14 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) - | GLWESwitchingKey::external_product_scratch_space( + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_in_infos) + | GLWESwitchingKey::external_product_tmp_bytes( module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos, ) - | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); let r: usize = 1; @@ -292,9 +292,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) - | GLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) - | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_out_infos) + | GLWESwitchingKey::external_product_inplace_tmp_bytes(module, &gglwe_out_infos, &ggsw_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); let r: usize = 1; 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 8d62b19..84a2f68 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 @@ -128,9 +128,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) + | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); @@ -282,9 +282,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) + | GGSW::external_product_inplace_tmp_bytes(module, &ggsw_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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 08f9695..60026b8 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 @@ -116,9 +116,9 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWE::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) + | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); @@ -259,9 +259,9 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWE::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::external_product_inplace_tmp_bytes(module, &glwe_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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 90c179b..c667848 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -119,11 +119,11 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) - | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_tmp_bytes( module, &gglwe_s0s1_infos, &gglwe_s0s2_infos, @@ -274,10 +274,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_tmp_bytes( module, &gglwe_s0s1_infos, &gglwe_s1s2_infos, 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 6d74811..4d3d556 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -132,10 +132,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSW::keyswitch_scratch_space( + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, &ggsw_in_infos, @@ -310,10 +310,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSW::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GGSW::keyswitch_inplace_tmp_bytes(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), ); let var_xs: f64 = 0.5; 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 7f629b8..2ea6e75 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -112,9 +112,9 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) - | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); @@ -244,9 +244,9 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWE::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); 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 b833209..130c356 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -99,8 +99,8 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - LWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | LWE::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), + LWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + | LWE::keyswitch_tmp_bytes(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), ); let mut sk_lwe_in: LWESecret> = LWESecret::alloc(n_lwe_in.into()); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index ba6d250..fdfbd57 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -105,9 +105,9 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 2f5b5a5..932b401 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -107,10 +107,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWE::decrypt_scratch_space(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWE::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWE::trace_inplace_tmp_bytes(module, &glwe_out_infos, &key_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); diff --git a/poulpy-hal/src/layouts/vec_znx.rs b/poulpy-hal/src/layouts/vec_znx.rs index c96de28..c084934 100644 --- a/poulpy-hal/src/layouts/vec_znx.rs +++ b/poulpy-hal/src/layouts/vec_znx.rs @@ -110,7 +110,7 @@ impl ZnxView for VecZnx { } impl VecZnx> { - pub fn rsh_scratch_space(n: usize) -> usize { + pub fn rsh_tmp_bytes(n: usize) -> usize { n * std::mem::size_of::() } } diff --git a/poulpy-hal/src/layouts/zn.rs b/poulpy-hal/src/layouts/zn.rs index eaba4b4..00f8067 100644 --- a/poulpy-hal/src/layouts/zn.rs +++ b/poulpy-hal/src/layouts/zn.rs @@ -98,7 +98,7 @@ impl ZnxView for Zn { } impl Zn> { - pub fn rsh_scratch_space(n: usize) -> usize { + pub fn rsh_tmp_bytes(n: usize) -> usize { n * std::mem::size_of::() } } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 1c2ebff..9a33e5e 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -21,7 +21,7 @@ use crate::tfhe::blind_rotation::{ }; #[allow(clippy::too_many_arguments)] -pub fn cggi_blind_rotate_scratch_space( +pub fn cggi_blind_rotate_tmp_bytes( module: &Module, block_size: usize, extension_factor: usize, @@ -61,7 +61,7 @@ where + vmp_xai + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes()))) } else { - GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) + GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_tmp_bytes(module, glwe_infos, brk_infos) } } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index 26c9721..3114ea1 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -43,12 +43,12 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { } impl BlindRotationKey, CGGI> { - pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSW::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_tmp_bytes(module, infos) } } @@ -145,12 +145,12 @@ impl BlindRotationKeyCompressed, CGGI> { } } - pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSWCompressed::encrypt_sk_scratch_space(module, infos) + GGSWCompressed::encrypt_sk_tmp_bytes(module, infos) } } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 9148df3..9cae713 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -19,7 +19,7 @@ use poulpy_hal::{ use crate::tfhe::blind_rotation::{ BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyLayout, - BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n, + BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_tmp_bytes, mod_switch_2n, }; use poulpy_core::layouts::{ @@ -123,7 +123,7 @@ where base2k: base2k.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_tmp_bytes( module, &brk_infos, )); @@ -134,7 +134,7 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_scratch_space( + let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_tmp_bytes( module, block_size, extension_factor, From 10817a85297a26a42f2a8d1e651dd4682e740906 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 17:15:51 +0200 Subject: [PATCH 14/60] wip --- .../benches/external_product_glwe_fft64.rs | 6 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 369 +++++++++--------- poulpy-core/src/keyswitching/ggsw_ct.rs | 4 +- poulpy-core/src/keyswitching/glwe_ct.rs | 10 +- poulpy-core/src/keyswitching/lwe_ct.rs | 87 ++--- poulpy-core/src/layouts/gglwe_ct.rs | 12 +- poulpy-core/src/layouts/ggsw_ct.rs | 14 +- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/layouts/lwe_ct.rs | 14 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/scratch.rs | 4 +- .../benches/circuit_bootstrapping.rs | 10 +- .../examples/circuit_bootstrapping.rs | 6 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 4 +- .../src/tfhe/bdd_arithmetic/test.rs | 4 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 2 +- .../tests/generic_blind_rotation.rs | 4 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 4 +- .../tests/circuit_bootstrapping.rs | 10 +- 19 files changed, 264 insertions(+), 304 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 4af5d1f..fad8523 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,5 +1,5 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -38,7 +38,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = Dnum(1); //(p.k_ct_in.div_ceil(p.base2k); - let ggsw_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_layout: GGSWLayout = GGSWLayout { n, base2k, k: k_ggsw, @@ -146,7 +146,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); - let ggsw_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_layout: GGSWLayout = GGSWLayout { n, base2k, k: k_ggsw, diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index a65266f..edda267 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -1,224 +1,205 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, -}; +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; -use crate::layouts::{ - AutomorphismKey, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, - prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, +use crate::{ + ScratchTakeCore, + keyswitching::glwe_ct::GLWEKeySwitch, + layouts::{ + AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey, + GLWESwitchingKeyToRef, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, + }, }; impl AutomorphismKey> { - pub fn keyswitch_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, { - GLWESwitchingKey::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) - } - - pub fn keyswitch_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize - where - OUT: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - GLWESwitchingKey::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } impl AutomorphismKey { - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &AutomorphismKey, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: AutomorphismKeyToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - self.key.keyswitch(module, &lhs.key, rhs, scratch); + module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - self.key.keyswitch_inplace(module, &rhs.key, scratch); + module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); } } impl GLWESwitchingKey> { - pub fn keyswitch_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_apply: &KEY, - ) -> usize + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, { - GLWE::keyswitch_tmp_bytes(module, out_infos, in_infos, key_apply) - } - - pub fn keyswitch_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize - where - OUT: GGLWEInfos + GLWEInfos, - KEY: GGLWEInfos + GLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - GLWE::keyswitch_inplace_tmp_bytes(module, out_infos, key_apply) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } impl GLWESwitchingKey { - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &GLWESwitchingKey, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GLWESwitchingKeyToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - #[cfg(debug_assertions)] - { - assert_eq!( - self.rank_in(), - lhs.rank_in(), - "ksk_out input rank: {} != ksk_in input rank: {}", - self.rank_in(), - lhs.rank_in() - ); - assert_eq!( - lhs.rank_out(), - rhs.rank_in(), - "ksk_in output rank: {} != ksk_apply input rank: {}", - self.rank_out(), - rhs.rank_in() - ); - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - assert!( - self.dnum() <= lhs.dnum(), - "self.dnum()={} > lhs.dnum()={}", - self.dnum(), - lhs.dnum() - ); - assert_eq!( - self.dsize(), - lhs.dsize(), - "ksk_out dsize: {} != ksk_in dsize: {}", - self.dsize(), - lhs.dsize() - ) - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .keyswitch(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - }); - - (self.dnum().min(lhs.dnum()).into()..self.dnum().into()).for_each(|row_i| { - (0..self.rank_in().into()).for_each(|col_j| { - self.at_mut(row_i, col_j).data.zero(); - }); - }); + module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &GLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - #[cfg(debug_assertions)] - { - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .keyswitch_inplace(module, rhs, scratch) - }); - }); + module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); } } + +impl GGLWE> { + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, + { + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } +} + +impl GGLWE { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, + { + module.gglwe_keyswitch(self, a, b, scratch); + } + + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, + { + module.gglwe_keyswitch_inplace(self, a, scratch); + } +} + +impl GGLWEKeySwitch for Module where Self: GLWEKeySwitch {} + +pub trait GGLWEKeySwitch +where + Self: GLWEKeySwitch, +{ + fn gglwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + { + self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } + + fn gglwe_keyswitch(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank_in(), + a.rank_in(), + "res input rank: {} != a input rank: {}", + res.rank_in(), + a.rank_in() + ); + assert_eq!( + a.rank_out(), + b.rank_in(), + "res output rank: {} != b input rank: {}", + a.rank_out(), + b.rank_in() + ); + assert_eq!( + res.rank_out(), + b.rank_out(), + "res output rank: {} != b output rank: {}", + res.rank_out(), + b.rank_out() + ); + assert!( + res.dnum() <= a.dnum(), + "res.dnum()={} > a.dnum()={}", + res.dnum(), + a.dnum() + ); + assert_eq!( + res.dsize(), + a.dsize(), + "res dsize: {} != a dsize: {}", + res.dsize(), + a.dsize() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_keyswitch(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } + } + } + + fn gglwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!( + res.rank_out(), + a.rank_out(), + "res output rank: {} != a output rank: {}", + res.rank_out(), + a.rank_out() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_keyswitch_inplace(&mut res.at_mut(row, col), a, scratch); + } + } + } +} + +impl GLWESwitchingKey {} diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 078739b..cfb4d8e 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeySwitching, + keyswitching::glwe_ct::GLWEKeySwitch, layouts::{ GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, @@ -53,7 +53,7 @@ impl GGSW { pub trait GGSWKeySwitch where - Self: GLWEKeySwitching + GGSWExpandRows, + Self: GLWEKeySwitch + GGSWExpandRows, { fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize where diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index ac4c7b8..6d7bff9 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -21,7 +21,7 @@ impl GLWE> { R: GLWEInfos, A: GLWEInfos, B: GGLWEInfos, - M: GLWEKeySwitching, + M: GLWEKeySwitch, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) } @@ -32,7 +32,7 @@ impl GLWE { where A: GLWEToRef, B: GLWESwitchingKeyPreparedToRef, - M: GLWEKeySwitching, + M: GLWEKeySwitch, Scratch: ScratchTakeCore, { module.glwe_keyswitch(self, a, b, scratch); @@ -41,14 +41,14 @@ impl GLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where A: GLWESwitchingKeyPreparedToRef, - M: GLWEKeySwitching, + M: GLWEKeySwitch, Scratch: ScratchTakeCore, { module.glwe_keyswitch_inplace(self, a, scratch); } } -impl GLWEKeySwitching for Module where +impl GLWEKeySwitch for Module where Self: Sized + ModuleN + VecZnxDftBytesOf @@ -69,7 +69,7 @@ impl GLWEKeySwitching for Module where { } -pub trait GLWEKeySwitching +pub trait GLWEKeySwitch where Self: Sized + ModuleN diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index e39259f..3ee7b75 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -7,85 +7,58 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}; +use crate::{ + keyswitching::glwe_ct::GLWEKeySwitch, + layouts::{prepared::LWESwitchingKeyPrepared, GGLWEInfos, GLWEAlloc, GLWELayout, GetDegree, LWEToRef, LWEInfos, Rank, TorusPrecision, GLWE, LWE}, +}; -impl LWE> { - pub fn keyswitch_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize +pub trait LWEKeySwitch +where + Self: GLWEKeySwitch + GLWEAlloc, +{ + fn keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: LWEInfos, - IN: LWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes, + R: LWEInfos, + A: LWEInfos, + K: GGLWEInfos, { - let max_k: TorusPrecision = in_infos.k().max(out_infos.k()); + let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); - let glwe_in_infos: GLWELayout = GLWELayout { - n: module.n().into(), - base2k: in_infos.base2k(), + let glwe_a_infos: GLWELayout = GLWELayout { + n: GetDegree::n(self), + base2k: a_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_out_infos: GLWELayout = GLWELayout { - n: module.n().into(), - base2k: out_infos.base2k(), + let glwe_res_infos: GLWELayout = GLWELayout { + n: GetDegree::n(self), + base2k: res_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_in: usize = GLWE::bytes_of_from_infos(module, &glwe_in_infos); - let glwe_out: usize = GLWE::bytes_of_from_infos(module, &glwe_out_infos); - let ks: usize = GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, key_infos); + let glwe_in: usize = GLWE::bytes_of_from_infos(self, &glwe_a_infos); + let glwe_out: usize = GLWE::bytes_of_from_infos(self, &glwe_res_infos); + let ks: usize = self.glwe_keyswitch_tmp_bytes(&glwe_res_infos, &glwe_a_infos, key_infos); glwe_in + glwe_out + ks } -} -impl LWE { - pub fn keyswitch( + fn keyswitch( &mut self, module: &Module, - a: &LWE, - ksk: &LWESwitchingKeyPrepared, + a: &A, + ksk: &K, scratch: &mut Scratch, ) where - A: DataRef, + A: LWEToRef, DKs: DataRef, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes - + VecZnxCopy, Scratch: ScratchAvailable, { - #[cfg(debug_assertions)] - { - assert!(self.n() <= module.n() as u32); + assert!(self.n() <= module.n() as u32); assert!(a.n() <= module.n() as u32); assert!(scratch.available() >= LWE::keyswitch_tmp_bytes(module, self, a, ksk)); - } let max_k: TorusPrecision = self.k().max(a.k()); @@ -118,3 +91,9 @@ impl LWE { self.sample_extract(&glwe_out); } } + +impl LWE> {} + +impl LWE { + +} diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 3ef1758..532a65a 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -18,8 +18,8 @@ where fn dsize(&self) -> Dsize; fn rank_in(&self) -> Rank; fn rank_out(&self) -> Rank; - fn gglwe_layout(&self) -> GGLWECiphertextLayout { - GGLWECiphertextLayout { + fn gglwe_layout(&self) -> GGLWELayout { + GGLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -36,7 +36,7 @@ pub trait SetGGLWEInfos { } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWECiphertextLayout { +pub struct GGLWELayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -46,7 +46,7 @@ pub struct GGLWECiphertextLayout { pub dsize: Dsize, } -impl LWEInfos for GGLWECiphertextLayout { +impl LWEInfos for GGLWELayout { fn base2k(&self) -> Base2K { self.base2k } @@ -60,13 +60,13 @@ impl LWEInfos for GGLWECiphertextLayout { } } -impl GLWEInfos for GGLWECiphertextLayout { +impl GLWEInfos for GGLWELayout { fn rank(&self) -> Rank { self.rank_out } } -impl GGLWEInfos for GGLWECiphertextLayout { +impl GGLWEInfos for GGLWELayout { fn rank_in(&self) -> Rank { self.rank_in } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index a4fffd3..494d06a 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -14,8 +14,8 @@ where { fn dnum(&self) -> Dnum; fn dsize(&self) -> Dsize; - fn ggsw_layout(&self) -> GGSWCiphertextLayout { - GGSWCiphertextLayout { + fn ggsw_layout(&self) -> GGSWLayout { + GGSWLayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -27,7 +27,7 @@ where } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGSWCiphertextLayout { +pub struct GGSWLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -36,7 +36,7 @@ pub struct GGSWCiphertextLayout { pub dsize: Dsize, } -impl LWEInfos for GGSWCiphertextLayout { +impl LWEInfos for GGSWLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -49,13 +49,13 @@ impl LWEInfos for GGSWCiphertextLayout { self.n } } -impl GLWEInfos for GGSWCiphertextLayout { +impl GLWEInfos for GGSWLayout { fn rank(&self) -> Rank { self.rank } } -impl GGSWInfos for GGSWCiphertextLayout { +impl GGSWInfos for GGSWLayout { fn dsize(&self) -> Dsize { self.dsize } @@ -117,7 +117,7 @@ impl fmt::Display for GGSW { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGSWCiphertext: k: {} base2k: {} dsize: {}) {}", + "(GGSW: k: {} base2k: {} dsize: {}) {}", self.k().0, self.base2k().0, self.dsize().0, diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 76e0ebc..3aea241 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -57,7 +57,7 @@ impl GGLWEInfos for GLWEToLWEKeyLayout { } } -/// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext]. +/// A special [GLWESwitchingKey] required to for the conversion from [GLWE] to [LWE]. #[derive(PartialEq, Eq, Clone)] pub struct GLWEToLWESwitchingKey(pub(crate) GLWESwitchingKey); diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index e5b218d..aed2807 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -18,8 +18,8 @@ pub trait LWEInfos { fn size(&self) -> usize { self.k().0.div_ceil(self.base2k().0) as usize } - fn lwe_layout(&self) -> LWECiphertextLayout { - LWECiphertextLayout { + fn lwe_layout(&self) -> LWELayout { + LWELayout { n: self.n(), k: self.k(), base2k: self.base2k(), @@ -33,13 +33,13 @@ pub trait SetLWEInfos { } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct LWECiphertextLayout { +pub struct LWELayout { pub n: Degree, pub k: TorusPrecision, pub base2k: Base2K, } -impl LWEInfos for LWECiphertextLayout { +impl LWEInfos for LWELayout { fn base2k(&self) -> Base2K { self.base2k } @@ -108,7 +108,7 @@ impl fmt::Display for LWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "LWECiphertext: base2k={} k={}: {}", + "LWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data @@ -187,11 +187,11 @@ impl LWE> { } } -pub trait LWECiphertextToRef { +pub trait LWEToRef { fn to_ref(&self) -> LWE<&[u8]>; } -impl LWECiphertextToRef for LWE { +impl LWEToRef for LWE { fn to_ref(&self) -> LWE<&[u8]> { LWE { k: self.k, diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 44b5af5..8a2242d 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -8,7 +8,7 @@ use crate::layouts::{ }, }; -/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. +/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. #[derive(PartialEq, Eq)] pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 8a9e78e..d1e95f9 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -326,7 +326,7 @@ where let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); + let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { @@ -359,7 +359,7 @@ where let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); + let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 9005309..a1f39c6 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,7 +3,7 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWE, LWECiphertextLayout, LWESecret, TensorKeyLayout, + AutomorphismKeyLayout, Dsize, GGSW, GGSWLayout, GLWESecret, LWE, LWELayout, LWESecret, TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_hal::{ @@ -113,8 +113,8 @@ where extension_factor: usize, k_pt: usize, block_size: usize, - lwe_infos: LWECiphertextLayout, - ggsw_infos: GGSWCiphertextLayout, + lwe_infos: LWELayout, + ggsw_infos: GGSWLayout, cbt_infos: CircuitBootstrappingKeyLayout, } @@ -238,13 +238,13 @@ where name: String::from("1-bit"), extension_factor: 1, k_pt: 1, - lwe_infos: LWECiphertextLayout { + lwe_infos: LWELayout { n: 574_u32.into(), k: 13_u32.into(), base2k: 13_u32.into(), }, block_size: 7, - ggsw_infos: GGSWCiphertextLayout { + ggsw_infos: GGSWLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 26_u32.into(), diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 73435a9..3fd8c6e 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,7 +1,7 @@ use poulpy_core::{ GLWEOperations, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, + AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWELayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, @@ -107,7 +107,7 @@ fn main() { }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), @@ -116,7 +116,7 @@ fn main() { rank: rank.into(), }; - let lwe_infos = LWECiphertextLayout { + let lwe_infos = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 40d91c2..17851cc 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,6 +1,6 @@ #[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, TensorKeyLayout, TorusPrecision, }; @@ -33,7 +33,7 @@ pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { }; #[cfg(test)] -pub(crate) static TEST_GGSW_INFOS: GGSWCiphertextLayout = GGSWCiphertextLayout { +pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GGSW), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 428ba95..1c2e69d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs @@ -4,7 +4,7 @@ use poulpy_backend::FFT64Ref; use poulpy_core::{ TakeGGSW, TakeGLWEPlaintext, layouts::{ - GGSWCiphertextLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, + GGSWLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -108,7 +108,7 @@ where BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { let glwe_infos: GLWELayout = TEST_GLWE_INFOS; - let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; let n_glwe: usize = glwe_infos.n().into(); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 9a33e5e..7aad6b9 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use poulpy_core::{ Distribution, GLWEOperations, TakeGLWE, - layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWECiphertextToRef, LWEInfos}, + layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWEToRef, LWEInfos}, }; use crate::tfhe::blind_rotation::{ diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 9cae713..409b685 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -23,7 +23,7 @@ use crate::tfhe::blind_rotation::{ }; use poulpy_core::layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, LWECiphertextToRef, LWEInfos, LWEPlaintext, LWESecret, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWELayout, LWEToRef, LWEInfos, LWEPlaintext, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }; @@ -117,7 +117,7 @@ where rank: rank.into(), }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe.into(), base2k: base2k.into(), diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 0704dff..1799127 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use poulpy_core::{ GLWEOperations, TakeGGLWE, TakeGLWE, - layouts::{Dsize, GGLWECiphertextLayout, GGSWInfos, GLWEInfos, LWEInfos}, + layouts::{Dsize, GGLWELayout, GGSWInfos, GLWEInfos, LWEInfos}, }; use poulpy_core::glwe_packing; @@ -214,7 +214,7 @@ pub fn circuit_bootstrap_core( // TODO: separate GGSW k from output of blind rotation k let (mut res_glwe, scratch_1) = scratch.take_glwe_ct(res); - let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout { + let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), base2k: base2k.into(), k: k.into(), diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index c68e907..9d5f898 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -32,7 +32,7 @@ use crate::tfhe::{ }; use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_core::layouts::{ @@ -128,7 +128,7 @@ where let k_ggsw_res: usize = 4 * base2k; let rows_ggsw_res: usize = 2; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), @@ -161,7 +161,7 @@ where }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), @@ -350,7 +350,7 @@ where let k_ggsw_res: usize = 4 * base2k; let rows_ggsw_res: usize = 3; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), @@ -383,7 +383,7 @@ where }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), From 2ea59310fbefca5cc899bd7e449ba2aebdb78397 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 18:26:32 +0200 Subject: [PATCH 15/60] wip --- .../benches/external_product_glwe_fft64.rs | 6 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 8 +- poulpy-core/examples/encryption.rs | 4 +- .../src/encryption/compressed/gglwe_ksk.rs | 4 +- poulpy-core/src/encryption/gglwe_ksk.rs | 4 +- poulpy-core/src/encryption/gglwe_tsk.rs | 6 +- poulpy-core/src/encryption/lwe_ksk.rs | 5 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 5 +- poulpy-core/src/keyswitching/lwe_ct.rs | 113 +++++++++++------- .../src/layouts/compressed/gglwe_atk.rs | 5 +- .../src/layouts/compressed/gglwe_ct.rs | 18 +-- .../src/layouts/compressed/gglwe_ksk.rs | 6 +- .../src/layouts/compressed/gglwe_tsk.rs | 4 +- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 12 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 36 ++++-- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 4 +- poulpy-core/src/layouts/compressed/lwe_ct.rs | 6 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 5 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 4 +- poulpy-core/src/layouts/gglwe_atk.rs | 10 +- poulpy-core/src/layouts/gglwe_ct.rs | 20 ++-- poulpy-core/src/layouts/gglwe_ksk.rs | 8 +- poulpy-core/src/layouts/gglwe_tsk.rs | 10 +- poulpy-core/src/layouts/ggsw_ct.rs | 18 +-- poulpy-core/src/layouts/glwe_ct.rs | 18 +-- poulpy-core/src/layouts/glwe_pk.rs | 18 +-- poulpy-core/src/layouts/glwe_pt.rs | 26 ++-- poulpy-core/src/layouts/glwe_sk.rs | 18 +-- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 10 +- poulpy-core/src/layouts/lwe_ct.rs | 20 ++-- poulpy-core/src/layouts/lwe_ksk.rs | 10 +- poulpy-core/src/layouts/lwe_pt.rs | 10 +- poulpy-core/src/layouts/lwe_sk.rs | 10 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 10 +- poulpy-core/src/layouts/mod.rs | 12 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 4 +- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 22 ++-- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 6 +- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 4 +- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 24 ++-- poulpy-core/src/layouts/prepared/glwe_pk.rs | 18 +-- poulpy-core/src/layouts/prepared/glwe_sk.rs | 18 +-- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 4 +- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 4 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 4 +- .../benches/circuit_bootstrapping.rs | 4 +- .../examples/circuit_bootstrapping.rs | 4 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 2 +- .../ciphertexts/block_prepared.rs | 4 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 2 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 18 +-- .../src/tfhe/blind_rotation/cggi_algo.rs | 2 +- poulpy-schemes/src/tfhe/blind_rotation/key.rs | 24 ++-- .../src/tfhe/blind_rotation/key_compressed.rs | 10 +- .../src/tfhe/blind_rotation/key_prepared.rs | 10 +- .../tests/generic_blind_rotation.rs | 2 +- .../tests/circuit_bootstrapping.rs | 4 +- 57 files changed, 363 insertions(+), 314 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index fad8523..2849e3d 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,5 +1,5 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, RingDegree, TorusPrecision, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -28,7 +28,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct_in: TorusPrecision = p.k_ct_in; let k_ct_out: TorusPrecision = p.k_ct_out; @@ -137,7 +137,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe: TorusPrecision = p.k_ct; let k_ggsw: TorusPrecision = p.k_ggsw; diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 736b53c..fc1dc69 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, - GLWESwitchingKeyLayout, Rank, TorusPrecision, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + GLWESwitchingKeyLayout, Rank, RingDegree, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration}; @@ -29,7 +29,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe_in: TorusPrecision = p.k_ct_in; let k_glwe_out: TorusPrecision = p.k_ct_out; @@ -148,7 +148,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct: TorusPrecision = p.k_ct; let k_ksk: TorusPrecision = p.k_ksk; diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index d4b17b9..efd838e 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -2,7 +2,7 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ GLWEOperations, SIGMA, layouts::{ - Base2K, Degree, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, TorusPrecision, + Base2K, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -16,7 +16,7 @@ fn main() { // Ring degree let log_n: usize = 10; - let n: Degree = Degree(1 << log_n); + let n: RingDegree = RingDegree(1 << log_n); // Base-2-k (implicit digit decomposition) let base2k: Base2K = Base2K(14); diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 5c2c8c2..93519b9 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree, compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, prepared::GLWESecretPrepared, }, @@ -110,7 +110,7 @@ where ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); (0..sk_out.rank().into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 8c3a70f..ef9b5bf 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, }; impl GLWESwitchingKey> { @@ -85,7 +85,7 @@ impl GLWESwitchingKey { ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); (0..sk_out.rank().into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 672808b..62d1a15 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, RingDegree, TensorKey, prepared::GLWESecretPrepared, }; impl TensorKey> { @@ -23,7 +23,7 @@ impl TensorKey> { + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + module.bytes_of_vec_znx_big(1, 1) + module.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + + GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } @@ -64,7 +64,7 @@ impl TensorKey { assert_eq!(self.n(), sk.n()); } - let n: Degree = sk.n(); + let n: RingDegree = sk.n(); let rank: Rank = self.rank_out(); let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(n, rank); diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 1f1820a..2fd60ff 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -10,7 +10,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::GLWESecretPrepared, + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, RingDegree, + prepared::GLWESecretPrepared, }; impl LWESwitchingKey> { @@ -34,7 +35,7 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + GLWESecretPrepared::bytes_of(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 56a6701..041c7c4 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}; +use crate::layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank, RingDegree}; impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize @@ -22,7 +22,8 @@ impl LWEToGLWESwitchingKey> { Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + + GLWESecret::bytes_of(RingDegree(module.n() as u32), infos.rank_in()) } } diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 3ee7b75..8546ccb 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -1,22 +1,48 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, + api::ScratchAvailable, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; use crate::{ + ScratchTakeCore, keyswitching::glwe_ct::GLWEKeySwitch, - layouts::{prepared::LWESwitchingKeyPrepared, GGLWEInfos, GLWEAlloc, GLWELayout, GetDegree, LWEToRef, LWEInfos, Rank, TorusPrecision, GLWE, LWE}, + layouts::{ + GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, + prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, + }, }; +impl LWE> { + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: LWEInfos, + A: LWEInfos, + K: GGLWEInfos, + M: LWEKeySwitch, + { + module.lwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } +} + +impl LWE { + pub fn keyswitch(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch) + where + A: LWEToRef, + K: LWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: LWEKeySwitch, + { + module.lwe_keyswitch(self, a, ksk, scratch); + } +} + +impl LWEKeySwitch for Module where Self: LWEKeySwitch {} + pub trait LWEKeySwitch where Self: GLWEKeySwitch + GLWEAlloc, { - fn keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: LWEInfos, A: LWEInfos, @@ -25,14 +51,14 @@ where let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); let glwe_a_infos: GLWELayout = GLWELayout { - n: GetDegree::n(self), + n: self.ring_degree(), base2k: a_infos.base2k(), k: max_k, rank: Rank(1), }; let glwe_res_infos: GLWELayout = GLWELayout { - n: GetDegree::n(self), + n: self.ring_degree(), base2k: res_infos.base2k(), k: max_k, rank: Rank(1), @@ -45,39 +71,46 @@ where glwe_in + glwe_out + ks } - fn keyswitch( - &mut self, - module: &Module, - a: &A, - ksk: &K, - scratch: &mut Scratch, - ) where + fn lwe_keyswitch(&self, res: &mut R, a: &A, ksk: &K, scratch: &mut Scratch) + where + R: LWEToMut, A: LWEToRef, - DKs: DataRef, - Scratch: ScratchAvailable, + K: LWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - assert!(self.n() <= module.n() as u32); - assert!(a.n() <= module.n() as u32); - assert!(scratch.available() >= LWE::keyswitch_tmp_bytes(module, self, a, ksk)); + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let a: &LWE<&[u8]> = &a.to_ref(); + let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); - let max_k: TorusPrecision = self.k().max(a.k()); + assert!(res.n().as_usize() <= self.n()); + assert!(a.n().as_usize() <= self.n()); + assert_eq!(ksk.n(), self.n() as u32); + assert!(scratch.available() >= self.lwe_keyswitch_tmp_bytes(res, a, ksk)); + + let max_k: TorusPrecision = res.k().max(a.k()); let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWELayout { - n: ksk.n(), - base2k: a.base2k(), - k: max_k, - rank: Rank(1), - }); + let (mut glwe_in, scratch_1) = scratch.take_glwe_ct( + self, + &GLWELayout { + n: ksk.n(), + base2k: a.base2k(), + k: max_k, + rank: Rank(1), + }, + ); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWELayout { - n: ksk.n(), - base2k: self.base2k(), - k: max_k, - rank: Rank(1), - }); + let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct( + self, + &GLWELayout { + n: ksk.n(), + base2k: res.base2k(), + k: max_k, + rank: Rank(1), + }, + ); let n_lwe: usize = a.n().into(); @@ -87,13 +120,7 @@ where glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } - glwe_out.keyswitch(module, &glwe_in, &ksk.0, scratch_1); - self.sample_extract(&glwe_out); + self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); + res.sample_extract(&glwe_out); } } - -impl LWE> {} - -impl LWE { - -} diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 629e3f3..61ba79a 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -4,7 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + AutomorphismKey, AutomorphismKeyToMut, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, + TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -21,7 +22,7 @@ pub struct AutomorphismKeyCompressed { } impl LWEInfos for AutomorphismKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index f54f759..9216ec4 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -24,8 +24,8 @@ pub struct GGLWECompressed { } impl LWEInfos for GGLWECompressed { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -88,7 +88,7 @@ impl fmt::Display for GGLWECompressed { pub trait GGLWECompressedAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_gglwe_compressed( &self, @@ -115,7 +115,7 @@ where GGLWECompressed { data: MatZnx::alloc( - self.n().into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), 1, @@ -133,7 +133,7 @@ where where A: GGLWEInfos, { - assert_eq!(infos.n(), self.n()); + assert_eq!(infos.n(), self.ring_degree()); self.alloc_gglwe_compressed( infos.base2k(), infos.k(), @@ -160,7 +160,7 @@ where ); MatZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), 1, @@ -172,7 +172,7 @@ where where A: GGLWEInfos, { - assert_eq!(infos.n(), self.n()); + assert_eq!(infos.n(), self.ring_degree()); self.bytes_of_gglwe_compressed( infos.base2k(), infos.k(), @@ -183,7 +183,7 @@ where } } -impl GGLWECompressedAlloc for Module where Self: GetDegree {} +impl GGLWECompressedAlloc for Module where Self: GetRingDegree {} impl GGLWECompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index f59587b..fb963c5 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, - LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, LWEInfos, + Rank, RingDegree, TorusPrecision, compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -19,7 +19,7 @@ pub struct GLWESwitchingKeyCompressed { } impl LWEInfos for GLWESwitchingKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 7c20bba..ff2b8b8 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToMut, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -19,7 +19,7 @@ pub struct TensorKeyCompressed { } impl LWEInfos for TensorKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index fa1c46b..adad621 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -23,8 +23,8 @@ pub struct GGSWCompressed { } impl LWEInfos for GGSWCompressed { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -78,7 +78,7 @@ impl FillUniform for GGSWCompressed { pub trait GGSWCompressedAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_ggsw_compressed( &self, @@ -104,7 +104,7 @@ where GGSWCompressed { data: MatZnx::alloc( - self.n().into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), 1, @@ -147,7 +147,7 @@ where ); MatZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), 1, diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 8a5193f..e558cf7 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,7 +6,9 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; +use crate::layouts::{ + Base2K, GLWE, GLWEInfos, GLWEToMut, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -32,8 +34,8 @@ impl LWEInfos for GLWECompressed { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } impl GLWEInfos for GLWECompressed { @@ -70,11 +72,15 @@ impl FillUniform for GLWECompressed { pub trait GLWECompressedAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { GLWECompressed { - data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), + data: VecZnx::alloc( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, rank, @@ -86,24 +92,28 @@ where where A: GLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) } fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + VecZnx::bytes_of( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ) } fn bytes_of_glwe_compressed_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) } } -impl GLWECompressedAlloc for Module where Self: GetDegree {} +impl GLWECompressedAlloc for Module where Self: GetRingDegree {} impl GLWECompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -159,7 +169,7 @@ impl WriterTo for GLWECompressed { pub trait GLWEDecompress where - Self: GetDegree + VecZnxFillUniform + VecZnxCopy, + Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy, { fn decompress_glwe(&self, res: &mut R, other: &O) where @@ -171,10 +181,10 @@ where let other: &GLWECompressed<&[u8]> = &other.to_ref(); assert_eq!( res.n(), - self.n(), + self.ring_degree(), "invalid receiver: res.n()={} != other.n()={}", res.n(), - self.n() + self.ring_degree() ); assert_eq!(res.lwe_layout(), other.lwe_layout()); @@ -193,7 +203,7 @@ where } } -impl GLWEDecompress for Module where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} +impl GLWEDecompress for Module where Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy {} impl GLWE { pub fn decompress(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index afdb493..5bfa3eb 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, RingDegree, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, @@ -26,7 +26,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index c21a6dd..0393758 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision}; +use crate::layouts::{Base2K, LWE, LWEInfos, LWEToMut, RingDegree, TorusPrecision}; #[derive(PartialEq, Eq, Clone)] pub struct LWECompressed { @@ -28,8 +28,8 @@ impl LWEInfos for LWECompressed { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 7e1b0cf..cf6ae9a 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -4,7 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, RingDegree, + TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -24,7 +25,7 @@ impl LWEInfos for LWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 5545518..33fcf49 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, RingDegree, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, @@ -17,7 +17,7 @@ use std::fmt; pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 2c99bd2..eb93bf4 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -13,7 +13,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct AutomorphismKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -34,7 +34,7 @@ impl AutomorphismKey { } impl LWEInfos for AutomorphismKey { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } @@ -84,7 +84,7 @@ impl LWEInfos for AutomorphismKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 532a65a..3b95d63 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -37,7 +37,7 @@ pub trait SetGGLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWELayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -55,7 +55,7 @@ impl LWEInfos for GGLWELayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -101,8 +101,8 @@ impl LWEInfos for GGLWE { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -162,7 +162,7 @@ impl fmt::Display for GGLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGLWECiphertext: k={} base2k={} dsize={}) {}", + "(GGLWE: k={} base2k={} dsize={}) {}", self.k().0, self.base2k().0, self.dsize().0, @@ -193,7 +193,7 @@ impl GGLWE { pub trait GGLWEAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_gglwe( &self, @@ -220,7 +220,7 @@ where GGLWE { data: MatZnx::alloc( - self.n().into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -270,7 +270,7 @@ where ); MatZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -293,7 +293,7 @@ where } } -impl GGLWEAlloc for Module where Self: GetDegree {} +impl GGLWEAlloc for Module where Self: GetRingDegree {} impl GGLWE> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index b261230..ddb4de7 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, + Base2K, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -13,7 +13,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESwitchingKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -23,7 +23,7 @@ pub struct GLWESwitchingKeyLayout { } impl LWEInfos for GLWESwitchingKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -98,7 +98,7 @@ impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { } impl LWEInfos for GLWESwitchingKey { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 10523b1..6ef67b8 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -13,7 +13,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct TensorKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -27,7 +27,7 @@ pub struct TensorKey { } impl LWEInfos for TensorKey { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } @@ -69,7 +69,7 @@ impl GGLWEInfos for TensorKey { } impl LWEInfos for TensorKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index 494d06a..aac4a20 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use std::fmt; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; pub trait GGSWInfos where @@ -28,7 +28,7 @@ where #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGSWLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -45,7 +45,7 @@ impl LWEInfos for GGSWLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -74,8 +74,8 @@ pub struct GGSW { } impl LWEInfos for GGSW { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -152,11 +152,11 @@ impl GGSW { } } -impl GGSWAlloc for Module where Self: GetDegree {} +impl GGSWAlloc for Module where Self: GetRingDegree {} pub trait GGSWAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { let size: usize = k.0.div_ceil(base2k.0) as usize; @@ -175,7 +175,7 @@ where GGSW { data: MatZnx::alloc( - self.n().into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -216,7 +216,7 @@ where ); MatZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 328b1ff..2361e3c 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -32,14 +32,14 @@ pub trait SetGLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWELayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } impl LWEInfos for GLWELayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -96,8 +96,8 @@ impl LWEInfos for GLWE { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -148,12 +148,12 @@ impl FillUniform for GLWE { pub trait GLWEAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { GLWE { data: VecZnx::alloc( - self.n().into(), + self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ), @@ -171,7 +171,7 @@ where fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { VecZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) @@ -185,7 +185,7 @@ where } } -impl GLWEAlloc for Module where Self: GetDegree {} +impl GLWEAlloc for Module where Self: GetRingDegree {} impl GLWE> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 0b0d058..16fc6ea 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -4,7 +4,7 @@ use poulpy_hal::layouts::{ use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -18,7 +18,7 @@ pub struct GLWEPublicKey { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -43,8 +43,8 @@ impl LWEInfos for GLWEPublicKey { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -67,7 +67,7 @@ impl LWEInfos for GLWEPublicKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -84,12 +84,12 @@ impl GLWEInfos for GLWEPublicKeyLayout { pub trait GLWEPublicKeyAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { GLWEPublicKey { data: VecZnx::alloc( - self.n().into(), + self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ), @@ -108,7 +108,7 @@ where fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { VecZnx::bytes_of( - self.n().into(), + self.ring_degree().into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) @@ -122,7 +122,7 @@ where } } -impl GLWEPublicKeyAlloc for Module where Self: GetDegree {} +impl GLWEPublicKeyAlloc for Module where Self: GetRingDegree {} impl GLWEPublicKey> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index e7ae403..3d5ce86 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -3,12 +3,12 @@ use std::fmt; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, + Base2K, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, } @@ -22,7 +22,7 @@ impl LWEInfos for GLWEPlaintextLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -62,8 +62,8 @@ impl LWEInfos for GLWEPlaintext { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } @@ -87,11 +87,15 @@ impl fmt::Display for GLWEPlaintext { pub trait GLWEPlaintextAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { GLWEPlaintext { - data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), + data: VecZnx::alloc( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, } @@ -105,7 +109,11 @@ where } fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + VecZnx::bytes_of( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ) } fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize @@ -116,7 +124,7 @@ where } } -impl GLWEPlaintextAlloc for Module where Self: GetDegree {} +impl GLWEPlaintextAlloc for Module where Self: GetRingDegree {} impl GLWEPlaintext> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 65caa70..9166388 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -8,12 +8,12 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESecretLayout { - pub n: Degree, + pub n: RingDegree, pub rank: Rank, } @@ -26,7 +26,7 @@ impl LWEInfos for GLWESecretLayout { TorusPrecision(0) } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -55,8 +55,8 @@ impl LWEInfos for GLWESecret { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -72,11 +72,11 @@ impl GLWEInfos for GLWESecret { pub trait GLWESecretAlloc where - Self: GetDegree, + Self: GetRingDegree, { fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { GLWESecret { - data: ScalarZnx::alloc(self.n().into(), rank.into()), + data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), dist: Distribution::NONE, } } @@ -89,7 +89,7 @@ where } fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { - ScalarZnx::bytes_of(self.n().into(), rank.into()) + ScalarZnx::bytes_of(self.ring_degree().into(), rank.into()) } fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize @@ -100,7 +100,7 @@ where } } -impl GLWESecretAlloc for Module where Self: GetDegree {} +impl GLWESecretAlloc for Module where Self: GetRingDegree {} impl GLWESecret> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 3aea241..671f018 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,15 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEToLWEKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -20,7 +20,7 @@ pub struct GLWEToLWEKeyLayout { } impl LWEInfos for GLWEToLWEKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -70,7 +70,7 @@ impl LWEInfos for GLWEToLWESwitchingKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index aed2807..0c8831f 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, TorusPrecision}; +use crate::layouts::{Base2K, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { - fn n(&self) -> Degree; + fn n(&self) -> RingDegree; fn k(&self) -> TorusPrecision; fn max_k(&self) -> TorusPrecision { TorusPrecision(self.k().0 * self.size() as u32) @@ -34,7 +34,7 @@ pub trait SetLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWELayout { - pub n: Degree, + pub n: RingDegree, pub k: TorusPrecision, pub base2k: Base2K, } @@ -48,7 +48,7 @@ impl LWEInfos for LWELayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -67,8 +67,8 @@ impl LWEInfos for LWE { fn k(&self) -> TorusPrecision { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32 - 1) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32 - 1) } fn size(&self) -> usize { @@ -126,7 +126,7 @@ where } pub trait LWEAlloc { - fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { + fn alloc_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> LWE> { LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, @@ -141,7 +141,7 @@ pub trait LWEAlloc { self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + fn bytes_of_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } @@ -164,7 +164,7 @@ impl LWE> { module.alloc_lwe_from_infos(infos) } - pub fn alloc(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self + pub fn alloc(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> Self where M: LWEAlloc, { @@ -179,7 +179,7 @@ impl LWE> { module.bytes_of_lwe_from_infos(infos) } - pub fn bytes_of(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize where M: LWEAlloc, { diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index e449da6..2ff0391 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -6,20 +6,20 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, } impl LWEInfos for LWESwitchingKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -68,7 +68,7 @@ impl LWEInfos for LWESwitchingKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index 01ae19b..6ffd650 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -2,7 +2,7 @@ use std::fmt; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; -use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; +use crate::layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEPlaintextLayout { @@ -19,8 +19,8 @@ impl LWEInfos for LWEPlaintextLayout { self.k } - fn n(&self) -> Degree { - Degree(0) + fn n(&self) -> RingDegree { + RingDegree(0) } fn size(&self) -> usize { @@ -43,8 +43,8 @@ impl LWEInfos for LWEPlaintext { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32 - 1) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32 - 1) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index b5f019f..d593ad5 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, Degree, LWEInfos, TorusPrecision}, + layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}, }; pub struct LWESecret { @@ -14,7 +14,7 @@ pub struct LWESecret { } pub trait LWESecretAlloc { - fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { + fn alloc_lwe_secret(&self, n: RingDegree) -> LWESecret> { LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, @@ -25,7 +25,7 @@ pub trait LWESecretAlloc { impl LWESecretAlloc for Module {} impl LWESecret> { - pub fn alloc(module: &M, n: Degree) -> Self + pub fn alloc(module: &M, n: RingDegree) -> Self where M: LWESecretAlloc, { @@ -55,8 +55,8 @@ impl LWEInfos for LWESecret { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 1427447..72b7514 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,13 +6,13 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEToGLWESwitchingKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_out: Rank, @@ -28,7 +28,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -69,7 +69,7 @@ impl LWEInfos for LWEToGLWESwitchingKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 1d8e30d..09064f3 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -35,12 +35,12 @@ pub use lwe_to_glwe_ksk::*; use poulpy_hal::layouts::{Backend, Module}; -pub trait GetDegree { - fn n(&self) -> Degree; +pub trait GetRingDegree { + fn ring_degree(&self) -> RingDegree; } -impl GetDegree for Module { - fn n(&self) -> Degree { +impl GetRingDegree for Module { + fn ring_degree(&self) -> RingDegree { Self::n(&self).into() } } @@ -202,14 +202,14 @@ macro_rules! newtype_u32 { }; } -newtype_u32!(Degree); +newtype_u32!(RingDegree); newtype_u32!(TorusPrecision); newtype_u32!(Base2K); newtype_u32!(Dnum); newtype_u32!(Rank); newtype_u32!(Dsize); -impl Degree { +impl RingDegree { pub fn log2(&self) -> usize { let n: usize = self.0 as usize; (usize::BITS - (n - 1).leading_zeros()) as _ diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index d09b2e6..7c8d0b9 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + AutomorphismKeyToRef, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -15,7 +15,7 @@ pub struct AutomorphismKeyPrepared { } impl LWEInfos for AutomorphismKeyPrepared { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 180393a..fd6033a 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -16,8 +16,8 @@ pub struct GGLWEPrepared { } impl LWEInfos for GGLWEPrepared { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -59,7 +59,7 @@ impl GGLWEInfos for GGLWEPrepared { pub trait GGLWEPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_gglwe_prepared( &self, @@ -96,7 +96,7 @@ where where A: GGLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.alloc_gglwe_prepared( infos.base2k(), infos.k(), @@ -137,7 +137,7 @@ where where A: GGLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.bytes_of_gglwe_prepared( infos.base2k(), infos.k(), @@ -149,7 +149,7 @@ where } } -impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGLWEPreparedAlloc for Module where Module: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGLWEPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -201,7 +201,7 @@ impl GGLWEPrepared, B> { pub trait GGLWEPrepare where - Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, + Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, { fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize where @@ -223,8 +223,8 @@ where let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut(); let other: GGLWE<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n()); - assert_eq!(other.n(), self.n()); + 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); @@ -233,7 +233,7 @@ where } } -impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} +impl GGLWEPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} impl GGLWEPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index d8ab0ca..2154b3e 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,8 +1,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, - GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, + LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; @@ -34,7 +34,7 @@ impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPre } impl LWEInfos for GLWESwitchingKeyPrepared { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.key.n() } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 8357e40..2d054c1 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToRef, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -14,7 +14,7 @@ pub struct TensorKeyPrepared { } impl LWEInfos for TensorKeyPrepared { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index f8f85c9..6365818 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -16,8 +16,8 @@ pub struct GGSWPrepared { } impl LWEInfos for GGSWPrepared { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -51,7 +51,7 @@ impl GGSWInfos for GGSWPrepared { pub trait GGSWPreparedAlloc where - Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_ggsw_prepared( &self, @@ -92,7 +92,7 @@ where where A: GGSWInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.alloc_ggsw_prepared( infos.base2k(), infos.k(), @@ -124,7 +124,7 @@ where where A: GGSWInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.bytes_of_ggsw_prepared( infos.base2k(), infos.k(), @@ -135,7 +135,7 @@ where } } -impl GGSWPreparedAlloc for Module where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGSWPreparedAlloc for Module where Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGSWPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -177,13 +177,13 @@ impl GGSWPrepared { pub trait GGSWPrepare where - Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, + Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), @@ -198,8 +198,8 @@ where { let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); let other: GGSW<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n()); - assert_eq!(other.n(), self.n()); + 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); @@ -207,7 +207,7 @@ where } } -impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} +impl GGSWPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} impl GGSWPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 8aa0a59..bca1826 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -5,7 +5,9 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, + layouts::{ + Base2K, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + }, }; #[derive(PartialEq, Eq)] @@ -39,8 +41,8 @@ impl LWEInfos for GLWEPublicKeyPrepared { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } @@ -52,7 +54,7 @@ impl GLWEInfos for GLWEPublicKeyPrepared { pub trait GLWEPublicKeyPreparedAlloc where - Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf, + Self: GetRingDegree + VecZnxDftAlloc + VecZnxDftBytesOf, { fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { @@ -118,7 +120,7 @@ impl GLWEPublicKeyPrepared, B> { pub trait GLWEPublicKeyPrepare where - Self: GetDegree + VecZnxDftApply, + Self: GetRingDegree + VecZnxDftApply, { fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where @@ -129,8 +131,8 @@ where let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); let other: GLWEPublicKey<&[u8]> = other.to_ref(); - assert_eq!(res.n(), self.n()); - assert_eq!(other.n(), self.n()); + 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()); @@ -144,7 +146,7 @@ where } } -impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} +impl GLWEPublicKeyPrepare for Module where Self: GetRingDegree + VecZnxDftApply {} impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 9e3f2ef..ac7e11f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, + Base2K, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::SetDist, }, }; @@ -31,8 +31,8 @@ impl LWEInfos for GLWESecretPrepared { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -47,7 +47,7 @@ impl GLWEInfos for GLWESecretPrepared { pub trait GLWESecretPreparedAlloc where - Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc, + Self: GetRingDegree + SvpPPolBytesOf + SvpPPolAlloc, { fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { @@ -59,7 +59,7 @@ where where A: GLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.alloc_glwe_secret_prepared(infos.rank()) } @@ -70,12 +70,12 @@ where where A: GLWEInfos, { - assert_eq!(self.n(), infos.n()); + assert_eq!(self.ring_degree(), infos.n()); self.bytes_of_glwe_secret(infos.rank()) } } -impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc {} +impl GLWESecretPreparedAlloc for Module where Self: GetRingDegree + SvpPPolBytesOf + SvpPPolAlloc {} impl GLWESecretPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -110,8 +110,8 @@ impl GLWESecretPrepared, B> { } impl GLWESecretPrepared { - pub fn n(&self) -> Degree { - Degree(self.data.n() as u32) + pub fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } pub fn rank(&self) -> Rank { diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 63dec0b..55a7bf9 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -20,7 +20,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 49914f2..a857bf9 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -20,7 +20,7 @@ impl LWEInfos for LWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 8a2242d..5df692e 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -21,7 +21,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index a1f39c6..c2ce4ad 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -188,8 +188,8 @@ where // Scratch space (4MB) let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let n_glwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_glwe(); - let n_lwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_lwe(); + let n_glwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_glwe(); + let n_lwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_lwe(); let rank: poulpy_core::layouts::Rank = params.cbt_infos.layout_brk.rank; let module: Module = Module::::new(n_glwe.as_u32() as u64); diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 3fd8c6e..3383114 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,8 +1,8 @@ use poulpy_core::{ GLWEOperations, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWELayout, - LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, + AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, + LWEPlaintext, LWESecret, TensorKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 70bd910..de4e30d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -38,7 +38,7 @@ impl LWEInfos for FheUintBlocks { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index c0cd09f..2f4bc44 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -225,7 +225,7 @@ impl LWEInfos for FheUintBlocksPrep< self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } @@ -256,7 +256,7 @@ impl LWEInfos for FheUintBlocksPrepDebug { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index a3a779a..38ea57d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -87,7 +87,7 @@ impl LWEInfos for FheUintWord { self.0.k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.0.n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 17851cc..924d53a 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,7 +1,7 @@ #[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, - TensorKeyLayout, TorusPrecision, + AutomorphismKeyLayout, Base2K, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, RingDegree, TensorKeyLayout, + TorusPrecision, }; #[cfg(test)] @@ -26,7 +26,7 @@ pub(crate) const TEST_RANK: u32 = 2; #[cfg(test)] pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GLWE), rank: Rank(TEST_RANK), @@ -34,7 +34,7 @@ pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { #[cfg(test)] pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GGSW), rank: Rank(TEST_RANK), @@ -46,15 +46,15 @@ pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { cbt: CircuitBootstrappingKeyLayout { layout_brk: BlindRotationKeyLayout { - n_glwe: Degree(TEST_N_GLWE), - n_lwe: Degree(TEST_N_LWE), + n_glwe: RingDegree(TEST_N_GLWE), + n_lwe: RingDegree(TEST_N_LWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), dnum: Dnum(3), rank: Rank(TEST_RANK), }, layout_atk: AutomorphismKeyLayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), @@ -62,7 +62,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dsize: Dsize(1), }, layout_tsk: TensorKeyLayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), @@ -71,7 +71,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { }, }, ks: GLWEToLWEKeyLayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(39), rank_in: Rank(TEST_RANK), diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 7aad6b9..8bf0a9e 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use poulpy_core::{ Distribution, GLWEOperations, TakeGLWE, - layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWEToRef, LWEInfos}, + layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWEInfos, LWEToRef}, }; use crate::tfhe::blind_rotation::{ diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/key.rs index d9bb0a9..276539f 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, RingDegree, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -19,8 +19,8 @@ use crate::tfhe::blind_rotation::BlindRotationAlgo; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct BlindRotationKeyLayout { - pub n_glwe: Degree, - pub n_lwe: Degree, + pub n_glwe: RingDegree, + pub n_lwe: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, @@ -28,11 +28,11 @@ pub struct BlindRotationKeyLayout { } impl BlindRotationKeyInfos for BlindRotationKeyLayout { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n_glwe } - fn n_lwe(&self) -> Degree { + fn n_lwe(&self) -> RingDegree { self.n_lwe } } @@ -62,7 +62,7 @@ impl LWEInfos for BlindRotationKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n_glwe } } @@ -71,8 +71,8 @@ pub trait BlindRotationKeyInfos where Self: GGSWInfos, { - fn n_glwe(&self) -> Degree; - fn n_lwe(&self) -> Degree; + fn n_glwe(&self) -> RingDegree; + fn n_lwe(&self) -> RingDegree; } pub trait BlindRotationKeyAlloc { @@ -178,12 +178,12 @@ impl WriterTo for BlindRotationKey { } impl BlindRotationKeyInfos for BlindRotationKey { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.keys.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.keys.len() as u32) } } @@ -206,7 +206,7 @@ impl LWEInfos for BlindRotationKey { self.keys[0].k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs index 784d332..af7e40a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, - layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed}, + layouts::{Base2K, Dsize, GGSWInfos, GLWEInfos, LWEInfos, RingDegree, TorusPrecision, compressed::GGSWCompressed}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; @@ -94,17 +94,17 @@ impl WriterTo for BlindRotationKeyCompressed } impl BlindRotationKeyInfos for BlindRotationKeyCompressed { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.keys.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.keys.len() as u32) } } impl LWEInfos for BlindRotationKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs index 2001083..1f99ef5 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -8,7 +8,7 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{GGSWPrepared, Prepare, PrepareAlloc}, }, }; @@ -30,12 +30,12 @@ pub struct BlindRotationKeyPrepared } impl BlindRotationKeyInfos for BlindRotationKeyPrepared { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.data.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.data.len() as u32) } } @@ -48,7 +48,7 @@ impl LWEInfos for BlindRotationKeyP self.data[0].k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.data[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 409b685..4b8131a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -23,7 +23,7 @@ use crate::tfhe::blind_rotation::{ }; use poulpy_core::layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWELayout, LWEToRef, LWEInfos, LWEPlaintext, LWESecret, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, LWEPlaintext, LWESecret, LWEToRef, prepared::{GLWESecretPrepared, PrepareAlloc}, }; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 9d5f898..cfe238f 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -31,9 +31,7 @@ use crate::tfhe::{ }, }; -use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc, -}; +use poulpy_core::layouts::{AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc}; use poulpy_core::layouts::{ GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, From 2f2c7aef0075a8b3cee59b8d6211409b1058feeb Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Wed, 15 Oct 2025 18:36:59 -0400 Subject: [PATCH 16/60] glwe and lwe decryption --- poulpy-core/src/decryption/glwe_ct.rs | 129 ++++++++++++++++++-------- poulpy-core/src/decryption/lwe_ct.rs | 56 +++++++---- 2 files changed, 132 insertions(+), 53 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 4306d33..d0ff213 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,80 +1,135 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + ModuleN, ScratchTakeBasic, + SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxBigBytesOf, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, - layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, + layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, }; -use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::{ + layouts::{ + GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, GLWEToMut, GLWEPlaintextToMut, + prepared::{GLWESecretPreparedToRef, GLWESecretPrepared}, + } +}; impl GLWE> { - pub fn decrypt_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn decrypt_tmp_bytes(module: &M, a_infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GLWEDecryption, { - let size: usize = infos.size(); - (module.vec_znx_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_dft(1, size) + module.glwe_decrypt_tmp_bytes(a_infos) } } -impl GLWE { - pub fn decrypt( - &self, - module: &Module, - pt: &mut GLWEPlaintext, - sk: &GLWESecretPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch:, +impl GLWE { + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) + where + P: GLWEPlaintextToMut, + S: GLWESecretPreparedToRef, + M: GLWEDecryption, + Scratch: ScratchTakeBasic, { + module.glwe_decrypt(self, pt, sk, scratch); + } +} + +pub trait GLWEDecryption +where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxBigAddInplace + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize +{ + fn glwe_decrypt_tmp_bytes(&self, infos: &A) -> usize + where + A: GLWEInfos + { + let size: usize = infos.size(); + (self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size) + } + + fn glwe_decrypt( + &self, + res: &mut R, + pt: &mut P, + sk: &S, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + P: GLWEPlaintextToMut, + S: GLWESecretPreparedToRef, + Scratch: ScratchTakeBasic, + { + + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk.rank()); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), sk.n()); assert_eq!(pt.n(), sk.n()); } - let cols: usize = (self.rank() + 1).into(); + let cols: usize = (res.rank() + 1).into(); - let (mut c0_big, scratch_1) = scratch.take_vec_znx_big(self.n().into(), 1, self.size()); // TODO optimize size when pt << ct + let (mut c0_big, scratch_1) = scratch.take_vec_znx_big(self, 1, res.size()); // TODO optimize size when pt << ct c0_big.data_mut().fill(0); { (1..cols).for_each(|i| { // ci_dft = DFT(a[i]) * DFT(s[i]) - let (mut ci_dft, _) = scratch_1.take_vec_znx_dft(self.n().into(), 1, self.size()); // TODO optimize size when pt << ct - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &self.data, i); - module.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); - let ci_big = module.vec_znx_idft_apply_consume(ci_dft); + let (mut ci_dft, _) = scratch_1.take_vec_znx_dft(self, 1, res.size()); // TODO optimize size when pt << ct + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &res.data, i); + self.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); + let ci_big = self.vec_znx_idft_apply_consume(ci_dft); // c0_big += a[i] * s[i] - module.vec_znx_big_add_inplace(&mut c0_big, 0, &ci_big, 0); + self.vec_znx_big_add_inplace(&mut c0_big, 0, &ci_big, 0); }); } // c0_big = (a * s) + (-a * s + m + e) = BIG(m + e) - module.vec_znx_big_add_small_inplace(&mut c0_big, 0, &self.data, 0); + self.vec_znx_big_add_small_inplace(&mut c0_big, 0, &res.data, 0); // pt = norm(BIG(m + e)) - module.vec_znx_big_normalize( - self.base2k().into(), + self.vec_znx_big_normalize( + res.base2k().into(), &mut pt.data, 0, - self.base2k().into(), + res.base2k().into(), &c0_big, 0, scratch_1, ); - pt.base2k = self.base2k(); - pt.k = pt.k().min(self.k()); + pt.base2k = res.base2k(); + pt.k = pt.k().min(res.k()); } + } + +impl GLWEDecryption for Module where + Self: ModuleN + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxBigAddInplace + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize +{ +} \ No newline at end of file diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index ade21e3..1042b72 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,40 +4,64 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToMut, LWESecretToRef}; -impl LWE -where - DataSelf: DataRef, +impl LWE { - pub fn decrypt(&self, module: &Module, pt: &mut LWEPlaintext, sk: &LWESecret) + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: S) where - DataPt: DataMut, - DataSk: DataRef, - Module: ZnNormalizeInplace, + P: LWEPlaintextToMut, + S: LWESecretToRef, + M: LWEDecrypt, B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { + module.lwe_decrypt(self, pt, sk); + } +} + +pub trait LWEDecrypt +where + Self: Sized + ZnNormalizeInplace +{ + fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: S) + where + R: LWEToMut, + P: LWEPlaintextToMut, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + { + + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); + let sk: LWESecret<&[u8]> = sk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), sk.n()); } - (0..pt.size().min(self.size())).for_each(|i| { - pt.data.at_mut(0, i)[0] = self.data.at(0, i)[0] - + self.data.at(0, i)[1..] + (0..pt.size().min(res.size())).for_each(|i| { + pt.data.at_mut(0, i)[0] = res.data.at(0, i)[0] + + res.data.at(0, i)[1..] .iter() .zip(sk.data.at(0, 0)) .map(|(x, y)| x * y) .sum::(); }); - module.zn_normalize_inplace( + self.zn_normalize_inplace( 1, - self.base2k().into(), + res.base2k().into(), &mut pt.data, 0, ScratchOwned::alloc(size_of::()).borrow(), ); - pt.base2k = self.base2k(); - pt.k = crate::layouts::TorusPrecision(self.k().0.min(pt.size() as u32 * self.base2k().0)); + pt.base2k = res.base2k(); + pt.k = crate::layouts::TorusPrecision(res.k().0.min(pt.size() as u32 * res.base2k().0)); } } + +impl LWEDecrypt for Module where + Self: Sized + ZnNormalizeInplace +{ + +} \ No newline at end of file From 9f35a92a334a01dac4d21fa977addbaa09890cdf Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Wed, 15 Oct 2025 21:31:48 -0400 Subject: [PATCH 17/60] wip all of Encryption --- .../src/encryption/compressed/gglwe_atk.rs | 19 +- .../src/encryption/compressed/gglwe_ct.rs | 10 +- .../src/encryption/compressed/gglwe_ksk.rs | 19 +- .../src/encryption/compressed/gglwe_tsk.rs | 43 ++-- .../src/encryption/compressed/ggsw_ct.rs | 9 +- poulpy-core/src/encryption/gglwe_atk.rs | 22 +- poulpy-core/src/encryption/gglwe_ct.rs | 10 +- poulpy-core/src/encryption/gglwe_ksk.rs | 24 ++- poulpy-core/src/encryption/gglwe_tsk.rs | 39 ++-- poulpy-core/src/encryption/ggsw_ct.rs | 10 +- poulpy-core/src/encryption/glwe_ct.rs | 192 +++++++++--------- poulpy-core/src/encryption/glwe_pk.rs | 2 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 24 ++- poulpy-core/src/encryption/lwe_ct.rs | 78 ++++--- poulpy-core/src/encryption/lwe_ksk.rs | 25 ++- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 21 +- 16 files changed, 316 insertions(+), 231 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index f0afcae..1ee58a6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,10 +1,11 @@ use poulpy_hal::{ - api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ + ScratchTakeCore, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, @@ -16,10 +17,10 @@ impl AutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out()) } } @@ -39,8 +40,14 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk { impl GGLWEAutomorphismKeyCompressedEncryptSk for Module where - Module: GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxAutomorphism, - Scratch: ScratchAvailable, + Module: ModuleN + + GGLWEKeyCompressedEncryptSk + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + SvpPPolBytesOf + + VecZnxAutomorphism + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, { fn gglwe_automorphism_key_compressed_encrypt_sk( &self, @@ -70,7 +77,7 @@ where ) } - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); + let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { (0..res.rank_out().into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index b67dc88..cca081f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, + ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, ZnNormalizeInplace, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, @@ -8,6 +8,7 @@ use poulpy_hal::{ }; use crate::{ + ScratchTakeCore, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, @@ -60,13 +61,14 @@ pub trait GGLWECompressedEncryptSk { impl GGLWECompressedEncryptSk for Module where - Module: GLWEEncryptSkInternal + Module: ModuleN + + GLWEEncryptSkInternal + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + ZnNormalizeInplace, - Scratch: ScratchAvailable, + Scratch: ScratchAvailable + ScratchTakeCore, { fn gglwe_compressed_encrypt_sk( &self, @@ -130,7 +132,7 @@ where let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res); (0..rank_in).for_each(|col_i| { (0..dnum).for_each(|d_i| { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 93519b9..3dca15f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,10 +1,11 @@ use poulpy_hal::{ - api::{ScratchAvailable, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ + ScratchTakeCore, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree, @@ -17,7 +18,7 @@ impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) @@ -59,13 +60,15 @@ pub trait GGLWEKeyCompressedEncryptSk { impl GGLWEKeyCompressedEncryptSk for Module where - Module: GGLWECompressedEncryptSk + Module: ModuleN + + GGLWECompressedEncryptSk + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxSwitchRing - + SvpPrepare, - Scratch: ScratchAvailable, + + SvpPrepare + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, { fn gglwe_key_compressed_encrypt_sk( &self, @@ -100,7 +103,7 @@ where let n: usize = sk_in.n().max(sk_out.n()).into(); - let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into()); + let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); (0..sk_in.rank().into()).for_each(|i| { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), @@ -110,9 +113,9 @@ where ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { - let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); + let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); (0..sk_out.rank().into()).for_each(|i| { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 2beaa4b..f3086df 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,16 +1,18 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, + oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ + ScratchTakeCore, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -19,7 +21,7 @@ impl TensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { TensorKey::encrypt_sk_tmp_bytes(module, infos) } @@ -35,18 +37,25 @@ pub trait GGLWETensorKeyCompressedEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef; + S: GLWESecretToRef + GetDist; } impl GGLWETensorKeyCompressedEncryptSk for Module where - Module: GGLWEKeyCompressedEncryptSk + Module: ModuleN + + GGLWEKeyCompressedEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxBigNormalize - + SvpPrepare, - Scratch:, + + SvpPrepare + + SvpPPolAllocBytesImpl + + SvpPPolBytesOf + + VecZnxDftAllocBytesImpl + + VecZnxBigAllocBytesImpl + + VecZnxDftBytesOf + + VecZnxBigBytesOf, + Scratch: ScratchTakeBasic + ScratchTakeCore, { fn gglwe_tensor_key_encrypt_sk( &self, @@ -57,9 +66,13 @@ where scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef, + S: GLWESecretToRef + GetDist, { let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); + + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out()); + sk_dft_prep.prepare(self, sk); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -68,21 +81,18 @@ where assert_eq!(res.n(), sk.n()); } - let n: usize = sk.n().into(); + // let n: usize = sk.n().into(); let rank: usize = res.rank_out().into(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), res.rank_out()); - sk_dft_prep.prepare(self, sk, scratch_1); - - let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1); + let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank, 1); for i in 0..rank { self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); } - let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1); - let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(sk.n(), Rank(1)); - let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n, 1, 1); + let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1); + let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1)); + let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1); let mut source_xa: Source = Source::new(seed_xa); @@ -125,6 +135,7 @@ impl TensorKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where + GLWESecret: GetDist, Module: GGLWETensorKeyCompressedEncryptSk, { module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 567f04f..6595e15 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,10 +1,11 @@ use poulpy_hal::{ - api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ + ScratchTakeCore, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, @@ -40,8 +41,8 @@ pub trait GGSWCompressedEncryptSk { impl GGSWCompressedEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch:, + Module: ModuleN + GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: ScratchTakeCore, { fn ggsw_compressed_encrypt_sk( &self, @@ -74,7 +75,7 @@ where let cols: usize = rank + 1; let dsize: usize = res.dsize().into(); - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); let mut source = Source::new(seed_xa); diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 6536c7e..972e06c 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, @@ -9,15 +9,18 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, +use crate::{ + ScratchTakeCore, + layouts::{ + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, + }, }; impl AutomorphismKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, { assert_eq!( infos.rank_in(), @@ -76,7 +79,8 @@ where impl GGLWEAutomorphismKeyEncryptSk for Module where - Module: VecZnxAddScalarInplace + Module: ModuleN + + VecZnxAddScalarInplace + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply @@ -93,8 +97,10 @@ where + SvpPrepare + VecZnxSwitchRing + SvpPPolBytesOf - + VecZnxAutomorphism, - Scratch: ScratchAvailable, + + VecZnxAutomorphism + + SvpPPolAlloc + + SvpPPolBytesOf, + Scratch: ScratchAvailable + ScratchTakeCore, { fn gglwe_automorphism_key_encrypt_sk( &self, @@ -126,7 +132,7 @@ where ) } - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); + let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { (0..res.rank_out().into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index d333892..d0ab3a0 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - encryption::glwe_ct::GLWEEncryptSk, + encryption::glwe_ct::GLWEEncryptSk, layouts::GLWEInfos, ScratchTakeCore, layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, @@ -47,8 +47,8 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: ScratchAvailable, + Module: ModuleN + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: ScratchAvailable + ScratchTakeCore, { fn gglwe_encrypt_sk( &self, @@ -111,7 +111,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(res); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns // // Example for ksk rank 2 to rank 3: diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index ef9b5bf..c825c73 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,22 +1,26 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchTakeBasic, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; -use crate::layouts::{ +use crate::{ + ScratchTakeCore, + layouts::{ GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, + }, }; impl GLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, { (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) @@ -42,7 +46,8 @@ impl GLWESwitchingKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace + Module: ModuleN + + VecZnxAddScalarInplace + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply @@ -58,8 +63,9 @@ impl GLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolBytesOf, - Scratch: ScratchAvailable, + + SvpPPolBytesOf + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -75,7 +81,7 @@ impl GLWESwitchingKey { let n: usize = sk_in.n().max(sk_out.n()).into(); - let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into()); + let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into()); (0..sk_in.rank().into()).for_each(|i| { module.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), @@ -85,9 +91,9 @@ impl GLWESwitchingKey { ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank()); { - let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); + let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1); (0..sk_out.rank().into()).for_each(|i| { module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 62d1a15..918eb8a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,29 +1,33 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, + oep::VecZnxBigAllocBytesImpl, source::Source, }; -use crate::layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, RingDegree, TensorKey, prepared::GLWESecretPrepared, +use crate::{ + ScratchTakeCore, + layouts::{ + GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, + }, }; impl TensorKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { GLWESecretPrepared::bytes_of(module, infos.rank_out()) + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + module.bytes_of_vec_znx_big(1, 1) + module.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + + GLWESecret::bytes_of(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } @@ -37,7 +41,9 @@ impl TensorKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpApplyDftToDft + GLWESecret: GetDist, + Module: ModuleN + + SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxDftBytesOf @@ -55,8 +61,11 @@ impl TensorKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolBytesOf, - Scratch:, + + SvpPPolBytesOf + + VecZnxBigAllocBytesImpl + + VecZnxBigBytesOf + + SvpPPolAlloc, + Scratch: ScratchTakeBasic + ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -64,21 +73,21 @@ impl TensorKey { assert_eq!(self.n(), sk.n()); } - let n: RingDegree = sk.n(); + // let n: RingDegree = sk.n(); let rank: Rank = self.rank_out(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(n, rank); - sk_dft_prep.prepare(module, sk, scratch_1); + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank); + sk_dft_prep.prepare(module, sk); - let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n.into(), rank.into(), 1); + let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1); (0..rank.into()).for_each(|i| { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); }); - let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n.into(), 1, 1); - let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(n, Rank(1)); - let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(n.into(), 1, 1); + let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1); + let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1)); + let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1); (0..rank.into()).for_each(|i| { (i..rank.into()).for_each(|j| { diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index b044ae3..3357380 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, source::Source, }; use crate::{ - SIGMA, + SIGMA, ScratchTakeCore, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, @@ -44,8 +44,8 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch:, + Module: ModuleN + GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: ScratchTakeCore, { fn ggsw_encrypt_sk( &self, @@ -80,7 +80,7 @@ where let dsize: usize = res.dsize().into(); let cols: usize = (rank + 1).into(); - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); 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_ct.rs index 16bbadf..fa314d5 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, + ModuleN, ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, ScratchTakeBasic, }, layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, @@ -19,19 +19,19 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) } - pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); @@ -42,68 +42,68 @@ impl GLWE> { } impl GLWE { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &Module, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef, - Module: GLWEEncryptSk, + S: GLWESecretPreparedToRef, + Module: GLWEEncryptSk, { module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } - pub fn encrypt_zero_sk( + pub fn encrypt_zero_sk( &mut self, - module: &Module, + module: &Module, sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - S: GLWESecretPreparedToRef, - Module: GLWEEncryptZeroSk, + S: GLWESecretPreparedToRef, + Module: GLWEEncryptZeroSk, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); } - pub fn encrypt_pk( + pub fn encrypt_pk( &mut self, - module: &Module, + module: &Module, pt: &P, pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where P: GLWEPlaintextToRef, - K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptPk, + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptPk, { module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); } - pub fn encrypt_zero_pk( + pub fn encrypt_zero_pk( &mut self, - module: &Module, + module: &Module, pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptZeroPk, + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptZeroPk, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); } } -pub trait GLWEEncryptSk { +pub trait GLWEEncryptSk { fn glwe_encrypt_sk( &self, res: &mut R, @@ -111,17 +111,17 @@ pub trait GLWEEncryptSk { sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GLWEEncryptSk for Module +impl GLWEEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - Scratch: ScratchAvailable, + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Scratch: ScratchAvailable, { fn glwe_encrypt_sk( &self, @@ -130,18 +130,18 @@ where sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let mut res: GLWE<&mut [u8]> = res.to_mut(); let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); #[cfg(debug_assertions)] { - let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); assert_eq!(res.rank(), sk.rank()); assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); @@ -171,23 +171,23 @@ where } } -pub trait GLWEEncryptZeroSk { +pub trait GLWEEncryptZeroSk { fn glwe_encrypt_zero_sk( &self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GLWEEncryptZeroSk for Module +impl GLWEEncryptZeroSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - Scratch: ScratchAvailable, + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Scratch: ScratchAvailable, { fn glwe_encrypt_zero_sk( &self, @@ -195,16 +195,16 @@ where sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let mut res: GLWE<&mut [u8]> = res.to_mut(); #[cfg(debug_assertions)] { - let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); assert_eq!(res.rank(), sk.rank()); assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); @@ -233,7 +233,7 @@ where } } -pub trait GLWEEncryptPk { +pub trait GLWEEncryptPk { fn glwe_encrypt_pk( &self, res: &mut R, @@ -241,16 +241,16 @@ pub trait GLWEEncryptPk { pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPublicKeyPreparedToRef; } -impl GLWEEncryptPk for Module +impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal, + Module: GLWEEncryptPkInternal, { fn glwe_encrypt_pk( &self, @@ -259,32 +259,32 @@ where pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); } } -pub trait GLWEEncryptZeroPk { +pub trait GLWEEncryptZeroPk { fn glwe_encrypt_zero_pk( &self, res: &mut R, pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPublicKeyPreparedToRef; } -impl GLWEEncryptZeroPk for Module +impl GLWEEncryptZeroPk for Module where - Module: GLWEEncryptPkInternal, + Module: GLWEEncryptPkInternal, { fn glwe_encrypt_zero_pk( &self, @@ -292,10 +292,10 @@ where pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal( res, @@ -308,7 +308,7 @@ where } } -pub(crate) trait GLWEEncryptPkInternal { +pub(crate) trait GLWEEncryptPkInternal { fn glwe_encrypt_pk_internal( &self, res: &mut R, @@ -316,22 +316,25 @@ pub(crate) trait GLWEEncryptPkInternal { pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - K: GLWEPublicKeyPreparedToRef; + K: GLWEPublicKeyPreparedToRef; } -impl GLWEEncryptPkInternal for Module +impl GLWEEncryptPkInternal for Module where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch:, + Module: SvpPrepare + + SvpApplyDftToDft + + VecZnxIdftApplyConsume + + VecZnxBigAddNormal + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + SvpPPolBytesOf + + ModuleN + + VecZnxDftBytesOf, + Scratch: ScratchTakeBasic, { fn glwe_encrypt_pk_internal( &self, @@ -340,14 +343,14 @@ where pk: &K, source_xu: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWEToMut, P: GLWEPlaintextToRef, - K: GLWEPublicKeyPreparedToRef, + K: GLWEPublicKeyPreparedToRef, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref(); + let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref(); #[cfg(debug_assertions)] { @@ -365,10 +368,10 @@ where let cols: usize = (res.rank() + 1).into(); // Generates u according to the underlying secret distribution. - let (mut u_dft, scratch_1) = scratch.take_svp_ppol(res.n().into(), 1); + let (mut u_dft, scratch_1) = scratch.take_svp_ppol(self, 1); { - let (mut u, _) = scratch_1.take_scalar_znx(res.n().into(), 1); + let (mut u, _) = scratch_1.take_scalar_znx(self, 1); match pk.dist { Distribution::NONE => panic!( "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \ @@ -387,7 +390,7 @@ where // 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(res.n().into(), 1, size_pk); + 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); @@ -418,7 +421,7 @@ where } } -pub(crate) trait GLWEEncryptSkInternal { +pub(crate) trait GLWEEncryptSkInternal { fn glwe_encrypt_sk_internal( &self, base2k: usize, @@ -431,29 +434,30 @@ pub(crate) trait GLWEEncryptSkInternal { source_xa: &mut Source, source_xe: &mut Source, sigma: f64, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: VecZnxToMut, P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GLWEEncryptSkInternal for Module +impl GLWEEncryptSkInternal for Module where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: ModuleN + + VecZnxDftBytesOf + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace - + VecZnxNormalizeInplace + + VecZnxNormalizeInplace + VecZnxAddNormal - + VecZnxNormalize + + VecZnxNormalize + VecZnxSub, - Scratch: ScratchAvailable, + Scratch: ScratchAvailable + ScratchTakeBasic, { fn glwe_encrypt_sk_internal( &self, @@ -467,14 +471,14 @@ where source_xa: &mut Source, source_xe: &mut Source, sigma: f64, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: VecZnxToMut, P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let ct: &mut VecZnx<&mut [u8]> = &mut res.to_mut(); - let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); #[cfg(debug_assertions)] { @@ -490,11 +494,11 @@ where let size: usize = ct.size(); - let (mut c0, scratch_1) = scratch.take_vec_znx(ct.n(), 1, size); + let (mut c0, scratch_1) = scratch.take_vec_znx(self, 1, size); c0.zero(); { - let (mut ci, scratch_2) = scratch_1.take_vec_znx(ct.n(), 1, size); + let (mut ci, scratch_2) = scratch_1.take_vec_znx(self, 1, size); // ct[i] = uniform // ct[0] -= c[i] * s[i], @@ -504,7 +508,7 @@ where // ct[i] = uniform (+ pt) self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); - let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(ct.n(), 1, size); + let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size); // ci = ct[i] - pt // i.e. we act as we sample ct[i] already as uniform + pt @@ -522,7 +526,7 @@ where } self.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); - let ci_big: VecZnxBig<&mut [u8], B> = self.vec_znx_idft_apply_consume(ci_dft); + let ci_big: VecZnxBig<&mut [u8], BE> = self.vec_znx_idft_apply_consume(ci_dft); // use c[0] as buffer, which is overwritten later by the normalization step self.vec_znx_big_normalize(base2k, &mut ci, 0, base2k, &ci_big, 0, scratch_3); diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index d89f515..9073cfe 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -47,7 +47,7 @@ where // Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); - let mut tmp: GLWE> = GLWE::alloc_from_infos(res); + let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 971766e..cd3ffec 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, @@ -9,18 +9,22 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, + prepared::GLWESecretPrepared, + }, }; impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { GLWESecretPrepared::bytes_of(module, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in())) } } @@ -37,7 +41,8 @@ impl GLWEToLWESwitchingKey { ) where DLwe: DataRef, DGlwe: DataRef, - Module: VecZnxAutomorphismInplace + Module: ModuleN + + VecZnxAutomorphismInplace + VecZnxAddScalarInplace + VecZnxDftBytesOf + VecZnxBigNormalize @@ -54,15 +59,16 @@ impl GLWEToLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolBytesOf, - Scratch: ScratchAvailable, + + SvpPPolBytesOf + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeCore, { #[cfg(debug_assertions)] { assert!(sk_lwe.n().0 <= module.n() as u32); } - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(sk_glwe.n(), Rank(1)); + let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index a01d95f..5a743eb 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -1,64 +1,79 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace}, - layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut}, + layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, source::Source, }; use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToRef, LWESecretToRef}, }; impl LWE { - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &LWEPlaintext, - sk: &LWESecret, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - DataPt: DataRef, - DataSk: DataRef, - Module: ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + + pub fn encrypt_sk(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + P: LWEPlaintextToRef, + S: LWESecretToRef, + M: LWEEncryptSk, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { + module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe); + } +} + + +pub trait LWEEncryptSk +where + Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, +{ + fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + R: LWEToMut, + P: LWEPlaintextToRef, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + { + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let pt: &LWEPlaintext<&[u8]> = &pt.to_ref(); + let sk: &LWESecret<&[u8]> = &sk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.n(), sk.n()) + assert_eq!(res.n(), sk.n()) } - let base2k: usize = self.base2k().into(); - let k: usize = self.k().into(); + let base2k: usize = res.base2k().into(); + let k: usize = res.k().into(); - module.zn_fill_uniform((self.n() + 1).into(), base2k, &mut self.data, 0, source_xa); + self.zn_fill_uniform((res.n() + 1).into(), base2k, &mut res.data, 0, source_xa); - let mut tmp_znx: Zn> = Zn::alloc(1, 1, self.size()); + let mut tmp_znx: Zn> = Zn::alloc(1, 1, res.size()); - let min_size = self.size().min(pt.size()); + let min_size = res.size().min(pt.size()); (0..min_size).for_each(|i| { tmp_znx.at_mut(0, i)[0] = pt.data.at(0, i)[0] - - self.data.at(0, i)[1..] + - res.data.at(0, i)[1..] .iter() .zip(sk.data.at(0, 0)) .map(|(x, y)| x * y) .sum::(); }); - (min_size..self.size()).for_each(|i| { - tmp_znx.at_mut(0, i)[0] -= self.data.at(0, i)[1..] + (min_size..res.size()).for_each(|i| { + tmp_znx.at_mut(0, i)[0] -= res.data.at(0, i)[1..] .iter() .zip(sk.data.at(0, 0)) .map(|(x, y)| x * y) .sum::(); }); - module.zn_add_normal( + self.zn_add_normal( 1, base2k, - &mut self.data, + &mut res.data, 0, k, source_xe, @@ -66,7 +81,7 @@ impl LWE { SIGMA_BOUND, ); - module.zn_normalize_inplace( + self.zn_normalize_inplace( 1, base2k, &mut tmp_znx, @@ -74,8 +89,13 @@ impl LWE { ScratchOwned::alloc(size_of::()).borrow(), ); - (0..self.size()).for_each(|i| { - self.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; - }); + (0..res.size()).for_each(|i| { + res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; + }); } } + +impl LWEEncryptSk for Module where + Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, +{ +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 2fd60ff..ed1c5d6 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, + ModuleN, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, @@ -9,16 +9,19 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, RingDegree, +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::GLWESecretPrepared, + }, }; impl LWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { debug_assert_eq!( infos.dsize().0, @@ -35,7 +38,7 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + GLWESecret::bytes_of(module, Rank(1)) + GLWESecretPrepared::bytes_of(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } @@ -54,7 +57,8 @@ impl LWESwitchingKey { ) where DIn: DataRef, DOut: DataRef, - Module: VecZnxAutomorphismInplace + Module: ModuleN + + VecZnxAutomorphismInplace + VecZnxAddScalarInplace + VecZnxDftBytesOf + VecZnxBigNormalize @@ -71,8 +75,9 @@ impl LWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolBytesOf, - Scratch:, + + SvpPPolBytesOf + + SvpPPolAlloc, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -81,8 +86,8 @@ impl LWESwitchingKey { assert!(self.n().0 <= module.n() as u32); } - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n(), Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n(), Rank(1)); + let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); + let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(module, Rank(1)); sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 041c7c4..32c75fc 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, @@ -9,13 +9,16 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank, RingDegree}; +use crate::{ + ScratchTakeCore, + layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, +}; impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, { debug_assert_eq!( infos.rank_in(), @@ -23,7 +26,7 @@ impl LWEToGLWESwitchingKey> { "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - + GLWESecret::bytes_of(RingDegree(module.n() as u32), infos.rank_in()) + + GLWESecret::bytes_of(module, infos.rank_in()) } } @@ -40,7 +43,8 @@ impl LWEToGLWESwitchingKey { ) where DLwe: DataRef, DGlwe: DataRef, - Module: VecZnxAutomorphismInplace + Module: ModuleN + + VecZnxAutomorphismInplace + VecZnxAddScalarInplace + VecZnxDftBytesOf + VecZnxBigNormalize @@ -57,8 +61,9 @@ impl LWEToGLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolBytesOf, - Scratch: ScratchAvailable, + + SvpPPolBytesOf + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -67,7 +72,7 @@ impl LWEToGLWESwitchingKey { assert!(sk_lwe.n().0 <= module.n() as u32); } - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(sk_glwe.n(), Rank(1)); + let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); From 2b2b994f7d110efd0ce5923fb154fc0290e00063 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Sun, 12 Oct 2025 21:34:10 +0200 Subject: [PATCH 18/60] wip --- poulpy-backend/src/cpu_fft64_avx/scratch.rs | 181 +---- poulpy-backend/src/cpu_fft64_avx/svp.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_big.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_avx/vmp.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/scratch.rs | 181 +---- poulpy-backend/src/cpu_fft64_ref/svp.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_big.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/vmp.rs | 2 +- .../src/cpu_spqlios/fft64/scratch.rs | 181 +---- .../src/cpu_spqlios/fft64/svp_ppol.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_dft.rs | 2 +- .../src/cpu_spqlios/fft64/vmp_pmat.rs | 2 +- .../src/cpu_spqlios/ntt120/svp_ppol.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_dft.rs | 2 +- poulpy-core/README.md | 4 +- .../benches/external_product_glwe_fft64.rs | 49 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 52 +- poulpy-core/examples/encryption.rs | 18 +- poulpy-core/src/automorphism/gglwe_atk.rs | 44 +- poulpy-core/src/automorphism/ggsw_ct.rs | 66 +- poulpy-core/src/automorphism/glwe_ct.rs | 82 +-- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 279 ++++++++ poulpy-core/src/conversion/glwe_to_lwe.rs | 38 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 34 +- poulpy-core/src/conversion/mod.rs | 3 + poulpy-core/src/decryption/glwe_ct.rs | 18 +- poulpy-core/src/decryption/lwe_ct.rs | 4 +- .../src/encryption/compressed/gglwe_atk.rs | 143 ++-- .../src/encryption/compressed/gglwe_ct.rs | 161 +++-- .../src/encryption/compressed/gglwe_ksk.rs | 119 ++-- .../src/encryption/compressed/gglwe_tsk.rs | 132 ++-- .../src/encryption/compressed/ggsw_ct.rs | 181 ++--- .../src/encryption/compressed/glwe_ct.rs | 137 ++-- poulpy-core/src/encryption/gglwe_atk.rs | 143 ++-- poulpy-core/src/encryption/gglwe_ct.rs | 171 +++-- poulpy-core/src/encryption/gglwe_ksk.rs | 46 +- poulpy-core/src/encryption/gglwe_tsk.rs | 46 +- poulpy-core/src/encryption/ggsw_ct.rs | 163 +++-- poulpy-core/src/encryption/glwe_ct.rs | 653 ++++++++++------- poulpy-core/src/encryption/glwe_pk.rs | 88 +-- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 32 +- poulpy-core/src/encryption/lwe_ct.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 33 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 27 +- poulpy-core/src/encryption/mod.rs | 2 - poulpy-core/src/external_product/gglwe_atk.rs | 101 +-- poulpy-core/src/external_product/gglwe_ksk.rs | 248 ++++--- poulpy-core/src/external_product/ggsw_ct.rs | 247 ++++--- poulpy-core/src/external_product/glwe_ct.rs | 213 +++--- poulpy-core/src/external_product/mod.rs | 21 +- poulpy-core/src/glwe_packing.rs | 103 ++- poulpy-core/src/glwe_trace.rs | 50 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 373 +++++----- poulpy-core/src/keyswitching/ggsw_ct.rs | 449 +++--------- poulpy-core/src/keyswitching/glwe_ct.rs | 660 ++++++++---------- poulpy-core/src/keyswitching/lwe_ct.rs | 190 ++--- .../src/layouts/compressed/gglwe_atk.rs | 191 +++-- .../src/layouts/compressed/gglwe_ct.rs | 257 ++++--- .../src/layouts/compressed/gglwe_ksk.rs | 186 +++-- .../src/layouts/compressed/gglwe_tsk.rs | 225 +++--- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 216 ++++-- poulpy-core/src/layouts/compressed/glwe_ct.rs | 230 +++--- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 151 +++- poulpy-core/src/layouts/compressed/lwe_ct.rs | 159 ++++- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 153 +++- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 152 +++- poulpy-core/src/layouts/compressed/mod.rs | 6 - poulpy-core/src/layouts/gglwe_atk.rs | 189 +++-- poulpy-core/src/layouts/gglwe_ct.rs | 322 ++++----- poulpy-core/src/layouts/gglwe_ksk.rs | 218 ++++-- poulpy-core/src/layouts/gglwe_tsk.rs | 180 +++-- poulpy-core/src/layouts/ggsw_ct.rs | 303 ++++---- poulpy-core/src/layouts/glwe_ct.rs | 264 +++---- poulpy-core/src/layouts/glwe_pk.rs | 198 +++--- poulpy-core/src/layouts/glwe_pt.rs | 215 +++--- poulpy-core/src/layouts/glwe_sk.rs | 108 ++- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 140 +++- poulpy-core/src/layouts/lwe_ct.rs | 222 +++--- poulpy-core/src/layouts/lwe_ksk.rs | 162 +++-- poulpy-core/src/layouts/lwe_pt.rs | 51 +- poulpy-core/src/layouts/lwe_sk.rs | 51 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 123 +++- poulpy-core/src/layouts/mod.rs | 28 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 232 ++++-- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 363 +++++----- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 225 ++++-- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 244 ++++--- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 340 ++++----- poulpy-core/src/layouts/prepared/glwe_pk.rs | 252 ++++--- poulpy-core/src/layouts/prepared/glwe_sk.rs | 167 +++-- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 197 ++++-- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 216 ++++-- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 205 ++++-- poulpy-core/src/layouts/prepared/mod.rs | 13 - poulpy-core/src/lib.rs | 3 +- poulpy-core/src/noise/gglwe_ct.rs | 20 +- poulpy-core/src/noise/ggsw_ct.rs | 40 +- poulpy-core/src/noise/glwe_ct.rs | 24 +- poulpy-core/src/operations/glwe.rs | 100 ++- poulpy-core/src/scratch.rs | 461 +++++------- poulpy-core/src/tests/serialization.rs | 52 +- .../test_suite/automorphism/gglwe_atk.rs | 78 +-- .../tests/test_suite/automorphism/ggsw_ct.rs | 76 +- .../tests/test_suite/automorphism/glwe_ct.rs | 74 +- .../src/tests/test_suite/conversion.rs | 63 +- .../tests/test_suite/encryption/gglwe_atk.rs | 40 +- .../tests/test_suite/encryption/gglwe_ct.rs | 39 +- .../tests/test_suite/encryption/ggsw_ct.rs | 78 +-- .../tests/test_suite/encryption/glwe_ct.rs | 94 ++- .../tests/test_suite/encryption/glwe_tsk.rs | 46 +- .../test_suite/external_product/gglwe_ksk.rs | 60 +- .../test_suite/external_product/ggsw_ct.rs | 56 +- .../test_suite/external_product/glwe_ct.rs | 60 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 78 +-- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 77 +- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 73 +- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 28 +- poulpy-core/src/tests/test_suite/packing.rs | 49 +- poulpy-core/src/tests/test_suite/trace.rs | 51 +- poulpy-hal/src/api/module.rs | 4 + poulpy-hal/src/api/scratch.rs | 159 +++-- poulpy-hal/src/api/svp_ppol.rs | 4 +- poulpy-hal/src/api/vec_znx_big.rs | 4 +- poulpy-hal/src/api/vec_znx_dft.rs | 4 +- poulpy-hal/src/api/vmp_pmat.rs | 4 +- poulpy-hal/src/delegates/scratch.rs | 114 +-- poulpy-hal/src/delegates/svp_ppol.rs | 8 +- poulpy-hal/src/delegates/vec_znx_big.rs | 8 +- poulpy-hal/src/delegates/vec_znx_dft.rs | 12 +- poulpy-hal/src/delegates/vmp_pmat.rs | 8 +- poulpy-hal/src/layouts/mat_znx.rs | 12 +- poulpy-hal/src/layouts/scalar_znx.rs | 6 +- poulpy-hal/src/layouts/svp_ppol.rs | 4 +- poulpy-hal/src/layouts/vec_znx.rs | 8 +- poulpy-hal/src/layouts/vec_znx_big.rs | 4 +- poulpy-hal/src/layouts/vec_znx_dft.rs | 4 +- poulpy-hal/src/layouts/vmp_pmat.rs | 6 +- poulpy-hal/src/layouts/zn.rs | 8 +- poulpy-hal/src/oep/scratch.rs | 110 +-- poulpy-hal/src/oep/svp_ppol.rs | 2 +- poulpy-hal/src/oep/vec_znx_big.rs | 2 +- poulpy-hal/src/oep/vec_znx_dft.rs | 2 +- poulpy-hal/src/oep/vmp_pmat.rs | 2 +- poulpy-hal/src/reference/fft64/vmp.rs | 2 +- .../benches/circuit_bootstrapping.rs | 50 +- .../examples/circuit_bootstrapping.rs | 32 +- .../src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs | 12 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 28 +- .../ciphertexts/block_prepared.rs | 28 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 28 +- .../src/tfhe/bdd_arithmetic/eval.rs | 36 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 30 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 26 +- .../src/tfhe/bdd_arithmetic/test.rs | 26 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 81 ++- .../src/tfhe/blind_rotation/cggi_key.rs | 39 +- poulpy-schemes/src/tfhe/blind_rotation/key.rs | 26 +- .../src/tfhe/blind_rotation/key_compressed.rs | 12 +- .../src/tfhe/blind_rotation/key_prepared.rs | 14 +- poulpy-schemes/src/tfhe/blind_rotation/mod.rs | 6 +- .../tests/generic_blind_rotation.rs | 35 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 48 +- .../src/tfhe/circuit_bootstrapping/key.rs | 62 +- .../src/tfhe/circuit_bootstrapping/mod.rs | 10 +- .../tests/circuit_bootstrapping.rs | 78 +-- 169 files changed, 8705 insertions(+), 7677 deletions(-) create mode 100644 poulpy-core/src/conversion/gglwe_to_ggsw.rs diff --git a/poulpy-backend/src/cpu_fft64_avx/scratch.rs b/poulpy-backend/src/cpu_fft64_avx/scratch.rs index 922166b..f6595f9 100644 --- a/poulpy-backend/src/cpu_fft64_avx/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_avx/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_fft64_avx::FFT64Avx; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Avx -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Avx -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Avx -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Avx -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Avx -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Avx -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Avx -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-backend/src/cpu_fft64_avx/svp.rs b/poulpy-backend/src/cpu_fft64_avx/svp.rs index f505597..1c2c999 100644 --- a/poulpy-backend/src/cpu_fft64_avx/svp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Avx { } unsafe impl SvpPPolAllocBytesImpl for FFT64Avx { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs index 99a39fd..08ec98a 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Avx { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs index 862f623..063ee26 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Avx { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Avx { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vmp.rs b/poulpy-backend/src/cpu_fft64_avx/vmp.rs index 6b87ce1..fcb6236 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_avx::{FFT64Avx, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl for FFT64Avx { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/scratch.rs b/poulpy-backend/src/cpu_fft64_ref/scratch.rs index 80b228d..1593370 100644 --- a/poulpy-backend/src/cpu_fft64_ref/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_ref/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_fft64_ref::FFT64Ref; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Ref -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Ref -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Ref -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Ref -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Ref -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Ref -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Ref -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-backend/src/cpu_fft64_ref/svp.rs b/poulpy-backend/src/cpu_fft64_ref/svp.rs index 06dad9e..37ca14a 100644 --- a/poulpy-backend/src/cpu_fft64_ref/svp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Ref { } unsafe impl SvpPPolAllocBytesImpl for FFT64Ref { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs index bb75c8f..348c9b6 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Ref { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs index a08b728..a2a743d 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Ref { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Ref { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vmp.rs b/poulpy-backend/src/cpu_fft64_ref/vmp.rs index 2286de5..34cbf07 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_ref::{FFT64Ref, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl for FFT64Ref { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs index 9bddcb3..d32b9f4 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs @@ -3,13 +3,8 @@ use std::marker::PhantomData; use poulpy_hal::{ DEFAULTALIGN, alloc_aligned, api::ScratchFromBytes, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, SvpPPolAllocBytesImpl, - TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, VecZnxBigAllocBytesImpl, - VecZnxDftAllocBytesImpl, VmpPMatAllocBytesImpl, - }, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; use crate::cpu_spqlios::FFT64Spqlios; @@ -64,178 +59,6 @@ where } } -unsafe impl TakeScalarZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeSvpPPolImpl for FFT64Spqlios -where - B: SvpPPolAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); - ( - SvpPPol::from_data(take_slice, n, cols), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxBigImpl for FFT64Spqlios -where - B: VecZnxBigAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), - ); - ( - VecZnxBig::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftImpl for FFT64Spqlios -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), - ); - - ( - VecZnxDft::from_data(take_slice, n, cols, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeVecZnxDftSliceImpl for FFT64Spqlios -where - B: VecZnxDftAllocBytesImpl + ScratchFromBytesImpl + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_dft_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVecZnxSliceImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl + TakeVecZnxImpl, -{ - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch) { - let mut scratch: &mut Scratch = scratch; - let mut slice: Vec> = Vec::with_capacity(len); - for _ in 0..len { - let (znx, new_scratch) = B::take_vec_znx_impl(scratch, n, cols, size); - scratch = new_scratch; - slice.push(znx); - } - (slice, scratch) - } -} - -unsafe impl TakeVmpPMatImpl for FFT64Spqlios -where - B: VmpPMatAllocBytesImpl + ScratchFromBytesImpl, -{ - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), - ); - ( - VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - -unsafe impl TakeMatZnxImpl for FFT64Spqlios -where - B: ScratchFromBytesImpl, -{ - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch) { - let (take_slice, rem_slice) = take_slice_aligned( - &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), - ); - ( - MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), - Scratch::from_bytes(rem_slice), - ) - } -} - fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8]) { let ptr: *mut u8 = data.as_mut_ptr(); let self_len: usize = data.len(); diff --git a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs index b917400..f46b795 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs @@ -27,7 +27,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Spqlios { } unsafe impl SvpPPolAllocBytesImpl for FFT64Spqlios { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { FFT64Spqlios::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs index 8becaf6..5021f6b 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs @@ -22,7 +22,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl for FFT64Spqlios { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs index 461d327..3b67089 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs @@ -30,7 +30,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Spqlios { } unsafe impl VecZnxDftAllocBytesImpl for FFT64Spqlios { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs index ca64992..ff1eaa2 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs @@ -16,7 +16,7 @@ use crate::cpu_spqlios::{ }; unsafe impl VmpPMatAllocBytesImpl for FFT64Spqlios { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs index c98237a..8c7fdcc 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs @@ -18,7 +18,7 @@ unsafe impl SvpPPolAllocImpl for NTT120 { } unsafe impl SvpPPolAllocBytesImpl for NTT120 { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs index 715b432..58ddf78 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs @@ -3,7 +3,7 @@ use poulpy_hal::{layouts::Backend, oep::VecZnxBigAllocBytesImpl}; use crate::cpu_spqlios::NTT120; unsafe impl VecZnxBigAllocBytesImpl for NTT120 { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_big_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs index 53dd24f..9e1666b 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::cpu_spqlios::NTT120; unsafe impl VecZnxDftAllocBytesImpl for NTT120 { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size * size_of::() } } diff --git a/poulpy-core/README.md b/poulpy-core/README.md index 07d5304..259988e 100644 --- a/poulpy-core/README.md +++ b/poulpy-core/README.md @@ -52,8 +52,8 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, n, base2k, ct.k()) - | GLWECiphertext::decrypt_scratch_space(&module, n, base2k, ct.k()), + GLWECiphertext::encrypt_sk_tmp_bytes(&module, n, base2k, ct.k()) + | GLWECiphertext::decrypt_tmp_bytes(&module, n, base2k, ct.k()), ); // Generate secret-key diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 360a9f4..2849e3d 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,7 +1,6 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, - TorusPrecision, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + Base2K, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, RingDegree, TorusPrecision, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -29,7 +28,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct_in: TorusPrecision = p.k_ct_in; let k_ct_out: TorusPrecision = p.k_ct_out; @@ -39,7 +38,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = Dnum(1); //(p.k_ct_in.div_ceil(p.base2k); - let ggsw_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_layout: GGSWLayout = GGSWLayout { n, base2k, k: k_ggsw, @@ -48,36 +47,36 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_out, rank, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_in, rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); - let mut ct_glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), + GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_in_layout) + | GLWE::external_product_tmp_bytes(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); let mut source_xs = Source::new([0u8; 32]); let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); @@ -98,7 +97,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); @@ -138,7 +137,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe: TorusPrecision = p.k_ct; let k_ggsw: TorusPrecision = p.k_ggsw; @@ -147,7 +146,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); - let ggsw_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_layout: GGSWLayout = GGSWLayout { n, base2k, k: k_ggsw, @@ -156,28 +155,28 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe, rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), + GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) + | GLWE::external_product_inplace_tmp_bytes(&module, &glwe_layout, &ggsw_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); @@ -198,7 +197,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { let scratch_borrow = scratch.borrow(); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 2da2032..fc1dc69 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::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWESwitchingKey, GGLWESwitchingKeyLayout, - GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + GLWESwitchingKeyLayout, Rank, RingDegree, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration}; @@ -29,7 +29,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe_in: TorusPrecision = p.k_ct_in; let k_glwe_out: TorusPrecision = p.k_ct_out; @@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); - let gglwe_atk_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n, base2k, k: k_gglwe, @@ -48,28 +48,28 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { dsize, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_in, rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_out, rank, }; - let mut ksk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&gglwe_atk_layout); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWECiphertext::keyswitch_scratch_space( + GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_in_layout) + | GLWE::keyswitch_tmp_bytes( &module, &glwe_out_layout, &glwe_in_layout, @@ -81,7 +81,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc(&glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); @@ -102,7 +102,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWEAutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: AutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); @@ -148,7 +148,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: Degree = Degree(module.n() as u32); + let n: RingDegree = RingDegree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct: TorusPrecision = p.k_ct; let k_ksk: TorusPrecision = p.k_ksk; @@ -157,7 +157,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); - let gglwe_layout: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_layout: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n, base2k, k: k_ksk, @@ -167,31 +167,31 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank_out: rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct, rank, }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_layout); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) - | GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), + GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) + | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) + | GLWE::keyswitch_inplace_tmp_bytes(&module, &glwe_layout, &gglwe_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( @@ -211,7 +211,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index a65b473..efd838e 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -2,8 +2,7 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ GLWEOperations, SIGMA, layouts::{ - Base2K, Degree, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, - TorusPrecision, + Base2K, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, RingDegree, TorusPrecision, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -17,7 +16,7 @@ fn main() { // Ring degree let log_n: usize = 10; - let n: Degree = Degree(1 << log_n); + let n: RingDegree = RingDegree(1 << log_n); // Base-2-k (implicit digit decomposition) let base2k: Base2K = Base2K(14); @@ -34,7 +33,7 @@ fn main() { // Instantiate Module (DFT Tables) let module: Module = Module::::new(n.0 as u64); - let glwe_ct_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_ct_infos: GLWELayout = GLWELayout { n, base2k, k: k_ct, @@ -44,9 +43,9 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -55,12 +54,11 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_ct_infos) - | GLWECiphertext::decrypt_scratch_space(&module, &glwe_ct_infos), + GLWE::encrypt_sk_tmp_bytes(&module, &glwe_ct_infos) | GLWE::decrypt_tmp_bytes(&module, &glwe_ct_infos), ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 9b08e68..9650aa2 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -1,16 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWE, prepared::AutomorphismKeyPrepared}; -impl GGLWEAutomorphismKey> { - pub fn automorphism_scratch_space( +impl AutomorphismKey> { + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -20,9 +20,9 @@ impl GGLWEAutomorphismKey> { OUT: GGLWEInfos, IN: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space( + GLWE::keyswitch_tmp_bytes( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), @@ -30,25 +30,25 @@ impl GGLWEAutomorphismKey> { ) } - pub fn automorphism_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GGLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWEAutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) + AutomorphismKey::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn automorphism( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &AutomorphismKey, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -61,7 +61,7 @@ impl GGLWEAutomorphismKey { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -103,8 +103,8 @@ impl GGLWEAutomorphismKey { (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); - let lhs_ct: GLWECiphertext<&[u8]> = lhs.at(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); + let lhs_ct: GLWE<&[u8]> = lhs.at(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| { @@ -133,10 +133,10 @@ impl GGLWEAutomorphismKey { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -149,7 +149,7 @@ impl GGLWEAutomorphismKey { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -176,7 +176,7 @@ impl GGLWEAutomorphismKey { (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index eef3082..a3cef86 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -1,20 +1,20 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared}, + GGLWEInfos, GGSW, GGSWInfos, GLWE, + prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, }; -impl GGSWCiphertext> { - pub fn automorphism_scratch_space( +impl GGSW> { + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -26,25 +26,22 @@ impl GGSWCiphertext> { IN: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { let out_size: usize = out_infos.size(); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes((key_infos.rank_out() + 1).into(), out_size); - let ks_internal: usize = GLWECiphertext::keyswitch_scratch_space( + let ci_dft: usize = module.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), out_size); + let ks_internal: usize = GLWE::keyswitch_tmp_bytes( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(), key_infos, ); - let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand: usize = GGSW::expand_row_tmp_bytes(module, out_infos, tsk_infos); ci_dft + (ks_internal | expand) } - pub fn automorphism_inplace_scratch_space( + pub fn automorphism_inplace_tmp_bytes( module: &Module, out_infos: &OUT, key_infos: &KEY, @@ -54,26 +51,23 @@ impl GGSWCiphertext> { OUT: GGSWInfos, KEY: GGLWEInfos, TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + Module: + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSWCiphertext::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) + GGSW::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos, tsk_infos) } } -impl GGSWCiphertext { +impl GGSW { pub fn automorphism( &mut self, module: &Module, - lhs: &GGSWCiphertext, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + lhs: &GGSW, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -83,13 +77,13 @@ impl GGSWCiphertext { + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -121,7 +115,7 @@ impl GGSWCiphertext { self.rank(), tensor_key.rank_out() ); - assert!(scratch.available() >= GGSWCiphertext::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) + assert!(scratch.available() >= GGSW::automorphism_tmp_bytes(module, self, lhs, auto_key, tensor_key)) }; // Keyswitch the j-th row of the col 0 @@ -137,11 +131,11 @@ impl GGSWCiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -151,13 +145,13 @@ impl GGSWCiphertext { + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftCopy + VecZnxDftAddInplace + VecZnxIdftApplyTmpA + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnxBig + TakeVecZnx, + Scratch: ScratchAvailable, { // Keyswitch the j-th row of the col 0 (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 79fcb12..0c8b581 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -1,17 +1,17 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, - VecZnxBigSubSmallNegateInplace, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, VecZnxBigSubSmallNegateInplace, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; -impl GLWECiphertext> { - pub fn automorphism_scratch_space( +impl GLWE> { + pub fn automorphism_tmp_bytes( module: &Module, out_infos: &OUT, in_infos: &IN, @@ -21,30 +21,30 @@ impl GLWECiphertext> { OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) + Self::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) } - pub fn automorphism_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + Self::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) } } -impl GLWECiphertext { +impl GLWE { pub fn automorphism( &mut self, module: &Module, - lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &GLWE, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -56,7 +56,7 @@ impl GLWECiphertext { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.keyswitch(module, lhs, &rhs.key, scratch); (0..(self.rank() + 1).into()).for_each(|i| { @@ -67,10 +67,10 @@ impl GLWECiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -82,7 +82,7 @@ impl GLWECiphertext { + VecZnxAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.keyswitch_inplace(module, &rhs.key, scratch); (0..(self.rank() + 1).into()).for_each(|i| { @@ -93,11 +93,11 @@ impl GLWECiphertext { pub fn automorphism_add( &mut self, module: &Module, - lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &GLWE, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -109,7 +109,7 @@ impl GLWECiphertext { + VecZnxBigAutomorphismInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -135,10 +135,10 @@ impl GLWECiphertext { pub fn automorphism_add_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -150,7 +150,7 @@ impl GLWECiphertext { + VecZnxBigAutomorphismInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -176,11 +176,11 @@ impl GLWECiphertext { pub fn automorphism_sub_ab( &mut self, module: &Module, - lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &GLWE, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -193,7 +193,7 @@ impl GLWECiphertext { + VecZnxBigSubSmallInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -219,10 +219,10 @@ impl GLWECiphertext { pub fn automorphism_sub_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -235,7 +235,7 @@ impl GLWECiphertext { + VecZnxBigSubSmallInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -261,11 +261,11 @@ impl GLWECiphertext { pub fn automorphism_sub_negate( &mut self, module: &Module, - lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &GLWE, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -278,7 +278,7 @@ impl GLWECiphertext { + VecZnxBigSubSmallNegateInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -304,10 +304,10 @@ impl GLWECiphertext { pub fn automorphism_sub_negate_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -320,7 +320,7 @@ impl GLWECiphertext { + VecZnxBigSubSmallNegateInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs new file mode 100644 index 0000000..a7b86fa --- /dev/null +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -0,0 +1,279 @@ +use poulpy_hal::{ + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAddInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, + VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + }, + layouts::{Backend, DataMut, Module, Scratch, VmpPMat, ZnxInfos}, +}; + +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, + prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + }, + operations::GLWEOperations, +}; + +impl GGLWE> { + pub fn from_gglw_tmp_bytes(module: &M, res_infos: &R, tsk_infos: &A) -> usize + where + M: GGSWFromGGLWE, + R: GGSWInfos, + A: GGLWEInfos, + { + module.ggsw_from_gglwe_tmp_bytes(res_infos, tsk_infos) + } +} + +impl GGSW { + pub fn from_gglwe(&mut self, module: &M, gglwe: &G, tsk: &T, scratch: &mut Scratch) + where + M: GGSWFromGGLWE, + G: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_from_gglwe(self, gglwe, tsk, scratch); + } +} + +impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + VecZnxCopy {} + +pub trait GGSWFromGGLWE +where + Self: GGSWExpandRows + VecZnxCopy, +{ + fn ggsw_from_gglwe_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos, + { + self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos) + } + + fn ggsw_from_gglwe(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + assert_eq!(res.rank(), a.rank_out()); + assert_eq!(res.dnum(), a.dnum()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(tsk.n(), self.n() as u32); + + for row in 0..res.dnum().into() { + res.at_mut(row, 0).copy(self, &a.at(row, 0)); + } + + self.ggsw_expand_row(res, tsk, scratch); + } +} + +impl GGSWExpandRows for Module where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VecZnxDftCopy + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftAddInplace + + VecZnxBigNormalize + + VecZnxIdftApplyTmpA + + VecZnxNormalize +{ +} + +pub(crate) trait GGSWExpandRows +where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VecZnxDftCopy + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftAddInplace + + VecZnxBigNormalize + + VecZnxIdftApplyTmpA + + VecZnxNormalize, +{ + fn ggsw_expand_rows_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos, + { + let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize; + let size_in: usize = res_infos + .k() + .div_ceil(tsk_infos.base2k()) + .div_ceil(tsk_infos.dsize().into()) as usize; + + let tmp_dft_i: usize = self.bytes_of_vec_znx_dft((tsk_infos.rank_out() + 1).into(), tsk_size); + let tmp_a: usize = self.bytes_of_vec_znx_dft(1, size_in); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( + tsk_size, + size_in, + size_in, + (tsk_infos.rank_in()).into(), // Verify if rank+1 + (tsk_infos.rank_out()).into(), // Verify if rank+1 + tsk_size, + ); + let tmp_idft: usize = self.bytes_of_vec_znx_big(1, tsk_size); + let norm: usize = self.vec_znx_normalize_tmp_bytes(); + + tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) + } + + fn ggsw_expand_row(&self, res: &mut R, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + let basek_in: usize = res.base2k().into(); + let basek_tsk: usize = tsk.base2k().into(); + + assert!(scratch.available() >= self.ggsw_expand_rows_tmp_bytes(res, tsk)); + + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + + let a_size: usize = (res.size() * basek_in).div_ceil(basek_tsk); + + // Keyswitch the j-th row of the col 0 + for row_i in 0..res.dnum().into() { + let a = &res.at(row_i, 0).data; + + // Pre-compute DFT of (a0, a1, a2) + let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, a_size); + + if basek_in == basek_tsk { + for i in 0..cols { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i); + } + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, a_size); + for i in 0..cols { + self.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2); + self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0); + } + } + + for col_j in 1..cols { + // Example for rank 3: + // + // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is + // actually composed of that many dnum and we focus on a specific row here + // implicitely given ci_dft. + // + // # Input + // + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) + // col 1: (0, 0, 0, 0) + // col 2: (0, 0, 0, 0) + // col 3: (0, 0, 0, 0) + // + // # Output + // + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) + // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 ) + // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 ) + // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i]) + + let dsize: usize = tsk.dsize().into(); + + let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, tsk.size()); + let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, ci_dft.size().div_ceil(dsize)); + + { + // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2 + // + // # Example for col=1 + // + // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2) + // + + // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2) + // + + // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2) + // = + // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) + for col_i in 1..cols { + let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) + + // Extracts a[i] and multipies with Enc(s[i]s[j]) + for di in 0..dsize { + tmp_a.set_size((ci_dft.size() + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize); + + self.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i); + if di == 0 && col_i == 1 { + self.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3); + } else { + self.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3); + } + } + } + } + + // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i + // + // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2) + // + + // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0) + // = + // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2) + // = + // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2) + self.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0); + let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(self, 1, tsk.size()); + for i in 0..cols { + self.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i); + self.vec_znx_big_normalize( + basek_in, + &mut res.at_mut(row_i, col_j).data, + i, + basek_tsk, + &tmp_idft, + 0, + scratch_3, + ); + } + } + } + } +} diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 517da90..b6c6ed1 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -1,22 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, + VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::{ - TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, Rank, - prepared::GLWEToLWESwitchingKeyPrepared, - }, -}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}; -impl LWECiphertext> { - pub fn from_glwe_scratch_space( +impl LWE> { + pub fn from_glwe_tmp_bytes( module: &Module, lwe_infos: &OUT, glwe_infos: &IN, @@ -26,26 +20,26 @@ impl LWECiphertext> { OUT: LWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: lwe_infos.base2k(), k: lwe_infos.k(), rank: Rank(1), }; - GLWECiphertext::alloc_bytes_with( + GLWE::bytes_of( module.n().into(), lwe_infos.base2k(), lwe_infos.k(), 1u32.into(), - ) + GLWECiphertext::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) + ) + GLWE::keyswitch_tmp_bytes(module, &glwe_layout, glwe_infos, key_infos) } } -impl LWECiphertext { - pub fn sample_extract(&mut self, a: &GLWECiphertext) { +impl LWE { + pub fn sample_extract(&mut self, a: &GLWE) { #[cfg(debug_assertions)] { assert!(self.n() <= a.n()); @@ -66,13 +60,13 @@ impl LWECiphertext { pub fn from_glwe( &mut self, module: &Module, - a: &GLWECiphertext, + a: &GLWE, ks: &GLWEToLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where DGlwe: DataRef, DKs: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -83,7 +77,7 @@ impl LWECiphertext { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx, + Scratch:, { #[cfg(debug_assertions)] { @@ -92,7 +86,7 @@ impl LWECiphertext { assert!(self.n() <= module.n() as u32); } - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: self.base2k(), k: self.k(), diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index d3ae616..c4a3b88 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -1,22 +1,16 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::{ - TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, - prepared::LWEToGLWESwitchingKeyPrepared, - }, -}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}; -impl GLWECiphertext> { - pub fn from_lwe_scratch_space( +impl GLWE> { + pub fn from_lwe_tmp_bytes( module: &Module, glwe_infos: &OUT, lwe_infos: &IN, @@ -26,35 +20,35 @@ impl GLWECiphertext> { OUT: GLWEInfos, IN: LWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let ct: usize = GLWECiphertext::alloc_bytes_with( + let ct: usize = GLWE::bytes_of( module.n().into(), key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), ); - let ks: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); + let ks: usize = GLWE::keyswitch_inplace_tmp_bytes(module, glwe_infos, key_infos); if lwe_infos.base2k() == key_infos.base2k() { ct + ks } else { - let a_conv = VecZnx::alloc_bytes(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); + let a_conv = VecZnx::bytes_of(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); ct + a_conv + ks } } } -impl GLWECiphertext { +impl GLWE { pub fn from_lwe( &mut self, module: &Module, - lwe: &LWECiphertext, + lwe: &LWE, ksk: &LWEToGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where DLwe: DataRef, DKsk: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -65,7 +59,7 @@ impl GLWECiphertext { + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -74,7 +68,7 @@ impl GLWECiphertext { assert!(lwe.n() <= module.n() as u32); } - let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: ksk.base2k(), k: lwe.k(), diff --git a/poulpy-core/src/conversion/mod.rs b/poulpy-core/src/conversion/mod.rs index 090208b..9771531 100644 --- a/poulpy-core/src/conversion/mod.rs +++ b/poulpy-core/src/conversion/mod.rs @@ -1,2 +1,5 @@ +mod gglwe_to_ggsw; mod glwe_to_lwe; mod lwe_to_glwe; + +pub use gglwe_to_ggsw::*; diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 19b4a82..4306d33 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,25 +1,25 @@ use poulpy_hal::{ api::{ - SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, }; -use crate::layouts::{GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext> { - pub fn decrypt_scratch_space(module: &Module, infos: &A) -> usize +impl GLWE> { + pub fn decrypt_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size: usize = infos.size(); - (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size) + (module.vec_znx_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_dft(1, size) } } -impl GLWECiphertext { +impl GLWE { pub fn decrypt( &self, module: &Module, @@ -33,7 +33,7 @@ impl GLWECiphertext { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index 57abdc6..ade21e3 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,9 +4,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}; -impl LWECiphertext +impl LWE where DataSelf: DataRef, { diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 95dcf20..f0afcae 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,35 +1,96 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - }, + api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, + encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, LWEInfos, - compressed::{GGLWEAutomorphismKeyCompressed, GGLWESwitchingKeyCompressed}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed}, }, }; -impl GGLWEAutomorphismKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl AutomorphismKeyCompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); - GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) } } -impl GGLWEAutomorphismKeyCompressed { +pub trait GGLWEAutomorphismKeyCompressedEncryptSk { + fn gglwe_automorphism_key_compressed_encrypt_sk( + &self, + res: &mut R, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: AutomorphismKeyCompressedToMut, + S: GLWESecretToRef; +} + +impl GGLWEAutomorphismKeyCompressedEncryptSk for Module +where + Module: GGLWEKeyCompressedEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxAutomorphism, + Scratch: ScratchAvailable, +{ + fn gglwe_automorphism_key_compressed_encrypt_sk( + &self, + res: &mut R, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: AutomorphismKeyCompressedToMut, + S: GLWESecretToRef, + { + let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + + #[cfg(debug_assertions)] + { + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); + assert!( + scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", + scratch.available(), + AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) + ) + } + + let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); + + { + (0..res.rank_out().into()).for_each(|i| { + self.vec_znx_automorphism( + self.galois_element_inv(p), + &mut sk_out.data.as_vec_znx_mut(), + i, + &sk.data.as_vec_znx(), + i, + ); + }); + } + + self.gglwe_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + + res.p = p; + } +} + +impl AutomorphismKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -40,56 +101,8 @@ impl GGLWEAutomorphismKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAutomorphism - + SvpPrepare - + SvpPPolAllocBytes - + VecZnxSwitchRing - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Module: GGLWEAutomorphismKeyCompressedEncryptSk, { - #[cfg(debug_assertions)] - { - assert_eq!(self.n(), sk.n()); - assert_eq!(self.rank_out(), self.rank_in()); - assert_eq!(sk.rank(), self.rank_out()); - assert!( - scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", - scratch.available(), - GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(module, self) - ) - } - - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); - - { - (0..self.rank_out().into()).for_each(|i| { - module.vec_znx_automorphism( - module.galois_element_inv(p), - &mut sk_out.data.as_vec_znx_mut(), - i, - &sk.data.as_vec_znx(), - i, - ); - }); - } - - self.key - .encrypt_sk(module, sk, &sk_out, seed_xa, source_xe, scratch_1); - - self.p = p; + module.gglwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 76871da..b67dc88 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -1,30 +1,22 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, + ZnNormalizeInplace, }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, - encryption::{SIGMA, glwe_encrypt_sk_internal}, - layouts::{GGLWECiphertext, GGLWEInfos, LWEInfos, compressed::GGLWECiphertextCompressed, prepared::GLWESecretPrepared}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, + layouts::{ + GGLWE, GGLWEInfos, LWEInfos, + compressed::{GGLWECompressed, GGLWECompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; -impl GGLWECiphertextCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize - where - A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, - { - GGLWECiphertext::encrypt_sk_scratch_space(module, infos) - } -} - -impl GGLWECiphertextCompressed { +impl GGLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -35,83 +27,124 @@ impl GGLWECiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGLWECompressedEncryptSk, { + module.gglwe_compressed_encrypt_sk(self, pt, sk, seed, source_xe, scratch); + } +} + +impl GGLWECompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + { + GGLWE::encrypt_sk_tmp_bytes(module, infos) + } +} + +pub trait GGLWECompressedEncryptSk { + fn gglwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWECompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal + + VecZnxNormalizeInplace + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAddScalarInplace + + ZnNormalizeInplace, + Scratch: ScratchAvailable, +{ + fn gglwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWECompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + #[cfg(debug_assertions)] { use poulpy_hal::layouts::ZnxInfos; + let sk = &sk.to_ref(); assert_eq!( - self.rank_in(), + res.rank_in(), pt.cols() as u32, - "self.rank_in(): {} != pt.cols(): {}", - self.rank_in(), + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), pt.cols() ); assert_eq!( - self.rank_out(), + res.rank_out(), sk.rank(), - "self.rank_out(): {} != sk.rank(): {}", - self.rank_out(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), sk.rank() ); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GGLWECiphertextCompressed::encrypt_sk_scratch_space(module, self) + GGLWECompressed::encrypt_sk_tmp_bytes(self, res) ); assert!( - self.dnum().0 * self.dsize().0 * self.base2k().0 <= self.k().0, - "self.dnum() : {} * self.dsize() : {} * self.base2k() : {} = {} >= self.k() = {}", - self.dnum(), - self.dsize(), - self.base2k(), - self.dnum().0 * self.dsize().0 * self.base2k().0, - self.k() + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() ); } - let dnum: usize = self.dnum().into(); - let dsize: usize = self.dsize().into(); - let base2k: usize = self.base2k().into(); - let rank_in: usize = self.rank_in().into(); - let cols: usize = (self.rank_out() + 1).into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + let base2k: usize = res.base2k().into(); + let rank_in: usize = res.rank_in().into(); + let cols: usize = (res.rank_out() + 1).into(); let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res); (0..rank_in).for_each(|col_i| { (0..dnum).for_each(|d_i| { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt tmp_pt.data.zero(); // zeroes for next iteration - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); let (seed, mut source_xa_tmp) = source_xa.branch(); - self.seed[col_i * dnum + d_i] = seed; + res.seed[col_i * dnum + d_i] = seed; - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.at_mut(d_i, col_i).data, + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(d_i, col_i).data, cols, true, Some((&tmp_pt, 0)), diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 8dd177f..93519b9 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,35 +1,31 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, - }, + api::{ScratchAvailable, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ - TakeGLWESecretPrepared, + encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, LWEInfos, compressed::GGLWESwitchingKeyCompressed, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, prepared::GLWESecretPrepared, }, }; -impl GGLWESwitchingKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GLWESwitchingKeyCompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) - + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) + (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of(module, infos.rank_out()) } } -impl GGLWESwitchingKeyCompressed { +impl GLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -40,36 +36,65 @@ impl GGLWESwitchingKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpPPolAllocBytes - + VecZnxSwitchRing - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + Module: GGLWEKeyCompressedEncryptSk, { + module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); + } +} + +pub trait GGLWEKeyCompressedEncryptSk { + fn gglwe_key_compressed_encrypt_sk( + &self, + res: &mut R, + sk_in: &SI, + sk_out: &SO, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWESwitchingKeyCompressedToMut, + SI: GLWESecretToRef, + SO: GLWESecretToRef; +} + +impl GGLWEKeyCompressedEncryptSk for Module +where + Module: GGLWECompressedEncryptSk + + SvpPPolBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxSwitchRing + + SvpPrepare, + Scratch: ScratchAvailable, +{ + fn gglwe_key_compressed_encrypt_sk( + &self, + res: &mut R, + sk_in: &SI, + sk_out: &SO, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWESwitchingKeyCompressedToMut, + SI: GLWESecretToRef, + SO: GLWESecretToRef, + { + let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); + let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); + #[cfg(debug_assertions)] { - use crate::layouts::GGLWESwitchingKey; + use crate::layouts::GLWESwitchingKey; - assert!(sk_in.n().0 <= module.n() as u32); - assert!(sk_out.n().0 <= module.n() as u32); + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", + scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) ) } @@ -77,7 +102,7 @@ impl GGLWESwitchingKeyCompressed { let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into()); (0..sk_in.rank().into()).for_each(|i| { - module.vec_znx_switch_ring( + self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), @@ -85,24 +110,24 @@ impl GGLWESwitchingKeyCompressed { ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); (0..sk_out.rank().into()).for_each(|i| { - module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); - module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); + self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); + self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); }); } - self.key.encrypt_sk( - module, + self.gglwe_compressed_encrypt_sk( + &mut res.key, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2, ); - self.sk_in_n = sk_in.n().into(); - self.sk_out_n = sk_out.n().into(); + res.sk_in_n = sk_in.n().into(); + res.sk_out_n = sk_out.n().into(); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 6a75a57..2beaa4b 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,86 +1,83 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, - TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, + encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, compressed::GGLWETensorKeyCompressed, - prepared::Prepare, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; -impl GGLWETensorKeyCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl TensorKeyCompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: - SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { - GGLWETensorKey::encrypt_sk_scratch_space(module, infos) + TensorKey::encrypt_sk_tmp_bytes(module, infos) } } -impl GGLWETensorKeyCompressed { - pub fn encrypt_sk( - &mut self, - module: &Module, - sk: &GLWESecret, +pub trait GGLWETensorKeyCompressedEncryptSk { + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + SvpPrepare - + SvpPPolAllocBytes - + SvpPPolAlloc, - Scratch: ScratchAvailable - + TakeScalarZnx - + TakeVecZnxDft - + TakeGLWESecretPrepared - + ScratchAvailable - + TakeVecZnx - + TakeVecZnxBig, + R: TensorKeyCompressedToMut, + S: GLWESecretToRef; +} + +impl GGLWETensorKeyCompressedEncryptSk for Module +where + Module: GGLWEKeyCompressedEncryptSk + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + + SvpPrepare, + Scratch:, +{ + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: TensorKeyCompressedToMut, + S: GLWESecretToRef, { + let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.rank_out(), sk.rank()); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.rank_out(), sk.rank()); + assert_eq!(res.n(), sk.n()); } let n: usize = sk.n().into(); - let rank: usize = self.rank_out().into(); + let rank: usize = res.rank_out().into(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), self.rank_out()); - sk_dft_prep.prepare(module, sk, scratch_1); + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(sk.n(), res.rank_out()); + sk_dft_prep.prepare(self, sk, scratch_1); let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(n, rank, 1); for i in 0..rank { - module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); + self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); } let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(n, 1, 1); @@ -91,14 +88,14 @@ impl GGLWETensorKeyCompressed { for i in 0..rank { for j in i..rank { - module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); - module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); - module.vec_znx_big_normalize( - self.base2k().into(), + self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); + self.vec_znx_big_normalize( + res.base2k().into(), &mut sk_ij.data.as_vec_znx_mut(), 0, - self.base2k().into(), + res.base2k().into(), &sk_ij_big, 0, scratch_5, @@ -106,9 +103,30 @@ impl GGLWETensorKeyCompressed { let (seed_xa_tmp, _) = source_xa.branch(); - self.at_mut(i, j) - .encrypt_sk(module, &sk_ij, sk, seed_xa_tmp, source_xe, scratch_5); + self.gglwe_key_compressed_encrypt_sk( + res.at_mut(i, j), + &sk_ij, + sk, + seed_xa_tmp, + source_xe, + scratch_5, + ); } } } } + +impl TensorKeyCompressed { + pub fn encrypt_sk( + &mut self, + module: &Module, + sk: &GLWESecret, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + Module: GGLWETensorKeyCompressedEncryptSk, + { + module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + } +} diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index e49f246..567f04f 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,32 +1,118 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, - encryption::{SIGMA, glwe_encrypt_sk_internal}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, compressed::GGSWCiphertextCompressed, prepared::GLWESecretPrepared, + GGSW, GGSWInfos, GLWEInfos, LWEInfos, + compressed::{GGSWCompressed, GGSWCompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertextCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GGSWCompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_tmp_bytes(module, infos) } } -impl GGSWCiphertextCompressed { +pub trait GGSWCompressedEncryptSk { + fn ggsw_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGSWCompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch:, +{ + fn ggsw_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + } + + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); + + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + + let mut source = Source::new(seed_xa); + + res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols]; + + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); + + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + + for col_j in 0..rank + 1 { + // rlwe encrypt of vec_znx_pt into vec_znx_ct + + let (seed, mut source_xa_tmp) = source.branch(); + + res.seed[row_i * cols + col_j] = seed; + + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(row_i, col_j).data, + cols, + true, + Some((&tmp_pt, col_j)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scratch_1, + ); + } + } + } +} + +impl GGSWCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -37,71 +123,8 @@ impl GGSWCiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGSWCompressedEncryptSk, { - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(self.rank(), sk.rank()); - assert_eq!(self.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = self.base2k().into(); - let rank: usize = self.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = self.dsize().into(); - - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&self.glwe_layout()); - - let mut source = Source::new(seed_xa); - - self.seed = vec![[0u8; 32]; self.dnum().0 as usize * cols]; - - (0..self.dnum().into()).for_each(|row_i| { - tmp_pt.data.zero(); - - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - - (0..rank + 1).for_each(|col_j| { - // rlwe encrypt of vec_znx_pt into vec_znx_ct - - let (seed, mut source_xa_tmp) = source.branch(); - - self.seed[row_i * cols + col_j] = seed; - - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.at_mut(row_i, col_j).data, - cols, - true, - Some((&tmp_pt, col_j)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scratch_1, - ); - }); - }); + module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 834f968..f04a07a 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,31 +1,83 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - encryption::{SIGMA, glwe_ct::glwe_encrypt_sk_internal}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + compressed::{GLWECompressed, GLWECompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertextCompressed> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GLWECompressed> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GLWECiphertext::encrypt_sk_scratch_space(module, infos) + GLWE::encrypt_sk_tmp_bytes(module, infos) } } -impl GLWECiphertextCompressed { +pub trait GLWECompressedEncryptSk { + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal, +{ + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); + let mut source_xa: Source = Source::new(seed_xa); + let cols: usize = (res.rank() + 1).into(); + + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.data, + cols, + true, + Some((pt, 0)), + sk, + &mut source_xa, + source_xe, + SIGMA, + scratch, + ); + + res.seed = seed_xa; + } +} + +impl GLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -36,65 +88,8 @@ impl GLWECiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GLWECompressedEncryptSk, { - self.encrypt_sk_internal(module, Some((pt, 0)), sk, seed_xa, source_xe, scratch); - } - - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_sk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - let mut source_xa = Source::new(seed_xa); - let cols: usize = (self.rank() + 1).into(); - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.data, - cols, - true, - pt, - sk, - &mut source_xa, - source_xe, - SIGMA, - scratch, - ); - self.seed = seed_xa; + module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 6d45b37..6536c7e 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,34 +1,33 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos}, +use crate::layouts::{ + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, }; -impl GGLWEAutomorphismKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl AutomorphismKey> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { @@ -37,62 +36,102 @@ impl GGLWEAutomorphismKey> { _infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) + GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) } } -impl GGLWEAutomorphismKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, +pub trait GGLWEAutomorphismKeyEncryptSk { + fn gglwe_automorphism_key_encrypt_sk( + &self, + res: &mut A, p: i64, - sk: &GLWESecret, + sk: &B, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolAllocBytes - + VecZnxAutomorphism, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + A: AutomorphismKeyToMut, + B: GLWESecretToRef; +} + +impl AutomorphismKey +where + Self: AutomorphismKeyToMut, +{ + pub fn encrypt_sk( + &mut self, + module: &Module, + p: i64, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretToRef, + Module: GGLWEAutomorphismKeyEncryptSk, { + module.gglwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + } +} + +impl GGLWEAutomorphismKeyEncryptSk for Module +where + Module: VecZnxAddScalarInplace + + VecZnxDftBytesOf + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalizeTmpBytes + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + SvpPPolBytesOf + + VecZnxAutomorphism, + Scratch: ScratchAvailable, +{ + fn gglwe_automorphism_key_encrypt_sk( + &self, + res: &mut A, + p: i64, + sk: &B, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + A: AutomorphismKeyToMut, + B: GLWESecretToRef, + { + let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + #[cfg(debug_assertions)] { use crate::layouts::{GLWEInfos, LWEInfos}; - assert_eq!(self.n(), sk.n()); - assert_eq!(self.rank_out(), self.rank_in()); - assert_eq!(sk.rank(), self.rank_out()); + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", + scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, self) + AutomorphismKey::encrypt_sk_tmp_bytes(self, res) ) } let (mut sk_out, scratch_1) = scratch.take_glwe_secret(sk.n(), sk.rank()); { - (0..self.rank_out().into()).for_each(|i| { - module.vec_znx_automorphism( - module.galois_element_inv(p), + (0..res.rank_out().into()).for_each(|i| { + self.vec_znx_automorphism( + self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), i, &sk.data.as_vec_znx(), @@ -101,9 +140,9 @@ impl GGLWEAutomorphismKey { }); } - self.key - .encrypt_sk(module, sk, &sk_out, source_xa, source_xe, scratch_1); + res.key + .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); - self.p = p; + res.p = p; } } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 51054cb..d333892 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,29 +1,28 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, ZnxZero}, + api::{ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, - layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}, + encryption::glwe_ct::GLWEEncryptSk, + layouts::{ + GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; -impl GGLWECiphertext> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GGLWE> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + (GLWEPlaintext::alloc_bytes(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) + + (GLWEPlaintext::bytes_of_from_infos(module, &infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) } - pub fn encrypt_pk_scratch_space(_module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(_module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { @@ -31,78 +30,88 @@ impl GGLWECiphertext> { } } -impl GGLWECiphertext { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, +pub trait GGLWEEncryptSk { + fn gglwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + R: GGLWEToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGLWEEncryptSk for Module +where + Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: ScratchAvailable, +{ + fn gglwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGLWEToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + #[cfg(debug_assertions)] { use poulpy_hal::layouts::ZnxInfos; + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); assert_eq!( - self.rank_in(), + res.rank_in(), pt.cols() as u32, - "self.rank_in(): {} != pt.cols(): {}", - self.rank_in(), + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), pt.cols() ); assert_eq!( - self.rank_out(), + res.rank_out(), sk.rank(), - "self.rank_out(): {} != sk.rank(): {}", - self.rank_out(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), sk.rank() ); - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(module, self), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(module, self.rank()={}, self.size()={}): {}", + scratch.available() >= GGLWE::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", scratch.available(), - self.rank_out(), - self.size(), - GGLWECiphertext::encrypt_sk_scratch_space(module, self) + res.rank_out(), + res.size(), + GGLWE::encrypt_sk_tmp_bytes(self, res) ); assert!( - self.dnum().0 * self.dsize().0 * self.base2k().0 <= self.k().0, - "self.dnum() : {} * self.dsize() : {} * self.base2k() : {} = {} >= self.k() = {}", - self.dnum(), - self.dsize(), - self.base2k(), - self.dnum().0 * self.dsize().0 * self.base2k().0, - self.k() + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() ); } - let dnum: usize = self.dnum().into(); - let dsize: usize = self.dsize().into(); - let base2k: usize = self.base2k().into(); - let rank_in: usize = self.rank_in().into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + 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); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res); // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns // // Example for ksk rank 2 to rank 3: @@ -114,17 +123,39 @@ impl GGLWECiphertext { // // (-(a*s) + s0, a) // (-(b*s) + s1, b) - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|row_i| { + + for col_i in 0..rank_in { + for row_i in 0..dnum { // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt tmp_pt.data.zero(); // zeroes for next iteration - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, col_i); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); - - // rlwe encrypt of vec_znx_pt into vec_znx_ct - self.at_mut(row_i, col_i) - .encrypt_sk(module, &tmp_pt, sk, source_xa, source_xe, scrach_1); - }); - }); + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + self.glwe_encrypt_sk( + &mut res.at_mut(row_i, col_i), + &tmp_pt, + sk, + source_xa, + source_xe, + scrach_1, + ); + } + } + } +} + +impl GGLWE { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: &ScalarZnx, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + Module: GGLWEEncryptSk, + { + module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 0629bec..ef9b5bf 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,41 +1,37 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos, prepared::GLWESecretPrepared, - }, +use crate::layouts::{ + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, }; -impl GGLWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GLWESwitchingKey> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) - + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) + (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, { - GGLWECiphertext::encrypt_pk_scratch_space(module, _infos) + GGLWE::encrypt_pk_tmp_bytes(module, _infos) } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -47,7 +43,7 @@ impl GGLWESwitchingKey { scratch: &mut Scratch, ) where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -62,18 +58,18 @@ impl GGLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + + SvpPPolBytesOf, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { assert!(sk_in.n().0 <= module.n() as u32); assert!(sk_out.n().0 <= module.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", + scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) ) } @@ -89,7 +85,7 @@ impl GGLWESwitchingKey { ); }); - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank()); + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1); (0..sk_out.rank().into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 1946929..62d1a15 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,39 +1,34 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, - TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, - prepared::{GLWESecretPrepared, Prepare}, - }, +use crate::layouts::{ + GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, RingDegree, TensorKey, prepared::GLWESecretPrepared, }; -impl GGLWETensorKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl TensorKey> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: - SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) - + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1) - + module.vec_znx_big_alloc_bytes(1, 1) - + module.vec_znx_dft_alloc_bytes(1, 1) - + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecretPrepared::bytes_of(module, infos.rank_out()) + + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + module.bytes_of_vec_znx_big(1, 1) + + module.bytes_of_vec_znx_dft(1, 1) + + GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } -impl GGLWETensorKey { +impl TensorKey { pub fn encrypt_sk( &mut self, module: &Module, @@ -45,7 +40,7 @@ impl GGLWETensorKey { Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -60,9 +55,8 @@ impl GGLWETensorKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, - Scratch: - TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared + TakeVecZnxBig, + + SvpPPolBytesOf, + Scratch:, { #[cfg(debug_assertions)] { @@ -70,7 +64,7 @@ impl GGLWETensorKey { assert_eq!(self.n(), sk.n()); } - let n: Degree = sk.n(); + let n: RingDegree = sk.n(); let rank: Rank = self.rank_out(); let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(n, rank); diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 6195458..b044ae3 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,33 +1,112 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, VecZnx, ZnxZero}, + api::{VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, source::Source, }; use crate::{ - TakeGLWEPt, - layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESecretPrepared}, + SIGMA, + encryption::glwe_ct::GLWEEncryptSkInternal, + layouts::{ + GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, }; -impl GGSWCiphertext> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GGSW> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size = infos.size(); - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + VecZnx::alloc_bytes(module.n(), (infos.rank() + 1).into(), size) - + VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes((infos.rank() + 1).into(), size) + GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) + + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + + VecZnx::bytes_of(module.n(), 1, size) + + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) } } -impl GGSWCiphertext { +pub trait GGSWEncryptSk { + fn ggsw_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef; +} + +impl GGSWEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch:, +{ + fn ggsw_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(pt.n(), self.n()); + assert_eq!(sk.n(), self.n() as u32); + } + + let k: usize = res.k().into(); + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let dsize: usize = res.dsize().into(); + let cols: usize = (rank + 1).into(); + + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&res.glwe_layout()); + + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + for col_j in 0..rank + 1 { + self.glwe_encrypt_sk_internal( + base2k, + k, + res.at_mut(row_i, col_j).data_mut(), + cols, + false, + Some((&tmp_pt, col_j)), + sk, + source_xa, + source_xe, + SIGMA, + scratch_1, + ); + } + } + } +} + +impl GGSW { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -38,56 +117,8 @@ impl GGSWCiphertext { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GGSWEncryptSk, { - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(self.rank(), sk.rank()); - assert_eq!(self.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = self.base2k().into(); - let rank: usize = self.rank().into(); - let dsize: usize = self.dsize().into(); - - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(&self.glwe_layout()); - - (0..self.dnum().into()).for_each(|row_i| { - tmp_pt.data.zero(); - - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - module.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - - (0..rank + 1).for_each(|col_j| { - // rlwe encrypt of vec_znx_pt into vec_znx_ct - - self.at_mut(row_i, col_j).encrypt_sk_internal( - module, - Some((&tmp_pt, col_j)), - sk, - source_xa, - source_xe, - scratch_1, - ); - }); - }); + module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 8ecacc6..16bbadf 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSvpPPol, - TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, + VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, ZnxInfos, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, }; @@ -13,157 +13,155 @@ use crate::{ dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, - prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEToMut, LWEInfos, + prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertext> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GLWE> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() - + 2 * VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) } - pub fn encrypt_pk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - ((module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_big_alloc_bytes(1, size)) - | ScalarZnx::alloc_bytes(module.n(), 1)) - + module.svp_ppol_alloc_bytes(1) + ((module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) + + module.bytes_of_svp_ppol(1) + module.vec_znx_normalize_tmp_bytes() } } -impl GLWECiphertext { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( +impl GLWE { + pub fn encrypt_sk( &mut self, module: &Module, - pt: &GLWEPlaintext, - sk: &GLWESecretPrepared, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + Module: GLWEEncryptSk, { + module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); + } + + pub fn encrypt_zero_sk( + &mut self, + module: &Module, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretPreparedToRef, + Module: GLWEEncryptZeroSk, + { + module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); + } + + pub fn encrypt_pk( + &mut self, + module: &Module, + pt: &P, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptPk, + { + module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); + } + + pub fn encrypt_zero_pk( + &mut self, + module: &Module, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + K: GLWEPublicKeyPreparedToRef, + Module: GLWEEncryptZeroPk, + { + module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); + } +} + +pub trait GLWEEncryptSk { + fn glwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Scratch: ScratchAvailable, +{ + fn glwe_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + { + let mut res: GLWE<&mut [u8]> = res.to_mut(); + let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.rank(), sk.rank()); - assert_eq!(sk.n(), self.n()); - assert_eq!(pt.n(), self.n()); + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert_eq!(pt.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", + scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self) + GLWE::encrypt_sk_tmp_bytes(self, &res) ) } - self.encrypt_sk_internal(module, Some((pt, 0)), sk, source_xa, source_xe, scratch); - } - - pub fn encrypt_zero_sk( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), sk.rank()); - assert_eq!(sk.n(), self.n()); - assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(module, self), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", - scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(module, self) - ) - } - self.encrypt_sk_internal( - module, - None::<(&GLWEPlaintext>, usize)>, - sk, - source_xa, - source_xe, - scratch, - ); - } - - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_sk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - let cols: usize = (self.rank() + 1).into(); - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.data, + let cols: usize = (res.rank() + 1).into(); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + res.data_mut(), cols, false, - pt, + Some((&pt, 0)), sk, source_xa, source_xe, @@ -171,46 +169,136 @@ impl GLWECiphertext { scratch, ); } +} - #[allow(clippy::too_many_arguments)] - pub fn encrypt_pk( - &mut self, - module: &Module, - pt: &GLWEPlaintext, - pk: &GLWEPublicKeyPrepared, - source_xu: &mut Source, +pub trait GLWEEncryptZeroSk { + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWEToMut, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptZeroSk for Module +where + Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Scratch: ScratchAvailable, +{ + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + S: GLWESecretPreparedToRef, { - self.encrypt_pk_internal::(module, Some((pt, 0)), pk, source_xu, source_xe, scratch); + let mut res: GLWE<&mut [u8]> = res.to_mut(); + + #[cfg(debug_assertions)] + { + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert!( + scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", + scratch.available(), + GLWE::encrypt_sk_tmp_bytes(self, &res) + ) + } + + let cols: usize = (res.rank() + 1).into(); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + res.data_mut(), + cols, + false, + None::<(&GLWEPlaintext>, usize)>, + sk, + source_xa, + source_xe, + SIGMA, + scratch, + ); } +} - pub fn encrypt_zero_pk( - &mut self, - module: &Module, - pk: &GLWEPublicKeyPrepared, +pub trait GLWEEncryptPk { + fn glwe_encrypt_pk( + &self, + res: &mut R, + pt: &P, + pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWEToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptPk for Module +where + Module: GLWEEncryptPkInternal, +{ + fn glwe_encrypt_pk( + &self, + res: &mut R, + pt: &P, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, { - self.encrypt_pk_internal::, DataPk, B>( - module, + self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); + } +} + +pub trait GLWEEncryptZeroPk { + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptZeroPk for Module +where + Module: GLWEEncryptPkInternal, +{ + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + K: GLWEPublicKeyPreparedToRef, + { + self.glwe_encrypt_pk_internal( + res, None::<(&GLWEPlaintext>, usize)>, pk, source_xu, @@ -218,45 +306,69 @@ impl GLWECiphertext { scratch, ); } +} - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_pk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - pk: &GLWEPublicKeyPrepared, +pub(crate) trait GLWEEncryptPkInternal { + fn glwe_encrypt_pk_internal( + &self, + res: &mut R, + pt: Option<(&P, usize)>, + pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: SvpPrepare - + SvpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddNormal - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch: TakeSvpPPol + TakeScalarZnx + TakeVecZnxDft, + R: GLWEToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef; +} + +impl GLWEEncryptPkInternal for Module +where + Module: SvpPrepare + + SvpApplyDftToDft + + VecZnxIdftApplyConsume + + VecZnxBigAddNormal + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize, + Scratch:, +{ + fn glwe_encrypt_pk_internal( + &self, + res: &mut R, + pt: Option<(&P, usize)>, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + P: GLWEPlaintextToRef, + K: GLWEPublicKeyPreparedToRef, { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref(); + #[cfg(debug_assertions)] { - assert_eq!(self.base2k(), pk.base2k()); - assert_eq!(self.n(), pk.n()); - assert_eq!(self.rank(), pk.rank()); + assert_eq!(res.base2k(), pk.base2k()); + assert_eq!(res.n(), pk.n()); + assert_eq!(res.rank(), pk.rank()); if let Some((pt, _)) = pt { - assert_eq!(pt.base2k(), pk.base2k()); - assert_eq!(pt.n(), pk.n()); + assert_eq!(pt.to_ref().base2k(), pk.base2k()); + assert_eq!(pt.to_ref().n(), pk.n()); } } let base2k: usize = pk.base2k().into(); let size_pk: usize = pk.size(); - let cols: usize = (self.rank() + 1).into(); + let cols: usize = (res.rank() + 1).into(); // Generates u according to the underlying secret distribution. - let (mut u_dft, scratch_1) = scratch.take_svp_ppol(self.n().into(), 1); + let (mut u_dft, scratch_1) = scratch.take_svp_ppol(res.n().into(), 1); { - let (mut u, _) = scratch_1.take_scalar_znx(self.n().into(), 1); + let (mut u, _) = scratch_1.take_scalar_znx(res.n().into(), 1); match pk.dist { Distribution::NONE => panic!( "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \ @@ -270,20 +382,20 @@ impl GLWECiphertext { Distribution::ZERO => {} } - module.svp_prepare(&mut u_dft, 0, &u, 0); + 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.n().into(), 1, size_pk); + let (mut ci_dft, scratch_2) = scratch_1.take_vec_znx_dft(res.n().into(), 1, size_pk); // ci_dft = DFT(u) * DFT(pk[i]) - module.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); + self.svp_apply_dft_to_dft(&mut ci_dft, 0, &u_dft, 0, &pk.data, i); // ci_big = u * p[i] - let mut ci_big = module.vec_znx_idft_apply_consume(ci_dft); + let mut ci_big = self.vec_znx_idft_apply_consume(ci_dft); // ci_big = u * pk[i] + e - module.vec_znx_big_add_normal( + self.vec_znx_big_add_normal( base2k, &mut ci_big, 0, @@ -297,31 +409,38 @@ impl GLWECiphertext { if let Some((pt, col)) = pt && col == i { - module.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.data, 0); + self.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.to_ref().data, 0); } // ct[i] = norm(ci_big) - module.vec_znx_big_normalize(base2k, &mut self.data, i, base2k, &ci_big, 0, scratch_2); + self.vec_znx_big_normalize(base2k, &mut res.data, i, base2k, &ci_big, 0, scratch_2); }); } } -#[allow(clippy::too_many_arguments)] -pub(crate) fn glwe_encrypt_sk_internal( - module: &Module, - base2k: usize, - k: usize, - ct: &mut VecZnx, - cols: usize, - compressed: bool, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - sigma: f64, - scratch: &mut Scratch, -) where - Module: VecZnxDftAllocBytes +pub(crate) trait GLWEEncryptSkInternal { + fn glwe_encrypt_sk_internal( + &self, + base2k: usize, + k: usize, + res: &mut R, + cols: usize, + compressed: bool, + pt: Option<(&P, usize)>, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + R: VecZnxToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWEEncryptSkInternal for Module +where + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -334,74 +453,96 @@ pub(crate) fn glwe_encrypt_sk_internal + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { - #[cfg(debug_assertions)] + fn glwe_encrypt_sk_internal( + &self, + base2k: usize, + k: usize, + res: &mut R, + cols: usize, + compressed: bool, + pt: Option<(&P, usize)>, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + sigma: f64, + scratch: &mut Scratch, + ) where + R: VecZnxToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, { - if compressed { - assert_eq!( - ct.cols(), - 1, - "invalid ciphertext: compressed tag=true but #cols={} != 1", - ct.cols() - ) - } - } + let ct: &mut VecZnx<&mut [u8]> = &mut res.to_mut(); + let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); - let size: usize = ct.size(); - - let (mut c0, scratch_1) = scratch.take_vec_znx(ct.n(), 1, size); - c0.zero(); - - { - let (mut ci, scratch_2) = scratch_1.take_vec_znx(ct.n(), 1, size); - - // ct[i] = uniform - // ct[0] -= c[i] * s[i], - (1..cols).for_each(|i| { - let col_ct: usize = if compressed { 0 } else { i }; - - // ct[i] = uniform (+ pt) - module.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); - - let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(ct.n(), 1, size); - - // ci = ct[i] - pt - // i.e. we act as we sample ct[i] already as uniform + pt - // and if there is a pt, then we subtract it before applying DFT - if let Some((pt, col)) = pt { - if i == col { - module.vec_znx_sub(&mut ci, 0, ct, col_ct, &pt.data, 0); - module.vec_znx_normalize_inplace(base2k, &mut ci, 0, scratch_3); - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &ci, 0); - } else { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); - } - } else { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + #[cfg(debug_assertions)] + { + if compressed { + assert_eq!( + ct.cols(), + 1, + "invalid ciphertext: compressed tag=true but #cols={} != 1", + ct.cols() + ) } + } - module.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); - let ci_big: VecZnxBig<&mut [u8], B> = module.vec_znx_idft_apply_consume(ci_dft); + let size: usize = ct.size(); - // use c[0] as buffer, which is overwritten later by the normalization step - module.vec_znx_big_normalize(base2k, &mut ci, 0, base2k, &ci_big, 0, scratch_3); + let (mut c0, scratch_1) = scratch.take_vec_znx(ct.n(), 1, size); + c0.zero(); - // c0_tmp = -c[i] * s[i] (use c[0] as buffer) - module.vec_znx_sub_inplace(&mut c0, 0, &ci, 0); - }); + { + let (mut ci, scratch_2) = scratch_1.take_vec_znx(ct.n(), 1, size); + + // ct[i] = uniform + // ct[0] -= c[i] * s[i], + (1..cols).for_each(|i| { + let col_ct: usize = if compressed { 0 } else { i }; + + // ct[i] = uniform (+ pt) + self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); + + let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(ct.n(), 1, size); + + // ci = ct[i] - pt + // i.e. we act as we sample ct[i] already as uniform + pt + // and if there is a pt, then we subtract it before applying DFT + if let Some((pt, col)) = pt { + if i == col { + self.vec_znx_sub(&mut ci, 0, ct, col_ct, &pt.to_ref().data, 0); + self.vec_znx_normalize_inplace(base2k, &mut ci, 0, scratch_3); + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &ci, 0); + } else { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + } + } else { + self.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, ct, col_ct); + } + + self.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1); + let ci_big: VecZnxBig<&mut [u8], B> = self.vec_znx_idft_apply_consume(ci_dft); + + // use c[0] as buffer, which is overwritten later by the normalization step + self.vec_znx_big_normalize(base2k, &mut ci, 0, base2k, &ci_big, 0, scratch_3); + + // c0_tmp = -c[i] * s[i] (use c[0] as buffer) + self.vec_znx_sub_inplace(&mut c0, 0, &ci, 0); + }); + } + + // c[0] += e + self.vec_znx_add_normal(base2k, &mut c0, 0, k, source_xe, sigma, SIGMA_BOUND); + + // c[0] += m if col = 0 + if let Some((pt, col)) = pt + && col == 0 + { + self.vec_znx_add_inplace(&mut c0, 0, &pt.to_ref().data, 0); + } + + // c[0] = norm(c[0]) + self.vec_znx_normalize(base2k, ct, 0, base2k, &c0, 0, scratch_1); } - - // c[0] += e - module.vec_znx_add_normal(base2k, &mut c0, 0, k, source_xe, sigma, SIGMA_BOUND); - - // c[0] += m if col = 0 - if let Some((pt, col)) = pt - && col == 0 - { - module.vec_znx_add_inplace(&mut c0, 0, &pt.data, 0); - } - - // c[0] = norm(c[0]) - module.vec_znx_normalize(base2k, ct, 0, base2k, &c0, 0, scratch_1); } diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index c7cdaeb..d89f515 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,50 +1,43 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, - oep::{ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxDftImpl, TakeVecZnxImpl}, source::Source, }; -use crate::layouts::{GLWECiphertext, GLWEPublicKey, prepared::GLWESecretPrepared}; +use crate::{ + encryption::glwe_ct::GLWEEncryptZeroSk, + layouts::{ + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, +}; -impl GLWEPublicKey { - pub fn generate_from_sk( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module:, - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl, +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, + S: GLWESecretPreparedToRef; +} + +impl GLWEPublicKeyGenerate for Module +where + Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, +{ + fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + R: GLWEPublicKeyToMut, + S: GLWESecretPreparedToRef, { + let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + #[cfg(debug_assertions)] { use crate::{Distribution, layouts::LWEInfos}; - assert_eq!(self.n(), sk.n()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); if sk.dist == Distribution::NONE { panic!("invalid sk: SecretDistribution::NONE") @@ -52,10 +45,25 @@ impl GLWEPublicKey { } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); - let mut tmp: GLWECiphertext> = GLWECiphertext::alloc(self); - tmp.encrypt_zero_sk(module, sk, source_xa, source_xe, scratch.borrow()); - self.dist = sk.dist; + let mut tmp: GLWE> = GLWE::alloc_from_infos(res); + + tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); + res.dist = sk.dist; + } +} + +impl GLWEPublicKey { + pub fn generate( + &mut self, + module: &Module, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + ) where + Module: GLWEPublicKeyGenerate, + { + module.glwe_public_key_generate(self, sk, source_xa, source_xe); } } diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index b65ce4e..971766e 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,32 +1,30 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{GGLWEInfos, GGLWESwitchingKey, GLWESecret, GLWEToLWEKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared}, +use crate::layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, }; -impl GLWEToLWEKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize +impl GLWEToLWESwitchingKey> { + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) - + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) + GLWESecretPrepared::bytes_of(module, infos.rank_in()) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) } } -impl GLWEToLWEKey { +impl GLWEToLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -41,7 +39,7 @@ impl GLWEToLWEKey { DGlwe: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -56,8 +54,8 @@ impl GLWEToLWEKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + + SvpPPolBytesOf, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 4dd09ac..a01d95f 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -7,10 +7,10 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}, }; -impl LWECiphertext { +impl LWE { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 66ae685..2fd60ff 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,27 +1,24 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, + VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWESwitchingKey, Rank, - prepared::GLWESecretPrepared, - }, +use crate::layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, RingDegree, + prepared::GLWESecretPrepared, }; impl LWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { debug_assert_eq!( infos.dsize().0, @@ -38,9 +35,9 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(RingDegree(module.n() as u32), Rank(1)) + + GLWESecretPrepared::bytes_of(module, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) } } @@ -59,7 +56,7 @@ impl LWESwitchingKey { DOut: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -74,8 +71,8 @@ impl LWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + + SvpPPolBytesOf, + Scratch:, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 204e84b..041c7c4 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,32 +1,29 @@ use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; -use crate::{ - TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, -}; +use crate::layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank, RingDegree}; impl LWEToGLWESwitchingKey> { - pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, + Module: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, { debug_assert_eq!( infos.rank_in(), Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + + GLWESecret::bytes_of(RingDegree(module.n() as u32), infos.rank_in()) } } @@ -45,7 +42,7 @@ impl LWEToGLWESwitchingKey { DGlwe: DataRef, Module: VecZnxAutomorphismInplace + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -60,8 +57,8 @@ impl LWEToGLWESwitchingKey { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared, + + SvpPPolBytesOf, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { diff --git a/poulpy-core/src/encryption/mod.rs b/poulpy-core/src/encryption/mod.rs index 9380933..fb9a459 100644 --- a/poulpy-core/src/encryption/mod.rs +++ b/poulpy-core/src/encryption/mod.rs @@ -11,7 +11,5 @@ mod lwe_ct; mod lwe_ksk; mod lwe_to_glwe_ksk; -pub(crate) use glwe_ct::glwe_encrypt_sk_internal; - pub const SIGMA: f64 = 3.2; pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA; diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index cb35a4c..871eab8 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -1,83 +1,46 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, +use poulpy_hal::layouts::{Backend, DataMut, Scratch}; + +use crate::{ + ScratchTakeCore, + external_product::gglwe_ksk::GGLWEExternalProduct, + layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GGSWInfos, prepared::GGSWCiphertextPrepared}; - -impl GGLWEAutomorphismKey> { - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, +impl AutomorphismKey> { + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, ) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, + M: GGLWEExternalProduct, { - GGLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) - } - - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - ggsw_infos: &GGSW, - ) -> usize - where - OUT: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, - { - GGLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) + module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } -impl GGLWEAutomorphismKey { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +impl AutomorphismKey { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: AutomorphismKeyToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - self.key.external_product(module, &lhs.key, rhs, scratch); + module.gglwe_external_product(&mut self.key.key, &a.to_ref().key.key, b, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - self.key.external_product_inplace(module, rhs, scratch); + module.gglwe_external_product_inplace(&mut self.key.key, a, scratch); } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 2eff45c..5bb4557 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -1,144 +1,134 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; + +use crate::{ + GLWEExternalProduct, ScratchTakeCore, + layouts::{ + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGLWESwitchingKey, GGSWInfos, GLWECiphertext, prepared::GGSWCiphertextPrepared}; - -impl GGLWESwitchingKey> { - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - ggsw_infos: &GGSW, - ) -> usize +pub trait GGLWEExternalProduct +where + Self: GLWEExternalProduct, +{ + fn gglwe_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + B: GGSWInfos, { - GLWECiphertext::external_product_scratch_space( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - ggsw_infos, - ) + self.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - ggsw_infos: &GGSW, - ) -> usize + fn gglwe_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) where - OUT: GGLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEToMut, + A: GGLWEToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let b: &GGSWPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank_in(), + a.rank_in(), + "res input rank_in: {} != a input rank_in: {}", + res.rank_in(), + a.rank_in() + ); + assert_eq!( + a.rank_out(), + b.rank(), + "a output rank_out: {} != b rank: {}", + a.rank_out(), + b.rank() + ); + assert_eq!( + res.rank_out(), + b.rank(), + "res output rank_out: {} != b rank: {}", + res.rank_out(), + b.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_external_product(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } + } + + for row in res.dnum().min(a.dnum()).into()..res.dnum().into() { + for col in 0..res.rank_in().into() { + res.at_mut(row, col).data_mut().zero(); + } + } + } + + fn gglwe_external_product_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGSWPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!( + res.rank_out(), + a.rank(), + "res output rank: {} != a rank: {}", + res.rank_out(), + a.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_external_product_inplace(&mut res.at_mut(row, col), a, scratch); + } + } } } -impl GGLWESwitchingKey { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +impl GGLWEExternalProduct for Module where Self: GLWEExternalProduct {} + +impl GLWESwitchingKey> { + 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, { - #[cfg(debug_assertions)] - { - use crate::layouts::GLWEInfos; - - assert_eq!( - self.rank_in(), - lhs.rank_in(), - "ksk_out input rank: {} != ksk_in input rank: {}", - self.rank_in(), - lhs.rank_in() - ); - assert_eq!( - lhs.rank_out(), - rhs.rank(), - "ksk_in output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - assert_eq!( - self.rank_out(), - rhs.rank(), - "ksk_out output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - }); - - (self.dnum().min(lhs.dnum()).into()..self.dnum().into()).for_each(|row_i| { - (0..self.rank_in().into()).for_each(|col_j| { - self.at_mut(row_i, col_j).data.zero(); - }); - }); - } - - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use crate::layouts::GLWEInfos; - - assert_eq!( - self.rank_out(), - rhs.rank(), - "ksk_out output rank: {} != ggsw rank: {}", - self.rank_out(), - rhs.rank() - ); - } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product_inplace(module, rhs, scratch); - }); - }); + module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) + } +} + +impl GLWESwitchingKey { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGLWEExternalProduct, + A: GLWESwitchingKeyToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.gglwe_external_product(&mut self.key, &a.to_ref().key, 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(&mut self.key, a, scratch); } } diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index a458de1..d3a59a6 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -1,143 +1,136 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, + api::ScratchAvailable, + layouts::{Backend, DataMut, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWCiphertextPrepared}; +use crate::{ + GLWEExternalProduct, ScratchTakeCore, + layouts::{ + GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, + }, +}; -impl GGSWCiphertext> { - #[allow(clippy::too_many_arguments)] - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - apply_infos: &GGSW, - ) -> usize +pub trait GGSWExternalProduct +where + Self: GLWEExternalProduct, +{ + fn ggsw_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GGSWInfos, - IN: GGSWInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + B: GGSWInfos, { - GLWECiphertext::external_product_scratch_space( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - apply_infos, - ) + self.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - apply_infos: &GGSW, - ) -> usize + fn ggsw_external_product(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) where - OUT: GGSWInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GGSWToMut, + A: GGSWToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + let b: &GGSWPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank(), + a.rank(), + "res rank: {} != a rank: {}", + res.rank(), + a.rank() + ); + assert_eq!( + res.rank(), + b.rank(), + "res rank: {} != b rank: {}", + res.rank(), + b.rank() + ); + + assert!(scratch.available() >= self.ggsw_external_product_tmp_bytes(res, a, b)); + + let min_dnum: usize = res.dnum().min(a.dnum()).into(); + + for row in 0..min_dnum { + for col in 0..(res.rank() + 1).into() { + self.glwe_external_product(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } + } + + for row in min_dnum..res.dnum().into() { + for col in 0..(res.rank() + 1).into() { + res.at_mut(row, col).data.zero(); + } + } + } + + fn ggsw_external_product_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSWPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!( + res.rank(), + a.rank(), + "res rank: {} != a rank: {}", + res.rank(), + a.rank() + ); + + for row in 0..res.dnum().into() { + for col in 0..(res.rank() + 1).into() { + self.glwe_external_product_inplace(&mut res.at_mut(row, col), a, scratch); + } + } } } -impl GGSWCiphertext { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GGSWCiphertext, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +impl GGSWExternalProduct for Module where Self: GLWEExternalProduct {} + +impl GGSW> { + pub fn external_product_tmp_bytes( + &self, + module: &M, + res_infos: &R, + a_infos: &A, + b_infos: &B, + ) -> usize + where + R: GGSWInfos, + A: GGSWInfos, + B: GGSWInfos, + M: GGSWExternalProduct, { - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; - - assert_eq!(lhs.n(), self.n()); - assert_eq!(rhs.n(), self.n()); - - assert_eq!( - self.rank(), - lhs.rank(), - "ggsw_out rank: {} != ggsw_in rank: {}", - self.rank(), - lhs.rank() - ); - assert_eq!( - self.rank(), - rhs.rank(), - "ggsw_in rank: {} != ggsw_apply rank: {}", - self.rank(), - rhs.rank() - ); - - assert!(scratch.available() >= GGSWCiphertext::external_product_scratch_space(module, self, lhs, rhs)) - } - - let min_dnum: usize = self.dnum().min(lhs.dnum()).into(); - - (0..(self.rank() + 1).into()).for_each(|col_i| { - (0..min_dnum).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - (min_dnum..self.dnum().into()).for_each(|row_i| { - self.at_mut(row_i, col_i).data.zero(); - }); - }); - } - - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; - - assert_eq!(rhs.n(), self.n()); - assert_eq!( - self.rank(), - rhs.rank(), - "ggsw_out rank: {} != ggsw_apply: {}", - self.rank(), - rhs.rank() - ); - } - - (0..(self.rank() + 1).into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .external_product_inplace(module, rhs, scratch); - }); - }); + module.ggsw_external_product_tmp_bytes(res_infos, a_infos, b_infos) + } +} + +impl GGSW { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + M: GGSWExternalProduct, + A: GGSWToRef, + B: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_external_product(self, a, b, scratch); + } + + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + M: GGSWExternalProduct, + A: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + module.ggsw_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index d764507..ab9968c 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -1,102 +1,57 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, + ModuleN, ScratchTakeBasic, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, - layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, + layouts::{Backend, DataMut, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, }; use crate::{ - GLWEExternalProduct, GLWEExternalProductInplace, + ScratchTakeCore, layouts::{ - GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + GGSWInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; -impl GLWECiphertext> { - #[allow(clippy::too_many_arguments)] - pub fn external_product_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - apply_infos: &GGSW, - ) -> usize +impl GLWE> { + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: GLWEInfos, + B: GGSWInfos, + M: GLWEExternalProduct, { - let in_size: usize = in_infos - .k() - .div_ceil(apply_infos.base2k()) - .div_ceil(apply_infos.dsize().into()) as usize; - let out_size: usize = out_infos.size(); - let ggsw_size: usize = apply_infos.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), in_size); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( - out_size, - in_size, - in_size, // rows - (apply_infos.rank() + 1).into(), // cols in - (apply_infos.rank() + 1).into(), // cols out - ggsw_size, - ); - let normalize_big: usize = module.vec_znx_normalize_tmp_bytes(); - - if in_infos.base2k() == apply_infos.base2k() { - res_dft + a_dft + (vmp | normalize_big) - } else { - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (apply_infos.rank() + 1).into(), in_size); - res_dft + ((a_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) - } - } - - pub fn external_product_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - apply_infos: &GGSW, - ) -> usize - where - OUT: GLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, - { - Self::external_product_scratch_space(module, out_infos, out_infos, apply_infos) + module.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos) } } -impl GLWECiphertext { - pub fn external_product( - &mut self, - module: &Module, - lhs: &GLWECiphertext, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: GLWEExternalProduct, +impl GLWE { + pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GLWEToRef, + B: GGSWPreparedToRef, + M: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.external_product(self, lhs, rhs, scratch); + module.glwe_external_product(self, a, b, scratch); } - pub fn external_product_inplace( - &mut self, - module: &Module, - rhs: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where - Module: GLWEExternalProductInplace, + pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GGSWPreparedToRef, + M: GLWEExternalProduct, + Scratch: ScratchTakeCore, { - module.external_product_inplace(self, rhs, scratch); + module.glwe_external_product_inplace(self, a, scratch); } } -impl GLWEExternalProductInplace for Module +pub trait GLWEExternalProduct where - Module: VecZnxDftAllocBytes + Self: Sized + + ModuleN + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -105,15 +60,47 @@ where + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) + fn glwe_external_product_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - R: GLWECiphertextToMut, - D: GGSWCiphertextPreparedToRef, + R: GLWEInfos, + A: GLWEInfos, + B: GGSWInfos, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &ggsw.to_ref(); + let in_size: usize = a_infos + .k() + .div_ceil(b_infos.base2k()) + .div_ceil(b_infos.dsize().into()) as usize; + let out_size: usize = res_infos.size(); + let ggsw_size: usize = b_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank() + 1).into(), in_size); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( + out_size, + in_size, + in_size, // rows + (b_infos.rank() + 1).into(), // cols in + (b_infos.rank() + 1).into(), // cols out + ggsw_size, + ); + let normalize_big: usize = self.vec_znx_normalize_tmp_bytes(); + + if a_infos.base2k() == b_infos.base2k() { + res_dft + a_dft + (vmp | normalize_big) + } else { + let normalize_conv: usize = VecZnx::bytes_of(self.n(), (b_infos.rank() + 1).into(), in_size); + res_dft + ((a_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) + } + } + + fn glwe_external_product_inplace(&self, res: &mut R, a: &D, scratch: &mut Scratch) + where + R: GLWEToMut, + D: GGSWPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let rhs: &GGSWPrepared<&[u8], BE> = &a.to_ref(); let basek_in: usize = res.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); @@ -124,15 +111,15 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_inplace_scratch_space(self, res, rhs)); + assert!(scratch.available() >= self.glwe_external_product_tmp_bytes(res, res, rhs)); } let cols: usize = (rhs.rank() + 1).into(); let dsize: usize = rhs.dsize().into(); let a_size: usize = (res.size() * basek_in).div_ceil(basek_ggsw); - let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(res.n().into(), cols, rhs.size()); // Todo optimise - let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(res.n().into(), cols, a_size.div_ceil(dsize)); + let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, rhs.size()); // Todo optimise + let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, a_size.div_ceil(dsize)); a_dft.data_mut().fill(0); if basek_in == basek_ggsw { @@ -160,7 +147,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -213,31 +200,18 @@ where ); } } -} -impl GLWEExternalProduct for Module -where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, -{ - fn external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) + fn glwe_external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, - D: GGSWCiphertextPreparedToRef, + R: GLWEToMut, + A: GLWEToRef, + D: GGSWPreparedToRef, + Scratch: ScratchTakeCore, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let lhs: &GLWE<&[u8]> = &lhs.to_ref(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &rhs.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref(); let basek_in: usize = lhs.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); @@ -251,7 +225,7 @@ where assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); assert_eq!(lhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_scratch_space(self, res, lhs, rhs)); + assert!(scratch.available() >= self.glwe_external_product_tmp_bytes(res, lhs, rhs)); } let cols: usize = (rhs.rank() + 1).into(); @@ -259,8 +233,8 @@ where let a_size: usize = (lhs.size() * basek_in).div_ceil(basek_ggsw); - let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n(), cols, rhs.size()); // Todo optimise - let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self.n(), cols, a_size.div_ceil(dsize)); + let (mut res_dft, scratch_1) = scratch.take_vec_znx_dft(self, cols, rhs.size()); // Todo optimise + let (mut a_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, cols, a_size.div_ceil(dsize)); a_dft.data_mut().fill(0); if basek_in == basek_ggsw { @@ -288,7 +262,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -342,3 +316,20 @@ where }); } } + +impl GLWEExternalProduct for Module where + Self: ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxIdftApplyConsume + + VecZnxBigNormalize + + VecZnxNormalize + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes +{ +} diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index f46b82c..532406b 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/poulpy-core/src/external_product/mod.rs @@ -1,23 +1,8 @@ -use poulpy_hal::layouts::{Backend, Scratch}; - -use crate::layouts::{GLWECiphertextToMut, GLWECiphertextToRef, prepared::GGSWCiphertextPreparedToRef}; - mod gglwe_atk; mod gglwe_ksk; mod ggsw_ct; mod glwe_ct; -pub trait GLWEExternalProduct { - fn external_product(&self, res: &mut R, a: &A, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, - D: GGSWCiphertextPreparedToRef; -} - -pub trait GLWEExternalProductInplace { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - D: GGSWCiphertextPreparedToRef; -} +pub use gglwe_ksk::*; +pub use ggsw_ct::*; +pub use glwe_ct::*; diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 3da962a..7dacb97 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -2,18 +2,18 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, - VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; use crate::{ - GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}, + GLWEOperations, + layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -29,7 +29,7 @@ pub struct GLWEPacker { /// [Accumulator] stores intermediate packing result. /// There are Log(N) such accumulators in a [GLWEPacker]. struct Accumulator { - data: GLWECiphertext>, + data: GLWE>, value: bool, // Implicit flag for zero ciphertext control: bool, // Can be combined with incoming value } @@ -43,12 +43,12 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(infos: &A) -> Self + pub fn alloc(module: &Module, infos: &A) -> Self where A: GLWEInfos, { Self { - data: GLWECiphertext::alloc(infos), + data: GLWE::alloc_from_infos(module, infos), value: false, control: false, } @@ -66,13 +66,13 @@ impl GLWEPacker { /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn new(infos: &A, log_batch: usize) -> Self + pub fn new(module: Module, infos: &A, log_batch: usize) -> Self where A: GLWEInfos, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); - (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos))); + (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(module, infos))); Self { accumulators, log_batch, @@ -90,17 +90,17 @@ impl GLWEPacker { } /// Number of scratch space bytes required to call [Self::add]. - pub fn scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - pack_core_scratch_space(module, out_infos, key_infos) + pack_core_tmp_bytes(module, out_infos, key_infos) } pub fn galois_elements(module: &Module) -> Vec { - GLWECiphertext::trace_galois_elements(module) + GLWE::trace_galois_elements(module) } /// Adds a GLWE ciphertext to the [GLWEPacker]. @@ -111,15 +111,15 @@ impl GLWEPacker { /// of packed ciphertexts reaches N/2^log_batch is a result written. /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. - /// * `scratch`: scratch space of size at least [Self::scratch_space]. + /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. pub fn add( &mut self, module: &Module, - a: Option<&GLWECiphertext>, - auto_keys: &HashMap>, + a: Option<&GLWE>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -142,7 +142,7 @@ impl GLWEPacker { + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { assert!( (self.counter as u32) < self.accumulators[0].data.n(), @@ -162,7 +162,7 @@ impl GLWEPacker { } /// Flush result to`res`. - pub fn flush(&mut self, module: &Module, res: &mut GLWECiphertext) + pub fn flush(&mut self, module: &Module, res: &mut GLWE) where Module: VecZnxCopy, { @@ -177,24 +177,24 @@ impl GLWEPacker { } } -fn pack_core_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn pack_core_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - combine_scratch_space(module, out_infos, key_infos) + combine_tmp_bytes(module, out_infos, key_infos) } fn pack_core( module: &Module, - a: Option<&GLWECiphertext>, + a: Option<&GLWE>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -217,7 +217,7 @@ fn pack_core( + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { let log_n: usize = module.log_n(); @@ -258,7 +258,7 @@ fn pack_core( } else { pack_core( module, - None::<&GLWECiphertext>>, + None::<&GLWE>>, acc_next, i + 1, auto_keys, @@ -268,27 +268,26 @@ fn pack_core( } } -fn combine_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn combine_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::alloc_bytes(out_infos) - + (GLWECiphertext::rsh_scratch_space(module.n()) - | GLWECiphertext::automorphism_inplace_scratch_space(module, out_infos, key_infos)) + GLWE::bytes_of_from_infos(module, out_infos) + + (GLWE::rsh_tmp_bytes(module.n()) | GLWE::automorphism_inplace_tmp_bytes(module, out_infos, key_infos)) } /// [combine] merges two ciphertexts together. fn combine( module: &Module, acc: &mut Accumulator, - b: Option<&GLWECiphertext>, + b: Option<&GLWE>, i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -311,10 +310,10 @@ fn combine( + VecZnxBigAutomorphismInplace + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECt, + Scratch: ScratchAvailable, { let log_n: usize = acc.data.n().log2(); - let a: &mut GLWECiphertext> = &mut acc.data; + let a: &mut GLWE> = &mut acc.data; let gal_el: i64 = if i == 0 { -1 @@ -395,9 +394,9 @@ fn combine( /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] pub fn glwe_packing( module: &Module, - cts: &mut HashMap>, + cts: &mut HashMap>, log_gap_out: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, @@ -414,7 +413,7 @@ pub fn glwe_packing( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -427,7 +426,7 @@ pub fn glwe_packing( + VecZnxBigSubSmallNegateInplace + VecZnxRotate + VecZnxNormalize, - Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, + Scratch: ScratchAvailable, { #[cfg(debug_assertions)] { @@ -439,15 +438,15 @@ pub fn glwe_packing( (0..log_n - log_gap_out).for_each(|i| { let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); - let auto_key: &GGLWEAutomorphismKeyPrepared = if i == 0 { + let auto_key: &AutomorphismKeyPrepared = if i == 0 { auto_keys.get(&-1).unwrap() } else { auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() }; (0..t).for_each(|j| { - let mut a: Option<&mut GLWECiphertext> = cts.remove(&j); - let mut b: Option<&mut GLWECiphertext> = cts.remove(&(j + t)); + let mut a: Option<&mut GLWE> = cts.remove(&j); + let mut b: Option<&mut GLWE> = cts.remove(&(j + t)); pack_internal(module, &mut a, &mut b, i, auto_key, scratch); @@ -463,10 +462,10 @@ pub fn glwe_packing( #[allow(clippy::too_many_arguments)] fn pack_internal( module: &Module, - a: &mut Option<&mut GLWECiphertext>, - b: &mut Option<&mut GLWECiphertext>, + a: &mut Option<&mut GLWE>, + b: &mut Option<&mut GLWE>, i: usize, - auto_key: &GGLWEAutomorphismKeyPrepared, + auto_key: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxRotateInplace @@ -481,7 +480,7 @@ fn pack_internal( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -494,7 +493,7 @@ fn pack_internal( + VecZnxBigSubSmallNegateInplace + VecZnxRotate + VecZnxNormalize, - Scratch: TakeVecZnx + TakeVecZnxDft + ScratchAvailable, + Scratch: ScratchAvailable, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) // We also use the identity: AUTO(a * X^t, g) = -X^t * AUTO(a, g) diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 4e1769e..36aabb9 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -2,22 +2,19 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, + VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx}, }; use crate::{ - TakeGLWECt, - layouts::{ - Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared, - }, + layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; -impl GLWECiphertext> { +impl GLWE> { pub fn trace_galois_elements(module: &Module) -> Vec { let mut gal_els: Vec = Vec::new(); (0..module.log_n()).for_each(|i| { @@ -30,21 +27,16 @@ impl GLWECiphertext> { gal_els } - pub fn trace_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize + pub fn trace_tmp_bytes(module: &Module, out_infos: &OUT, in_infos: &IN, key_infos: &KEY) -> usize where OUT: GLWEInfos, IN: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let trace: usize = Self::automorphism_inplace_scratch_space(module, out_infos, key_infos); + let trace: usize = Self::automorphism_inplace_tmp_bytes(module, out_infos, key_infos); if in_infos.base2k() != key_infos.base2k() { - let glwe_conv: usize = VecZnx::alloc_bytes( + let glwe_conv: usize = VecZnx::bytes_of( module.n(), (key_infos.rank_out() + 1).into(), out_infos.k().min(in_infos.k()).div_ceil(key_infos.base2k()) as usize, @@ -55,27 +47,27 @@ impl GLWECiphertext> { trace } - pub fn trace_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn trace_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize where OUT: GLWEInfos, KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - Self::trace_scratch_space(module, out_infos, out_infos, key_infos) + Self::trace_tmp_bytes(module, out_infos, out_infos, key_infos) } } -impl GLWECiphertext { +impl GLWE { pub fn trace( &mut self, module: &Module, start: usize, end: usize, - lhs: &GLWECiphertext, - auto_keys: &HashMap>, + lhs: &GLWE, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -89,7 +81,7 @@ impl GLWECiphertext { + VecZnxCopy + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { self.copy(module, lhs); self.trace_inplace(module, start, end, auto_keys, scratch); @@ -100,10 +92,10 @@ impl GLWECiphertext { module: &Module, start: usize, end: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -116,7 +108,7 @@ impl GLWECiphertext { + VecZnxRshInplace + VecZnxNormalizeTmpBytes + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Scratch: ScratchAvailable, { let basek_ksk: Base2K = auto_keys .get(auto_keys.keys().next().unwrap()) @@ -137,7 +129,7 @@ impl GLWECiphertext { } if self.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: module.n().into(), base2k: basek_ksk, k: self.k(), diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 9f6caa5..edda267 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -1,224 +1,205 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; + +use crate::{ + ScratchTakeCore, + keyswitching::glwe_ct::GLWEKeySwitch, + layouts::{ + AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey, + GLWESwitchingKeyToRef, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{ - GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared}, -}; - -impl GGLWEAutomorphismKey> { - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize +impl AutomorphismKey> { + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, { - GGLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) - } - - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize - where - OUT: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } -impl GGLWEAutomorphismKey { - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +impl AutomorphismKey { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: AutomorphismKeyToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - self.key.keyswitch(module, &lhs.key, rhs, scratch); + module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - self.key.keyswitch_inplace(module, &rhs.key, scratch); + module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); } } -impl GGLWESwitchingKey> { - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_apply: &KEY, - ) -> usize +impl GLWESwitchingKey> { + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, { - GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) - } - - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize - where - OUT: GGLWEInfos + GLWEInfos, - KEY: GGLWEInfos + GLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - GLWECiphertext::keyswitch_inplace_scratch_space(module, out_infos, key_apply) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } -impl GGLWESwitchingKey { - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, +impl GLWESwitchingKey { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GLWESwitchingKeyToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, { - #[cfg(debug_assertions)] - { - assert_eq!( - self.rank_in(), - lhs.rank_in(), - "ksk_out input rank: {} != ksk_in input rank: {}", - self.rank_in(), - lhs.rank_in() - ); - assert_eq!( - lhs.rank_out(), - rhs.rank_in(), - "ksk_in output rank: {} != ksk_apply input rank: {}", - self.rank_out(), - rhs.rank_in() - ); - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - assert!( - self.dnum() <= lhs.dnum(), - "self.dnum()={} > lhs.dnum()={}", - self.dnum(), - lhs.dnum() - ); - assert_eq!( - self.dsize(), - lhs.dsize(), - "ksk_out dsize: {} != ksk_in dsize: {}", - self.dsize(), - lhs.dsize() - ) + module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); + } + + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, + { + module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); + } +} + +impl GGLWE> { + pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch, + { + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } +} + +impl GGLWE { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, + { + module.gglwe_keyswitch(self, a, b, scratch); + } + + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGLWEKeySwitch, + { + module.gglwe_keyswitch_inplace(self, a, scratch); + } +} + +impl GGLWEKeySwitch for Module where Self: GLWEKeySwitch {} + +pub trait GGLWEKeySwitch +where + Self: GLWEKeySwitch, +{ + fn gglwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + { + self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } + + fn gglwe_keyswitch(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); + let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); + + assert_eq!( + res.rank_in(), + a.rank_in(), + "res input rank: {} != a input rank: {}", + res.rank_in(), + a.rank_in() + ); + assert_eq!( + a.rank_out(), + b.rank_in(), + "res output rank: {} != b input rank: {}", + a.rank_out(), + b.rank_in() + ); + assert_eq!( + res.rank_out(), + b.rank_out(), + "res output rank: {} != b output rank: {}", + res.rank_out(), + b.rank_out() + ); + assert!( + res.dnum() <= a.dnum(), + "res.dnum()={} > a.dnum()={}", + res.dnum(), + a.dnum() + ); + assert_eq!( + res.dsize(), + a.dsize(), + "res dsize: {} != a dsize: {}", + res.dsize(), + a.dsize() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_keyswitch(&mut res.at_mut(row, col), &a.at(row, col), b, scratch); + } } - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .keyswitch(module, &lhs.at(row_j, col_i), rhs, scratch); - }); - }); - - (self.dnum().min(lhs.dnum()).into()..self.dnum().into()).for_each(|row_i| { - (0..self.rank_in().into()).for_each(|col_j| { - self.at_mut(row_i, col_j).data.zero(); - }); - }); } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, + fn gglwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GGLWEToMut, + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - } + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - self.at_mut(row_j, col_i) - .keyswitch_inplace(module, rhs, scratch) - }); - }); + assert_eq!( + res.rank_out(), + a.rank_out(), + "res output rank: {} != a output rank: {}", + res.rank_out(), + a.rank_out() + ); + + for row in 0..res.dnum().into() { + for col in 0..res.rank_in().into() { + self.glwe_keyswitch_inplace(&mut res.at_mut(row, col), a, scratch); + } + } } } + +impl GLWESwitchingKey {} diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index d261f03..cfb4d8e 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -1,366 +1,131 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, VmpPMat, ZnxInfos}, -}; +use poulpy_hal::layouts::{Backend, DataMut, Scratch, VecZnx}; use crate::{ + GGSWExpandRows, ScratchTakeCore, + keyswitching::glwe_ct::GLWEKeySwitch, layouts::{ - GGLWECiphertext, GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared}, + GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, + prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, }, - operations::GLWEOperations, }; -impl GGSWCiphertext> { - pub(crate) fn expand_row_scratch_space(module: &Module, out_infos: &OUT, tsk_infos: &TSK) -> usize - where - OUT: GGSWInfos, - TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes, - { - let tsk_size: usize = tsk_infos.k().div_ceil(tsk_infos.base2k()) as usize; - let size_in: usize = out_infos - .k() - .div_ceil(tsk_infos.base2k()) - .div_ceil(tsk_infos.dsize().into()) as usize; - - let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes((tsk_infos.rank_out() + 1).into(), tsk_size); - let tmp_a: usize = module.vec_znx_dft_alloc_bytes(1, size_in); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( - tsk_size, - size_in, - size_in, - (tsk_infos.rank_in()).into(), // Verify if rank+1 - (tsk_infos.rank_out()).into(), // Verify if rank+1 - tsk_size, - ); - let tmp_idft: usize = module.vec_znx_big_alloc_bytes(1, tsk_size); - let norm: usize = module.vec_znx_normalize_tmp_bytes(); - - tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) - } - - #[allow(clippy::too_many_arguments)] - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - apply_infos: &KEY, - tsk_infos: &TSK, +impl GGSW> { + pub fn keyswitch_tmp_bytes( + module: &M, + res_infos: &R, + a_infos: &A, + key_infos: &K, + tsk_infos: &T, ) -> usize where - OUT: GGSWInfos, - IN: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + M: GGSWKeySwitch, { - #[cfg(debug_assertions)] - { - assert_eq!(apply_infos.rank_in(), apply_infos.rank_out()); - assert_eq!(tsk_infos.rank_in(), tsk_infos.rank_out()); - assert_eq!(apply_infos.rank_in(), tsk_infos.rank_in()); - } + module.ggsw_keyswitch_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos) + } +} - let rank: usize = apply_infos.rank_out().into(); +impl GGSW { + pub fn keyswitch(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) + where + A: GGSWToRef, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWKeySwitch, + { + module.ggsw_keyswitch(self, a, key, tsk, scratch); + } - let size_out: usize = out_infos.k().div_ceil(out_infos.base2k()) as usize; - let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); - let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); - let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); + pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) + where + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWKeySwitch, + { + module.ggsw_keyswitch_inplace(self, key, tsk, scratch); + } +} - if in_infos.base2k() == tsk_infos.base2k() { +pub trait GGSWKeySwitch +where + Self: GLWEKeySwitch + GGSWExpandRows, +{ + fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize + where + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + { + assert_eq!(key_infos.rank_in(), key_infos.rank_out()); + assert_eq!(tsk_infos.rank_in(), tsk_infos.rank_out()); + assert_eq!(key_infos.rank_in(), tsk_infos.rank_in()); + + let rank: usize = key_infos.rank_out().into(); + + let size_out: usize = res_infos.k().div_ceil(res_infos.base2k()) as usize; + let res_znx: usize = VecZnx::bytes_of(self.n(), rank + 1, size_out); + let ci_dft: usize = self.bytes_of_vec_znx_dft(rank + 1, size_out); + let ks: usize = self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos); + let expand_rows: usize = self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos); + let res_dft: usize = self.bytes_of_vec_znx_dft(rank + 1, size_out); + + if a_infos.base2k() == tsk_infos.base2k() { res_znx + ci_dft + (ks | expand_rows | res_dft) } else { - let a_conv: usize = VecZnx::alloc_bytes( - module.n(), + let a_conv: usize = VecZnx::bytes_of( + self.n(), 1, - out_infos.k().div_ceil(tsk_infos.base2k()) as usize, - ) + module.vec_znx_normalize_tmp_bytes(); + res_infos.k().div_ceil(tsk_infos.base2k()) as usize, + ) + self.vec_znx_normalize_tmp_bytes(); res_znx + ci_dft + (a_conv | ks | expand_rows | res_dft) } } - #[allow(clippy::too_many_arguments)] - pub fn keyswitch_inplace_scratch_space( - module: &Module, - out_infos: &OUT, - apply_infos: &KEY, - tsk_infos: &TSK, - ) -> usize + fn ggsw_keyswitch(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where - OUT: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxBigNormalizeTmpBytes, + R: GGSWToMut, + A: GGSWToRef, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, { - GGSWCiphertext::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + + assert_eq!(res.ggsw_layout(), a.ggsw_layout()); + + for row in 0..a.dnum().into() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) + self.glwe_keyswitch(&mut res.at_mut(row, 0), &a.at(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); + } + + fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + K: GLWESwitchingKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + + for row in 0..res.dnum().into() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) + self.glwe_keyswitch_inplace(&mut res.at_mut(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); } } -impl GGSWCiphertext { - pub fn from_gglwe( - &mut self, - module: &Module, - a: &GGLWECiphertext, - tsk: &GGLWETensorKeyPrepared, - scratch: &mut Scratch, - ) where - DataA: DataRef, - DataTsk: DataRef, - Module: VecZnxCopy - + VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VecZnxDftCopy - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftAddInplace - + VecZnxBigNormalize - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use crate::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(self.rank(), a.rank_out()); - assert_eq!(self.dnum(), a.dnum()); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(a.n(), module.n() as u32); - assert_eq!(tsk.n(), module.n() as u32); - } - (0..self.dnum().into()).for_each(|row_i| { - self.at_mut(row_i, 0).copy(module, &a.at(row_i, 0)); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn keyswitch( - &mut self, - module: &Module, - lhs: &GGSWCiphertext, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, - { - (0..lhs.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .keyswitch(module, &lhs.at(row_i, 0), ksk, scratch); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn keyswitch_inplace( - &mut self, - module: &Module, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, - { - (0..self.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .keyswitch_inplace(module, ksk, scratch); - }); - self.expand_row(module, tsk, scratch); - } - - pub fn expand_row( - &mut self, - module: &Module, - tsk: &GGLWETensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigAllocBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VecZnxDftCopy - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftAddInplace - + VecZnxBigNormalize - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnx, - { - let basek_in: usize = self.base2k().into(); - let basek_tsk: usize = tsk.base2k().into(); - - assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self, tsk)); - - let n: usize = self.n().into(); - let rank: usize = self.rank().into(); - let cols: usize = rank + 1; - - let a_size: usize = (self.size() * basek_in).div_ceil(basek_tsk); - - // Keyswitch the j-th row of the col 0 - for row_i in 0..self.dnum().into() { - let a = &self.at(row_i, 0).data; - - // Pre-compute DFT of (a0, a1, a2) - let (mut ci_dft, scratch_1) = scratch.take_vec_znx_dft(n, cols, a_size); - - if basek_in == basek_tsk { - for i in 0..cols { - module.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i); - } - } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(n, 1, a_size); - for i in 0..cols { - module.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2); - module.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0); - } - } - - for col_j in 1..cols { - // Example for rank 3: - // - // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is - // actually composed of that many dnum and we focus on a specific row here - // implicitely given ci_dft. - // - // # Input - // - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) - // col 1: (0, 0, 0, 0) - // col 2: (0, 0, 0, 0) - // col 3: (0, 0, 0, 0) - // - // # Output - // - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0 , a1 , a2 ) - // col 1: (-(b0s0 + b1s1 + b2s2) , b0 + M[i], b1 , b2 ) - // col 2: (-(c0s0 + c1s1 + c2s2) , c0 , c1 + M[i], c2 ) - // col 3: (-(d0s0 + d1s1 + d2s2) , d0 , d1 , d2 + M[i]) - - let dsize: usize = tsk.dsize().into(); - - let (mut tmp_dft_i, scratch_2) = scratch_1.take_vec_znx_dft(n, cols, tsk.size()); - let (mut tmp_a, scratch_3) = scratch_2.take_vec_znx_dft(n, 1, ci_dft.size().div_ceil(dsize)); - - { - // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2 - // - // # Example for col=1 - // - // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2) - // + - // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2) - // + - // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2) - // = - // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) - for col_i in 1..cols { - let pmat: &VmpPMat = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) - - // Extracts a[i] and multipies with Enc(s[i]s[j]) - for di in 0..dsize { - tmp_a.set_size((ci_dft.size() + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - tmp_dft_i.set_size(tsk.size() - ((dsize - di) as isize - 2).max(0) as usize); - - module.vec_znx_dft_copy(dsize, dsize - 1 - di, &mut tmp_a, 0, &ci_dft, col_i); - if di == 0 && col_i == 1 { - module.vmp_apply_dft_to_dft(&mut tmp_dft_i, &tmp_a, pmat, scratch_3); - } else { - module.vmp_apply_dft_to_dft_add(&mut tmp_dft_i, &tmp_a, pmat, di, scratch_3); - } - } - } - } - - // Adds -(sum a[i] * s[i]) + m) on the i-th column of tmp_idft_i - // - // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2) - // + - // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0) - // = - // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2) - // = - // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2) - module.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, &ci_dft, 0); - let (mut tmp_idft, scratch_3) = scratch_2.take_vec_znx_big(n, 1, tsk.size()); - for i in 0..cols { - module.vec_znx_idft_apply_tmpa(&mut tmp_idft, 0, &mut tmp_dft_i, i); - module.vec_znx_big_normalize( - basek_in, - &mut self.at_mut(row_i, col_j).data, - i, - basek_tsk, - &tmp_idft, - 0, - scratch_3, - ); - } - } - } - } -} +impl GGSW {} diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index 07d95e9..6d7bff9 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -1,186 +1,179 @@ use poulpy_hal::{ api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWESwitchingKeyPrepared}; +use crate::{ + ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, + }, +}; -impl GLWECiphertext> { - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_apply: &KEY, - ) -> usize +impl GLWE> { + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: GLWEInfos, + B: GGLWEInfos, + M: GLWEKeySwitch, { - let in_size: usize = in_infos + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) + } +} + +impl GLWE { + pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) + where + A: GLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + M: GLWEKeySwitch, + Scratch: ScratchTakeCore, + { + module.glwe_keyswitch(self, a, b, scratch); + } + + pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) + where + A: GLWESwitchingKeyPreparedToRef, + M: GLWEKeySwitch, + Scratch: ScratchTakeCore, + { + module.glwe_keyswitch_inplace(self, a, scratch); + } +} + +impl GLWEKeySwitch for Module where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize + + VecZnxNormalizeTmpBytes +{ +} + +pub trait GLWEKeySwitch +where + Self: Sized + + ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize + + VecZnxNormalizeTmpBytes, +{ + fn glwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + where + R: GLWEInfos, + A: GLWEInfos, + B: GGLWEInfos, + { + let in_size: usize = a_infos .k() - .div_ceil(key_apply.base2k()) - .div_ceil(key_apply.dsize().into()) as usize; - let out_size: usize = out_infos.size(); - let ksk_size: usize = key_apply.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( + .div_ceil(b_infos.base2k()) + .div_ceil(b_infos.dsize().into()) as usize; + let out_size: usize = res_infos.size(); + let ksk_size: usize = b_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, in_size, - (key_apply.rank_in()).into(), - (key_apply.rank_out() + 1).into(), + (b_infos.rank_in()).into(), + (b_infos.rank_out() + 1).into(), ksk_size, - ) + module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); - let normalize_big: usize = module.vec_znx_big_normalize_tmp_bytes(); - if in_infos.base2k() == key_apply.base2k() { + ) + self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + let normalize_big: usize = self.vec_znx_big_normalize_tmp_bytes(); + if a_infos.base2k() == b_infos.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) - } else if key_apply.dsize() == 1 { + } else if b_infos.dsize() == 1 { // In this case, we only need one column, temporary, that we can drop once a_dft is computed. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); + let normalize_conv: usize = VecZnx::bytes_of(self.n(), 1, in_size) + self.vec_znx_normalize_tmp_bytes(); res_dft + (((ai_dft + normalize_conv) | vmp) | normalize_big) } else { // Since we stride over a to get a_dft when dsize > 1, we need to store the full columns of a with in the base conversion. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (key_apply.rank_in()).into(), in_size); - res_dft + ((ai_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) + let normalize_conv: usize = VecZnx::bytes_of(self.n(), (b_infos.rank_in()).into(), in_size); + res_dft + ((ai_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } - pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize + fn glwe_keyswitch(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEToMut, + A: GLWEToRef, + B: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - Self::keyswitch_scratch_space(module, out_infos, out_infos, key_apply) - } -} + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); -impl GLWECiphertext { - #[allow(dead_code)] - pub(crate) fn assert_keyswitch( - &self, - module: &Module, - lhs: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &Scratch, - ) where - DataLhs: DataRef, - DataRhs: DataRef, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { assert_eq!( - lhs.rank(), - rhs.rank_in(), - "lhs.rank(): {} != rhs.rank_in(): {}", - lhs.rank(), - rhs.rank_in() + a.rank(), + b.rank_in(), + "a.rank(): {} != b.rank_in(): {}", + a.rank(), + b.rank_in() ); assert_eq!( - self.rank(), - rhs.rank_out(), - "self.rank(): {} != rhs.rank_out(): {}", - self.rank(), - rhs.rank_out() + res.rank(), + b.rank_out(), + "res.rank(): {} != b.rank_out(): {}", + res.rank(), + b.rank_out() ); - assert_eq!(rhs.n(), self.n()); - assert_eq!(lhs.n(), self.n()); - let scrach_needed: usize = GLWECiphertext::keyswitch_scratch_space(module, self, lhs, rhs); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(b.n(), self.n() as u32); + + let scrach_needed: usize = self.glwe_keyswitch_tmp_bytes(res, a, b); assert!( scratch.available() >= scrach_needed, - "scratch.available()={} < GLWECiphertext::keyswitch_scratch_space( - module, - self.base2k(), - self.k(), - lhs.base2k(), - lhs.k(), - rhs.base2k(), - rhs.k(), - rhs.dsize(), - rhs.rank_in(), - rhs.rank_out(), - )={scrach_needed}", + "scratch.available()={} < glwe_keyswitch_tmp_bytes={scrach_needed}", scratch.available(), ); - } - #[allow(dead_code)] - pub(crate) fn assert_keyswitch_inplace( - &self, - module: &Module, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &Scratch, - ) where - DataRhs: DataRef, - Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, - { - assert_eq!( - self.rank(), - rhs.rank_out(), - "self.rank(): {} != rhs.rank_out(): {}", - self.rank(), - rhs.rank_out() - ); + let basek_out: usize = res.base2k().into(); + let base2k_out: usize = b.base2k().into(); - assert_eq!(rhs.n(), self.n()); - - let scrach_needed: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, self, rhs); - - assert!( - scratch.available() >= scrach_needed, - "scratch.available()={} < GLWECiphertext::keyswitch_scratch_space()={scrach_needed}", - scratch.available(), - ); - } -} - -impl GLWECiphertext { - pub fn keyswitch( - &mut self, - module: &Module, - glwe_in: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - self.assert_keyswitch(module, glwe_in, rhs, scratch); - } - - let basek_out: usize = self.base2k().into(); - let basek_ksk: usize = rhs.base2k().into(); - - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // Todo optimise - let res_big: VecZnxBig<_, B> = glwe_in.keyswitch_internal(module, res_dft, rhs, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_normalize( + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), b.size()); // Todo optimise + let res_big: VecZnxBig<&mut [u8], BE> = keyswitch_internal(self, res_dft, a, b, scratch_1); + (0..(res.rank() + 1).into()).for_each(|i| { + self.vec_znx_big_normalize( basek_out, - &mut self.data, + &mut res.data, i, - basek_ksk, + base2k_out, &res_big, i, scratch_1, @@ -188,227 +181,190 @@ impl GLWECiphertext { }) } - pub fn keyswitch_inplace( - &mut self, - module: &Module, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - self.assert_keyswitch_inplace(module, rhs, scratch); - } - - let basek_in: usize = self.base2k().into(); - let basek_ksk: usize = rhs.base2k().into(); - - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // Todo optimise - let res_big: VecZnxBig<_, B> = self.keyswitch_internal(module, res_dft, rhs, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_normalize( - basek_in, - &mut self.data, - i, - basek_ksk, - &res_big, - i, - scratch_1, - ); - }) - } -} - -impl GLWECiphertext { - pub(crate) fn keyswitch_internal( - &self, - module: &Module, - res_dft: VecZnxDft, - rhs: &GGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) -> VecZnxBig + fn glwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) where - DataRes: DataMut, - DataKey: DataRef, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, + R: GLWEToMut, + A: GLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - if rhs.dsize() == 1 { - return keyswitch_vmp_one_digit( - module, - self.base2k().into(), - rhs.base2k().into(), - res_dft, - &self.data, - &rhs.key.data, - scratch, + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); + + assert_eq!( + res.rank(), + a.rank_in(), + "res.rank(): {} != a.rank_in(): {}", + res.rank(), + a.rank_in() + ); + assert_eq!( + res.rank(), + a.rank_out(), + "res.rank(): {} != b.rank_out(): {}", + res.rank(), + a.rank_out() + ); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + + let scrach_needed: usize = self.glwe_keyswitch_tmp_bytes(res, res, a); + + assert!( + scratch.available() >= scrach_needed, + "scratch.available()={} < glwe_keyswitch_tmp_bytes={scrach_needed}", + scratch.available(), + ); + + let base2k_in: usize = res.base2k().into(); + let base2k_out: usize = a.base2k().into(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), a.size()); // Todo optimise + let res_big: VecZnxBig<&mut [u8], BE> = keyswitch_internal(self, res_dft, res, a, scratch_1); + (0..(res.rank() + 1).into()).for_each(|i| { + self.vec_znx_big_normalize( + base2k_in, + &mut res.data, + i, + base2k_out, + &res_big, + i, + scratch_1, ); - } - - keyswitch_vmp_multiple_digits( - module, - self.base2k().into(), - rhs.base2k().into(), - res_dft, - &self.data, - &rhs.key.data, - rhs.dsize().into(), - scratch, - ) + }) } } -fn keyswitch_vmp_one_digit( - module: &Module, - basek_in: usize, - basek_ksk: usize, - mut res_dft: VecZnxDft, - a: &VecZnx, - mat: &VmpPMat, - scratch: &mut Scratch, -) -> VecZnxBig +impl GLWE> {} + +impl GLWE {} + +fn keyswitch_internal( + module: &M, + mut res: VecZnxDft, + a: &GLWE, + b: &GLWESwitchingKeyPrepared, + scratch: &mut Scratch, +) -> VecZnxBig where - DataRes: DataMut, - DataIn: DataRef, - DataVmp: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, + DR: DataMut, + DA: DataRef, + DB: DataRef, + M: ModuleN + + VecZnxDftBytesOf + + VmpApplyDftToDftTmpBytes + + VecZnxBigNormalizeTmpBytes + + VmpApplyDftToDftTmpBytes + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxDftApply + + VecZnxIdftApplyConsume + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalize, + Scratch: ScratchTakeCore, { - let cols: usize = a.cols(); + let base2k_in: usize = a.base2k().into(); + let base2k_out: usize = b.base2k().into(); + let cols: usize = (a.rank() + 1).into(); + let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); + let pmat: &VmpPMat = &b.key.data; - let a_size: usize = (a.size() * basek_in).div_ceil(basek_ksk); - let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(a.n(), cols - 1, a.size()); + if b.dsize() == 1 { + let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); - if basek_in == basek_ksk { - (0..cols - 1).for_each(|col_i| { - module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, a, col_i + 1); - }); + if base2k_in == base2k_out { + (0..cols - 1).for_each(|col_i| { + module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, a.data(), col_i + 1); + }); + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, 1, a_size); + (0..cols - 1).for_each(|col_i| { + module.vec_znx_normalize( + base2k_out, + &mut a_conv, + 0, + base2k_in, + a.data(), + col_i + 1, + scratch_2, + ); + module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, &a_conv, 0); + }); + } + + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_1); } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(a.n(), 1, a_size); - (0..cols - 1).for_each(|col_i| { - module.vec_znx_normalize(basek_ksk, &mut a_conv, 0, basek_in, a, col_i + 1, scratch_2); - module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, &a_conv, 0); - }); + let dsize: usize = b.dsize().into(); + + let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a_size.div_ceil(dsize)); + ai_dft.data_mut().fill(0); + + if base2k_in == base2k_out { + for di in 0..dsize { + ai_dft.set_size((a_size + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + res.set_size(pmat.size() - ((dsize - di) as isize - 2).max(0) as usize); + + for j in 0..cols - 1 { + module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, a.data(), j + 1); + } + + if di == 0 { + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_1); + } else { + module.vmp_apply_dft_to_dft_add(&mut res, &ai_dft, pmat, di, scratch_1); + } + } + } else { + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, cols - 1, a_size); + for j in 0..cols - 1 { + module.vec_znx_normalize( + base2k_out, + &mut a_conv, + j, + base2k_in, + a.data(), + j + 1, + scratch_2, + ); + } + + for di in 0..dsize { + ai_dft.set_size((a_size + di) / dsize); + + // Small optimization for dsize > 2 + // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then + // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. + // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. + // It is possible to further ignore the last dsize-1 limbs, but this introduce + // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same + // noise is kept with respect to the ideal functionality. + res.set_size(pmat.size() - ((dsize - di) as isize - 2).max(0) as usize); + + for j in 0..cols - 1 { + module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, &a_conv, j); + } + + if di == 0 { + module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_2); + } else { + module.vmp_apply_dft_to_dft_add(&mut res, &ai_dft, pmat, di, scratch_2); + } + } + } + + res.set_size(res.max_size()); } - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_1); - let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res_dft); - module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0); - res_big -} - -#[allow(clippy::too_many_arguments)] -fn keyswitch_vmp_multiple_digits( - module: &Module, - basek_in: usize, - basek_ksk: usize, - mut res_dft: VecZnxDft, - a: &VecZnx, - mat: &VmpPMat, - dsize: usize, - scratch: &mut Scratch, -) -> VecZnxBig -where - DataRes: DataMut, - DataIn: DataRef, - DataVmp: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxNormalize, - Scratch: TakeVecZnxDft + TakeVecZnx, -{ - let cols: usize = a.cols(); - let a_size: usize = (a.size() * basek_in).div_ceil(basek_ksk); - let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(a.n(), cols - 1, a_size.div_ceil(dsize)); - ai_dft.data_mut().fill(0); - - if basek_in == basek_ksk { - for di in 0..dsize { - ai_dft.set_size((a_size + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - res_dft.set_size(mat.size() - ((dsize - di) as isize - 2).max(0) as usize); - - for j in 0..cols - 1 { - module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, a, j + 1); - } - - if di == 0 { - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_1); - } else { - module.vmp_apply_dft_to_dft_add(&mut res_dft, &ai_dft, mat, di, scratch_1); - } - } - } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(a.n(), cols - 1, a_size); - for j in 0..cols - 1 { - module.vec_znx_normalize(basek_ksk, &mut a_conv, j, basek_in, a, j + 1, scratch_2); - } - - for di in 0..dsize { - ai_dft.set_size((a_size + di) / dsize); - - // Small optimization for dsize > 2 - // VMP produce some error e, and since we aggregate vmp * 2^{di * B}, then - // we also aggregate ei * 2^{di * B}, with the largest error being ei * 2^{(dsize-1) * B}. - // As such we can ignore the last dsize-2 limbs safely of the sum of vmp products. - // It is possible to further ignore the last dsize-1 limbs, but this introduce - // ~0.5 to 1 bit of additional noise, and thus not chosen here to ensure that the same - // noise is kept with respect to the ideal functionality. - res_dft.set_size(mat.size() - ((dsize - di) as isize - 2).max(0) as usize); - - for j in 0..cols - 1 { - module.vec_znx_dft_apply(dsize, dsize - di - 1, &mut ai_dft, j, &a_conv, j); - } - - if di == 0 { - module.vmp_apply_dft_to_dft(&mut res_dft, &ai_dft, mat, scratch_2); - } else { - module.vmp_apply_dft_to_dft_add(&mut res_dft, &ai_dft, mat, di, scratch_2); - } - } - } - - res_dft.set_size(res_dft.max_size()); - let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res_dft); - module.vec_znx_big_add_small_inplace(&mut res_big, 0, a, 0); + let mut res_big: VecZnxBig = module.vec_znx_idft_apply_consume(res); + module.vec_znx_big_add_small_inplace(&mut res_big, 0, a.data(), 0); res_big } diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 7d9e08e..8546ccb 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -1,116 +1,116 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, + api::ScratchAvailable, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; use crate::{ - TakeGLWECt, + ScratchTakeCore, + keyswitching::glwe_ct::GLWEKeySwitch, layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, LWECiphertext, LWEInfos, Rank, TorusPrecision, - prepared::LWESwitchingKeyPrepared, + GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, + prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, }, }; -impl LWECiphertext> { - pub fn keyswitch_scratch_space( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize +impl LWE> { + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: LWEInfos, - IN: LWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes, + R: LWEInfos, + A: LWEInfos, + K: GGLWEInfos, + M: LWEKeySwitch, { - let max_k: TorusPrecision = in_infos.k().max(out_infos.k()); - - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { - n: module.n().into(), - base2k: in_infos.base2k(), - k: max_k, - rank: Rank(1), - }; - - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { - n: module.n().into(), - base2k: out_infos.base2k(), - k: max_k, - rank: Rank(1), - }; - - let glwe_in: usize = GLWECiphertext::alloc_bytes(&glwe_in_infos); - let glwe_out: usize = GLWECiphertext::alloc_bytes(&glwe_out_infos); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); - - glwe_in + glwe_out + ks + module.lwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } -impl LWECiphertext { - pub fn keyswitch( - &mut self, - module: &Module, - a: &LWECiphertext, - ksk: &LWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - A: DataRef, - DKs: DataRef, - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes - + VecZnxCopy, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +impl LWE { + pub fn keyswitch(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch) + where + A: LWEToRef, + K: LWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: LWEKeySwitch, { - #[cfg(debug_assertions)] - { - assert!(self.n() <= module.n() as u32); - assert!(a.n() <= module.n() as u32); - assert!(scratch.available() >= LWECiphertext::keyswitch_scratch_space(module, self, a, ksk)); - } + module.lwe_keyswitch(self, a, ksk, scratch); + } +} - let max_k: TorusPrecision = self.k().max(a.k()); +impl LWEKeySwitch for Module where Self: LWEKeySwitch {} + +pub trait LWEKeySwitch +where + Self: GLWEKeySwitch + GLWEAlloc, +{ + fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: LWEInfos, + A: LWEInfos, + K: GGLWEInfos, + { + let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); + + let glwe_a_infos: GLWELayout = GLWELayout { + n: self.ring_degree(), + base2k: a_infos.base2k(), + k: max_k, + rank: Rank(1), + }; + + let glwe_res_infos: GLWELayout = GLWELayout { + n: self.ring_degree(), + base2k: res_infos.base2k(), + k: max_k, + rank: Rank(1), + }; + + let glwe_in: usize = GLWE::bytes_of_from_infos(self, &glwe_a_infos); + let glwe_out: usize = GLWE::bytes_of_from_infos(self, &glwe_res_infos); + let ks: usize = self.glwe_keyswitch_tmp_bytes(&glwe_res_infos, &glwe_a_infos, key_infos); + + glwe_in + glwe_out + ks + } + + fn lwe_keyswitch(&self, res: &mut R, a: &A, ksk: &K, scratch: &mut Scratch) + where + R: LWEToMut, + A: LWEToRef, + K: LWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let a: &LWE<&[u8]> = &a.to_ref(); + let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); + + assert!(res.n().as_usize() <= self.n()); + assert!(a.n().as_usize() <= self.n()); + assert_eq!(ksk.n(), self.n() as u32); + assert!(scratch.available() >= self.lwe_keyswitch_tmp_bytes(res, a, ksk)); + + let max_k: TorusPrecision = res.k().max(a.k()); let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { - n: ksk.n(), - base2k: a.base2k(), - k: max_k, - rank: Rank(1), - }); + let (mut glwe_in, scratch_1) = scratch.take_glwe_ct( + self, + &GLWELayout { + n: ksk.n(), + base2k: a.base2k(), + k: max_k, + rank: Rank(1), + }, + ); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWECiphertextLayout { - n: ksk.n(), - base2k: self.base2k(), - k: max_k, - rank: Rank(1), - }); + let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct( + self, + &GLWELayout { + n: ksk.n(), + base2k: res.base2k(), + k: max_k, + rank: Rank(1), + }, + ); let n_lwe: usize = a.n().into(); @@ -120,7 +120,7 @@ impl LWECiphertext { glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } - glwe_out.keyswitch(module, &glwe_in, &ksk.0, scratch_1); - self.sample_extract(&glwe_out); + self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); + res.sample_extract(&glwe_out); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 2a10765..61ba79a 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -1,24 +1,28 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + AutomorphismKey, AutomorphismKeyToMut, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKeyCompressed { - pub(crate) key: GGLWESwitchingKeyCompressed, +pub struct AutomorphismKeyCompressed { + pub(crate) key: GLWESwitchingKeyCompressed, pub(crate) p: i64, } -impl LWEInfos for GGLWEAutomorphismKeyCompressed { - fn n(&self) -> Degree { +impl LWEInfos for AutomorphismKeyCompressed { + fn n(&self) -> RingDegree { self.key.n() } @@ -34,13 +38,13 @@ impl LWEInfos for GGLWEAutomorphismKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GLWEInfos for AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GGLWEInfos for AutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -58,76 +62,185 @@ impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { } } -impl fmt::Debug for GGLWEAutomorphismKeyCompressed { +impl fmt::Debug for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKeyCompressed { +impl FillUniform for AutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKeyCompressed { +impl fmt::Display for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKeyCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - Self { - key: GGLWESwitchingKeyCompressed::alloc(infos), +pub trait AutomorphismKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyCompressed> { + AutomorphismKeyCompressed { + key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), p: 0, } } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - Self { - key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) + assert_eq!(infos.rank_in(), infos.rank_out()); + self.alloc_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + fn bytes_of_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.rank_in(), infos.rank_out()); + self.bytes_of_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } -impl ReaderFrom for GGLWEAutomorphismKeyCompressed { +impl AutomorphismKeyCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: AutomorphismKeyCompressedAlloc, + { + module.alloc_automorphism_key_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: AutomorphismKeyCompressedAlloc, + { + module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: AutomorphismKeyCompressedAlloc, + { + module.bytes_of_automorphism_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: AutomorphismKeyCompressedAlloc, + { + module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl ReaderFrom for AutomorphismKeyCompressed { 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 GGLWEAutomorphismKeyCompressed { +impl WriterTo for AutomorphismKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } } -impl Decompress> for GGLWEAutomorphismKey +pub trait AutomorphismKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWEAutomorphismKeyCompressed) { - self.key.decompress(module, &other.key); - self.p = other.p; + fn decompress_automorphism_key(&self, res: &mut R, other: &O) + where + R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + { + self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); + res.set_p(other.p()); + } +} + +impl AutomorphismKeyDecompress for Module where Self: AutomorphismKeyDecompress {} + +impl AutomorphismKey +where + Self: SetAutomorphismGaloisElement, +{ + pub fn decompress(&mut self, module: &M, other: &O) + where + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + M: AutomorphismKeyDecompress, + { + module.decompress_automorphism_key(self, other); + } +} + +pub trait AutomorphismKeyCompressedToRef { + fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; +} + +impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + 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 + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { + AutomorphismKeyCompressed { + p: self.p, + key: self.key.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index f7a4df9..9216ec4 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -1,18 +1,20 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertextCompressed { +pub struct GGLWECompressed { pub(crate) data: MatZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -21,9 +23,9 @@ pub struct GGLWECiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGLWECiphertextCompressed { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) +impl LWEInfos for GGLWECompressed { + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -38,13 +40,13 @@ impl LWEInfos for GGLWECiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGLWECiphertextCompressed { +impl GLWEInfos for GGLWECompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextCompressed { +impl GGLWEInfos for GGLWECompressed { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -62,53 +64,41 @@ impl GGLWEInfos for GGLWECiphertextCompressed { } } -impl fmt::Debug for GGLWECiphertextCompressed { +impl fmt::Debug for GGLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertextCompressed { +impl FillUniform for GGLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertextCompressed { +impl fmt::Display for GGLWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGLWECiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGLWECompressed: base2k={} k={} dsize={}) {}", self.base2k.0, self.k.0, self.dsize.0, self.data ) } } -impl GGLWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWECompressedAlloc +where + Self: GetRingDegree, +{ + fn alloc_gglwe_compressed( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWECompressed> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -123,9 +113,9 @@ impl GGLWECiphertextCompressed> { dsize.0, ); - Self { + GGLWECompressed { data: MatZnx::alloc( - n.into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), 1, @@ -139,21 +129,22 @@ impl GGLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_gglwe_compressed_from_infos(&self, infos: &A) -> GGLWECompressed> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + assert_eq!(infos.n(), self.ring_degree()); + self.alloc_gglwe_compressed( infos.base2k(), infos.k(), infos.rank_in(), + infos.rank_out(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -168,20 +159,76 @@ impl GGLWECiphertextCompressed> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.ring_degree().into(), dnum.into(), rank_in.into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.n(), self.ring_degree()); + self.bytes_of_gglwe_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + infos.dsize(), + ) + } } -impl GGLWECiphertextCompressed { - pub(crate) fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGLWECompressedAlloc for Module where Self: GetRingDegree {} + +impl GGLWECompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed_from_infos(infos) + } + + pub fn alloc( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + M: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed_from_infos(infos) + } + + pub fn byte_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } +} + +impl GGLWECompressed { + pub(crate) fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k, @@ -191,10 +238,10 @@ impl GGLWECiphertextCompressed { } } -impl GGLWECiphertextCompressed { - pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGLWECompressed { + pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { k: self.k, base2k: self.base2k, rank: self.rank_out, @@ -204,7 +251,7 @@ impl GGLWECiphertextCompressed { } } -impl ReaderFrom for GGLWECiphertextCompressed { +impl ReaderFrom for GGLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -219,7 +266,7 @@ impl ReaderFrom for GGLWECiphertextCompressed { } } -impl WriterTo for GGLWECiphertextCompressed { +impl WriterTo for GGLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -233,59 +280,73 @@ impl WriterTo for GGLWECiphertextCompressed { } } -impl Decompress> for GGLWECiphertext +pub trait GGLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWECiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", - self.n(), - other.n() - ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank_in(), - other.rank_in(), - "invalid receiver: self.rank_in()={} != other.rank_in()={}", - self.rank_in(), - other.rank_in() - ); - assert_eq!( - self.rank_out(), - other.rank_out(), - "invalid receiver: self.rank_out()={} != other.rank_out()={}", - self.rank_out(), - other.rank_out() - ); + fn decompress_gglwe(&self, res: &mut R, other: &O) + where + R: GGLWEToMut, + O: GGLWECompressedToRef, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GGLWECompressed<&[u8]> = &other.to_ref(); - assert_eq!( - self.dnum(), - other.dnum(), - "invalid receiver: self.dnum()={} != other.dnum()={}", - self.dnum(), - other.dnum() - ); + assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + + let rank_in: usize = res.rank_in().into(); + let dnum: usize = res.dnum().into(); + + for row_i in 0..dnum { + for col_i in 0..rank_in { + self.decompress_glwe(&mut res.at_mut(row_i, col_i), &other.at(row_i, col_i)); + } + } + } +} + +impl GGLWEDecompress for Module where Self: VecZnxFillUniform + VecZnxCopy {} + +impl GGLWE { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: GGLWECompressedToRef, + M: GGLWEDecompress, + { + module.decompress_gglwe(self, other); + } +} + +pub trait GGLWECompressedToMut { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]>; +} + +impl GGLWECompressedToMut for GGLWECompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + GGLWECompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + seed: self.seed.clone(), + rank_out: self.rank_out, + data: self.data.to_mut(), + } + } +} + +pub trait GGLWECompressedToRef { + fn to_ref(&self) -> GGLWECompressed<&[u8]>; +} + +impl GGLWECompressedToRef for GGLWECompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + GGLWECompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + seed: self.seed.clone(), + rank_out: self.rank_out, + data: self.data.to_ref(), } - - let rank_in: usize = self.rank_in().into(); - let dnum: usize = self.dnum().into(); - - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|row_i| { - self.at_mut(row_i, col_i) - .decompress(module, &other.at(row_i, col_i)); - }); - }); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 60d9316..fb963c5 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -1,25 +1,25 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWECiphertextCompressed}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, LWEInfos, + Rank, RingDegree, TorusPrecision, + compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWESwitchingKeyCompressed { - pub(crate) key: GGLWECiphertextCompressed, +pub struct GLWESwitchingKeyCompressed { + pub(crate) key: GGLWECompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKeyCompressed { - fn n(&self) -> Degree { +impl LWEInfos for GLWESwitchingKeyCompressed { + fn n(&self) -> RingDegree { self.key.n() } @@ -35,13 +35,13 @@ impl LWEInfos for GGLWESwitchingKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWESwitchingKeyCompressed { +impl GLWEInfos for GLWESwitchingKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyCompressed { +impl GGLWEInfos for GLWESwitchingKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -59,19 +59,19 @@ impl GGLWEInfos for GGLWESwitchingKeyCompressed { } } -impl fmt::Debug for GGLWESwitchingKeyCompressed { +impl fmt::Debug for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWESwitchingKeyCompressed { +impl FillUniform for GLWESwitchingKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWESwitchingKeyCompressed { +impl fmt::Display for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -81,47 +81,100 @@ impl fmt::Display for GGLWESwitchingKeyCompressed { } } -impl GGLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GGLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +pub trait GLWESwitchingKeyCompressedAlloc +where + Self: GGLWECompressedAlloc, +{ + fn alloc_glwe_switching_key_compressed( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { - GGLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + ) -> GLWESwitchingKeyCompressed> { + GLWESwitchingKeyCompressed { + key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWESwitchingKeyCompressed> where A: GGLWEInfos, { - GGLWECiphertextCompressed::alloc_bytes(infos) + self.alloc_glwe_switching_key_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, dsize) + fn bytes_of_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe_compressed_from_infos(infos) } } -impl ReaderFrom for GGLWESwitchingKeyCompressed { +impl GLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + M: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) + } +} + +impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -129,7 +182,7 @@ impl ReaderFrom for GGLWESwitchingKeyCompressed { } } -impl WriterTo for GGLWESwitchingKeyCompressed { +impl WriterTo for GLWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; @@ -137,13 +190,64 @@ impl WriterTo for GGLWESwitchingKeyCompressed { } } -impl Decompress> for GGLWESwitchingKey +pub trait GLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GGLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWESwitchingKeyCompressed) { - self.key.decompress(module, &other.key); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + fn decompress_glwe_switching_key(&self, res: &mut R, other: &O) + where + R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyCompressedToRef, + { + let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); + self.decompress_gglwe(&mut res.to_mut().key, &other.key); + res.set_sk_in_n(other.sk_in_n); + res.set_sk_out_n(other.sk_out_n); + } +} + +impl GLWESwitchingKeyDecompress for Module where Self: GGLWEDecompress {} + +impl GLWESwitchingKey { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: GLWESwitchingKeyCompressedToRef, + M: GLWESwitchingKeyDecompress, + { + module.decompress_glwe_switching_key(self, other); + } +} + +pub trait GLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed +where + GGLWECompressed: GGLWECompressedToMut, +{ + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { + GLWESwitchingKeyCompressed { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_mut(), + } + } +} + +pub trait GLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; +} + +impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed +where + GGLWECompressed: GGLWECompressedToRef, +{ + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { + GLWESwitchingKeyCompressed { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index fef4647..ff2b8b8 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -1,23 +1,25 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToMut, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKeyCompressed { - pub(crate) keys: Vec>, +pub struct TensorKeyCompressed { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyCompressed { - fn n(&self) -> Degree { +impl LWEInfos for TensorKeyCompressed { + fn n(&self) -> RingDegree { self.keys[0].n() } @@ -32,13 +34,13 @@ impl LWEInfos for GGLWETensorKeyCompressed { self.keys[0].size() } } -impl GLWEInfos for GGLWETensorKeyCompressed { +impl GLWEInfos for TensorKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyCompressed { +impl GGLWEInfos for TensorKeyCompressed { fn rank_in(&self) -> Rank { self.rank_out() } @@ -56,21 +58,21 @@ impl GGLWEInfos for GGLWETensorKeyCompressed { } } -impl fmt::Debug for GGLWETensorKeyCompressed { +impl fmt::Debug for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKeyCompressed { +impl FillUniform for TensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKeyCompressed { +impl fmt::Display for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKeyCompressed)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -80,8 +82,27 @@ impl fmt::Display for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait TensorKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_tensor_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> TensorKeyCompressed> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyCompressed { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn alloc_tensor_key_compressed_from_infos(&self, infos: &A) -> TensorKeyCompressed> where A: GGLWEInfos, { @@ -90,62 +111,67 @@ impl GGLWETensorKeyCompressed> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" ); - Self::alloc_with( - infos.n(), + self.alloc_tensor_key_compressed( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_tensor_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GGLWESwitchingKeyCompressed::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) + self.bytes_of_tensor_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } -impl ReaderFrom for GGLWETensorKeyCompressed { +impl TensorKeyCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: TensorKeyCompressedAlloc, + { + module.alloc_tensor_key_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: TensorKeyCompressedAlloc, + { + module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: TensorKeyCompressedAlloc, + { + module.bytes_of_tensor_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: TensorKeyCompressedAlloc, + { + module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl ReaderFrom for TensorKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -161,7 +187,7 @@ impl ReaderFrom for GGLWETensorKeyCompressed { } } -impl WriterTo for GGLWETensorKeyCompressed { +impl WriterTo for TensorKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -171,8 +197,8 @@ impl WriterTo for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyCompressed { +impl TensorKeyCompressed { + pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -181,27 +207,70 @@ impl GGLWETensorKeyCompressed { } } -impl Decompress> for GGLWETensorKey +pub trait TensorKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWETensorKeyCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.keys.len(), - other.keys.len(), - "invalid receiver: self.keys.len()={} != other.keys.len()={}", - self.keys.len(), - other.keys.len() - ); - } + fn decompress_tensor_key(&self, res: &mut R, other: &O) + where + R: TensorKeyToMut, + O: TensorKeyCompressedToRef, + { + let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let other: &TensorKeyCompressed<&[u8]> = &other.to_ref(); - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.decompress(module, b); - }); + assert_eq!( + res.keys.len(), + other.keys.len(), + "invalid receiver: res.keys.len()={} != other.keys.len()={}", + res.keys.len(), + other.keys.len() + ); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.decompress_glwe_switching_key(a, b); + } + } +} + +impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl TensorKey { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: TensorKeyCompressedToRef, + M: TensorKeyDecompress, + { + module.decompress_tensor_key(self, other); + } +} + +pub trait TensorKeyCompressedToMut { + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]>; +} + +impl TensorKeyCompressedToMut for TensorKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { + TensorKeyCompressed { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} + +pub trait TensorKeyCompressedToRef { + fn to_ref(&self) -> TensorKeyCompressed<&[u8]>; +} + +impl TensorKeyCompressedToRef for TensorKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { + TensorKeyCompressed { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index f0a62cc..adad621 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -1,18 +1,19 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertextCompressed { +pub struct GGSWCompressed { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, @@ -21,9 +22,9 @@ pub struct GGSWCiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGSWCiphertextCompressed { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) +impl LWEInfos for GGSWCompressed { + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -37,13 +38,13 @@ impl LWEInfos for GGSWCiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGSWCiphertextCompressed { +impl GLWEInfos for GGSWCompressed { fn rank(&self) -> Rank { self.rank } } -impl GGSWInfos for GGSWCiphertextCompressed { +impl GGSWInfos for GGSWCompressed { fn dsize(&self) -> Dsize { self.dsize } @@ -53,46 +54,42 @@ impl GGSWInfos for GGSWCiphertextCompressed { } } -impl fmt::Debug for GGSWCiphertextCompressed { +impl fmt::Debug for GGSWCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertextCompressed { +impl fmt::Display for GGSWCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGSWCiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGSWCompressed: base2k={} k={} dsize={}) {}", self.base2k, self.k, self.dsize, self.data ) } } -impl FillUniform for GGSWCiphertextCompressed { +impl FillUniform for GGSWCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWCompressedAlloc +where + Self: GetRingDegree, +{ + fn alloc_ggsw_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGSWCompressed> { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 @@ -105,9 +102,9 @@ impl GGSWCiphertextCompressed> { dsize.0, ); - Self { + GGSWCompressed { data: MatZnx::alloc( - n.into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), 1, @@ -121,12 +118,11 @@ impl GGSWCiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_compressed_from_infos(&self, infos: &A) -> GGSWCompressed> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw_compressed( infos.base2k(), infos.k(), infos.rank(), @@ -135,9 +131,9 @@ impl GGSWCiphertextCompressed> { ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0 @@ -150,20 +146,65 @@ impl GGSWCiphertextCompressed> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.ring_degree().into(), dnum.into(), (rank + 1).into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_ggsw_compressed_key_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } } -impl GGSWCiphertextCompressed { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGSWCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGSWInfos, + M: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGSWInfos, + M: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed_key_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl GGSWCompressed { + pub fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k, @@ -173,10 +214,10 @@ impl GGSWCiphertextCompressed { } } -impl GGSWCiphertextCompressed { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGSWCompressed { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at_mut(row, col), k: self.k, base2k: self.base2k, @@ -186,7 +227,7 @@ impl GGSWCiphertextCompressed { } } -impl ReaderFrom for GGSWCiphertextCompressed { +impl ReaderFrom for GGSWCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -201,7 +242,7 @@ impl ReaderFrom for GGSWCiphertextCompressed { } } -impl WriterTo for GGSWCiphertextCompressed { +impl WriterTo for GGSWCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -215,23 +256,72 @@ impl WriterTo for GGSWCiphertextCompressed { } } -impl Decompress> for GGSWCiphertext +pub trait GGSWDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGSWCiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), other.rank()) - } + fn decompress_ggsw(&self, res: &mut R, other: &O) + where + R: GGSWToMut, + O: GGSWCompressedToRef, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let other: &GGSWCompressed<&[u8]> = &other.to_ref(); - let dnum: usize = self.dnum().into(); - let rank: usize = self.rank().into(); - (0..dnum).for_each(|row_i| { - (0..rank + 1).for_each(|col_j| { - self.at_mut(row_i, col_j) - .decompress(module, &other.at(row_i, col_j)); - }); - }); + assert_eq!(res.rank(), other.rank()); + let dnum: usize = res.dnum().into(); + let rank: usize = res.rank().into(); + + for row_i in 0..dnum { + for col_j in 0..rank + 1 { + self.decompress_glwe(&mut res.at_mut(row_i, col_j), &other.at(row_i, col_j)); + } + } + } +} + +impl GGSWDecompress for Module where Self: GGSWDecompress {} + +impl GGSW { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: GGSWCompressedToRef, + M: GGSWDecompress, + { + module.decompress_ggsw(self, other); + } +} + +pub trait GGSWCompressedToMut { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]>; +} + +impl GGSWCompressedToMut for GGSWCompressed { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]> { + GGSWCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + rank: self.rank(), + seed: self.seed.clone(), + data: self.data.to_mut(), + } + } +} + +pub trait GGSWCompressedToRef { + fn to_ref(&self) -> GGSWCompressed<&[u8]>; +} + +impl GGSWCompressedToRef for GGSWCompressed { + fn to_ref(&self) -> GGSWCompressed<&[u8]> { + GGSWCompressed { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + rank: self.rank(), + seed: self.seed.clone(), + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 30a3733..e558cf7 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -1,15 +1,19 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; +use crate::layouts::{ + Base2K, GLWE, GLWEInfos, GLWEToMut, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertextCompressed { +pub struct GLWECompressed { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -17,7 +21,7 @@ pub struct GLWECiphertextCompressed { pub(crate) seed: [u8; 32], } -impl LWEInfos for GLWECiphertextCompressed { +impl LWEInfos for GLWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -30,27 +34,27 @@ impl LWEInfos for GLWECiphertextCompressed { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } -impl GLWEInfos for GLWECiphertextCompressed { +impl GLWEInfos for GLWECompressed { fn rank(&self) -> Rank { self.rank } } -impl fmt::Debug for GLWECiphertextCompressed { +impl fmt::Debug for GLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertextCompressed { +impl fmt::Display for GLWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertextCompressed: base2k={} k={} rank={} seed={:?}: {}", + "GLWECompressed: base2k={} k={} rank={} seed={:?}: {}", self.base2k(), self.k(), self.rank(), @@ -60,23 +64,23 @@ impl fmt::Display for GLWECiphertextCompressed { } } -impl FillUniform for GLWECiphertextCompressed { +impl FillUniform for GLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), +pub trait GLWECompressedAlloc +where + Self: GetRingDegree, +{ + fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { + GLWECompressed { + data: VecZnx::alloc( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, rank, @@ -84,19 +88,66 @@ impl GLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_compressed_from_infos(&self, infos: &A) -> GLWECompressed> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + assert_eq!(self.ring_degree(), infos.n()); + self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::alloc_bytes(n.into(), 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + assert_eq!(self.ring_degree(), infos.n()); + self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) } } -impl ReaderFrom for GLWECiphertextCompressed { +impl GLWECompressedAlloc for Module where Self: GetRingDegree {} + +impl GLWECompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWECompressedAlloc, + { + module.alloc_glwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + M: GLWECompressedAlloc, + { + module.alloc_glwe_compressed(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize + where + M: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed(base2k, k) + } +} + +impl ReaderFrom for GLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -106,7 +157,7 @@ impl ReaderFrom for GLWECiphertextCompressed { } } -impl WriterTo for GLWECiphertextCompressed { +impl WriterTo for GLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -116,63 +167,82 @@ impl WriterTo for GLWECiphertextCompressed { } } -impl Decompress> for GLWECiphertext +pub trait GLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GLWECiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", - self.n(), - other.n() - ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank(), - other.rank(), - "invalid receiver: self.rank()={} != other.rank()={}", - self.rank(), - other.rank() - ); - } - - let mut source: Source = Source::new(other.seed); - self.decompress_internal(module, other, &mut source); - } -} - -impl GLWECiphertext { - pub(crate) fn decompress_internal( - &mut self, - module: &Module, - other: &GLWECiphertextCompressed, - source: &mut Source, - ) where - DataOther: DataRef, - Module: VecZnxCopy + VecZnxFillUniform, + fn decompress_glwe(&self, res: &mut R, other: &O) + where + R: GLWEToMut + SetGLWEInfos, + O: GLWECompressedToRef + GLWEInfos, { - #[cfg(debug_assertions)] { - assert_eq!(self.rank(), other.rank()); - debug_assert_eq!(self.size(), other.size()); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GLWECompressed<&[u8]> = &other.to_ref(); + assert_eq!( + res.n(), + self.ring_degree(), + "invalid receiver: res.n()={} != other.n()={}", + res.n(), + self.ring_degree() + ); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + assert_eq!(res.glwe_layout(), other.glwe_layout()); + + let mut source: Source = Source::new(other.seed); + + self.vec_znx_copy(&mut res.data, 0, &other.data, 0); + (1..(other.rank() + 1).into()).for_each(|i| { + self.vec_znx_fill_uniform(other.base2k.into(), &mut res.data, i, &mut source); + }); } - module.vec_znx_copy(&mut self.data, 0, &other.data, 0); - (1..(other.rank() + 1).into()).for_each(|i| { - module.vec_znx_fill_uniform(other.base2k.into(), &mut self.data, i, source); - }); - - self.base2k = other.base2k; - self.k = other.k; + res.set_base2k(other.base2k()); + res.set_k(other.k()); + } +} + +impl GLWEDecompress for Module where Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy {} + +impl GLWE { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: GLWECompressedToRef + GLWEInfos, + M: GLWEDecompress, + { + module.decompress_glwe(self, other); + } +} + +pub trait GLWECompressedToRef { + fn to_ref(&self) -> GLWECompressed<&[u8]>; +} + +impl GLWECompressedToRef for GLWECompressed { + fn to_ref(&self) -> GLWECompressed<&[u8]> { + GLWECompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_ref(), + } + } +} + +pub trait GLWECompressedToMut { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]>; +} + +impl GLWECompressedToMut for GLWECompressed { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> { + GLWECompressed { + seed: self.seed.clone(), + base2k: self.base2k, + k: self.k, + rank: self.rank, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 63933e8..5bfa3eb 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -1,16 +1,21 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWESwitchingKeyCompressed, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, RingDegree, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for GLWEToLWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -21,7 +26,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } fn size(&self) -> usize { @@ -83,54 +88,146 @@ impl WriterTo for GLWEToLWESwitchingKeyCompressed { } } -impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyCompressed> { + GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } +} + +impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + +impl GLWEToLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + M: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize + where + M: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O) + where + R: GLWEToLWESwitchingKeyToMut, + O: GLWEToLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl GLWEToLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl GLWEToLWESwitchingKey { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: GLWEToLWESwitchingKeyCompressedToRef, + M: GLWEToLWESwitchingKeyDecompress, + { + module.decompress_glwe_to_lwe_switching_key(self, other); + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index e11b3f3..0393758 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -2,21 +2,24 @@ use std::fmt; use poulpy_hal::{ api::ZnFillUniform, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnxInfos, ZnxView, ZnxViewMut}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos, ZnxView, + ZnxViewMut, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, LWECiphertext, LWEInfos, TorusPrecision, compressed::Decompress}; +use crate::layouts::{Base2K, LWE, LWEInfos, LWEToMut, RingDegree, TorusPrecision}; #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertextCompressed { +pub struct LWECompressed { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) seed: [u8; 32], } -impl LWEInfos for LWECiphertextCompressed { +impl LWEInfos for LWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -25,8 +28,8 @@ impl LWEInfos for LWECiphertextCompressed { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -34,17 +37,17 @@ impl LWEInfos for LWECiphertextCompressed { } } -impl fmt::Debug for LWECiphertextCompressed { +impl fmt::Debug for LWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertextCompressed { +impl fmt::Display for LWECompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "LWECiphertextCompressed: base2k={} k={} seed={:?}: {}", + "LWECompressed: base2k={} k={} seed={:?}: {}", self.base2k(), self.k(), self.seed, @@ -53,22 +56,15 @@ impl fmt::Display for LWECiphertextCompressed { } } -impl FillUniform for LWECiphertextCompressed { +impl FillUniform for LWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl LWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWECompressedAlloc { + fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed> { + LWECompressed { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, @@ -76,21 +72,62 @@ impl LWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_compressed_from_infos(&self, infos: &A) -> LWECompressed> where A: LWEInfos, { - Self::alloc_bytes_with(infos.base2k(), infos.k()) + self.alloc_lwe_compressed(infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes(1, 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_lwe_compressed_from_infos(&self, infos: &A) -> usize + where + A: LWEInfos, + { + self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) + } +} + +impl LWECompressedAlloc for Module {} + +impl LWECompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: LWEInfos, + M: LWECompressedAlloc, + { + module.alloc_lwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self + where + M: LWECompressedAlloc, + { + module.alloc_lwe_compressed(base2k, k) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: LWEInfos, + M: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize + where + M: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed(base2k, k) } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for LWECiphertextCompressed { +impl ReaderFrom for LWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -99,7 +136,7 @@ impl ReaderFrom for LWECiphertextCompressed { } } -impl WriterTo for LWECiphertextCompressed { +impl WriterTo for LWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -108,22 +145,72 @@ impl WriterTo for LWECiphertextCompressed { } } -impl Decompress> for LWECiphertext +pub trait LWEDecompress where - Module: ZnFillUniform, + Self: ZnFillUniform, { - fn decompress(&mut self, module: &Module, other: &LWECiphertextCompressed) { - debug_assert_eq!(self.size(), other.size()); + fn decompress_lwe(&self, res: &mut R, other: &O) + where + R: LWEToMut, + O: LWECompressedToRef, + { + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let other: &LWECompressed<&[u8]> = &other.to_ref(); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + let mut source: Source = Source::new(other.seed); - module.zn_fill_uniform( - self.n().into(), + self.zn_fill_uniform( + res.n().into(), other.base2k().into(), - &mut self.data, + &mut res.data, 0, &mut source, ); - (0..self.size()).for_each(|i| { - self.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; - }); + for i in 0..res.size() { + res.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; + } + } +} + +impl LWEDecompress for Module where Self: ZnFillUniform {} + +impl LWE { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: LWECompressedToRef, + M: LWEDecompress, + { + module.decompress_lwe(self, other); + } +} + +pub trait LWECompressedToRef { + fn to_ref(&self) -> LWECompressed<&[u8]>; +} + +impl LWECompressedToRef for LWECompressed { + fn to_ref(&self) -> LWECompressed<&[u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_ref(), + } + } +} + +pub trait LWECompressedToMut { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]>; +} + +impl LWECompressedToMut for LWECompressed { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 480707b..cf6ae9a 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -1,17 +1,20 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, RingDegree, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct LWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -22,7 +25,7 @@ impl LWEInfos for LWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } fn size(&self) -> usize { @@ -83,73 +86,149 @@ impl WriterTo for LWESwitchingKeyCompressed { } } -impl LWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait LWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyCompressed> { + LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, - "dsize > 1 is not supported for LWESwitchingKey" + "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, - "rank_in > 1 is not supported for LWESwitchingKey" + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, - "rank_out > 1 is not supported for LWESwitchingKey" + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) } } -impl Decompress> for LWESwitchingKey +impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + +impl LWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + M: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + M: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_switching_key(&self, res: &mut R, other: &O) + where + R: LWESwitchingKeyToMut, + O: LWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWESwitchingKey { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: LWESwitchingKeyCompressedToRef, + M: LWESwitchingKeyDecompress, + { + module.decompress_lwe_switching_key(self, other); + } +} + +pub trait LWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; +} + +impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { + LWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { + LWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 86c353b..33fcf49 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -1,20 +1,23 @@ use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, RingDegree, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWESwitchingKeyCompressed); +pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -83,63 +86,138 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } -impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_to_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyCompressed> { + LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GGLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - GGLWESwitchingKeyCompressed::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } } -impl Decompress> for LWEToGLWESwitchingKey +impl LWEToGLWESwitchingKeyCompressed> { + pub fn alloc(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + M: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + M: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWEToGLWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O) + where + R: LWEToGLWESwitchingKeyToMut, + O: LWEToGLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl LWEToGLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWEToGLWESwitchingKey { + pub fn decompress(&mut self, module: &M, other: &O) + where + O: LWEToGLWESwitchingKeyCompressedToRef, + M: LWEToGLWESwitchingKeyDecompress, + { + module.decompress_lwe_to_glwe_switching_key(self, other); + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/mod.rs b/poulpy-core/src/layouts/compressed/mod.rs index c1fcacf..cd7c459 100644 --- a/poulpy-core/src/layouts/compressed/mod.rs +++ b/poulpy-core/src/layouts/compressed/mod.rs @@ -19,9 +19,3 @@ pub use glwe_to_lwe_ksk::*; pub use lwe_ct::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; - -use poulpy_hal::layouts::{Backend, Module}; - -pub trait Decompress { - fn decompress(&mut self, module: &Module, other: &C); -} diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 5c786d2..eb93bf4 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -1,18 +1,19 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWEAutomorphismKeyLayout { - pub n: Degree, +pub struct AutomorphismKeyLayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -21,19 +22,19 @@ pub struct GGLWEAutomorphismKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKey { - pub(crate) key: GGLWESwitchingKey, +pub struct AutomorphismKey { + pub(crate) key: GLWESwitchingKey, pub(crate) p: i64, } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for GGLWEAutomorphismKey { - fn n(&self) -> Degree { +impl LWEInfos for AutomorphismKey { + fn n(&self) -> RingDegree { self.key.n() } @@ -50,13 +51,13 @@ impl LWEInfos for GGLWEAutomorphismKey { } } -impl GLWEInfos for GGLWEAutomorphismKey { +impl GLWEInfos for AutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKey { +impl GGLWEInfos for AutomorphismKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -74,7 +75,7 @@ impl GGLWEInfos for GGLWEAutomorphismKey { } } -impl LWEInfos for GGLWEAutomorphismKeyLayout { +impl LWEInfos for AutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -83,18 +84,18 @@ impl LWEInfos for GGLWEAutomorphismKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } -impl GLWEInfos for GGLWEAutomorphismKeyLayout { +impl GLWEInfos for AutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } -impl GGLWEInfos for GGLWEAutomorphismKeyLayout { +impl GGLWEInfos for AutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -112,84 +113,164 @@ impl GGLWEInfos for GGLWEAutomorphismKeyLayout { } } -impl fmt::Debug for GGLWEAutomorphismKey { +impl fmt::Debug for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKey { +impl FillUniform for AutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKey { +impl fmt::Display for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKey> { - pub fn alloc(infos: &A) -> Self +impl AutomorphismKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +pub trait AutomorphismKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_automorphism_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKey> { + AutomorphismKey { + key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_from_infos(&self, infos: &A) -> AutomorphismKey> + where + A: GGLWEInfos, + { + self.alloc_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" + "rank_in != rank_out is not supported for AutomorphismKey" ); - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc(infos), - p: 0, - } - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - pub fn alloc_bytes(infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GGLWESwitchingKey::alloc_bytes(infos) - } - - pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) + self.bytes_of_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } -impl GGLWEAutomorphismKey { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { +impl AutomorphismKey> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key_from_infos(infos) + } + + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) + } +} + +pub trait AutomorphismKeyToMut { + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; +} + +impl AutomorphismKeyToMut for AutomorphismKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> { + AutomorphismKey { + key: self.key.to_mut(), + p: self.p, + } + } +} + +pub trait AutomorphismKeyToRef { + fn to_ref(&self) -> AutomorphismKey<&[u8]>; +} + +impl AutomorphismKeyToRef for AutomorphismKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> AutomorphismKey<&[u8]> { + AutomorphismKey { + p: self.p, + key: self.key.to_ref(), + } + } +} + +impl AutomorphismKey { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } -impl GGLWEAutomorphismKey { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { +impl AutomorphismKey { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWEAutomorphismKey { +impl ReaderFrom for AutomorphismKey { 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 GGLWEAutomorphismKey { +impl WriterTo for AutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index ca8236c..3b95d63 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -1,9 +1,11 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -16,8 +18,8 @@ where fn dsize(&self) -> Dsize; fn rank_in(&self) -> Rank; fn rank_out(&self) -> Rank; - fn layout(&self) -> GGLWECiphertextLayout { - GGLWECiphertextLayout { + fn gglwe_layout(&self) -> GGLWELayout { + GGLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -29,9 +31,13 @@ where } } +pub trait SetGGLWEInfos { + fn set_dsize(&mut self, dsize: usize); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWECiphertextLayout { - pub n: Degree, +pub struct GGLWELayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -40,7 +46,7 @@ pub struct GGLWECiphertextLayout { pub dsize: Dsize, } -impl LWEInfos for GGLWECiphertextLayout { +impl LWEInfos for GGLWELayout { fn base2k(&self) -> Base2K { self.base2k } @@ -49,18 +55,18 @@ impl LWEInfos for GGLWECiphertextLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } -impl GLWEInfos for GGLWECiphertextLayout { +impl GLWEInfos for GGLWELayout { fn rank(&self) -> Rank { self.rank_out } } -impl GGLWEInfos for GGLWECiphertextLayout { +impl GGLWEInfos for GGLWELayout { fn rank_in(&self) -> Rank { self.rank_in } @@ -79,14 +85,14 @@ impl GGLWEInfos for GGLWECiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertext { +pub struct GGLWE { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertext { +impl LWEInfos for GGLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -95,8 +101,8 @@ impl LWEInfos for GGLWECiphertext { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -104,13 +110,13 @@ impl LWEInfos for GGLWECiphertext { } } -impl GLWEInfos for GGLWECiphertext { +impl GLWEInfos for GGLWE { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertext { +impl GGLWEInfos for GGLWE { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -128,136 +134,35 @@ impl GGLWEInfos for GGLWECiphertext { } } -pub struct GGLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGLWECiphertext { - #[inline] - pub fn builder() -> GGLWECiphertextBuilder { - GGLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - { - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWECiphertext { - data, - base2k, - k, - dsize, - }) - } -} - -impl GGLWECiphertext { +impl GGLWE { pub fn data(&self) -> &MatZnx { &self.data } } -impl GGLWECiphertext { +impl GGLWE { pub fn data_mut(&mut self) -> &mut MatZnx { &mut self.data } } -impl fmt::Debug for GGLWECiphertext { +impl fmt::Debug for GGLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertext { +impl FillUniform for GGLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertext { +impl fmt::Display for GGLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGLWECiphertext: k={} base2k={} dsize={}) {}", + "(GGLWE: k={} base2k={} dsize={}) {}", self.k().0, self.base2k().0, self.dsize().0, @@ -266,53 +171,39 @@ impl fmt::Display for GGLWECiphertext { } } -impl GGLWECiphertext { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() +impl GGLWE { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } -impl GGLWECiphertext { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() +impl GGLWE { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } -impl GGLWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWEAlloc +where + Self: GetRingDegree, +{ + fn alloc_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWE> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -327,9 +218,9 @@ impl GGLWECiphertext> { dsize.0, ); - Self { + GGLWE { data: MatZnx::alloc( - n.into(), + self.ring_degree().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -341,12 +232,11 @@ impl GGLWECiphertext> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GGLWE> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_gglwe( infos.base2k(), infos.k(), infos.rank_in(), @@ -356,8 +246,8 @@ impl GGLWECiphertext> { ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -379,17 +269,111 @@ impl GGLWECiphertext> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.ring_degree().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } } -impl ReaderFrom for GGLWECiphertext { +impl GGLWEAlloc for Module where Self: GetRingDegree {} + +impl GGLWE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GGLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + M: GGLWEAlloc, + { + module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GGLWEAlloc, + { + module.bytes_of_gglwe_from_infos(infos) + } + + pub fn bytes_of( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + M: GGLWEAlloc, + { + module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } +} + +pub trait GGLWEToMut { + fn to_mut(&mut self) -> GGLWE<&mut [u8]>; +} + +impl GGLWEToMut for GGLWE { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + GGLWE { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + data: self.data.to_mut(), + } + } +} + +pub trait GGLWEToRef { + fn to_ref(&self) -> GGLWE<&[u8]>; +} + +impl GGLWEToRef for GGLWE { + fn to_ref(&self) -> GGLWE<&[u8]> { + GGLWE { + k: self.k(), + base2k: self.base2k(), + dsize: self.dsize(), + data: self.data.to_ref(), + } + } +} + +impl ReaderFrom for GGLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -398,7 +382,7 @@ impl ReaderFrom for GGLWECiphertext { } } -impl WriterTo for GGLWECiphertext { +impl WriterTo for GGLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 31a483b..ddb4de7 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -1,18 +1,19 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, RingDegree, + TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWESwitchingKeyLayout { - pub n: Degree, +pub struct GLWESwitchingKeyLayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -21,8 +22,8 @@ pub struct GGLWESwitchingKeyLayout { pub dsize: Dsize, } -impl LWEInfos for GGLWESwitchingKeyLayout { - fn n(&self) -> Degree { +impl LWEInfos for GLWESwitchingKeyLayout { + fn n(&self) -> RingDegree { self.n } @@ -35,13 +36,13 @@ impl LWEInfos for GGLWESwitchingKeyLayout { } } -impl GLWEInfos for GGLWESwitchingKeyLayout { +impl GLWEInfos for GLWESwitchingKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyLayout { +impl GGLWEInfos for GLWESwitchingKeyLayout { fn rank_in(&self) -> Rank { self.rank_in } @@ -60,14 +61,44 @@ impl GGLWEInfos for GGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWESwitchingKey { - pub(crate) key: GGLWECiphertext, +pub struct GLWESwitchingKey { + pub(crate) key: GGLWE, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKey { - fn n(&self) -> Degree { +pub(crate) trait GLWESwitchingKeySetMetaData { + fn set_sk_in_n(&mut self, sk_in_n: usize); + fn set_sk_out_n(&mut self, sk_out_n: usize); +} + +impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { + fn set_sk_in_n(&mut self, sk_in_n: usize) { + self.sk_in_n = sk_in_n + } + + fn set_sk_out_n(&mut self, sk_out_n: usize) { + self.sk_out_n = sk_out_n + } +} + +pub(crate) trait GLWESwtichingKeyGetMetaData { + fn sk_in_n(&self) -> usize; + fn sk_out_n(&self) -> usize; +} + +impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { + fn sk_in_n(&self) -> usize { + self.sk_in_n + } + + fn sk_out_n(&self) -> usize { + self.sk_out_n + } +} + +impl LWEInfos for GLWESwitchingKey { + fn n(&self) -> RingDegree { self.key.n() } @@ -84,13 +115,13 @@ impl LWEInfos for GGLWESwitchingKey { } } -impl GLWEInfos for GGLWESwitchingKey { +impl GLWEInfos for GLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKey { +impl GGLWEInfos for GLWESwitchingKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -108,13 +139,13 @@ impl GGLWEInfos for GGLWESwitchingKey { } } -impl fmt::Debug for GGLWESwitchingKey { +impl fmt::Debug for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GGLWESwitchingKey { +impl fmt::Display for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -126,49 +157,48 @@ impl fmt::Display for GGLWESwitchingKey { } } -impl FillUniform for GGLWESwitchingKey { +impl FillUniform for GLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl GGLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +pub trait GLWESwitchingKeyAlloc +where + Self: GGLWEAlloc, +{ + fn alloc_glwe_switching_key( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + ) -> GLWESwitchingKey> { + GLWESwitchingKey { + key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_from_infos(&self, infos: &A) -> GLWESwitchingKey> where A: GGLWEInfos, { - GGLWECiphertext::alloc_bytes(infos) + self.alloc_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_glwe_switching_key( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -176,23 +206,121 @@ impl GGLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> usize { - GGLWECiphertext::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } } -impl GGLWESwitchingKey { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { +impl GLWESwitchingKeyAlloc for Module where Self: GGLWEAlloc {} + +impl GLWESwitchingKey> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key_from_infos(infos) + } + + pub fn alloc( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + M: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key_from_infos(infos) + } + + pub fn bytes_of( + module: &M, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + M: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + } +} + +pub trait GLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; +} + +impl GLWESwitchingKeyToMut for GLWESwitchingKey +where + GGLWE: GGLWEToMut, +{ + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { + GLWESwitchingKey { + key: self.key.to_mut(), + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + } + } +} + +pub trait GLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; +} + +impl GLWESwitchingKeyToRef for GLWESwitchingKey +where + GGLWE: GGLWEToRef, +{ + fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { + GLWESwitchingKey { + key: self.key.to_ref(), + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + } + } +} + +impl GLWESwitchingKey { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } -impl GGLWESwitchingKey { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { +impl GLWESwitchingKey { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWESwitchingKey { +impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -200,7 +328,7 @@ impl ReaderFrom for GGLWESwitchingKey { } } -impl WriterTo for GGLWESwitchingKey { +impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index a949b7e..6ef67b8 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -1,16 +1,19 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWETensorKeyLayout { - pub n: Degree, +pub struct TensorKeyLayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -19,12 +22,12 @@ pub struct GGLWETensorKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKey { - pub(crate) keys: Vec>, +pub struct TensorKey { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKey { - fn n(&self) -> Degree { +impl LWEInfos for TensorKey { + fn n(&self) -> RingDegree { self.keys[0].n() } @@ -41,13 +44,13 @@ impl LWEInfos for GGLWETensorKey { } } -impl GLWEInfos for GGLWETensorKey { +impl GLWEInfos for TensorKey { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl GGLWEInfos for GGLWETensorKey { +impl GGLWEInfos for TensorKey { fn rank_in(&self) -> Rank { self.rank_out() } @@ -65,8 +68,8 @@ impl GGLWEInfos for GGLWETensorKey { } } -impl LWEInfos for GGLWETensorKeyLayout { - fn n(&self) -> Degree { +impl LWEInfos for TensorKeyLayout { + fn n(&self) -> RingDegree { self.n } @@ -79,13 +82,13 @@ impl LWEInfos for GGLWETensorKeyLayout { } } -impl GLWEInfos for GGLWETensorKeyLayout { +impl GLWEInfos for TensorKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyLayout { +impl GGLWEInfos for TensorKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -103,21 +106,21 @@ impl GGLWEInfos for GGLWETensorKeyLayout { } } -impl fmt::Debug for GGLWETensorKey { +impl fmt::Debug for TensorKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKey { +impl FillUniform for TensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWESwitchingKey| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKey| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKey { +impl fmt::Display for TensorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -127,8 +130,20 @@ impl fmt::Display for GGLWETensorKey { } } -impl GGLWETensorKey> { - pub fn alloc(infos: &A) -> Self +pub trait TensorKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey> { + let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); + TensorKey { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn alloc_tensor_key_from_infos(&self, infos: &A) -> TensorKey> where A: GGLWEInfos, { @@ -137,34 +152,21 @@ impl GGLWETensorKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - Self::alloc_with( - infos.n(), + self.alloc_tensor_key( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_tensor_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -173,29 +175,53 @@ impl GGLWETensorKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GGLWESwitchingKey::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) + self.bytes_of_tensor_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } -impl GGLWETensorKey { +impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +impl TensorKey> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: TensorKeyAlloc, + { + module.alloc_tensor_key_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: TensorKeyAlloc, + { + module.alloc_tensor_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: TensorKeyAlloc, + { + module.bytes_of_tensor_key_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: TensorKeyAlloc, + { + module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) + } +} + +impl TensorKey { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKey { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -204,9 +230,9 @@ impl GGLWETensorKey { } } -impl GGLWETensorKey { +impl TensorKey { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKey { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -215,7 +241,7 @@ impl GGLWETensorKey { } } -impl ReaderFrom for GGLWETensorKey { +impl ReaderFrom for TensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -231,7 +257,7 @@ impl ReaderFrom for GGLWETensorKey { } } -impl WriterTo for GGLWETensorKey { +impl WriterTo for TensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -240,3 +266,33 @@ impl WriterTo for GGLWETensorKey { Ok(()) } } + +pub trait TensorKeyToRef { + fn to_ref(&self) -> TensorKey<&[u8]>; +} + +impl TensorKeyToRef for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> TensorKey<&[u8]> { + TensorKey { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } + } +} + +pub trait TensorKeyToMut { + fn to_mut(&mut self) -> TensorKey<&mut [u8]>; +} + +impl TensorKeyToMut for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> TensorKey<&mut [u8]> { + TensorKey { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index f1bb228..aac4a20 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -1,10 +1,12 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; use std::fmt; -use crate::layouts::{Base2K, BuildError, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; pub trait GGSWInfos where @@ -12,8 +14,8 @@ where { fn dnum(&self) -> Dnum; fn dsize(&self) -> Dsize; - fn ggsw_layout(&self) -> GGSWCiphertextLayout { - GGSWCiphertextLayout { + fn ggsw_layout(&self) -> GGSWLayout { + GGSWLayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -25,8 +27,8 @@ where } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGSWCiphertextLayout { - pub n: Degree, +pub struct GGSWLayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -34,7 +36,7 @@ pub struct GGSWCiphertextLayout { pub dsize: Dsize, } -impl LWEInfos for GGSWCiphertextLayout { +impl LWEInfos for GGSWLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -43,17 +45,17 @@ impl LWEInfos for GGSWCiphertextLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } -impl GLWEInfos for GGSWCiphertextLayout { +impl GLWEInfos for GGSWLayout { fn rank(&self) -> Rank { self.rank } } -impl GGSWInfos for GGSWCiphertextLayout { +impl GGSWInfos for GGSWLayout { fn dsize(&self) -> Dsize { self.dsize } @@ -64,16 +66,16 @@ impl GGSWInfos for GGSWCiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertext { +pub struct GGSW { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertext { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) +impl LWEInfos for GGSW { + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -89,13 +91,13 @@ impl LWEInfos for GGSWCiphertext { } } -impl GLWEInfos for GGSWCiphertext { +impl GLWEInfos for GGSW { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertext { +impl GGSWInfos for GGSW { fn dsize(&self) -> Dsize { self.dsize } @@ -105,133 +107,17 @@ impl GGSWInfos for GGSWCiphertext { } } -pub struct GGSWCiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSWCiphertext { - #[inline] - pub fn builder() -> GGSWCiphertextBuilder { - GGSWCiphertextBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextBuilder> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(MatZnx::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextBuilder { - #[inline] - pub fn data(mut self, data: MatZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSWCiphertext { - data, - base2k, - k, - dsize, - }) - } -} - -impl fmt::Debug for GGSWCiphertext { +impl fmt::Debug for GGSW { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertext { +impl fmt::Display for GGSW { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGSWCiphertext: k: {} base2k: {} dsize: {}) {}", + "(GGSW: k: {} base2k: {} dsize: {}) {}", self.k().0, self.base2k().0, self.dsize().0, @@ -240,50 +126,39 @@ impl fmt::Display for GGSWCiphertext { } } -impl FillUniform for GGSWCiphertext { +impl FillUniform for GGSW { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertext { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.at(row, col)) - .base2k(self.base2k()) - .k(self.k()) - .build() - .unwrap() +impl GGSW { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.at(row, col), + } } } -impl GGSWCiphertext { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.at_mut(row, col)) - .build() - .unwrap() +impl GGSW { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.at_mut(row, col), + } } } -impl GGSWCiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } +impl GGSWAlloc for Module where Self: GetRingDegree {} - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWAlloc +where + Self: GetRingDegree, +{ + fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -298,9 +173,9 @@ impl GGSWCiphertext> { dsize.0, ); - Self { + GGSW { data: MatZnx::alloc( - n.into(), + self.ring_degree().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -312,12 +187,11 @@ impl GGSWCiphertext> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_from_infos(&self, infos: &A) -> GGSW> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw( infos.base2k(), infos.k(), infos.rank(), @@ -326,7 +200,7 @@ impl GGSWCiphertext> { ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -341,19 +215,64 @@ impl GGSWCiphertext> { dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.ring_degree().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_ggsw_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGSW> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGSWInfos, + M: GGSWAlloc, + { + module.alloc_ggsw_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + M: GGSWAlloc, + { + module.alloc_ggsw(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGSWInfos, + M: GGSWAlloc, + { + module.bytes_of_ggsw_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: GGSWAlloc, + { + module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) + } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for GGSWCiphertext { +impl ReaderFrom for GGSW { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -362,7 +281,7 @@ impl ReaderFrom for GGSWCiphertext { } } -impl WriterTo for GGSWCiphertext { +impl WriterTo for GGSW { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -370,3 +289,33 @@ impl WriterTo for GGSWCiphertext { self.data.write_to(writer) } } + +pub trait GGSWToMut { + fn to_mut(&mut self) -> GGSW<&mut [u8]>; +} + +impl GGSWToMut for GGSW { + fn to_mut(&mut self) -> GGSW<&mut [u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} + +pub trait GGSWToRef { + fn to_ref(&self) -> GGSW<&[u8]>; +} + +impl GGSWToRef for GGSW { + fn to_ref(&self) -> GGSW<&[u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 23b6ef9..2361e3c 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -1,11 +1,12 @@ use poulpy_hal::{ layouts::{ - Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, + WriterTo, ZnxInfos, }, source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -14,8 +15,8 @@ where Self: LWEInfos, { fn rank(&self) -> Rank; - fn glwe_layout(&self) -> GLWECiphertextLayout { - GLWECiphertextLayout { + fn glwe_layout(&self) -> GLWELayout { + GLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(), @@ -24,21 +25,21 @@ where } } -pub trait GLWELayoutSet { +pub trait SetGLWEInfos { fn set_k(&mut self, k: TorusPrecision); - fn set_basek(&mut self, base2k: Base2K); + fn set_base2k(&mut self, base2k: Base2K); } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GLWECiphertextLayout { - pub n: Degree, +pub struct GLWELayout { + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } -impl LWEInfos for GLWECiphertextLayout { - fn n(&self) -> Degree { +impl LWEInfos for GLWELayout { + fn n(&self) -> RingDegree { self.n } @@ -51,21 +52,21 @@ impl LWEInfos for GLWECiphertextLayout { } } -impl GLWEInfos for GLWECiphertextLayout { +impl GLWEInfos for GLWELayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertext { +pub struct GLWE { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, } -impl GLWELayoutSet for GLWECiphertext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWE { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -74,99 +75,19 @@ impl GLWELayoutSet for GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub fn data(&self) -> &VecZnx { &self.data } } -impl GLWECiphertext { +impl GLWE { pub fn data_mut(&mut self) -> &mut VecZnx { &mut self.data } } -pub struct GLWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWECiphertext { - #[inline] - pub fn builder() -> GLWECiphertextBuilder { - GLWECiphertextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWECiphertext { data, base2k, k }) - } -} - -impl LWEInfos for GLWECiphertext { +impl LWEInfos for GLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -175,8 +96,8 @@ impl LWEInfos for GLWECiphertext { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -184,16 +105,16 @@ impl LWEInfos for GLWECiphertext { } } -impl GLWEInfos for GLWECiphertext { +impl GLWEInfos for GLWE { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } -impl ToOwnedDeep for GLWECiphertext { - type Owned = GLWECiphertext>; +impl ToOwnedDeep for GLWE { + type Owned = GLWE>; fn to_owned_deep(&self) -> Self::Owned { - GLWECiphertext { + GLWE { data: self.data.to_owned_deep(), k: self.k, base2k: self.base2k, @@ -201,17 +122,17 @@ impl ToOwnedDeep for GLWECiphertext { } } -impl fmt::Debug for GLWECiphertext { +impl fmt::Debug for GLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertext { +impl fmt::Display for GLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertext: base2k={} k={}: {}", + "GLWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data @@ -219,71 +140,86 @@ impl fmt::Display for GLWECiphertext { } } -impl FillUniform for GLWECiphertext { +impl FillUniform for GLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEAlloc +where + Self: GetRingDegree, +{ + fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { + GLWE { + data: VecZnx::alloc( + self.ring_degree().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GLWE> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.ring_degree().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) } } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; -} +impl GLWEAlloc for Module where Self: GetRingDegree {} -impl GLWECiphertextToRef for GLWECiphertext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GLWE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + M: GLWEAlloc, + { + module.alloc_glwe(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWEAlloc, + { + module.bytes_of_glwe_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + M: GLWEAlloc, + { + module.bytes_of_glwe(base2k, k, rank) } } -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; -} - -impl GLWECiphertextToMut for GLWECiphertext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() - } -} - -impl ReaderFrom for GLWECiphertext { +impl ReaderFrom for GLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -291,10 +227,38 @@ impl ReaderFrom for GLWECiphertext { } } -impl WriterTo for GLWECiphertext { +impl WriterTo for GLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; self.data.write_to(writer) } } + +pub trait GLWEToRef { + fn to_ref(&self) -> GLWE<&[u8]>; +} + +impl GLWEToRef for GLWE { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } + } +} + +pub trait GLWEToMut { + fn to_mut(&mut self) -> GLWE<&mut [u8]>; +} + +impl GLWEToMut for GLWE { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index fc4b0fa..16fc6ea 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -1,8 +1,10 @@ -use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo, ZnxInfos}; +use poulpy_hal::layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, +}; use crate::{ dist::Distribution, - layouts::{Base2K, BuildError, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -16,12 +18,22 @@ pub struct GLWEPublicKey { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } +pub trait GetDist { + fn get_dist(&self) -> Distribution; +} + +impl GetDist for GLWEPublicKey { + fn get_dist(&self) -> Distribution { + self.dist + } +} + impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k @@ -31,8 +43,8 @@ impl LWEInfos for GLWEPublicKey { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -55,7 +67,7 @@ impl LWEInfos for GLWEPublicKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -70,117 +82,77 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -pub struct GLWEPublicKeyBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKey { - #[inline] - pub fn builder() -> GLWEPublicKeyBuilder { - GLWEPublicKeyBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyBuilder> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - { - self.data = Some(VecZnx::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKey { - data, +pub trait GLWEPublicKeyAlloc +where + Self: GetRingDegree, +{ + fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { + GLWEPublicKey { + data: VecZnx::alloc( + self.ring_degree().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, dist: Distribution::NONE, - }) + } + } + + fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> + where + A: GLWEInfos, + { + self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + } + + fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.ring_degree().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_public_key_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) } } +impl GLWEPublicKeyAlloc for Module where Self: GetRingDegree {} + impl GLWEPublicKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, + M: GLWEPublicKeyAlloc, { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + module.alloc_glwe_public_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - dist: Distribution::NONE, - } + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + M: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key(base2k, k, rank) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, + M: GLWEPublicKeyAlloc, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + module.bytes_of_glwe_public_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + M: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key(base2k, k, rank) } } @@ -207,3 +179,33 @@ impl WriterTo for GLWEPublicKey { self.data.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, + } + } +} + +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(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index b565055..3d5ce86 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -1,15 +1,14 @@ use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, BuildError, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, - Rank, TorusPrecision, + Base2K, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, } @@ -23,7 +22,7 @@ impl LWEInfos for GLWEPlaintextLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -40,8 +39,8 @@ pub struct GLWEPlaintext { pub k: TorusPrecision, } -impl GLWELayoutSet for GLWEPlaintext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWEPlaintext { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -63,8 +62,8 @@ impl LWEInfos for GLWEPlaintext { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } @@ -74,69 +73,6 @@ impl GLWEInfos for GLWEPlaintext { } } -pub struct GLWEPlaintextBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPlaintext { - #[inline] - pub fn builder() -> GLWEPlaintextBuilder { - GLWEPlaintextBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPlaintextBuilder { - #[inline] - pub fn data(mut self, data: VecZnx) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() != 1 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPlaintext { data, base2k, k }) - } -} - impl fmt::Display for GLWEPlaintext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -149,54 +85,123 @@ impl fmt::Display for GLWEPlaintext { } } -impl GLWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - debug_assert!(rank.0 == 0); - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEPlaintextAlloc +where + Self: GetRingDegree, +{ + fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { + GLWEPlaintext { + data: VecZnx::alloc( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) + self.alloc_glwe_plaintext(infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - debug_assert!(rank.0 == 0); - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of( + self.ring_degree().into(), + 1, + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) } } -impl GLWECiphertextToRef for GLWEPlaintext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .data(self.data.to_ref()) - .k(self.k()) - .base2k(self.base2k()) - .build() - .unwrap() +impl GLWEPlaintextAlloc for Module where Self: GetRingDegree {} + +impl GLWEPlaintext> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self + where + M: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext(base2k, k) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize + where + M: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext(base2k, k) } } -impl GLWECiphertextToMut for GLWEPlaintext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl GLWEToRef for GLWEPlaintext { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } + } +} + +impl GLWEToMut for GLWEPlaintext { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} + +pub trait GLWEPlaintextToRef { + fn to_ref(&self) -> GLWEPlaintext<&[u8]>; +} + +impl GLWEPlaintextToRef for GLWEPlaintext { + fn to_ref(&self) -> GLWEPlaintext<&[u8]> { + GLWEPlaintext { + data: self.data.to_ref(), + base2k: self.base2k, + k: self.k, + } + } +} + +pub trait GLWEPlaintextToMut { + fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>; +} + +impl GLWEPlaintextToMut for GLWEPlaintext { + fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> { + GLWEPlaintext { + base2k: self.base2k, + k: self.k, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 8870d35..9166388 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -1,16 +1,19 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, WriterTo, ZnxInfos, ZnxZero}, + layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, + ZnxZero, + }, source::Source, }; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESecretLayout { - pub n: Degree, + pub n: RingDegree, pub rank: Rank, } @@ -23,7 +26,7 @@ impl LWEInfos for GLWESecretLayout { TorusPrecision(0) } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -52,8 +55,8 @@ impl LWEInfos for GLWESecret { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -67,30 +70,67 @@ impl GLWEInfos for GLWESecret { } } -impl GLWESecret> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.rank()) - } - - pub fn alloc_with(n: Degree, rank: Rank) -> Self { - Self { - data: ScalarZnx::alloc(n.into(), rank.into()), +pub trait GLWESecretAlloc +where + Self: GetRingDegree, +{ + fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { + GLWESecret { + data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), dist: Distribution::NONE, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_secret_from_infos(&self, infos: &A) -> GLWESecret> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.rank()) + self.alloc_glwe_secret(infos.rank()) } - pub fn alloc_bytes_with(n: Degree, rank: Rank) -> usize { - ScalarZnx::alloc_bytes(n.into(), rank.into()) + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + ScalarZnx::bytes_of(self.ring_degree().into(), rank.into()) + } + + fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_secret(infos.rank()) + } +} + +impl GLWESecretAlloc for Module where Self: GetRingDegree {} + +impl GLWESecret> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GLWEInfos, + M: GLWESecretAlloc, + { + module.alloc_glwe_secret_from_infos(infos) + } + + pub fn alloc(module: &M, rank: Rank) -> Self + where + M: GLWESecretAlloc, + { + module.alloc_glwe_secret(rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GLWEInfos, + M: GLWESecretAlloc, + { + module.bytes_of_glwe_secret_from_infos(infos) + } + + pub fn bytes_of(module: &M, rank: Rank) -> usize + where + M: GLWESecretAlloc, + { + module.bytes_of_glwe_secret(rank) } } @@ -136,6 +176,32 @@ impl GLWESecret { } } +pub trait GLWESecretToMut { + fn to_mut(&mut self) -> GLWESecret<&mut [u8]>; +} + +impl GLWESecretToMut for GLWESecret { + fn to_mut(&mut self) -> GLWESecret<&mut [u8]> { + GLWESecret { + dist: self.dist, + data: self.data.to_mut(), + } + } +} + +pub trait GLWESecretToRef { + fn to_ref(&self) -> GLWESecret<&[u8]>; +} + +impl GLWESecretToRef for GLWESecret { + fn to_ref(&self) -> GLWESecret<&[u8]> { + GLWESecret { + data: self.data.to_ref(), + dist: self.dist, + } + } +} + impl ReaderFrom for GLWESecret { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { match Distribution::read_from(reader) { diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index f227c9c..671f018 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -1,15 +1,18 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, +}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEToLWEKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -17,7 +20,7 @@ pub struct GLWEToLWEKeyLayout { } impl LWEInfos for GLWEToLWEKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -54,11 +57,11 @@ impl GGLWEInfos for GLWEToLWEKeyLayout { } } -/// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext]. +/// A special [GLWESwitchingKey] required to for the conversion from [GLWE] to [LWE]. #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWEKey(pub(crate) GGLWESwitchingKey); +pub struct GLWEToLWESwitchingKey(pub(crate) GLWESwitchingKey); -impl LWEInfos for GLWEToLWEKey { +impl LWEInfos for GLWEToLWESwitchingKey { fn base2k(&self) -> Base2K { self.0.base2k() } @@ -67,7 +70,7 @@ impl LWEInfos for GLWEToLWEKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -76,12 +79,12 @@ impl LWEInfos for GLWEToLWEKey { } } -impl GLWEInfos for GLWEToLWEKey { +impl GLWEInfos for GLWEToLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GLWEToLWEKey { +impl GGLWEInfos for GLWEToLWESwitchingKey { fn rank_in(&self) -> Rank { self.0.rank_in() } @@ -99,84 +102,145 @@ impl GGLWEInfos for GLWEToLWEKey { } } -impl fmt::Debug for GLWEToLWEKey { +impl fmt::Debug for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GLWEToLWEKey { +impl FillUniform for GLWEToLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.0.fill_uniform(log_bound, source); } } -impl fmt::Display for GLWEToLWEKey { +impl fmt::Display for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(GLWEToLWESwitchingKey) {}", self.0) } } -impl ReaderFrom for GLWEToLWEKey { +impl ReaderFrom for GLWEToLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.0.read_from(reader) } } -impl WriterTo for GLWEToLWEKey { +impl WriterTo for GLWEToLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { self.0.write_to(writer) } } -impl GLWEToLWEKey> { - pub fn alloc(infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_glwe_to_lwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKey> { + GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKey> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } +} + +impl GLWEToLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +impl GLWEToLWESwitchingKey> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + M: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + where + M: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; +} + +impl GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> { + GLWEToLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { + GLWEToLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 1560ea4..0c8831f 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -1,15 +1,15 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, source::Source, }; -use crate::layouts::{Base2K, BuildError, Degree, TorusPrecision}; +use crate::layouts::{Base2K, RingDegree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { - fn n(&self) -> Degree; + fn n(&self) -> RingDegree; fn k(&self) -> TorusPrecision; fn max_k(&self) -> TorusPrecision { TorusPrecision(self.k().0 * self.size() as u32) @@ -18,8 +18,8 @@ pub trait LWEInfos { fn size(&self) -> usize { self.k().0.div_ceil(self.base2k().0) as usize } - fn lwe_layout(&self) -> LWECiphertextLayout { - LWECiphertextLayout { + fn lwe_layout(&self) -> LWELayout { + LWELayout { n: self.n(), k: self.k(), base2k: self.base2k(), @@ -27,14 +27,19 @@ pub trait LWEInfos { } } +pub trait SetLWEInfos { + fn set_k(&mut self, k: TorusPrecision); + fn set_base2k(&mut self, base2k: Base2K); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct LWECiphertextLayout { - pub n: Degree, +pub struct LWELayout { + pub n: RingDegree, pub k: TorusPrecision, pub base2k: Base2K, } -impl LWEInfos for LWECiphertextLayout { +impl LWEInfos for LWELayout { fn base2k(&self) -> Base2K { self.base2k } @@ -43,19 +48,18 @@ impl LWEInfos for LWECiphertextLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } - #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertext { +pub struct LWE { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, } -impl LWEInfos for LWECiphertext { +impl LWEInfos for LWE { fn base2k(&self) -> Base2K { self.base2k } @@ -63,8 +67,8 @@ impl LWEInfos for LWECiphertext { fn k(&self) -> TorusPrecision { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32 - 1) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32 - 1) } fn size(&self) -> usize { @@ -72,29 +76,39 @@ impl LWEInfos for LWECiphertext { } } -impl LWECiphertext { +impl SetLWEInfos for LWE { + fn set_base2k(&mut self, base2k: Base2K) { + self.base2k = base2k + } + + fn set_k(&mut self, k: TorusPrecision) { + self.k = k + } +} + +impl LWE { pub fn data(&self) -> &Zn { &self.data } } -impl LWECiphertext { +impl LWE { pub fn data_mut(&mut self) -> &Zn { &mut self.data } } -impl fmt::Debug for LWECiphertext { +impl fmt::Debug for LWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertext { +impl fmt::Display for LWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "LWECiphertext: base2k={} k={}: {}", + "LWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data @@ -102,7 +116,7 @@ impl fmt::Display for LWECiphertext { } } -impl FillUniform for LWECiphertext +impl FillUniform for LWE where Zn: FillUniform, { @@ -111,142 +125,98 @@ where } } -impl LWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEAlloc { + fn alloc_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> LWE> { + LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_from_infos(&self, infos: &A) -> LWE> where A: LWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } -} -impl LWECiphertextBuilder> { - #[inline] - pub fn layout(mut self, layout: A) -> Self + fn bytes_of_lwe_from_infos(&self, infos: &A) -> usize where A: LWEInfos, { - self.data = Some(Zn::alloc((layout.n() + 1).into(), 1, layout.size())); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self + self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) } } -pub struct LWECiphertextBuilder { - data: Option>, - base2k: Option, - k: Option, +impl LWEAlloc for Module {} + +impl LWE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: LWEInfos, + M: LWEAlloc, + { + module.alloc_lwe_from_infos(infos) + } + + pub fn alloc(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> Self + where + M: LWEAlloc, + { + module.alloc_lwe(n, base2k, k) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: LWEInfos, + M: LWEAlloc, + { + module.bytes_of_lwe_from_infos(infos) + } + + pub fn bytes_of(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize + where + M: LWEAlloc, + { + module.bytes_of_lwe(n, base2k, k) + } } -impl LWECiphertext { - #[inline] - pub fn builder() -> LWECiphertextBuilder { - LWECiphertextBuilder { - data: None, - base2k: None, - k: None, +pub trait LWEToRef { + fn to_ref(&self) -> LWE<&[u8]>; +} + +impl LWEToRef for LWE { + fn to_ref(&self) -> LWE<&[u8]> { + LWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), } } } -impl LWECiphertextBuilder { - #[inline] - pub fn data(mut self, data: Zn) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: Zn = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k.0 == 0 { - return Err(BuildError::ZeroBase2K); - } - - if k.0 == 0 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(LWECiphertext { data, base2k, k }) - } -} - -pub trait LWECiphertextToRef { - fn to_ref(&self) -> LWECiphertext<&[u8]>; -} - -impl LWECiphertextToRef for LWECiphertext { - fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() - } -} - -pub trait LWECiphertextToMut { +pub trait LWEToMut { #[allow(dead_code)] - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]>; + fn to_mut(&mut self) -> LWE<&mut [u8]>; } -impl LWECiphertextToMut for LWECiphertext { - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl LWEToMut for LWE { + fn to_mut(&mut self) -> LWE<&mut [u8]> { + LWE { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } -impl ReaderFrom for LWECiphertext { +impl ReaderFrom for LWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -254,7 +224,7 @@ impl ReaderFrom for LWECiphertext { } } -impl WriterTo for LWECiphertext { +impl WriterTo for LWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 314322c..2ff0391 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -1,22 +1,25 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, } impl LWEInfos for LWESwitchingKeyLayout { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } @@ -54,7 +57,7 @@ impl GGLWEInfos for LWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWESwitchingKey { fn base2k(&self) -> Base2K { @@ -65,7 +68,7 @@ impl LWEInfos for LWESwitchingKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -98,65 +101,94 @@ impl GGLWEInfos for LWESwitchingKey { } } +pub trait LWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { + LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } + + fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + } + + fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } +} + +impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl LWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, + M: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - Self(GGLWESwitchingKey::alloc(infos)) + module.alloc_lwe_switching_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + M: LWESwitchingKeyAlloc, + { + module.alloc_lwe_switching_key(base2k, k, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GGLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_glwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + M: LWESwitchingKeyAlloc, + { + module.bytes_of_lwe_switching_key(base2k, k, dnum) } } @@ -189,3 +221,29 @@ impl WriterTo for LWESwitchingKey { self.0.write_to(writer) } } + +pub trait LWESwitchingKeyToRef { + fn to_ref(&self) -> LWESwitchingKey<&[u8]>; +} + +impl LWESwitchingKeyToRef for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWESwitchingKey<&[u8]> { + LWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyToMut { + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; +} + +impl LWESwitchingKeyToMut for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { + LWESwitchingKey(self.0.to_mut()) + } +} diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index e739722..6ffd650 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -1,8 +1,8 @@ use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; -use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; +use crate::layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEPlaintextLayout { @@ -19,8 +19,8 @@ impl LWEInfos for LWEPlaintextLayout { self.k } - fn n(&self) -> Degree { - Degree(0) + fn n(&self) -> RingDegree { + RingDegree(0) } fn size(&self) -> usize { @@ -43,8 +43,8 @@ impl LWEInfos for LWEPlaintext { self.k } - fn n(&self) -> Degree { - Degree(self.data.n() as u32 - 1) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32 - 1) } fn size(&self) -> usize { @@ -52,21 +52,40 @@ impl LWEInfos for LWEPlaintext { } } -impl LWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEPlaintextAlloc { + fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext> { + LWEPlaintext { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } + + fn alloc_lwe_plaintext_from_infos(&self, infos: &A) -> LWEPlaintext> + where + A: LWEInfos, + { + self.alloc_lwe_plaintext(infos.base2k(), infos.k()) + } +} + +impl LWEPlaintextAlloc for Module {} + +impl LWEPlaintext> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: LWEInfos, + M: LWEPlaintextAlloc, + { + module.alloc_lwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self + where + M: LWEPlaintextAlloc, + { + module.alloc_lwe_plaintext(base2k, k) + } } impl fmt::Display for LWEPlaintext { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index a5b7d4e..d593ad5 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, LWEInfos, TorusPrecision}, + layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}, }; pub struct LWESecret { @@ -13,15 +13,26 @@ pub struct LWESecret { pub(crate) dist: Distribution, } -impl LWESecret> { - pub fn alloc(n: Degree) -> Self { - Self { +pub trait LWESecretAlloc { + fn alloc_lwe_secret(&self, n: RingDegree) -> LWESecret> { + LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, } } } +impl LWESecretAlloc for Module {} + +impl LWESecret> { + pub fn alloc(module: &M, n: RingDegree) -> Self + where + M: LWESecretAlloc, + { + module.alloc_lwe_secret(n) + } +} + impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) @@ -44,8 +55,8 @@ impl LWEInfos for LWESecret { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -84,3 +95,29 @@ impl LWESecret { self.dist = Distribution::ZERO; } } + +pub trait LWESecretToRef { + fn to_ref(&self) -> LWESecret<&[u8]>; +} + +impl LWESecretToRef for LWESecret { + fn to_ref(&self) -> LWESecret<&[u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_ref(), + } + } +} + +pub trait LWESecretToMut { + fn to_mut(&mut self) -> LWESecret<&mut [u8]>; +} + +impl LWESecretToMut for LWESecret { + fn to_mut(&mut self) -> LWESecret<&mut [u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index b3ba74b..72b7514 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -1,15 +1,18 @@ use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEToGLWESwitchingKeyLayout { - pub n: Degree, + pub n: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_out: Rank, @@ -25,7 +28,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n } } @@ -55,7 +58,7 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWEToGLWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWEToGLWESwitchingKey { fn base2k(&self) -> Base2K { @@ -66,7 +69,7 @@ impl LWEInfos for LWEToGLWESwitchingKey { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -128,54 +131,116 @@ impl WriterTo for LWEToGLWESwitchingKey { } } -impl LWEToGLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_to_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKey> { + LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKey> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + + self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - pub fn alloc_bytes(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) - } - - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } +} + +impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +impl LWEToGLWESwitchingKey> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + M: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + where + M: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; +} + +impl LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> { + LWEToGLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { + LWEToGLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 2b13751..09064f3 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -33,20 +33,16 @@ pub use lwe_pt::*; pub use lwe_sk::*; pub use lwe_to_glwe_ksk::*; -#[derive(Debug)] -pub enum BuildError { - MissingData, - MissingBase2K, - MissingK, - MissingDigits, - ZeroDegree, - NonPowerOfTwoDegree, - ZeroBase2K, - ZeroTorusPrecision, - ZeroCols, - ZeroLimbs, - ZeroRank, - ZeroDigits, +use poulpy_hal::layouts::{Backend, Module}; + +pub trait GetRingDegree { + fn ring_degree(&self) -> RingDegree; +} + +impl GetRingDegree for Module { + fn ring_degree(&self) -> RingDegree { + Self::n(&self).into() + } } /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. @@ -206,14 +202,14 @@ macro_rules! newtype_u32 { }; } -newtype_u32!(Degree); +newtype_u32!(RingDegree); newtype_u32!(TorusPrecision); newtype_u32!(Base2K); newtype_u32!(Dnum); newtype_u32!(Rank); newtype_u32!(Dsize); -impl Degree { +impl RingDegree { pub fn log2(&self) -> usize { let n: usize = self.0 as usize; (usize::BITS - (n - 1).leading_zeros()) as _ diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 594aa0a..7c8d0b9 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,27 +1,21 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + AutomorphismKeyToRef, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWEAutomorphismKeyPrepared { - pub(crate) key: GGLWESwitchingKeyPrepared, +pub struct AutomorphismKeyPrepared { + pub(crate) key: GLWESwitchingKeyPrepared, pub(crate) p: i64, } -impl GGLWEAutomorphismKeyPrepared { - pub fn p(&self) -> i64 { - self.p - } -} - -impl LWEInfos for GGLWEAutomorphismKeyPrepared { - fn n(&self) -> Degree { +impl LWEInfos for AutomorphismKeyPrepared { + fn n(&self) -> RingDegree { self.key.n() } @@ -38,13 +32,33 @@ impl LWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GLWEInfos for GGLWEAutomorphismKeyPrepared { +pub trait GetAutomorphismGaloisElement { + fn p(&self) -> i64; +} + +impl GetAutomorphismGaloisElement for AutomorphismKeyPrepared { + fn p(&self) -> i64 { + self.p + } +} + +pub trait SetAutomorphismGaloisElement { + fn set_p(&mut self, p: i64); +} + +impl SetAutomorphismGaloisElement for AutomorphismKeyPrepared { + fn set_p(&mut self, p: i64) { + self.p = p + } +} + +impl GLWEInfos for AutomorphismKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { +impl GGLWEInfos for AutomorphismKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -62,80 +76,170 @@ impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GGLWEAutomorphismKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait AutomorphismKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn alloc_automorphism_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyPrepared, B> { + AutomorphismKeyPrepared::, B> { + key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for AutomorphismKeyPrepared" + ); + self.alloc_automorphism_key_prepared( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - GGLWEAutomorphismKeyPrepared::, B> { - key: GGLWESwitchingKeyPrepared::alloc(module, infos), - p: 0, - } + self.bytes_of_automorphism_key_prepared( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } +} - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - Module: VmpPMatAlloc, - { - GGLWEAutomorphismKeyPrepared { - key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } +impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} - pub fn alloc_bytes(module: &Module, infos: &A) -> usize +impl AutomorphismKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: AutomorphismKeyPreparedAlloc, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" - ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + module.alloc_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where - Module: VmpPMatAllocBytes, + M: AutomorphismKeyPreparedAlloc, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) + 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) } } -impl PrepareScratchSpace for GGLWEAutomorphismKeyPrepared, B> +pub trait PrepareAutomorphismKey where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Self: GLWESwitchingKeyPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.prepare_glwe_switching_key_tmp_bytes(infos) + } + + fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + { + self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); + res.set_p(other.p()); } } -impl Prepare> for GGLWEAutomorphismKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); - self.p = other.p; +impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} + +impl AutomorphismKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M) -> usize + where + M: PrepareAutomorphismKey, + { + module.prepare_automorphism_key_tmp_bytes(self) } } -impl PrepareAlloc, B>> for GGLWEAutomorphismKey -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWEAutomorphismKeyPrepared, B> { - let mut atk_prepared: GGLWEAutomorphismKeyPrepared, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared +impl AutomorphismKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + M: PrepareAutomorphismKey, + { + module.prepare_automorphism_key(self, other, scratch); + } +} + +pub trait AutomorphismKeyPreparedToMut { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; +} + +impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_mut(), + } + } +} + +pub trait AutomorphismKeyPreparedToRef { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; +} + +impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 4f22e6e..fd6033a 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,25 +1,23 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, + api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGLWECiphertextPrepared { +pub struct GGLWEPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertextPrepared { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) +impl LWEInfos for GGLWEPrepared { + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -35,13 +33,13 @@ impl LWEInfos for GGLWECiphertextPrepared { } } -impl GLWEInfos for GGLWECiphertextPrepared { +impl GLWEInfos for GGLWEPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextPrepared { +impl GGLWEInfos for GGLWEPrepared { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -59,117 +57,47 @@ impl GGLWEInfos for GGLWECiphertextPrepared { } } -pub struct GGLWECiphertextPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} +pub trait GGLWEPreparedAlloc +where + Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, +{ + fn alloc_gglwe_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGLWEPrepared, B> { + let size: usize = k.0.div_ceil(base2k.0) as usize; + debug_assert!( + size as u32 > dsize.0, + "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", + dsize.0 + ); -impl GGLWECiphertextPrepared { - #[inline] - pub fn builder() -> GGLWECiphertextPreparedBuilder { - GGLWECiphertextPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} + assert!( + dnum.0 * dsize.0 <= size as u32, + "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", + dnum.0, + dsize.0, + ); -impl GGLWECiphertextPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGLWEInfos, - B: VmpPMatAllocBytesImpl, - { - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - infos.rank_in().into(), - (infos.rank_out() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGLWECiphertextPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGLWECiphertextPrepared { - data, - base2k, + GGLWEPrepared { + data: self.vmp_pmat_alloc(dnum.into(), rank_in.into(), (rank_out + 1).into(), size), k, + base2k, dsize, - }) + } } -} -impl GGLWECiphertextPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self + fn alloc_gglwe_prepared_from_infos(&self, infos: &A) -> GGLWEPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_with( - module, + assert_eq!(self.ring_degree(), infos.n()); + self.alloc_gglwe_prepared( infos.base2k(), infos.k(), infos.rank_in(), @@ -179,8 +107,61 @@ impl GGLWECiphertextPrepared, B> { ) } - pub fn alloc_with( - module: &Module, + fn bytes_of_gglwe_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + let size: usize = k.0.div_ceil(base2k.0) as usize; + debug_assert!( + size as u32 > dsize.0, + "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", + dsize.0 + ); + + assert!( + dnum.0 * dsize.0 <= size as u32, + "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", + dnum.0, + dsize.0, + ); + + self.bytes_of_vmp_pmat(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + } + + fn bytes_of_gglwe_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(self.ring_degree(), infos.n()); + self.bytes_of_gglwe_prepared( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGLWEPreparedAlloc for Module where Module: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} + +impl GGLWEPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GGLWEPreparedAlloc, + { + module.alloc_gglwe_prepared_from_infos(infos) + } + + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -189,49 +170,21 @@ impl GGLWECiphertextPrepared, B> { dsize: Dsize, ) -> Self where - Module: VmpPMatAlloc, + M: GGLWEPreparedAlloc, { - let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( - size as u32 > dsize.0, - "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", - dsize.0 - ); - - assert!( - dnum.0 * dsize.0 <= size as u32, - "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", - dnum.0, - dsize.0, - ); - - Self { - data: module.vmp_pmat_alloc(dnum.into(), rank_in.into(), (rank_out + 1).into(), size), - k, - base2k, - dsize, - } + module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: GGLWEPreparedAlloc, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_bytes_with( - module, - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + module.bytes_of_gglwe_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -240,59 +193,93 @@ impl GGLWECiphertextPrepared, B> { dsize: Dsize, ) -> usize where - Module: VmpPMatAllocBytes, + M: GGLWEPreparedAlloc, { - let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( - size as u32 > dsize.0, - "invalid gglwe: ceil(k/base2k): {size} <= dsize: {}", - dsize.0 - ); - - assert!( - dnum.0 * dsize.0 <= size as u32, - "invalid gglwe: dnum: {} * dsize:{} > ceil(k/base2k): {size}", - dnum.0, - dsize.0, - ); - - module.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } -impl PrepareScratchSpace for GGLWECiphertextPrepared, B> +pub trait GGLWEPrepare where - Module: VmpPrepareTmpBytes, + Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + 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(), ) } -} -impl Prepare> for GGLWECiphertextPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; + 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 PrepareAlloc, B>> for GGLWECiphertext -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWECiphertextPrepared, B> { - let mut atk_prepared: GGLWECiphertextPrepared, B> = GGLWECiphertextPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared +impl GGLWEPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} + +impl GGLWEPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: GGLWEToRef, + M: GGLWEPrepare, + { + module.prepare_gglwe(self, other, scratch); + } +} + +impl GGLWEPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M) -> usize + where + M: GGLWEPrepare, + { + module.prepare_gglwe_tmp_bytes(self) + } +} + +pub trait GGLWEPreparedToMut { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; +} + +impl GGLWEPreparedToMut for GGLWEPrepared { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_mut(), + } + } +} + +pub trait GGLWEPreparedToRef { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B>; +} + +impl GGLWEPreparedToRef for GGLWEPrepared { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index c9110c1..2154b3e 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,22 +1,40 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, + LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] -pub struct GGLWESwitchingKeyPrepared { - pub(crate) key: GGLWECiphertextPrepared, +pub struct GLWESwitchingKeyPrepared { + pub(crate) key: GGLWEPrepared, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKeyPrepared { - fn n(&self) -> Degree { +impl GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared { + fn set_sk_in_n(&mut self, sk_in_n: usize) { + self.sk_in_n = sk_in_n + } + + fn set_sk_out_n(&mut self, sk_out_n: usize) { + self.sk_out_n = sk_out_n + } +} + +impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared { + fn sk_in_n(&self) -> usize { + self.sk_in_n + } + + fn sk_out_n(&self) -> usize { + self.sk_out_n + } +} + +impl LWEInfos for GLWESwitchingKeyPrepared { + fn n(&self) -> RingDegree { self.key.n() } @@ -33,13 +51,13 @@ impl LWEInfos for GGLWESwitchingKeyPrepared { } } -impl GLWEInfos for GGLWESwitchingKeyPrepared { +impl GLWEInfos for GLWESwitchingKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyPrepared { +impl GGLWEInfos for GLWESwitchingKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -57,22 +75,80 @@ impl GGLWEInfos for GGLWESwitchingKeyPrepared { } } -impl GGLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GGLWEInfos, - Module: VmpPMatAlloc, - { - debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc(module, infos), +pub trait GLWESwitchingKeyPreparedAlloc +where + Self: GGLWEPreparedAlloc, +{ + fn alloc_glwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GLWESwitchingKeyPrepared, B> { + GLWESwitchingKeyPrepared::, B> { + key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_with( - module: &Module, + fn alloc_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> GLWESwitchingKeyPrepared, B> + where + A: GGLWEInfos, + { + self.alloc_glwe_switching_key_prepared( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_glwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_glwe_switching_key_prepared( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GLWESwitchingKeyPreparedAlloc for Module where Self: GGLWEPreparedAlloc {} + +impl GLWESwitchingKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: GLWESwitchingKeyPreparedAlloc, + { + module.alloc_glwe_switching_key_prepared_from_infos(infos) + } + + pub fn alloc( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -81,26 +157,21 @@ impl GGLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> Self where - Module: VmpPMatAlloc, + M: GLWESwitchingKeyPreparedAlloc, { - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } + module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: GLWESwitchingKeyPreparedAlloc, { - debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWECiphertextPrepared::alloc_bytes(module, infos) + module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module, + pub fn bytes_of( + module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -109,39 +180,79 @@ impl GGLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> usize where - Module: VmpPMatAllocBytes, + M: GLWESwitchingKeyPreparedAlloc, { - GGLWECiphertextPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) + module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } } -impl PrepareScratchSpace for GGLWESwitchingKeyPrepared, B> +pub trait GLWESwitchingKeyPrepare where - GGLWECiphertextPrepared, B>: PrepareScratchSpace, + Self: GGLWEPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWECiphertextPrepared::prepare_scratch_space(module, infos) + 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: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + { + self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); + res.set_sk_in_n(other.sk_in_n()); + res.set_sk_out_n(other.sk_out_n()); } } -impl Prepare> for GGLWESwitchingKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; +impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepare {} + +impl GLWESwitchingKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + M: GLWESwitchingKeyPrepare, + { + module.prepare_glwe_switching(self, other, scratch); } } -impl PrepareAlloc, B>> for GGLWESwitchingKey -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWESwitchingKeyPrepared, B> { - let mut atk_prepared: GGLWESwitchingKeyPrepared, B> = GGLWESwitchingKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared +impl GLWESwitchingKeyPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M) -> usize + where + M: GLWESwitchingKeyPrepare, + { + module.prepare_glwe_switching_key_tmp_bytes(self) + } +} + +pub trait GLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_mut(), + } + } +} + +pub trait GLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; +} + +impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 4343e30..2d054c1 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,20 +1,20 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToRef, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWETensorKeyPrepared { - pub(crate) keys: Vec>, +pub struct TensorKeyPrepared { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyPrepared { - fn n(&self) -> Degree { +impl LWEInfos for TensorKeyPrepared { + fn n(&self) -> RingDegree { self.keys[0].n() } @@ -31,13 +31,13 @@ impl LWEInfos for GGLWETensorKeyPrepared { } } -impl GLWEInfos for GGLWETensorKeyPrepared { +impl GLWEInfos for TensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyPrepared { +impl GGLWEInfos for TensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } @@ -55,19 +55,36 @@ impl GGLWEInfos for GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self +pub trait TensorKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn alloc_tensor_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> TensorKeyPrepared, B> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyPrepared { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, - Module: VmpPMatAlloc, { assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - Self::alloc_with( - module, + self.alloc_tensor_key_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -76,62 +93,62 @@ impl GGLWETensorKeyPrepared, B> { ) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self - where - Module: VmpPMatAlloc, - { - let mut keys: Vec, B>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyPrepared::alloc_with( - module, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; + pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn bytes_of_tensor_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GGLWESwitchingKeyPrepared::alloc_bytes_with( - module, - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - Module: VmpPMatAllocBytes, - { - let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) + self.bytes_of_tensor_key_prepared( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} + +impl TensorKeyPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGLWEInfos, + M: TensorKeyPreparedAlloc, + { + module.alloc_tensor_key_prepared_from_infos(infos) + } + + pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + where + M: TensorKeyPreparedAlloc, + { + module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: TensorKeyPreparedAlloc, + { + module.bytes_of_tensor_key_prepared_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + where + M: TensorKeyPreparedAlloc, + { + module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) + } +} + +impl TensorKeyPrepared { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -140,9 +157,9 @@ impl GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -151,40 +168,81 @@ impl GGLWETensorKeyPrepared { } } -impl PrepareScratchSpace for GGLWETensorKeyPrepared, B> +pub trait TensorKeyPrepare where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Self: GLWESwitchingKeyPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.prepare_glwe_switching_key_tmp_bytes(infos) } -} -impl Prepare> for GGLWETensorKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) { - #[cfg(debug_assertions)] - { - assert_eq!(self.keys.len(), other.keys.len()); + 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_glwe_switching(a, b, scratch); } - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.prepare(module, b, scratch); - }); } } -impl PrepareAlloc, B>> for GGLWETensorKey -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWETensorKeyPrepared, B> { - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, self); - tsk_prepared.prepare(module, self, scratch); - tsk_prepared +impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} + +impl TensorKeyPrepared, B> { + fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize + where + A: GGLWEInfos, + M: TensorKeyPrepare, + { + module.prepare_tensor_key_tmp_bytes(infos) + } +} + +impl TensorKeyPrepared { + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef, + M: TensorKeyPrepare, + { + module.prepare_tensor_key(self, other, scratch); + } +} + +pub trait TensorKeyPreparedToMut { + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +} + +impl TensorKeyPreparedToMut for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { + TensorKeyPrepared { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} + +pub trait TensorKeyPreparedToRef { + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +} + +impl TensorKeyPreparedToRef for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { + TensorKeyPrepared { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } } } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index eb79a5a..6365818 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -1,25 +1,23 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToRef, ZnxInfos}, - oep::VmpPMatAllocBytesImpl, + api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGSWCiphertextPrepared { +pub struct GGSWPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertextPrepared { - fn n(&self) -> Degree { - Degree(self.data.n() as u32) +impl LWEInfos for GGSWPrepared { + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -35,13 +33,13 @@ impl LWEInfos for GGSWCiphertextPrepared { } } -impl GLWEInfos for GGSWCiphertextPrepared { +impl GLWEInfos for GGSWPrepared { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertextPrepared { +impl GGSWInfos for GGSWPrepared { fn dsize(&self) -> Dsize { self.dsize } @@ -51,143 +49,18 @@ impl GGSWInfos for GGSWCiphertextPrepared { } } -pub struct GGSWCiphertextPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, - dsize: Option, -} - -impl GGSWCiphertextPrepared { - #[inline] - pub fn builder() -> GGSWCiphertextPreparedBuilder { - GGSWCiphertextPreparedBuilder { - data: None, - base2k: None, - k: None, - dsize: None, - } - } -} - -impl GGSWCiphertextPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, infos: &A) -> Self - where - A: GGSWInfos, - B: VmpPMatAllocBytesImpl, - { - debug_assert!( - infos.size() as u32 > infos.dsize().0, - "invalid ggsw: ceil(k/base2k): {} <= dsize: {}", - infos.size(), - infos.dsize() - ); - - assert!( - infos.dnum().0 * infos.dsize().0 <= infos.size() as u32, - "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {}", - infos.dnum(), - infos.dsize(), - infos.size(), - ); - - self.data = Some(VmpPMat::alloc( - infos.n().into(), - infos.dnum().into(), - (infos.rank() + 1).into(), - (infos.rank() + 1).into(), - infos.size(), - )); - self.base2k = Some(infos.base2k()); - self.k = Some(infos.k()); - self.dsize = Some(infos.dsize()); - self - } -} - -impl GGSWCiphertextPreparedBuilder { - #[inline] - pub fn data(mut self, data: VmpPMat) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - #[inline] - pub fn dsize(mut self, dsize: Dsize) -> Self { - self.dsize = Some(dsize); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - let dsize: Dsize = self.dsize.ok_or(BuildError::MissingDigits)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if dsize == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GGSWCiphertextPrepared { - data, - base2k, - k, - dsize, - }) - } -} - -impl GGSWCiphertextPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GGSWInfos, - Module: VmpPMatAlloc, - { - Self::alloc_with( - module, - infos.base2k(), - infos.k(), - infos.dnum(), - infos.dsize(), - infos.rank(), - ) - } - - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self - where - Module: VmpPMatAlloc, - { +pub trait GGSWPreparedAlloc +where + Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, +{ + fn alloc_ggsw_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> GGSWPrepared, B> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -202,8 +75,8 @@ impl GGSWCiphertextPrepared, B> { dsize.0, ); - Self { - data: module.vmp_pmat_alloc( + GGSWPrepared { + data: self.vmp_pmat_alloc( dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -215,13 +88,12 @@ impl GGSWCiphertextPrepared, B> { } } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn alloc_ggsw_prepared_from_infos(&self, infos: &A) -> GGSWPrepared, B> where A: GGSWInfos, - Module: VmpPMatAllocBytes, { - Self::alloc_bytes_with( - module, + assert_eq!(self.ring_degree(), infos.n()); + self.alloc_ggsw_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -230,10 +102,7 @@ impl GGSWCiphertextPrepared, B> { ) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize - where - Module: VmpPMatAllocBytes, - { + fn bytes_of_ggsw_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -248,65 +117,144 @@ impl GGSWCiphertextPrepared, B> { dsize.0, ); - module.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.bytes_of_vmp_pmat(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + } + + fn bytes_of_ggsw_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + assert_eq!(self.ring_degree(), infos.n()); + self.bytes_of_ggsw_prepared( + infos.base2k(), + infos.k(), + infos.dnum(), + infos.dsize(), + infos.rank(), + ) } } -impl GGSWCiphertextPrepared { +impl GGSWPreparedAlloc for Module where Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} + +impl GGSWPrepared, B> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + A: GGSWInfos, + M: GGSWPreparedAlloc, + { + module.alloc_ggsw_prepared_from_infos(infos) + } + + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + where + M: GGSWPreparedAlloc, + { + module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank) + } + + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + where + A: GGSWInfos, + M: GGSWPreparedAlloc, + { + module.bytes_of_ggsw_prepared_from_infos(infos) + } + + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize + where + M: GGSWPreparedAlloc, + { + module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) + } +} + +impl GGSWPrepared { pub fn data(&self) -> &VmpPMat { &self.data } } -impl PrepareScratchSpace for GGSWCiphertextPrepared, B> +pub trait GGSWPrepare where - Module: VmpPrepareTmpBytes, + Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + 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(), ) } -} - -impl Prepare> for GGSWCiphertextPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; + 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 PrepareAlloc, B>> for GGSWCiphertext -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGSWCiphertextPrepared, B> { - let mut ggsw_prepared: GGSWCiphertextPrepared, B> = GGSWCiphertextPrepared::alloc(module, self); - ggsw_prepared.prepare(module, self, scratch); - ggsw_prepared +impl GGSWPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} + +impl GGSWPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize + where + A: GGSWInfos, + M: GGSWPrepare, + { + module.ggsw_prepare_tmp_bytes(infos) } } -pub trait GGSWCiphertextPreparedToRef { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>; -} - -impl GGSWCiphertextPreparedToRef for GGSWCiphertextPrepared { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B> { - GGSWCiphertextPrepared::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GGSWPrepared { + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef, + M: GGSWPrepare, + { + module.ggsw_prepare(self, other, scratch); + } +} + +pub trait GGSWPreparedToMut { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; +} + +impl GGSWPreparedToMut for GGSWPrepared { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_mut(), + } + } +} + +pub trait GGSWPreparedToRef { + fn to_ref(&self) -> GGSWPrepared<&[u8], B>; +} + +impl GGSWPreparedToRef for GGSWPrepared { + fn to_ref(&self) -> GGSWPrepared<&[u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 6834f58..bca1826 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -1,14 +1,12 @@ use poulpy_hal::{ - api::{VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, ZnxInfos}, - oep::VecZnxDftAllocBytesImpl, + api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf}, + layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos}, }; use crate::{ dist::Distribution, layouts::{ - Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, }, }; @@ -20,6 +18,16 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } +pub(crate) trait SetDist { + fn set_dist(&mut self, dist: Distribution); +} + +impl SetDist for GLWEPublicKeyPrepared { + fn set_dist(&mut self, dist: Distribution) { + self.dist = dist + } +} + impl LWEInfos for GLWEPublicKeyPrepared { fn base2k(&self) -> Base2K { self.base2k @@ -33,8 +41,8 @@ impl LWEInfos for GLWEPublicKeyPrepared { self.data.size() } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } } @@ -44,164 +52,138 @@ impl GLWEInfos for GLWEPublicKeyPrepared { } } -pub struct GLWEPublicKeyPreparedBuilder { - data: Option>, - base2k: Option, - k: Option, -} - -impl GLWEPublicKeyPrepared { - #[inline] - pub fn builder() -> GLWEPublicKeyPreparedBuilder { - GLWEPublicKeyPreparedBuilder { - data: None, - base2k: None, - k: None, - } - } -} - -impl GLWEPublicKeyPreparedBuilder, B> { - #[inline] - pub fn layout(mut self, layout: &A) -> Self - where - A: GLWEInfos, - B: VecZnxDftAllocBytesImpl, - { - self.data = Some(VecZnxDft::alloc( - layout.n().into(), - (layout.rank() + 1).into(), - layout.size(), - )); - self.base2k = Some(layout.base2k()); - self.k = Some(layout.k()); - self - } -} - -impl GLWEPublicKeyPreparedBuilder { - #[inline] - pub fn data(mut self, data: VecZnxDft) -> Self { - self.data = Some(data); - self - } - #[inline] - pub fn base2k(mut self, base2k: Base2K) -> Self { - self.base2k = Some(base2k); - self - } - #[inline] - pub fn k(mut self, k: TorusPrecision) -> Self { - self.k = Some(k); - self - } - - pub fn build(self) -> Result, BuildError> { - let data: VecZnxDft = self.data.ok_or(BuildError::MissingData)?; - let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; - let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; - - if base2k == 0_u32 { - return Err(BuildError::ZeroBase2K); - } - - if k == 0_u32 { - return Err(BuildError::ZeroTorusPrecision); - } - - if data.n() == 0 { - return Err(BuildError::ZeroDegree); - } - - if data.cols() == 0 { - return Err(BuildError::ZeroCols); - } - - if data.size() == 0 { - return Err(BuildError::ZeroLimbs); - } - - Ok(GLWEPublicKeyPrepared { - data, +pub trait GLWEPublicKeyPreparedAlloc +where + Self: GetRingDegree + VecZnxDftAlloc + VecZnxDftBytesOf, +{ + 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, dist: Distribution::NONE, - }) + } + } + + fn alloc_glwe_public_key_prepared_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> + where + A: GLWEInfos, + { + self.alloc_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) + } + + 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) + } + + fn bytes_of_glwe_public_key_prepared_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } } +impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf {} + impl GLWEPublicKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - Module: VecZnxDftAlloc, + M: GLWEPublicKeyPreparedAlloc, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank()) + module.alloc_glwe_public_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - Module: VecZnxDftAlloc, + M: GLWEPublicKeyPreparedAlloc, { - Self { - data: module.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - dist: Distribution::NONE, - } + module.alloc_glwe_public_key_prepared(base2k, k, rank) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftAllocBytes, + M: GLWEPublicKeyPreparedAlloc, { - debug_assert_eq!(module.n(), infos.n().0 as usize, "module.n() != infos.n()"); - Self::alloc_bytes_with(module, infos.base2k(), infos.k(), infos.rank()) + module.bytes_of_glwe_public_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where - Module: VecZnxDftAllocBytes, + M: GLWEPublicKeyPreparedAlloc, { - module.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } } -impl PrepareAlloc, B>> for GLWEPublicKey +pub trait GLWEPublicKeyPrepare where - Module: VecZnxDftAlloc + VecZnxDftApply, + Self: GetRingDegree + VecZnxDftApply, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEPublicKeyPrepared, B> { - let mut pk_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc(module, self); - pk_prepared.prepare(module, self, scratch); - pk_prepared - } -} - -impl PrepareScratchSpace for GLWEPublicKeyPrepared, B> { - fn prepare_scratch_space(_module: &Module, _infos: &A) -> usize { - 0 - } -} - -impl Prepare> for GLWEPublicKeyPrepared -where - Module: VecZnxDftApply, -{ - fn prepare(&mut self, module: &Module, other: &GLWEPublicKey, _scratch: &mut Scratch) { - #[cfg(debug_assertions)] + fn prepare_glwe_public_key(&self, res: &mut R, other: &O) + where + R: GLWEPublicKeyPreparedToMut + SetDist, + O: GLWEPublicKeyToRef + GetDist, + { { - assert_eq!(self.n(), other.n()); - assert_eq!(self.size(), other.size()); + 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); + } } - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_dft_apply(1, 0, &mut self.data, i, &other.data, i); - }); - self.k = other.k(); - self.base2k = other.base2k(); - self.dist = other.dist; + res.set_dist(other.get_dist()); + } +} + +impl GLWEPublicKeyPrepare for Module where Self: GetRingDegree + VecZnxDftApply {} + +impl GLWEPublicKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &O) + where + O: GLWEPublicKeyToRef + GetDist, + M: GLWEPublicKeyPrepare, + { + 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(), + } + } +} + +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, + } } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index d3f638b..ac7e11f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -1,13 +1,13 @@ use poulpy_hal::{ - api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, ZnxInfos}, + api::{SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare}, + layouts::{Backend, Data, DataMut, DataRef, Module, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos}, }; use crate::{ dist::Distribution, layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::SetDist, }, }; @@ -16,6 +16,12 @@ pub struct GLWESecretPrepared { pub(crate) dist: Distribution, } +impl SetDist for GLWESecretPrepared { + fn set_dist(&mut self, dist: Distribution) { + self.dist = dist + } +} + impl LWEInfos for GLWESecretPrepared { fn base2k(&self) -> Base2K { Base2K(0) @@ -25,8 +31,8 @@ impl LWEInfos for GLWESecretPrepared { TorusPrecision(0) } - fn n(&self) -> Degree { - Degree(self.data.n() as u32) + fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } fn size(&self) -> usize { @@ -38,46 +44,74 @@ impl GLWEInfos for GLWESecretPrepared { Rank(self.data.cols() as u32) } } -impl GLWESecretPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self - where - A: GLWEInfos, - Module: SvpPPolAlloc, - { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_with(module, infos.rank()) - } - pub fn alloc_with(module: &Module, rank: Rank) -> Self - where - Module: SvpPPolAlloc, - { - Self { - data: module.svp_ppol_alloc(rank.into()), +pub trait GLWESecretPreparedAlloc +where + Self: GetRingDegree + SvpPPolBytesOf + SvpPPolAlloc, +{ + fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { + GLWESecretPrepared { + data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + fn alloc_glwe_secret_prepared_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> where A: GLWEInfos, - Module: SvpPPolAllocBytes, { - assert_eq!(module.n() as u32, infos.n()); - Self::alloc_bytes_with(module, infos.rank()) + assert_eq!(self.ring_degree(), infos.n()); + self.alloc_glwe_secret_prepared(infos.rank()) } - pub fn alloc_bytes_with(module: &Module, rank: Rank) -> usize + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + self.bytes_of_svp_ppol(rank.into()) + } + fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize where - Module: SvpPPolAllocBytes, + A: GLWEInfos, { - module.svp_ppol_alloc_bytes(rank.into()) + assert_eq!(self.ring_degree(), infos.n()); + self.bytes_of_glwe_secret(infos.rank()) + } +} + +impl GLWESecretPreparedAlloc for Module where Self: GetRingDegree + 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_from_infos(infos) + } + + pub fn bytes_of(module: &M, rank: Rank) -> usize + where + M: GLWESecretPreparedAlloc, + { + module.bytes_of_glwe_secret(rank) } } impl GLWESecretPrepared { - pub fn n(&self) -> Degree { - Degree(self.data.n() as u32) + pub fn n(&self) -> RingDegree { + RingDegree(self.data.n() as u32) } pub fn rank(&self) -> Rank { @@ -85,31 +119,62 @@ impl GLWESecretPrepared { } } -impl PrepareScratchSpace for GLWESecretPrepared, B> { - fn prepare_scratch_space(_module: &Module, _infos: &A) -> usize { - 0 +pub trait GLWESecretPrepare +where + Self: SvpPrepare, +{ + fn prepare_glwe_secret(&self, res: &mut R, other: &O) + where + R: GLWESecretPreparedToMut + SetDist, + O: GLWESecretToRef + GetDist, + { + { + let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); + let other: GLWESecret<&[u8]> = other.to_ref(); + + for i in 0..res.rank().into() { + self.svp_prepare(&mut res.data, i, &other.data, i); + } + } + + res.set_dist(other.get_dist()); } } -impl PrepareAlloc, B>> for GLWESecret -where - Module: SvpPrepare + SvpPPolAlloc, -{ - fn prepare_alloc(&self, module: &Module, _scratch: &mut Scratch) -> GLWESecretPrepared, B> { - let mut sk_dft: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, self); - sk_dft.prepare(module, self, _scratch); - sk_dft +impl GLWESecretPrepare for Module where Self: SvpPrepare {} + +impl GLWESecretPrepared { + pub fn prepare(&mut self, module: &M, other: &O) + where + M: GLWESecretPrepare, + O: GLWESecretToRef + GetDist, + { + module.prepare_glwe_secret(self, other); } } -impl Prepare> for GLWESecretPrepared -where - Module: SvpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GLWESecret, _scratch: &mut Scratch) { - (0..self.rank().into()).for_each(|i| { - module.svp_prepare(&mut self.data, i, &other.data, i); - }); - self.dist = other.dist +pub trait GLWESecretPreparedToRef { + fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>; +} + +impl GLWESecretPreparedToRef for GLWESecretPrepared { + fn to_ref(&self) -> GLWESecretPrepared<&[u8], B> { + GLWESecretPrepared { + data: self.data.to_ref(), + dist: self.dist, + } + } +} + +pub trait GLWESecretPreparedToMut { + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B>; +} + +impl GLWESecretPreparedToMut for GLWESecretPrepared { + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B> { + GLWESecretPrepared { + dist: self.dist, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index f241c6d..55a7bf9 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,15 +1,15 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWEKey, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for GLWEToLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -20,7 +20,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -53,91 +53,156 @@ impl GGLWEInfos for GLWEToLWESwitchingKeyPrepared { } } +pub trait GLWEToLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn alloc_glwe_to_lwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyPrepared, B> { + GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + fn alloc_glwe_to_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared" + ); + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" + ); + self.alloc_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } + + fn bytes_of_glwe_to_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + } + + fn bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared" + ); + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" + ); + self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } +} + +impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} + impl GLWEToLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: VmpPMatAlloc, + M: GLWEToLWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" - ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where - Module: VmpPMatAlloc, + M: GLWEToLWESwitchingKeyPreparedAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( - module, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: GLWEToLWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" - ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where - Module: VmpPMatAllocBytes, + M: GLWEToLWESwitchingKeyPreparedAlloc, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } } -impl PrepareScratchSpace for GLWEToLWESwitchingKeyPrepared, B> +pub trait GLWEToLWESwitchingKeyPrepare where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Self: GLWESwitchingKeyPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + 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: GLWEToLWESwitchingKeyPreparedToMut, + O: GLWEToLWESwitchingKeyToRef, + { + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } -impl PrepareAlloc, B>> for GLWEToLWEKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWEToLWESwitchingKeyPrepared, B> { - let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = GLWEToLWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +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, + { + module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos); } } -impl Prepare> for GLWEToLWESwitchingKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GLWEToLWEKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); +impl GLWEToLWESwitchingKeyPrepared { + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: GLWEToLWESwitchingKeyToRef, + M: GLWEToLWESwitchingKeyPrepare, + { + module.prepare_glwe_to_lwe_switching_key(self, other, scratch); + } +} + +pub trait GLWEToLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; +} + +impl GLWEToLWESwitchingKeyPreparedToRef for GLWEToLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> { + GLWEToLWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl GLWEToLWESwitchingKeyPreparedToMut for GLWEToLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> { + GLWEToLWESwitchingKeyPrepared(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 5f0cf14..a857bf9 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,15 +1,15 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct LWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -20,7 +20,7 @@ impl LWEInfos for LWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -52,101 +52,165 @@ impl GGLWEInfos for LWESwitchingKeyPrepared { } } +pub trait LWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn alloc_lwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyPrepared, B> { + LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> LWESwitchingKeyPrepared, B> + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.alloc_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) + } + + fn bytes_of_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + } + + fn bytes_of_lwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.bytes_of_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) + } +} + +impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} + impl LWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: VmpPMatAlloc, + M: LWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + module.alloc_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where - Module: VmpPMatAlloc, + M: LWESwitchingKeyPreparedAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( - module, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + module.alloc_lwe_switching_key_prepared(base2k, k, dnum) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: LWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + module.bytes_of_lwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where - Module: VmpPMatAllocBytes, + M: LWESwitchingKeyPreparedAlloc, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + module.bytes_of_lwe_switching_key_prepared(base2k, k, dnum) } } -impl PrepareScratchSpace for LWESwitchingKeyPrepared, B> +pub trait LWESwitchingKeyPrepare where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Self: GLWESwitchingKeyPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + 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: LWESwitchingKeyPreparedToMut, + O: LWESwitchingKeyToRef, + { + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } -impl PrepareAlloc, B>> for LWESwitchingKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWESwitchingKeyPrepared, B> { - let mut ksk_prepared: LWESwitchingKeyPrepared, B> = LWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +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, + { + module.prepare_lwe_switching_key_tmp_bytes(infos); } } -impl Prepare> for LWESwitchingKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &LWESwitchingKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); +impl LWESwitchingKeyPrepared { + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: LWESwitchingKeyToRef, + M: LWESwitchingKeyPrepare, + { + module.prepare_lwe_switching_key(self, other, scratch); + } +} + +pub trait LWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; +} + +impl LWESwitchingKeyPreparedToRef for LWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> { + LWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl LWESwitchingKeyPreparedToMut for LWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> { + LWESwitchingKeyPrepared(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 7c2023a..5df692e 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,16 +1,16 @@ -use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; -/// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. +/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. #[derive(PartialEq, Eq)] -pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWEToGLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -21,7 +21,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.0.n() } @@ -54,91 +54,162 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyPrepared { } } +pub trait LWEToGLWESwitchingKeyPreparedAlloc +where + Self: GLWESwitchingKeyPreparedAlloc, +{ + fn alloc_lwe_to_glwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyPrepared, B> { + LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + fn alloc_lwe_to_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + self.alloc_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } + + fn bytes_of_lwe_to_glwe_switching_key_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + } + + fn bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + self.bytes_of_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } +} + +impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} + impl LWEToGLWESwitchingKeyPrepared, B> { - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - Module: VmpPMatAlloc, + M: LWEToGLWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where - Module: VmpPMatAlloc, + M: LWEToGLWESwitchingKeyPreparedAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( - module, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VmpPMatAllocBytes, + M: LWEToGLWESwitchingKeyPreparedAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize where - Module: VmpPMatAllocBytes, + M: LWEToGLWESwitchingKeyPreparedAlloc, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } } -impl PrepareScratchSpace for LWEToGLWESwitchingKeyPrepared, B> +pub trait LWEToGLWESwitchingKeyPrepare where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Self: GLWESwitchingKeyPrepare, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + 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: LWEToGLWESwitchingKeyPreparedToMut, + O: LWEToGLWESwitchingKeyToRef, + { + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } } -impl PrepareAlloc, B>> for LWEToGLWESwitchingKey -where - Module: VmpPrepare + VmpPMatAlloc, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> LWEToGLWESwitchingKeyPrepared, B> { - let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = LWEToGLWESwitchingKeyPrepared::alloc(module, self); - ksk_prepared.prepare(module, self, scratch); - ksk_prepared +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, + { + module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos); } } -impl Prepare> for LWEToGLWESwitchingKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &LWEToGLWESwitchingKey, scratch: &mut Scratch) { - self.0.prepare(module, &other.0, scratch); +impl LWEToGLWESwitchingKeyPrepared { + fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + where + O: LWEToGLWESwitchingKeyToRef, + M: LWEToGLWESwitchingKeyPrepare, + { + module.prepare_lwe_to_glwe_switching_key(self, other, scratch); + } +} + +pub trait LWEToGLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; +} + +impl LWEToGLWESwitchingKeyPreparedToRef for LWEToGLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> { + LWEToGLWESwitchingKeyPrepared(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl LWEToGLWESwitchingKeyPreparedToMut for LWEToGLWESwitchingKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> { + LWEToGLWESwitchingKeyPrepared(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/prepared/mod.rs b/poulpy-core/src/layouts/prepared/mod.rs index eb47848..296144a 100644 --- a/poulpy-core/src/layouts/prepared/mod.rs +++ b/poulpy-core/src/layouts/prepared/mod.rs @@ -19,16 +19,3 @@ pub use glwe_sk::*; pub use glwe_to_lwe_ksk::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; -use poulpy_hal::layouts::{Backend, Module, Scratch}; - -pub trait PrepareScratchSpace { - fn prepare_scratch_space(module: &Module, infos: &T) -> usize; -} - -pub trait PrepareAlloc { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> T; -} - -pub trait Prepare { - fn prepare(&mut self, module: &Module, other: &T, scratch: &mut Scratch); -} diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 70035af..15e6c76 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -14,6 +14,7 @@ mod utils; pub use operations::*; pub mod layouts; +pub use conversion::*; pub use dist::*; pub use external_product::*; pub use glwe_packing::*; @@ -22,4 +23,4 @@ pub use encryption::SIGMA; pub use scratch::*; -pub mod tests; +// pub mod tests; diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 0712b7f..516a210 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,16 +1,16 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, - VecZnxNormalizeTmpBytes, VecZnxSubScalarInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + VecZnxSubScalarInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGLWECiphertext { +impl GGLWE { pub fn assert_noise( &self, module: &Module, @@ -20,8 +20,8 @@ impl GGLWECiphertext { ) where DataSk: DataRef, DataWant: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -30,13 +30,13 @@ impl GGLWECiphertext { + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + VecZnxSubScalarInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 03bb0c0..92f806b 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,19 +1,17 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalizeTmpBytes, VecZnxSubInplace, + VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{ - GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, -}; +use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGSWCiphertext { +impl GGSW { pub fn assert_noise( &self, module: &Module, @@ -23,8 +21,8 @@ impl GGSWCiphertext { ) where DataSk: DataRef, DataScalar: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -38,19 +36,19 @@ impl GGSWCiphertext { + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, F: Fn(usize) -> f64, { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { @@ -87,7 +85,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext { +impl GGSW { pub fn print_noise( &self, module: &Module, @@ -96,8 +94,8 @@ impl GGSWCiphertext { ) where DataSk: DataRef, DataScalar: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -111,18 +109,18 @@ impl GGSWCiphertext { + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index f7af2a1..40b86d9 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,16 +1,16 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxIdftApplyConsume, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext { +impl GLWE { pub fn noise( &self, module: &Module, @@ -30,9 +30,9 @@ impl GLWECiphertext { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig, + Scratch:, { - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); self.decrypt(module, &mut pt_have, sk_prepared, scratch); module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); @@ -48,8 +48,8 @@ impl GLWECiphertext { ) where DataSk: DataRef, DataPt: DataRef, - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -59,9 +59,9 @@ impl GLWECiphertext { + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, - B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index b0ee4f6..b8b32ce 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -7,24 +7,22 @@ use poulpy_hal::{ layouts::{Backend, DataMut, Module, Scratch, VecZnx, ZnxZero}, }; -use crate::layouts::{ - GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, GLWEPlaintext, LWEInfos, TorusPrecision, -}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}; impl GLWEOperations for GLWEPlaintext where D: DataMut, - GLWEPlaintext: GLWECiphertextToMut + GLWEInfos, + GLWEPlaintext: GLWEToMut + GLWEInfos, { } -impl GLWEOperations for GLWECiphertext where GLWECiphertext: GLWECiphertextToMut + GLWEInfos {} +impl GLWEOperations for GLWE where GLWE: GLWEToMut + GLWEInfos {} -pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Sized { +pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { fn add(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxAdd + VecZnxCopy, { #[cfg(debug_assertions)] @@ -39,9 +37,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_add(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); @@ -64,13 +62,13 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn add_inplace(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxAddInplace, { #[cfg(debug_assertions)] @@ -80,8 +78,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_add_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -92,8 +90,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, { #[cfg(debug_assertions)] @@ -108,9 +106,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_sub(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); @@ -134,13 +132,13 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn sub_inplace_ab(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubInplace, { #[cfg(debug_assertions)] @@ -150,8 +148,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -162,7 +160,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub_inplace_ba(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubNegateInplace, { #[cfg(debug_assertions)] @@ -172,8 +170,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_negate_inplace(&mut self_mut.data, i, &a_ref.data, i); @@ -184,7 +182,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn rotate(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxRotate, { #[cfg(debug_assertions)] @@ -193,14 +191,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) } @@ -208,7 +206,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxRotateInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate_inplace(k, &mut self_mut.data, i, scratch); @@ -217,7 +215,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn mul_xp_minus_one(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxMulXpMinusOne, { #[cfg(debug_assertions)] @@ -226,14 +224,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) } @@ -241,17 +239,17 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxMulXpMinusOneInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one_inplace(k, &mut self_mut.data, i, scratch); }); } - fn copy(&mut self, module: &Module, a: &A) + fn copy(&mut self, module: &M, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, - Module: VecZnxCopy, + A: GLWEToRef + GLWEInfos, + M: VecZnxCopy, { #[cfg(debug_assertions)] { @@ -259,15 +257,15 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); }); self.set_k(a.k().min(self.max_k())); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); } fn rsh(&mut self, module: &Module, k: usize, scratch: &mut Scratch) @@ -282,7 +280,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn normalize(&mut self, module: &Module, a: &A, scratch: &mut Scratch) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxNormalize, { #[cfg(debug_assertions)] @@ -291,8 +289,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize( @@ -305,7 +303,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size scratch, ); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(a.k().min(self.k())); } @@ -313,16 +311,16 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxNormalizeInplace, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize_inplace(self_mut.base2k().into(), &mut self_mut.data, i, scratch); }); } } -impl GLWECiphertext> { - pub fn rsh_scratch_space(n: usize) -> usize { - VecZnx::rsh_scratch_space(n) +impl GLWE> { + pub fn rsh_tmp_bytes(n: usize) -> usize { + VecZnx::rsh_tmp_bytes(n) } } diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 1a5a6ce..d1e95f9 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -1,365 +1,230 @@ use poulpy_hal::{ - api::{TakeMatZnx, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxDft, TakeVmpPMat}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, layouts::{Backend, Scratch}, }; use crate::{ dist::Distribution, layouts::{ - Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank, + AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, + GLWESwitchingKey, Rank, TensorKey, prepared::{ - GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, - GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + GLWESwitchingKeyPrepared, TensorKeyPrepared, }, }, }; -pub trait TakeGLWECt { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWECtSlice { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWEPt { - fn take_glwe_pt(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGGLWE { - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEPrepared { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGSW { - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGGSWPrepared { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGGSWPreparedSlice { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) - where - A: GGSWInfos; -} - -pub trait TakeGLWESecret { - fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self); -} - -pub trait TakeGLWESecretPrepared { - fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self); -} - -pub trait TakeGLWEPk { - fn take_glwe_pk(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWEPkPrepared { - fn take_glwe_pk_prepared(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GLWEInfos; -} - -pub trait TakeGLWESwitchingKey { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWESwitchingKeyPrepared { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeTensorKey { - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWETensorKeyPrepared { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEAutomorphismKey { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) - where - A: GGLWEInfos; -} - -pub trait TakeGGLWEAutomorphismKeyPrepared { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) - where - A: GGLWEInfos; -} - -impl TakeGLWECt for Scratch +pub trait ScratchTakeCore where - Scratch: TakeVecZnx, + Self: ScratchTakeBasic + ScratchAvailable, { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, module: &M, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size()); ( - GLWECiphertext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWE { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } -} -impl TakeGLWECtSlice for Scratch -where - Scratch: TakeVecZnx, -{ - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, module: &M, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + 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(infos); + let (ct, new_scratch) = scratch.take_glwe_ct(module, infos); scratch = new_scratch; cts.push(ct); } (cts, scratch) } -} -impl TakeGLWEPt for Scratch -where - Scratch: TakeVecZnx, -{ - fn take_glwe_pt(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) + fn take_glwe_pt(&mut self, module: &M, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), 1, infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx(module, 1, infos.size()); ( - GLWEPlaintext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPlaintext { + k: infos.k(), + base2k: infos.base2k(), + data, + }, scratch, ) } -} -impl TakeGGLWE for Scratch -where - Scratch: TakeMatZnx, -{ - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, module: &M, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - infos.n().into(), + module, infos.dnum().0.div_ceil(infos.dsize().0) as usize, infos.rank_in().into(), (infos.rank_out() + 1).into(), infos.size(), ); ( - GGLWECiphertext::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .dsize(infos.dsize()) - .data(data) - .build() - .unwrap(), + GGLWE { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } -} -impl TakeGGLWEPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, module: &M, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_vmp_pmat( - infos.n().into(), + module, infos.dnum().into(), infos.rank_in().into(), (infos.rank_out() + 1).into(), infos.size(), ); ( - GGLWECiphertextPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGLWEPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } -} -impl TakeGGSW for Scratch -where - Scratch: TakeMatZnx, -{ - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, module: &M, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - infos.n().into(), + module, infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), infos.size(), ); ( - GGSWCiphertext::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSW { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } -} -impl TakeGGSWPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, module: &M, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_vmp_pmat( - infos.n().into(), + module, infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), infos.size(), ); ( - GGSWCiphertextPrepared::builder() - .base2k(infos.base2k()) - .dsize(infos.dsize()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GGSWPrepared { + k: infos.k(), + base2k: infos.base2k(), + dsize: infos.dsize(), + data, + }, scratch, ) } -} -impl TakeGGSWPreparedSlice for Scratch -where - Scratch: TakeGGSWPrepared, -{ - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice( + &mut self, + module: &M, + size: usize, + infos: &A, + ) -> (Vec>, &mut Self) where A: GGSWInfos, + M: ModuleN + VmpPMatBytesOf, { - let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + let mut scratch: &mut Self = self; + let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { - let (ct, new_scratch) = scratch.take_ggsw_prepared(infos); + let (ct, new_scratch) = scratch.take_ggsw_prepared(module, infos); scratch = new_scratch; cts.push(ct) } (cts, scratch) } -} -impl TakeGLWEPk for Scratch -where - Scratch: TakeVecZnx, -{ - fn take_glwe_pk(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) + fn take_glwe_pk(&mut self, module: &M, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) where A: GLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKey::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .base2k(infos.base2k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKey { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } -} -impl TakeGLWEPkPrepared for Scratch -where - Scratch: TakeVecZnxDft, -{ - fn take_glwe_pk_prepared(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_pk_prepared(&mut self, module: &M, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self) where A: GLWEInfos, + M: ModuleN + VecZnxDftBytesOf, { - let (data, scratch) = self.take_vec_znx_dft(infos.n().into(), (infos.rank() + 1).into(), infos.size()); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_vec_znx_dft(module, (infos.rank() + 1).into(), infos.size()); ( - GLWEPublicKeyPrepared::builder() - .base2k(infos.base2k()) - .k(infos.k()) - .data(data) - .build() - .unwrap(), + GLWEPublicKeyPrepared { + k: infos.k(), + dist: Distribution::NONE, + base2k: infos.base2k(), + data, + }, scratch, ) } -} -impl TakeGLWESecret for Scratch -where - Scratch: TakeScalarZnx, -{ - fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) { - let (data, scratch) = self.take_scalar_znx(n.into(), rank.into()); + fn take_glwe_secret(&mut self, module: &M, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) + where + M: ModuleN, + { + let (data, scratch) = self.take_scalar_znx(module, rank.into()); ( GLWESecret { data, @@ -368,14 +233,12 @@ where scratch, ) } -} -impl TakeGLWESecretPrepared for Scratch -where - Scratch: TakeSvpPPol, -{ - fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) { - let (data, scratch) = self.take_svp_ppol(n.into(), rank.into()); + fn take_glwe_secret_prepared(&mut self, module: &M, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) + where + M: ModuleN + SvpPPolBytesOf, + { + let (data, scratch) = self.take_svp_ppol(module, rank.into()); ( GLWESecretPrepared { data, @@ -384,19 +247,16 @@ where scratch, ) } -} -impl TakeGLWESwitchingKey for Scratch -where - Scratch: TakeMatZnx, -{ - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, module: &M, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_gglwe(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe(module, infos); ( - GGLWESwitchingKey { + GLWESwitchingKey { key: data, sk_in_n: 0, sk_out_n: 0, @@ -404,19 +264,20 @@ where scratch, ) } -} -impl TakeGGLWESwitchingKeyPrepared for Scratch -where - Scratch: TakeGGLWEPrepared, -{ - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared( + &mut self, + module: &M, + infos: &A, + ) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { - let (data, scratch) = self.take_gglwe_prepared(infos); + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe_prepared(module, infos); ( - GGLWESwitchingKeyPrepared { + GLWESwitchingKeyPrepared { key: data, sk_in_n: 0, sk_out_n: 0, @@ -424,101 +285,95 @@ where scratch, ) } -} -impl TakeGGLWEAutomorphismKey for Scratch -where - Scratch: TakeMatZnx, -{ - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, module: &M, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { - let (data, scratch) = self.take_glwe_switching_key(infos); - (GGLWEAutomorphismKey { key: data, p: 0 }, scratch) + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_glwe_switching_key(module, infos); + (AutomorphismKey { key: data, p: 0 }, scratch) } -} -impl TakeGGLWEAutomorphismKeyPrepared for Scratch -where - Scratch: TakeGGLWESwitchingKeyPrepared, -{ - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared( + &mut self, + module: &M, + infos: &A, + ) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { - let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); - (GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) + assert_eq!(module.n() as u32, infos.n()); + let (data, scratch) = self.take_gglwe_switching_key_prepared(module, infos); + (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } -} -impl TakeTensorKey for Scratch -where - Scratch: TakeMatZnx, -{ - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, module: &M, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, + M: ModuleN, { + assert_eq!(module.n() as u32, infos.n()); assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; - let mut scratch: &mut Scratch = self; + let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_glwe_switching_key(&ksk_infos); + let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_glwe_switching_key(&ksk_infos); + let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); scratch = s; keys.push(gglwe); } - (GGLWETensorKey { keys }, scratch) + (TensorKey { keys }, scratch) } -} -impl TakeGGLWETensorKeyPrepared for Scratch -where - Scratch: TakeVmpPMat, -{ - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, + M: ModuleN + VmpPMatBytesOf, { + assert_eq!(module.n() as u32, infos.n()); assert_eq!( infos.rank_in(), infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; - let mut scratch: &mut Scratch = self; + let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(&ksk_infos); + let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(&ksk_infos); + let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } - (GGLWETensorKeyPrepared { keys }, scratch) + (TensorKeyPrepared { keys }, scratch) } } + +impl ScratchTakeCore for Scratch where Self: ScratchTakeBasic + ScratchAvailable {} diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 8fe477c..3c4e564 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,12 +1,12 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, - GLWECiphertext, GLWEToLWEKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, + LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWESwitchingKeyCompressed, GGLWETensorKeyCompressed, - GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, - LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + AutomorphismKeyCompressed, GGLWECompressed, GGSWCompressed, GLWECompressed, GLWESwitchingKeyCompressed, + GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + TensorKeyCompressed, }, }; @@ -20,95 +20,93 @@ const DSIZE: Dsize = Dsize(1); #[test] fn glwe_serialization() { - let original: GLWECiphertext> = GLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWE> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let original: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let original: LWECiphertext> = LWECiphertext::alloc_with(N_LWE, BASE2K, K); + let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let original: LWECiphertextCompressed> = LWECiphertextCompressed::alloc_with(BASE2K, K); + let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let original: GGLWECiphertext> = GGLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let original: GGLWECiphertextCompressed> = - GGLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let original: GGLWESwitchingKey> = GGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { - let original: GGLWESwitchingKeyCompressed> = - GGLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKeyCompressed> = + GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let original: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey> = AutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let original: GGLWEAutomorphismKeyCompressed> = - GGLWEAutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: GGLWETensorKey> = GGLWETensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWEKey> = GLWEToLWEKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { let original: GLWEToLWESwitchingKeyCompressed> = - GLWEToLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } @@ -121,24 +119,24 @@ fn lwe_to_glwe_switching_key_compressed_serialization() { #[test] fn lwe_switching_key_serialization() { - let original: LWESwitchingKey> = LWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let original: GGSWCiphertext> = GGSWCiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let original: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed> = GGSWCompressed::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 1dd3e58..37b5403 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -38,8 +38,8 @@ where + VecZnxBigNormalize + VecZnxAutomorphism + VecZnxAutomorphismInplace - + SvpPPolAllocBytes - + VecZnxDftAllocBytes + + SvpPPolBytesOf + + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VmpPMatAlloc + VmpPrepare @@ -84,7 +84,7 @@ where let dnum_out: usize = k_out / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_in_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +93,7 @@ where rank: rank.into(), }; - let auto_key_out_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -102,7 +102,7 @@ where rank: rank.into(), }; - let auto_key_apply_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -111,18 +111,18 @@ where rank: rank.into(), }; - let mut auto_key_in: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) - | GGLWEAutomorphismKey::automorphism_scratch_space( + AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) + | AutomorphismKey::automorphism_tmp_bytes( module, &auto_key_out_infos, &auto_key_in_infos, @@ -130,7 +130,7 @@ where ), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key_in); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key_in); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 @@ -153,8 +153,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -166,9 +166,9 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&auto_key_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key_out_infos); + let mut sk_auto: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key_out_infos); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { module.vec_znx_automorphism( @@ -224,7 +224,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -238,9 +238,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDftTmpBytes @@ -255,8 +255,8 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxAutomorphismInplace - + VecZnxDftAllocBytes - + VecZnxBigAllocBytes + + VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -293,7 +293,7 @@ where let dnum_in: usize = k_in / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -302,7 +302,7 @@ where rank: rank.into(), }; - let auto_key_apply_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -311,20 +311,20 @@ where rank: rank.into(), }; - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) - | GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) + | AutomorphismKey::automorphism_inplace_tmp_bytes(module, &auto_key, &auto_key_apply), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 @@ -347,17 +347,17 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow()); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&auto_key); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk_auto: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { 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 2fd7151..7c2a427 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -19,16 +19,16 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; pub fn test_ggsw_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -41,7 +41,7 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpA + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAddScalarInplace + VecZnxCopy @@ -110,7 +110,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -119,7 +119,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -128,10 +128,10 @@ where rank: rank.into(), }; - let mut ct_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_layout); - let mut ct_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_layout); + let mut ct_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -139,15 +139,15 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct_in) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), + GGSW::encrypt_sk_tmp_bytes(module, &ct_in) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -178,11 +178,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -219,8 +219,8 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -233,7 +233,7 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpA + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAddScalarInplace + VecZnxCopy @@ -291,7 +291,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -300,7 +300,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -309,9 +309,9 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -319,15 +319,15 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), + GGSW::encrypt_sk_tmp_bytes(module, &ct) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GGSW::automorphism_inplace_tmp_bytes(module, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -358,11 +358,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); 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 5828c48..02afcb8 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -18,15 +18,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; pub fn test_glwe_automorphism(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,9 +40,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VmpApplyDftToDftTmpBytes @@ -77,21 +77,21 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let ct_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -100,10 +100,10 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&ct_in_infos); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&ct_in_infos); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -112,13 +112,13 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWECiphertext::decrypt_scratch_space(module, &ct_out) - | GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in) - | GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + | GLWE::decrypt_tmp_bytes(module, &ct_out) + | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) + | GLWE::automorphism_tmp_bytes(module, &ct_out, &ct_in, &autokey), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -140,8 +140,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey_infos); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); @@ -169,7 +169,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_glwe_automorphism_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -183,9 +183,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VmpApplyDftToDftTmpBytes @@ -219,14 +219,14 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -235,9 +235,9 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -246,13 +246,13 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) - | GLWECiphertext::decrypt_scratch_space(module, &ct) - | GLWECiphertext::encrypt_sk_scratch_space(module, &ct) - | GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey), + AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + | GLWE::decrypt_tmp_bytes(module, &ct) + | GLWE::encrypt_sk_tmp_bytes(module, &ct) + | GLWE::automorphism_inplace_tmp_bytes(module, &ct, &autokey), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -274,8 +274,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::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 c2c81dc..972e0c6 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, }, @@ -16,15 +16,14 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, - TorusPrecision, + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, + LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, }; pub fn test_lwe_to_glwe(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -37,9 +36,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -83,7 +82,7 @@ where rank_out: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -97,12 +96,12 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, &lwe_to_glwe_infos) - | GLWECiphertext::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos) + | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -112,13 +111,13 @@ where let data: i64 = 17; - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_pt.encode_i64(data, k_lwe_pt); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe); - let mut ksk: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(&lwe_to_glwe_infos); + let mut ksk: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos); ksk.encrypt_sk( module, @@ -129,13 +128,13 @@ where scratch.borrow(), ); - let mut glwe_ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow()); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); @@ -143,7 +142,7 @@ where pub fn test_glwe_to_lwe(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -156,9 +155,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -196,7 +195,7 @@ where rank_in: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -214,12 +213,12 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWEToLWEKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) - | LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos) + | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -228,10 +227,10 @@ where sk_lwe.fill_ternary_prob(0.5, &mut source_xs); let data: i64 = 17; - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); - let mut glwe_ct = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt, @@ -241,7 +240,7 @@ where scratch.borrow(), ); - let mut ksk: GLWEToLWEKey> = GLWEToLWEKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); ksk.encrypt_sk( module, @@ -252,13 +251,13 @@ where scratch.borrow(), ); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); let ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); 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 e9b7c93..bd164dc 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -18,15 +18,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, - compressed::{Decompress, GGLWEAutomorphismKeyCompressed}, + AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, + compressed::{AutomorphismKeyCompressed, Decompress}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; pub fn test_gglwe_automorphisk_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,7 +40,7 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes @@ -51,7 +51,7 @@ where + VecZnxSwitchRing + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -75,7 +75,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -84,17 +84,17 @@ where rank: rank.into(), }; - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -129,7 +129,7 @@ where pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -143,7 +143,7 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes @@ -154,7 +154,7 @@ where + VecZnxSwitchRing + VecZnxAddScalarInplace + VecZnxAutomorphismInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -178,7 +178,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -187,16 +187,16 @@ where rank: rank.into(), }; - let mut atk_compressed: GGLWEAutomorphismKeyCompressed> = GGLWEAutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::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(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -217,7 +217,7 @@ where }); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key 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 60bb7e2..5e09781 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, }, @@ -17,15 +17,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWECiphertextLayout, GGLWESwitchingKey, GLWESecret, - compressed::{Decompress, GGLWESwitchingKeyCompressed}, + GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey, + compressed::{Decompress, GLWESwitchingKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,12 +39,12 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxSwitchRing + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -81,21 +81,21 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -117,7 +117,7 @@ where pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -131,12 +131,12 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxSwitchRing + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxSubScalarInplace + VecZnxCopy @@ -173,20 +173,21 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GGLWESwitchingKeyCompressed> = GGLWESwitchingKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed> = + GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_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(GGLWESwitchingKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -201,7 +202,7 @@ where scratch.borrow(), ); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key 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 9eacf38..7620709 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScalarZnx, ScratchOwned}, oep::{ @@ -17,53 +17,16 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - compressed::{Decompress, GGSWCiphertextCompressed}, + GGSW, GGSWCiphertextLayout, GLWESecret, + compressed::{Decompress, GGSWCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; -pub fn test_ggsw_encrypt_sk(module: &Module) +pub fn test_ggsw_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolAllocBytes - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxBigAllocBytes - + VecZnxBigAddInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc, + Module: SvpPrepare, { let base2k: usize = 12; let k: usize = 54; @@ -82,7 +45,7 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -92,12 +55,9 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertext::encrypt_sk_scratch_space( - module, - &ggsw_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -119,7 +79,7 @@ where pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -133,11 +93,11 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VmpPMatAlloc @@ -175,7 +135,7 @@ where rank: rank.into(), }; - let mut ct_compressed: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc(&ggsw_infos); + let mut ct_compressed: GGSWCompressed> = GGSWCompressed::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -184,12 +144,12 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertextCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes( module, &ggsw_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -206,7 +166,7 @@ where let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); 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 a1169f6..ed2c1e3 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -1,10 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -17,8 +16,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, - compressed::{Decompress, GLWECiphertextCompressed}, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, + compressed::{Decompress, GLWECompressed}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, operations::GLWEOperations, @@ -26,8 +25,8 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -36,7 +35,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -64,7 +63,7 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), @@ -77,20 +76,19 @@ where k: k_pt.into(), }; - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -118,8 +116,8 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -128,7 +126,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -157,7 +155,7 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), @@ -170,21 +168,20 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc(&glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertextCompressed::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -201,7 +198,7 @@ where scratch.borrow(), ); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); @@ -221,8 +218,8 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -231,7 +228,7 @@ where + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + SvpApplyDftToDft + VecZnxBigAddNormal @@ -258,29 +255,28 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos), + GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module, @@ -297,7 +293,7 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -311,10 +307,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc @@ -336,16 +332,16 @@ where for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -353,17 +349,17 @@ where let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_pk_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_infos) + | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc(&glwe_infos); - pk.generate_from_sk(module, &sk_prepared, &mut source_xa, &mut source_xe); + let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(&glwe_infos); + pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); 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 8bb9730..da05ba1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, }, @@ -17,15 +17,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - Dsize, GGLWETensorKey, GGLWETensorKeyLayout, GLWEPlaintext, GLWESecret, - compressed::{Decompress, GGLWETensorKeyCompressed}, + Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, + compressed::{Decompress, TensorKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,10 +39,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc @@ -71,7 +71,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos = GGLWETensorKeyLayout { + let tensor_key_infos = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -80,18 +80,18 @@ where rank: rank.into(), }; - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -103,11 +103,11 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { @@ -145,7 +145,7 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -159,10 +159,10 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxBigAddSmallInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxCopy + VecZnxDftAlloc @@ -190,7 +190,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -199,17 +199,17 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::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(GGLWETensorKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -217,14 +217,14 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { 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 2cfa618..a369978 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 @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -41,9 +41,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxSwitchRing @@ -81,7 +81,7 @@ where let dnum: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_in_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_in_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -91,7 +91,7 @@ where rank_out: rank_out.into(), }; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,9 +110,9 @@ where rank: rank_out.into(), }; - let mut ct_gglwe_in: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -121,14 +121,14 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) - | GGLWESwitchingKey::external_product_scratch_space( + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_in_infos) + | GLWESwitchingKey::external_product_tmp_bytes( module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos, ) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); let r: usize = 1; @@ -137,10 +137,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -163,7 +163,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); @@ -209,7 +209,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -223,9 +223,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxSwitchRing @@ -263,7 +263,7 @@ where let dsize_in: usize = 1; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -282,8 +282,8 @@ where rank: rank_out.into(), }; - let mut ct_gglwe: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -292,9 +292,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) - | GGLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_out_infos) + | GLWESwitchingKey::external_product_inplace_tmp_bytes(module, &gglwe_out_infos, &ggsw_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); let r: usize = 1; @@ -303,10 +303,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -329,7 +329,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); 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 464eac2..84a2f68 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 @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, + VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, oep::{ @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -27,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_ggsw_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -41,9 +41,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -128,12 +128,12 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGSWCiphertext::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) + | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -155,7 +155,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); @@ -192,7 +192,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -206,9 +206,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxAddScalarInplace @@ -265,8 +265,8 @@ where rank: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -282,12 +282,12 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGSWCiphertext::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) + | GGSW::external_product_inplace_tmp_bytes(module, &ggsw_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -309,7 +309,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); 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 1a21a0c..60026b8 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 @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -26,7 +26,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_external_product(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -73,14 +73,14 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -96,11 +96,11 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -116,12 +116,12 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) + | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -143,7 +143,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); @@ -178,7 +178,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_glwe_external_product_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -191,9 +191,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -224,7 +224,7 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -240,10 +240,10 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -259,12 +259,12 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::external_product_inplace_tmp_bytes(module, &glwe_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -286,7 +286,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); 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 f131556..c667848 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, + VmpPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -18,15 +18,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; pub fn test_gglwe_switching_key_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -39,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -80,7 +80,7 @@ where let dnum_apply: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -90,7 +90,7 @@ where rank_out: rank_out_s0s1.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -100,7 +100,7 @@ where rank_out: rank_out_s1s2.into(), }; - let gglwe_s0s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,33 +110,33 @@ where rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_tmp_bytes( module, &gglwe_s0s1_infos, &gglwe_s0s2_infos, &gglwe_s1s2_infos, )); - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in_s0s1.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in_s0s1.into()); sk0.fill_ternary_prob(0.5, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s0s1.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s0s1.into()); sk1.fill_ternary_prob(0.5, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s1s2.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s1s2.into()); sk2.fill_ternary_prob(0.5, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); @@ -160,7 +160,7 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) @@ -196,7 +196,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -209,9 +209,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -246,7 +246,7 @@ where let dnum: usize = k_out.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -256,7 +256,7 @@ where rank_out: rank_out.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -266,18 +266,18 @@ where rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_inplace_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_tmp_bytes( module, &gglwe_s0s1_infos, &gglwe_s1s2_infos, @@ -285,13 +285,13 @@ where let var_xs: f64 = 0.5; - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk0.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk1.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk2.fill_ternary_prob(var_xs, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); @@ -315,13 +315,13 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow()); - let gglwe_s0s2: GGLWESwitchingKey> = gglwe_s0s1; + let gglwe_s0s2: GLWESwitchingKey> = gglwe_s0s1; let max_noise: f64 = log2_std_noise_gglwe_product( n as f64, 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 c7e7c82..4d3d556 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAlloc, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAlloc, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, @@ -18,9 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, - GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, TensorKey, TensorKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -28,7 +27,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_ggsw_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -41,9 +40,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -103,7 +102,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -112,7 +111,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -122,10 +121,10 @@ where rank_out: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -133,10 +132,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_scratch_space( + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, &ggsw_in_infos, @@ -147,11 +146,11 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -182,8 +181,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch( module, @@ -217,7 +216,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_ggsw_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -230,9 +229,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -282,7 +281,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -291,7 +290,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -301,9 +300,9 @@ where rank_out: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -311,19 +310,19 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), + GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) + | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GGSW::keyswitch_inplace_tmp_bytes(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), ); let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -354,8 +353,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); 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 d745f1d..2ea6e75 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, + VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, + VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, Module, ScratchOwned}, oep::{ @@ -18,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -27,7 +26,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -40,9 +39,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -77,21 +76,21 @@ where let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank_in.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), rank: rank_out.into(), }; - let key_apply: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -101,10 +100,10 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -113,16 +112,16 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) - | GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -144,7 +143,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -169,7 +168,7 @@ where pub fn test_glwe_keyswitch_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -182,9 +181,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -217,14 +216,14 @@ where let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), rank: rank.into(), }; - let key_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -234,9 +233,9 @@ where rank_out: rank.into(), }; - let mut key_apply: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -245,16 +244,16 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), ); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -276,7 +275,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); 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 1c29c70..130c356 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, }, @@ -16,13 +16,13 @@ use poulpy_hal::{ }; use crate::layouts::{ - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, + LWE, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, }; pub fn test_lwe_keyswitch(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -35,9 +35,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -99,8 +99,8 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - LWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | LWECiphertext::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), + LWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + | LWE::keyswitch_tmp_bytes(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), ); let mut sk_lwe_in: LWESecret> = LWESecret::alloc(n_lwe_in.into()); @@ -111,10 +111,10 @@ where let data: i64 = 17; - let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); lwe_pt_in.encode_i64(data, k_lwe_pt.into()); - let mut lwe_ct_in: LWECiphertext> = LWECiphertext::alloc(&lwe_in_infos); + let mut lwe_ct_in: LWE> = LWE::alloc_from_infos(&lwe_in_infos); lwe_ct_in.encrypt_sk( module, &lwe_pt_in, @@ -123,7 +123,7 @@ where &mut source_xe, ); - let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc(&key_apply_infos); + let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc_from_infos(&key_apply_infos); ksk.encrypt_sk( module, @@ -134,13 +134,13 @@ where scratch.borrow(), ); - let mut lwe_ct_out: LWECiphertext> = LWECiphertext::alloc(&lwe_out_infos); + let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); let ksk_prepared: LWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); - let mut lwe_pt_out: LWEPlaintext> = LWEPlaintext::alloc(&lwe_out_infos); + let mut lwe_pt_out: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_out_infos); lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out); assert_eq!(lwe_pt_in.data.at(0, 0)[0], lwe_pt_out.data.at(0, 0)[0]); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index de7fc9d..fdfbd57 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -2,10 +2,10 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, @@ -21,14 +21,14 @@ use poulpy_hal::{ use crate::{ GLWEOperations, GLWEPacker, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; pub fn test_glwe_packing(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxAutomorphism + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallNegateInplace @@ -48,9 +48,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -88,14 +88,14 @@ where let dnum: usize = k_ct.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -105,16 +105,16 @@ where }; let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { *x = i as i64; @@ -124,8 +124,8 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = HashMap::new(); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -135,7 +135,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); @@ -143,7 +143,7 @@ where let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); ct.encrypt_sk( module, @@ -171,19 +171,14 @@ where if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); } else { - packer.add( - module, - None::<&GLWECiphertext>>, - &auto_keys, - scratch.borrow(), - ) + packer.add(module, None::<&GLWE>>, &auto_keys, scratch.borrow()) } }); - let mut res: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); packer.flush(module, &mut res); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { if i.is_multiple_of(5) { diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index bf348ca..932b401 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, - VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, + VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, + VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, + VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, + VmpPrepare, }, layouts::{Backend, Module, ScratchOwned, ZnxView, ZnxViewMut}, oep::{ @@ -21,16 +21,15 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - LWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWEInfos, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product, }; pub fn test_glwe_trace_inplace(module: &Module) where - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxAutomorphism + VecZnxBigAutomorphismInplace + VecZnxBigSubSmallNegateInplace @@ -48,9 +47,9 @@ where + VecZnxNormalize + VecZnxSub + SvpPrepare - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxNormalizeTmpBytes @@ -83,14 +82,14 @@ where let dsize: usize = 1; let dnum: usize = k.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k.into(), rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_autokey.into(), @@ -99,22 +98,22 @@ where dnum: dnum.into(), }; - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), + GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) + | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos) + | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWE::trace_inplace_tmp_bytes(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -135,9 +134,9 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, B>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut auto_keys: HashMap, B>> = 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| { tmp.encrypt_sk( module, @@ -147,7 +146,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-hal/src/api/module.rs b/poulpy-hal/src/api/module.rs index 6e5faed..3dd6176 100644 --- a/poulpy-hal/src/api/module.rs +++ b/poulpy-hal/src/api/module.rs @@ -4,3 +4,7 @@ use crate::layouts::Backend; pub trait ModuleNew { fn new(n: u64) -> Self; } + +pub trait ModuleN { + fn n(&self) -> usize; +} diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index 38901bf..ee4a080 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -1,4 +1,7 @@ -use crate::layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}; +use crate::{ + api::{ModuleN, SvpPPolBytesOf, VecZnxBigBytesOf, VecZnxDftBytesOf, VmpPMatBytesOf}, + layouts::{Backend, MatZnx, ScalarZnx, Scratch, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, +}; /// Allocates a new [crate::layouts::ScratchOwned] of `size` aligned bytes. pub trait ScratchOwnedAlloc { @@ -25,76 +28,130 @@ pub trait TakeSlice { fn take_slice(&mut self, len: usize) -> (&mut [T], &mut Self); } -/// Take a slice of bytes from a [Scratch], wraps it into a [ScalarZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeScalarZnx { - fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self); -} +pub trait ScratchTakeBasic +where + Self: TakeSlice, +{ + fn take_scalar_znx(&mut self, module: &M, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(module.n(), cols)); + ( + ScalarZnx::from_data(take_slice, module.n(), cols), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [SvpPPol] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeSvpPPol { - fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self); -} + fn take_svp_ppol(&mut self, module: &M, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) + where + M: SvpPPolBytesOf + ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_svp_ppol(cols)); + (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnx { - fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self); -} + fn take_vec_znx(&mut self, module: &M, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(module.n(), cols, size)); + ( + VecZnx::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnx] aand returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxSlice { - fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec>, &mut Self); -} + fn take_vec_znx_big(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) + where + M: VecZnxBigBytesOf + ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_big(cols, size)); + ( + VecZnxBig::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxBig] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxBig { - fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self); -} + fn take_vec_znx_dft(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) + where + M: VecZnxDftBytesOf + ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vec_znx_dft(cols, size)); -/// Take a slice of bytes from a [Scratch], wraps it into a [VecZnxDft] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxDft { - fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self); -} + ( + VecZnxDft::from_data(take_slice, module.n(), cols, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], slices it into a vector of [VecZnxDft] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVecZnxDftSlice { - fn take_vec_znx_dft_slice( + fn take_vec_znx_dft_slice( &mut self, + module: &M, len: usize, - n: usize, cols: usize, size: usize, - ) -> (Vec>, &mut Self); -} + ) -> (Vec>, &mut Self) + where + M: VecZnxDftBytesOf + ModuleN, + { + let mut scratch: &mut Self = self; + let mut slice: Vec> = Vec::with_capacity(len); + for _ in 0..len { + let (znx, new_scratch) = scratch.take_vec_znx_dft(module, cols, size); + scratch = new_scratch; + slice.push(znx); + } + (slice, scratch) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [VmpPMat] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeVmpPMat { - fn take_vmp_pmat( + fn take_vec_znx_slice(&mut self, module: &M, len: usize, cols: usize, size: usize) -> (Vec>, &mut Self) + where + M: ModuleN, + { + let mut scratch: &mut Self = self; + let mut slice: Vec> = Vec::with_capacity(len); + for _ in 0..len { + let (znx, new_scratch) = scratch.take_vec_znx(module, cols, size); + scratch = new_scratch; + slice.push(znx); + } + (slice, scratch) + } + + fn take_vmp_pmat( &mut self, - n: usize, + module: &M, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Self); -} + ) -> (VmpPMat<&mut [u8], B>, &mut Self) + where + M: VmpPMatBytesOf + ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(module.bytes_of_vmp_pmat(rows, cols_in, cols_out, size)); + ( + VmpPMat::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), + rem_slice, + ) + } -/// Take a slice of bytes from a [Scratch], wraps it into a [MatZnx] and returns it -/// as well as a new [Scratch] minus the taken array of bytes. -pub trait TakeMatZnx { - fn take_mat_znx( + fn take_mat_znx( &mut self, - n: usize, + module: &M, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self); + ) -> (MatZnx<&mut [u8]>, &mut Self) + where + M: ModuleN, + { + let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(module.n(), rows, cols_in, cols_out, size)); + ( + MatZnx::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), + rem_slice, + ) + } } diff --git a/poulpy-hal/src/api/svp_ppol.rs b/poulpy-hal/src/api/svp_ppol.rs index 5a72367..6678584 100644 --- a/poulpy-hal/src/api/svp_ppol.rs +++ b/poulpy-hal/src/api/svp_ppol.rs @@ -8,8 +8,8 @@ pub trait SvpPPolAlloc { } /// Returns the size in bytes to allocate a [crate::layouts::SvpPPol]. -pub trait SvpPPolAllocBytes { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize; +pub trait SvpPPolBytesOf { + fn bytes_of_svp_ppol(&self, cols: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::MatZnx]. diff --git a/poulpy-hal/src/api/vec_znx_big.rs b/poulpy-hal/src/api/vec_znx_big.rs index 08159bb..8cb5105 100644 --- a/poulpy-hal/src/api/vec_znx_big.rs +++ b/poulpy-hal/src/api/vec_znx_big.rs @@ -16,8 +16,8 @@ pub trait VecZnxBigAlloc { } /// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig]. -pub trait VecZnxBigAllocBytes { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize; +pub trait VecZnxBigBytesOf { + fn bytes_of_vec_znx_big(&self, cols: usize, size: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::VecZnxBig]. diff --git a/poulpy-hal/src/api/vec_znx_dft.rs b/poulpy-hal/src/api/vec_znx_dft.rs index 58589c3..3a003a9 100644 --- a/poulpy-hal/src/api/vec_znx_dft.rs +++ b/poulpy-hal/src/api/vec_znx_dft.rs @@ -10,8 +10,8 @@ pub trait VecZnxDftFromBytes { fn vec_znx_dft_from_bytes(&self, cols: usize, size: usize, bytes: Vec) -> VecZnxDftOwned; } -pub trait VecZnxDftAllocBytes { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize; +pub trait VecZnxDftBytesOf { + fn bytes_of_vec_znx_dft(&self, cols: usize, size: usize) -> usize; } pub trait VecZnxDftApply { diff --git a/poulpy-hal/src/api/vmp_pmat.rs b/poulpy-hal/src/api/vmp_pmat.rs index 3d0e248..de3433a 100644 --- a/poulpy-hal/src/api/vmp_pmat.rs +++ b/poulpy-hal/src/api/vmp_pmat.rs @@ -6,8 +6,8 @@ pub trait VmpPMatAlloc { fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned; } -pub trait VmpPMatAllocBytes { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; +pub trait VmpPMatBytesOf { + fn bytes_of_vmp_pmat(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPMatFromBytes { diff --git a/poulpy-hal/src/delegates/scratch.rs b/poulpy-hal/src/delegates/scratch.rs index ac022e3..b91afbb 100644 --- a/poulpy-hal/src/delegates/scratch.rs +++ b/poulpy-hal/src/delegates/scratch.rs @@ -1,14 +1,7 @@ use crate::{ - api::{ - ScratchAvailable, ScratchFromBytes, ScratchOwnedAlloc, ScratchOwnedBorrow, TakeMatZnx, TakeScalarZnx, TakeSlice, - TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, TakeVmpPMat, - }, - layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, - oep::{ - ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, - TakeScalarZnxImpl, TakeSliceImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, - TakeVecZnxImpl, TakeVecZnxSliceImpl, TakeVmpPMatImpl, - }, + api::{ScratchAvailable, ScratchFromBytes, ScratchOwnedAlloc, ScratchOwnedBorrow, TakeSlice}, + layouts::{Backend, Scratch, ScratchOwned}, + oep::{ScratchAvailableImpl, ScratchFromBytesImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, }; impl ScratchOwnedAlloc for ScratchOwned @@ -55,104 +48,3 @@ where B::take_slice_impl(self, len) } } - -impl TakeScalarZnx for Scratch -where - B: Backend + TakeScalarZnxImpl, -{ - fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { - B::take_scalar_znx_impl(self, n, cols) - } -} - -impl TakeSvpPPol for Scratch -where - B: Backend + TakeSvpPPolImpl, -{ - fn take_svp_ppol(&mut self, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) { - B::take_svp_ppol_impl(self, n, cols) - } -} - -impl TakeVecZnx for Scratch -where - B: Backend + TakeVecZnxImpl, -{ - fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) { - B::take_vec_znx_impl(self, n, cols, size) - } -} - -impl TakeVecZnxSlice for Scratch -where - B: Backend + TakeVecZnxSliceImpl, -{ - fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec>, &mut Self) { - B::take_vec_znx_slice_impl(self, len, n, cols, size) - } -} - -impl TakeVecZnxBig for Scratch -where - B: Backend + TakeVecZnxBigImpl, -{ - fn take_vec_znx_big(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) { - B::take_vec_znx_big_impl(self, n, cols, size) - } -} - -impl TakeVecZnxDft for Scratch -where - B: Backend + TakeVecZnxDftImpl, -{ - fn take_vec_znx_dft(&mut self, n: usize, cols: usize, size: usize) -> (VecZnxDft<&mut [u8], B>, &mut Self) { - B::take_vec_znx_dft_impl(self, n, cols, size) - } -} - -impl TakeVecZnxDftSlice for Scratch -where - B: Backend + TakeVecZnxDftSliceImpl, -{ - fn take_vec_znx_dft_slice( - &mut self, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Self) { - B::take_vec_znx_dft_slice_impl(self, len, n, cols, size) - } -} - -impl TakeVmpPMat for Scratch -where - B: Backend + TakeVmpPMatImpl, -{ - fn take_vmp_pmat( - &mut self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Self) { - B::take_vmp_pmat_impl(self, n, rows, cols_in, cols_out, size) - } -} - -impl TakeMatZnx for Scratch -where - B: Backend + TakeMatZnxImpl, -{ - fn take_mat_znx( - &mut self, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self) { - B::take_mat_znx_impl(self, n, rows, cols_in, cols_out, size) - } -} diff --git a/poulpy-hal/src/delegates/svp_ppol.rs b/poulpy-hal/src/delegates/svp_ppol.rs index 54a99b2..de36fd0 100644 --- a/poulpy-hal/src/delegates/svp_ppol.rs +++ b/poulpy-hal/src/delegates/svp_ppol.rs @@ -1,6 +1,6 @@ use crate::{ api::{ - SvpApplyDft, SvpApplyDftToDft, SvpApplyDftToDftAdd, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + SvpApplyDft, SvpApplyDftToDft, SvpApplyDftToDftAdd, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPPolFromBytes, SvpPrepare, }, layouts::{ @@ -30,12 +30,12 @@ where } } -impl SvpPPolAllocBytes for Module +impl SvpPPolBytesOf for Module where B: Backend + SvpPPolAllocBytesImpl, { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize { - B::svp_ppol_alloc_bytes_impl(self.n(), cols) + fn bytes_of_svp_ppol(&self, cols: usize) -> usize { + B::svp_ppol_bytes_of_impl(self.n(), cols) } } diff --git a/poulpy-hal/src/delegates/vec_znx_big.rs b/poulpy-hal/src/delegates/vec_znx_big.rs index 1556a87..a1cc307 100644 --- a/poulpy-hal/src/delegates/vec_znx_big.rs +++ b/poulpy-hal/src/delegates/vec_znx_big.rs @@ -1,7 +1,7 @@ use crate::{ api::{ VecZnxBigAdd, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmall, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigAllocBytes, VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace, VecZnxBigAutomorphismInplaceTmpBytes, + VecZnxBigAutomorphism, VecZnxBigAutomorphismInplace, VecZnxBigAutomorphismInplaceTmpBytes, VecZnxBigBytesOf, VecZnxBigFromBytes, VecZnxBigFromSmall, VecZnxBigNegate, VecZnxBigNegateInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSub, VecZnxBigSubInplace, VecZnxBigSubNegateInplace, VecZnxBigSubSmallA, VecZnxBigSubSmallB, VecZnxBigSubSmallInplace, VecZnxBigSubSmallNegateInplace, @@ -49,12 +49,12 @@ where } } -impl VecZnxBigAllocBytes for Module +impl VecZnxBigBytesOf for Module where B: Backend + VecZnxBigAllocBytesImpl, { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_big_alloc_bytes_impl(self.n(), cols, size) + fn bytes_of_vec_znx_big(&self, cols: usize, size: usize) -> usize { + B::vec_znx_big_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vec_znx_dft.rs b/poulpy-hal/src/delegates/vec_znx_dft.rs index 3736e34..16a583f 100644 --- a/poulpy-hal/src/delegates/vec_znx_dft.rs +++ b/poulpy-hal/src/delegates/vec_znx_dft.rs @@ -1,8 +1,8 @@ use crate::{ api::{ - VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, - VecZnxDftFromBytes, VecZnxDftSub, VecZnxDftSubInplace, VecZnxDftSubNegateInplace, VecZnxDftZero, VecZnxIdftApply, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxIdftApplyTmpBytes, + VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxDftFromBytes, + VecZnxDftSub, VecZnxDftSubInplace, VecZnxDftSubNegateInplace, VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxIdftApplyTmpBytes, }, layouts::{ Backend, Data, Module, Scratch, VecZnxBig, VecZnxBigToMut, VecZnxDft, VecZnxDftOwned, VecZnxDftToMut, VecZnxDftToRef, @@ -24,12 +24,12 @@ where } } -impl VecZnxDftAllocBytes for Module +impl VecZnxDftBytesOf for Module where B: Backend + VecZnxDftAllocBytesImpl, { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_dft_alloc_bytes_impl(self.n(), cols, size) + fn bytes_of_vec_znx_dft(&self, cols: usize, size: usize) -> usize { + B::vec_znx_dft_bytes_of_impl(self.n(), cols, size) } } diff --git a/poulpy-hal/src/delegates/vmp_pmat.rs b/poulpy-hal/src/delegates/vmp_pmat.rs index a875a40..2c65508 100644 --- a/poulpy-hal/src/delegates/vmp_pmat.rs +++ b/poulpy-hal/src/delegates/vmp_pmat.rs @@ -1,7 +1,7 @@ use crate::{ api::{ VmpApplyDft, VmpApplyDftTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftAddTmpBytes, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPMatAllocBytes, VmpPMatFromBytes, VmpPrepare, VmpPrepareTmpBytes, + VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPMatBytesOf, VmpPMatFromBytes, VmpPrepare, VmpPrepareTmpBytes, }, layouts::{ Backend, MatZnxToRef, Module, Scratch, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef, VmpPMatOwned, VmpPMatToMut, @@ -23,12 +23,12 @@ where } } -impl VmpPMatAllocBytes for Module +impl VmpPMatBytesOf for Module where B: Backend + VmpPMatAllocBytesImpl, { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - B::vmp_pmat_alloc_bytes_impl(self.n(), rows, cols_in, cols_out, size) + fn bytes_of_vmp_pmat(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + B::vmp_pmat_bytes_of_impl(self.n(), rows, cols_in, cols_out, size) } } diff --git a/poulpy-hal/src/layouts/mat_znx.rs b/poulpy-hal/src/layouts/mat_znx.rs index 01be1a1..5ccb860 100644 --- a/poulpy-hal/src/layouts/mat_znx.rs +++ b/poulpy-hal/src/layouts/mat_znx.rs @@ -114,12 +114,12 @@ impl MatZnx { } impl MatZnx> { - pub fn alloc_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - rows * cols_in * VecZnx::>::alloc_bytes(n, cols_out, size) + pub fn bytes_of(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + rows * cols_in * VecZnx::>::bytes_of(n, cols_out, size) } pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec = alloc_aligned(Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + let data: Vec = alloc_aligned(Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n, @@ -132,7 +132,7 @@ impl MatZnx> { pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + assert!(data.len() == Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n, @@ -153,7 +153,7 @@ impl MatZnx { } let self_ref: MatZnx<&[u8]> = self.to_ref(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(self.n, self.cols_out, self.size); + let nb_bytes: usize = VecZnx::>::bytes_of(self.n, self.cols_out, self.size); let start: usize = nb_bytes * self.cols() * row + col * nb_bytes; let end: usize = start + nb_bytes; @@ -181,7 +181,7 @@ impl MatZnx { let size: usize = self.size(); let self_ref: MatZnx<&mut [u8]> = self.to_mut(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(n, cols_out, size); + let nb_bytes: usize = VecZnx::>::bytes_of(n, cols_out, size); let start: usize = nb_bytes * cols_in * row + col * nb_bytes; let end: usize = start + nb_bytes; diff --git a/poulpy-hal/src/layouts/scalar_znx.rs b/poulpy-hal/src/layouts/scalar_znx.rs index 296071b..bdf2159 100644 --- a/poulpy-hal/src/layouts/scalar_znx.rs +++ b/poulpy-hal/src/layouts/scalar_znx.rs @@ -132,18 +132,18 @@ impl ScalarZnx { } impl ScalarZnx> { - pub fn alloc_bytes(n: usize, cols: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize) -> usize { n * cols * size_of::() } pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols)); Self { data, n, cols } } pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data, n, cols } } } diff --git a/poulpy-hal/src/layouts/svp_ppol.rs b/poulpy-hal/src/layouts/svp_ppol.rs index 50523fc..234ebca 100644 --- a/poulpy-hal/src/layouts/svp_ppol.rs +++ b/poulpy-hal/src/layouts/svp_ppol.rs @@ -77,7 +77,7 @@ where B: SvpPPolAllocBytesImpl, { pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(B::svp_ppol_alloc_bytes_impl(n, cols)); + let data: Vec = alloc_aligned::(B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n, @@ -88,7 +88,7 @@ where pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::svp_ppol_alloc_bytes_impl(n, cols)); + assert!(data.len() == B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vec_znx.rs b/poulpy-hal/src/layouts/vec_znx.rs index d40ef4b..c084934 100644 --- a/poulpy-hal/src/layouts/vec_znx.rs +++ b/poulpy-hal/src/layouts/vec_znx.rs @@ -110,7 +110,7 @@ impl ZnxView for VecZnx { } impl VecZnx> { - pub fn rsh_scratch_space(n: usize) -> usize { + pub fn rsh_tmp_bytes(n: usize) -> usize { n * std::mem::size_of::() } } @@ -125,12 +125,12 @@ impl ZnxZero for VecZnx { } impl VecZnx> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -142,7 +142,7 @@ impl VecZnx> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/layouts/vec_znx_big.rs b/poulpy-hal/src/layouts/vec_znx_big.rs index c50cf66..73a3e0f 100644 --- a/poulpy-hal/src/layouts/vec_znx_big.rs +++ b/poulpy-hal/src/layouts/vec_znx_big.rs @@ -96,7 +96,7 @@ where B: VecZnxBigAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data = alloc_aligned::(B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + let data = alloc_aligned::(B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -109,7 +109,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vec_znx_dft.rs b/poulpy-hal/src/layouts/vec_znx_dft.rs index 3dc92d5..19d28e1 100644 --- a/poulpy-hal/src/layouts/vec_znx_dft.rs +++ b/poulpy-hal/src/layouts/vec_znx_dft.rs @@ -116,7 +116,7 @@ where B: VecZnxDftAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let data: Vec = alloc_aligned::(B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -129,7 +129,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/vmp_pmat.rs b/poulpy-hal/src/layouts/vmp_pmat.rs index ce83458..bd469ec 100644 --- a/poulpy-hal/src/layouts/vmp_pmat.rs +++ b/poulpy-hal/src/layouts/vmp_pmat.rs @@ -88,9 +88,7 @@ where B: VmpPMatAllocBytesImpl, { pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec = alloc_aligned(B::vmp_pmat_alloc_bytes_impl( - n, rows, cols_in, cols_out, size, - )); + let data: Vec = alloc_aligned(B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n, @@ -104,7 +102,7 @@ where pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size)); + assert!(data.len() == B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n, diff --git a/poulpy-hal/src/layouts/zn.rs b/poulpy-hal/src/layouts/zn.rs index 40f5622..00f8067 100644 --- a/poulpy-hal/src/layouts/zn.rs +++ b/poulpy-hal/src/layouts/zn.rs @@ -98,7 +98,7 @@ impl ZnxView for Zn { } impl Zn> { - pub fn rsh_scratch_space(n: usize) -> usize { + pub fn rsh_tmp_bytes(n: usize) -> usize { n * std::mem::size_of::() } } @@ -113,12 +113,12 @@ impl ZnxZero for Zn { } impl Zn> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -130,7 +130,7 @@ impl Zn> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/oep/scratch.rs b/poulpy-hal/src/oep/scratch.rs index 51a9c56..c973b04 100644 --- a/poulpy-hal/src/oep/scratch.rs +++ b/poulpy-hal/src/oep/scratch.rs @@ -1,4 +1,4 @@ -use crate::layouts::{Backend, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}; +use crate::layouts::{Backend, Scratch, ScratchOwned}; /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. @@ -39,111 +39,3 @@ pub unsafe trait ScratchAvailableImpl { pub unsafe trait TakeSliceImpl { fn take_slice_impl(scratch: &mut Scratch, len: usize) -> (&mut [T], &mut Scratch); } - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeScalarZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeScalarZnxImpl { - fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeSvpPPol] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeSvpPPolImpl { - fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxImpl { - fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxSlice] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxSliceImpl { - fn take_vec_znx_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxBig] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxBigImpl { - fn take_vec_znx_big_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxDft] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxDftImpl { - fn take_vec_znx_dft_impl( - scratch: &mut Scratch, - n: usize, - cols: usize, - size: usize, - ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVecZnxDftSlice] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVecZnxDftSliceImpl { - fn take_vec_znx_dft_slice_impl( - scratch: &mut Scratch, - len: usize, - n: usize, - cols: usize, - size: usize, - ) -> (Vec>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeVmpPMat] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeVmpPMatImpl { - fn take_vmp_pmat_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (VmpPMat<&mut [u8], B>, &mut Scratch); -} - -/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) -/// * See the [poulpy-backend/src/cpu_fft64_ref/scratch.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/scratch.rs) reference implementation. -/// * See [crate::api::TakeMatZnx] for corresponding public API. -/// # Safety [crate::doc::backend_safety] for safety contract. -pub unsafe trait TakeMatZnxImpl { - fn take_mat_znx_impl( - scratch: &mut Scratch, - n: usize, - rows: usize, - cols_in: usize, - cols_out: usize, - size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Scratch); -} diff --git a/poulpy-hal/src/oep/svp_ppol.rs b/poulpy-hal/src/oep/svp_ppol.rs index 6550b6f..42c50ea 100644 --- a/poulpy-hal/src/oep/svp_ppol.rs +++ b/poulpy-hal/src/oep/svp_ppol.rs @@ -23,7 +23,7 @@ pub unsafe trait SvpPPolAllocImpl { /// * See [crate::api::SvpPPolAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocBytesImpl { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize; + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/oep/vec_znx_big.rs b/poulpy-hal/src/oep/vec_znx_big.rs index b2bd4c8..4c12e6a 100644 --- a/poulpy-hal/src/oep/vec_znx_big.rs +++ b/poulpy-hal/src/oep/vec_znx_big.rs @@ -35,7 +35,7 @@ pub unsafe trait VecZnxBigFromBytesImpl { /// * See [crate::api::VecZnxBigAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocBytesImpl { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } #[allow(clippy::too_many_arguments)] diff --git a/poulpy-hal/src/oep/vec_znx_dft.rs b/poulpy-hal/src/oep/vec_znx_dft.rs index e5a2bcb..0f9288b 100644 --- a/poulpy-hal/src/oep/vec_znx_dft.rs +++ b/poulpy-hal/src/oep/vec_znx_dft.rs @@ -42,7 +42,7 @@ pub unsafe trait VecZnxDftApplyImpl { /// * See [crate::api::VecZnxDftAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocBytesImpl { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/oep/vmp_pmat.rs b/poulpy-hal/src/oep/vmp_pmat.rs index e399f00..bdca416 100644 --- a/poulpy-hal/src/oep/vmp_pmat.rs +++ b/poulpy-hal/src/oep/vmp_pmat.rs @@ -15,7 +15,7 @@ pub unsafe trait VmpPMatAllocImpl { /// * See [crate::api::VmpPMatAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocBytesImpl { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) diff --git a/poulpy-hal/src/reference/fft64/vmp.rs b/poulpy-hal/src/reference/fft64/vmp.rs index f6fb73c..07e1a8d 100644 --- a/poulpy-hal/src/reference/fft64/vmp.rs +++ b/poulpy-hal/src/reference/fft64/vmp.rs @@ -140,7 +140,7 @@ where assert!(a.cols() <= cols); } - let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_bytes_of_impl(n, cols, size)); let mut a_dft: VecZnxDft<&mut [u8], BE> = VecZnxDft::from_data(cast_mut(data), n, cols, size); diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 47c848e..c2ce4ad 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,16 +3,16 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, LWECiphertext, - LWECiphertextLayout, LWESecret, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSW, GGSWLayout, GLWESecret, LWE, LWELayout, LWESecret, TensorKeyLayout, + prepared::PrepareAlloc, }; use poulpy_hal::{ api::{ ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, - SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, + SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, + VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, + VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, @@ -42,7 +42,7 @@ where + VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -55,7 +55,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -70,7 +70,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -80,7 +80,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -113,8 +113,8 @@ where extension_factor: usize, k_pt: usize, block_size: usize, - lwe_infos: LWECiphertextLayout, - ggsw_infos: GGSWCiphertextLayout, + lwe_infos: LWELayout, + ggsw_infos: GGSWLayout, cbt_infos: CircuitBootstrappingKeyLayout, } @@ -124,7 +124,7 @@ where + VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -137,7 +137,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -152,7 +152,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -162,7 +162,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -188,8 +188,8 @@ where // Scratch space (4MB) let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let n_glwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_glwe(); - let n_lwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_lwe(); + let n_glwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_glwe(); + let n_lwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_lwe(); let rank: poulpy_core::layouts::Rank = params.cbt_infos.layout_brk.rank; let module: Module = Module::::new(n_glwe.as_u32() as u64); @@ -202,10 +202,10 @@ where sk_lwe.fill_binary_block(params.block_size, &mut source_xs); sk_lwe.fill_zero(); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe, rank); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let ct_lwe: LWECiphertext> = LWECiphertext::alloc(¶ms.lwe_infos); + let ct_lwe: LWE> = LWE::alloc_from_infos(¶ms.lwe_infos); // Circuit bootstrapping evaluation key let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( @@ -218,7 +218,7 @@ where scratch.borrow(), ); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(¶ms.ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(¶ms.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || { @@ -238,13 +238,13 @@ where name: String::from("1-bit"), extension_factor: 1, k_pt: 1, - lwe_infos: LWECiphertextLayout { + lwe_infos: LWELayout { n: 574_u32.into(), k: 13_u32.into(), base2k: 13_u32.into(), }, block_size: 7, - ggsw_infos: GGSWCiphertextLayout { + ggsw_infos: GGSWLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 26_u32.into(), @@ -261,7 +261,7 @@ where dnum: 3_u32.into(), rank: 2_u32.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -269,7 +269,7 @@ where dsize: Dsize(1), rank: 2_u32.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 4fec699..3383114 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,9 +1,9 @@ use poulpy_core::{ GLWEOperations, layouts::{ - GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, - GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, + LWEPlaintext, LWESecret, TensorKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; use std::time::Instant; @@ -89,7 +89,7 @@ fn main() { dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_trace.into(), @@ -97,7 +97,7 @@ fn main() { dsize: 1_u32.into(), rank: rank.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -107,7 +107,7 @@ fn main() { }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), @@ -116,7 +116,7 @@ fn main() { rank: rank.into(), }; - let lwe_infos = LWECiphertextLayout { + let lwe_infos = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), @@ -140,7 +140,7 @@ fn main() { sk_lwe.fill_zero(); // GLWE secret - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); // sk_glwe.fill_zero(); @@ -151,7 +151,7 @@ fn main() { let data: i64 = 1 % (1 << k_lwe_pt); // LWE plaintext - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); // LWE plaintext(data * 2^{- (k_lwe_pt - 1)}) pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); // +1 for padding bit @@ -167,7 +167,7 @@ fn main() { println!("pt_lwe: {pt_lwe}"); // LWE ciphertext - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); // Encrypt LWE Plaintext ct_lwe.encrypt_sk(&module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); @@ -187,7 +187,7 @@ fn main() { println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = @@ -214,7 +214,7 @@ fn main() { // Tests RLWE(1) * GGSW(data) - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: (k_ggsw_res - base2k).into(), @@ -222,11 +222,11 @@ fn main() { }; // GLWE ciphertext modulus - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_infos); // Some GLWE plaintext with signed data let k_glwe_pt: usize = 3; - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut data_vec: Vec = vec![0i64; n_glwe]; data_vec .iter_mut() @@ -249,13 +249,13 @@ fn main() { ); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWCiphertextPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let res_prepared: GGSWPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); // Apply GLWE x GGSW ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); // Decrypt - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); ct_glwe.decrypt(&module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); println!("pt_res: {:?}", &pt_res.data.at(0, 0)[..64]); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs index 938c45e..ba0d38f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs @@ -1,5 +1,5 @@ use itertools::Itertools; -use poulpy_core::layouts::prepared::GGSWCiphertextPreparedToRef; +use poulpy_core::layouts::prepared::GGSWPreparedToRef; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use crate::tfhe::bdd_arithmetic::{ @@ -60,15 +60,11 @@ pub fn eval_bdd_2w_to_1w> = a + let inputs: Vec<&dyn GGSWPreparedToRef> = a .blocks .iter() - .map(|x| x as &dyn GGSWCiphertextPreparedToRef) - .chain( - b.blocks - .iter() - .map(|x| x as &dyn GGSWCiphertextPreparedToRef), - ) + .map(|x| x as &dyn GGSWPreparedToRef) + .chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef)) .collect_vec(); // Evaluates out[i] = circuit[i](a, b) diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 145ce6b..de4e30d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -1,9 +1,9 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{Base2K, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; +use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; -use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared}; -use poulpy_hal::api::VecZnxBigAllocBytes; +use poulpy_core::{TakeGLWEPlaintext, layouts::prepared::GLWESecretPrepared}; +use poulpy_hal::api::VecZnxBigBytesOf; #[cfg(test)] use poulpy_hal::api::{ ScratchAvailable, TakeVecZnx, VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, @@ -12,8 +12,8 @@ use poulpy_hal::api::{ use poulpy_hal::source::Source; use poulpy_hal::{ api::{ - TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, - VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; @@ -24,7 +24,7 @@ use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocks { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -38,7 +38,7 @@ impl LWEInfos for FheUintBlocks { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } @@ -62,7 +62,7 @@ impl FheUintBlocks, T> { pub(crate) fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GLWECiphertext::alloc_with(module.n().into(), base2k, k, rank)) + .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) .collect(), _base: 1, _phantom: PhantomData, @@ -83,7 +83,7 @@ impl FheUintBlocks { scratch: &mut Scratch, ) where S: DataRef, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -96,7 +96,7 @@ impl FheUintBlocks { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPt, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, { use poulpy_core::layouts::GLWEPlaintextLayout; @@ -136,7 +136,7 @@ impl FheUintBlocks { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { @@ -175,8 +175,8 @@ impl FheUintBlocks { scratch: &mut Scratch, ) -> Vec where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -186,7 +186,7 @@ impl FheUintBlocks { + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPt + TakeVecZnxDft + TakeVecZnxBig, + Scratch: TakeGLWEPlaintext + TakeVecZnxDft + TakeVecZnxBig, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 3cd50a6..2f4bc44 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -1,12 +1,10 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{ - Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWCiphertextPrepared, -}; +use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}; #[cfg(test)] use poulpy_core::{ TakeGGSW, - layouts::{GGSWCiphertext, prepared::GLWESecretPrepared}, + layouts::{GGSW, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ api::VmpPMatAlloc, @@ -16,8 +14,8 @@ use poulpy_hal::{ use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, + VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPrepare, }, @@ -29,7 +27,7 @@ use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, Unsigne #[cfg(test)] pub(crate) struct FheUintBlocksPrepDebug { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -62,7 +60,7 @@ impl FheUintBlocksPrepDebug, T> { ) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertext::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) + .map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize)) .collect(), _base: 1, _phantom: PhantomData, @@ -72,7 +70,7 @@ impl FheUintBlocksPrepDebug, T> { /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocksPrep { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -103,7 +101,7 @@ where { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertextPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) + .map(|_| GGSWPrepared::alloc(module, base2k, k, dnum, dsize, rank)) .collect(), _base: 1, _phantom: PhantomData, @@ -125,7 +123,7 @@ impl FheUintBlocksPrep: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -192,8 +190,8 @@ impl FheUintBlocksPrepDebug { #[allow(dead_code)] pub(crate) fn noise(&self, module: &Module, sk: &GLWESecretPrepared, want: T) where - Module: VecZnxDftAllocBytes - + VecZnxBigAllocBytes + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume @@ -227,7 +225,7 @@ impl LWEInfos for FheUintBlocksPrep< self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } @@ -258,7 +256,7 @@ impl LWEInfos for FheUintBlocksPrepDebug { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index cc754bc..38ea57d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -1,9 +1,9 @@ use itertools::Itertools; use poulpy_core::{ - GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, + GLWEOperations, TakeGLWEPlaintext, TakeGLWESlice, glwe_packing, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared}, + GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; use poulpy_hal::{ @@ -11,7 +11,7 @@ use poulpy_hal::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -24,22 +24,22 @@ use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// A FHE ciphertext encrypting a [UnsignedInteger]. -pub struct FheUintWord(pub(crate) GLWECiphertext, pub(crate) PhantomData); +pub struct FheUintWord(pub(crate) GLWE, pub(crate) PhantomData); impl FheUintWord { #[allow(dead_code)] fn post_process( &mut self, module: &Module, - mut tmp_res: Vec>, - auto_keys: &HashMap>, + mut tmp_res: Vec>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxAddInplace + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes @@ -62,12 +62,12 @@ impl FheUintWord { + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotate, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECtSlice, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWESlice, { // Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; - let mut cts: HashMap> = HashMap::new(); + let mut cts: HashMap> = HashMap::new(); for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); } @@ -87,7 +87,7 @@ impl LWEInfos for FheUintWord { self.0.k() } - fn n(&self) -> poulpy_core::layouts::Degree { + fn n(&self) -> poulpy_core::layouts::RingDegree { self.0.n() } } @@ -109,7 +109,7 @@ impl FheUintWord { scratch: &mut Scratch, ) where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -122,7 +122,7 @@ impl FheUintWord { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPt, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { @@ -167,7 +167,7 @@ impl FheUintWord { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 98e1083..245fe03 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -1,9 +1,9 @@ use itertools::Itertools; use poulpy_core::{ - GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, + GLWEExternalProductInplace, GLWEOperations, TakeGLWESlice, layouts::{ - GLWECiphertext, GLWECiphertextToMut, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + GLWE, GLWEToMut, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; use poulpy_hal::{ @@ -38,8 +38,8 @@ where fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], - inputs: &[&dyn GGSWCiphertextPreparedToRef], + out: &mut [GLWE], + inputs: &[&dyn GGSWPreparedToRef], scratch: &mut Scratch, ) where O: DataMut; @@ -49,13 +49,13 @@ impl Circuit where Self: GetBitCircuitInfo, Module: Cmux + VecZnxCopy, - Scratch: TakeGLWECtSlice, + Scratch: TakeGLWESlice, { fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], - inputs: &[&dyn GGSWCiphertextPreparedToRef], + out: &mut [GLWE], + inputs: &[&dyn GGSWPreparedToRef], scratch: &mut Scratch, ) where O: DataMut, @@ -159,14 +159,8 @@ impl Node { } pub trait Cmux { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, @@ -177,14 +171,8 @@ impl Cmux for Module where Module: GLWEExternalProductInplace + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index febabed..fdcf5b3 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -9,18 +9,18 @@ use crate::tfhe::{ }, }; use poulpy_core::{ - TakeGGSW, TakeGLWECt, + TakeGGSW, TakeGLWE, layouts::{ - GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, }; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPrepare, }, @@ -56,7 +56,7 @@ where BRA: BlindRotationAlgo, { cbt: CircuitBootstrappingKey, - ks: GLWEToLWEKey, + ks: GLWEToLWESwitchingKey, } impl BDDKey, Vec, BRA> { @@ -77,7 +77,7 @@ impl BDDKey, Vec, BRA> { Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -92,13 +92,13 @@ impl BDDKey, Vec, BRA> { + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAutomorphism + VecZnxAutomorphismInplace, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, { - let mut ks: GLWEToLWEKey> = GLWEToLWEKey::alloc(&infos.ks_infos()); + let mut ks: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos()); ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); Self { @@ -131,7 +131,7 @@ impl PrepareAll for BDDKey where CircuitBootstrappingKey: PrepareAlloc>, - GLWEToLWEKey: PrepareAlloc>, + GLWEToLWESwitchingKey: PrepareAlloc>, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BDDKeyPrepared { BDDKeyPrepared { @@ -157,7 +157,7 @@ where BE: Backend, Module: VmpPrepare + VecZnxRotate - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -168,7 +168,7 @@ where + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx + TakeGGSW, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, { fn prepare( @@ -182,7 +182,7 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out); for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch_1); @@ -206,7 +206,7 @@ where BE: Backend, Module: VmpPrepare + VecZnxRotate - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -217,7 +217,7 @@ where + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWECt + TakeVecZnx + TakeGGSW, + Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, { fn prepare( @@ -231,7 +231,7 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch); self.cbt diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 6b56f79..924d53a 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,7 +1,7 @@ #[cfg(test)] use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, GLWECiphertextLayout, - GLWEToLWEKeyLayout, Rank, TorusPrecision, + AutomorphismKeyLayout, Base2K, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, RingDegree, TensorKeyLayout, + TorusPrecision, }; #[cfg(test)] @@ -25,16 +25,16 @@ pub(crate) const TEST_BLOCK_SIZE: u32 = 7; pub(crate) const TEST_RANK: u32 = 2; #[cfg(test)] -pub(crate) static TEST_GLWE_INFOS: GLWECiphertextLayout = GLWECiphertextLayout { - n: Degree(TEST_N_GLWE), +pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GLWE), rank: Rank(TEST_RANK), }; #[cfg(test)] -pub(crate) static TEST_GGSW_INFOS: GGSWCiphertextLayout = GGSWCiphertextLayout { - n: Degree(TEST_N_GLWE), +pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GGSW), rank: Rank(TEST_RANK), @@ -46,23 +46,23 @@ pub(crate) static TEST_GGSW_INFOS: GGSWCiphertextLayout = GGSWCiphertextLayout { pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { cbt: CircuitBootstrappingKeyLayout { layout_brk: BlindRotationKeyLayout { - n_glwe: Degree(TEST_N_GLWE), - n_lwe: Degree(TEST_N_LWE), + n_glwe: RingDegree(TEST_N_GLWE), + n_lwe: RingDegree(TEST_N_LWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), dnum: Dnum(3), rank: Rank(TEST_RANK), }, - layout_atk: GGLWEAutomorphismKeyLayout { - n: Degree(TEST_N_GLWE), + layout_atk: AutomorphismKeyLayout { + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), dnum: Dnum(3), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { - n: Degree(TEST_N_GLWE), + layout_tsk: TensorKeyLayout { + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), @@ -71,7 +71,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { }, }, ks: GLWEToLWEKeyLayout { - n: Degree(TEST_N_GLWE), + n: RingDegree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(39), rank_in: Rank(TEST_RANK), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 1889e02..1c2e69d 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs @@ -2,20 +2,20 @@ use std::time::Instant; use poulpy_backend::FFT64Ref; use poulpy_core::{ - TakeGGSW, TakeGLWEPt, + TakeGGSW, TakeGLWEPlaintext, layouts::{ - GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret, + GGSWLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; use poulpy_hal::{ api::{ ModuleNew, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, - SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, + SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, @@ -51,7 +51,7 @@ where Module: ModuleNew + SvpPPolAlloc + SvpPrepare + VmpPMatAlloc, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -68,16 +68,16 @@ where Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx + TakeSlice, Module: VecZnxCopy + VecZnxNegateInplace + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd, Module: VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, + Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, Module: VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAlloc + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -85,7 +85,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -107,8 +107,8 @@ where BlindRotationKeyPrepared, BRA, BE>: BlincRotationExecute, BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { - let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS; - let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS; + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; + let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; let n_glwe: usize = glwe_infos.n().into(); @@ -120,7 +120,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prep: GLWESecretPrepared, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 03f36e9..8bf0a9e 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -1,9 +1,9 @@ use itertools::izip; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpPPolAllocBytes, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, - TakeVecZnxSlice, VecZnxAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftAllocBytes, VecZnxDftApply, + ScratchAvailable, SvpApplyDftToDft, SvpPPolBytesOf, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, + TakeVecZnxSlice, VecZnxAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -12,8 +12,8 @@ use poulpy_hal::{ }; use poulpy_core::{ - Distribution, GLWEOperations, TakeGLWECt, - layouts::{GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWEInfos, LWECiphertext, LWECiphertextToRef, LWEInfos}, + Distribution, GLWEOperations, TakeGLWE, + layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWEInfos, LWEToRef}, }; use crate::tfhe::blind_rotation::{ @@ -21,7 +21,7 @@ use crate::tfhe::blind_rotation::{ }; #[allow(clippy::too_many_arguments)] -pub fn cggi_blind_rotate_scratch_space( +pub fn cggi_blind_rotate_tmp_bytes( module: &Module, block_size: usize, extension_factor: usize, @@ -31,10 +31,10 @@ pub fn cggi_blind_rotate_scratch_space( where OUT: GLWEInfos, GGSW: GGSWInfos, - Module: VecZnxDftAllocBytes + Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpBytes + VecZnxBigNormalizeTmpBytes, { @@ -43,14 +43,14 @@ where if block_size > 1 { let cols: usize = (brk_infos.rank() + 1).into(); let dnum: usize = brk_infos.dnum().into(); - let acc_dft: usize = module.vec_znx_dft_alloc_bytes(cols, dnum) * extension_factor; - let acc_big: usize = module.vec_znx_big_alloc_bytes(1, brk_size); - let vmp_res: usize = module.vec_znx_dft_alloc_bytes(cols, brk_size) * extension_factor; - let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(1, brk_size); + let acc_dft: usize = module.bytes_of_vec_znx_dft(cols, dnum) * extension_factor; + let acc_big: usize = module.bytes_of_vec_znx_big(1, brk_size); + let vmp_res: usize = module.bytes_of_vec_znx_dft(cols, brk_size) * extension_factor; + let vmp_xai: usize = module.bytes_of_vec_znx_dft(1, brk_size); let acc_dft_add: usize = vmp_res; let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) let acc: usize = if extension_factor > 1 { - VecZnx::alloc_bytes(module.n(), cols, glwe_infos.size()) * extension_factor + VecZnx::bytes_of(module.n(), cols, glwe_infos.size()) * extension_factor } else { 0 }; @@ -61,16 +61,15 @@ where + vmp_xai + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes()))) } else { - GLWECiphertext::alloc_bytes(glwe_infos) - + GLWECiphertext::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) + GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_tmp_bytes(module, glwe_infos, brk_infos) } } impl BlincRotationExecute for BlindRotationKeyPrepared where - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -99,8 +98,8 @@ where fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ) { @@ -121,8 +120,8 @@ where fn execute_block_binary_extended( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -130,9 +129,9 @@ fn execute_block_binary_extended( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -179,7 +178,7 @@ fn execute_block_binary_extended( } let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).as_usize()]; // TODO: from scratch space - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = 2 * n_glwe; let two_n_ext: usize = 2 * lut.domain_size(); @@ -288,8 +287,8 @@ fn execute_block_binary_extended( fn execute_block_binary( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -297,9 +296,9 @@ fn execute_block_binary( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -324,8 +323,8 @@ fn execute_block_binary( { let n_glwe: usize = brk.n_glwe().into(); let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = n_glwe << 1; let base2k: usize = brk.base2k().into(); let dnum: usize = brk.dnum().into(); @@ -410,8 +409,8 @@ fn execute_block_binary( fn execute_standard( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -419,9 +418,9 @@ fn execute_standard( DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -480,8 +479,8 @@ fn execute_standard( } let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); mod_switch_2n( 2 * lut.domain_size(), @@ -519,7 +518,7 @@ fn execute_standard( out_mut.normalize_inplace(module, scratch_1); } -pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8]>, rot_dir: LookUpTableRotationDirection) { +pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) { let base2k: usize = lwe.base2k().into(); let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index fbf506b..3114ea1 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -1,9 +1,8 @@ use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VmpPMatAlloc, VmpPrepare, + VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, source::Source, @@ -14,9 +13,9 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - GGSWCiphertext, GGSWInfos, LWESecret, - compressed::GGSWCiphertextCompressed, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + GGSW, GGSWInfos, LWESecret, + compressed::GGSWCompressed, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; @@ -30,9 +29,9 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSWCiphertext::alloc(infos)); + data.push(GGSW::alloc_from_infos(infos)); } Self { @@ -44,19 +43,19 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { } impl BlindRotationKey, CGGI> { - pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_tmp_bytes(module, infos) } } impl BlindRotationKeyEncryptSk for BlindRotationKey where Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -121,8 +120,8 @@ where where A: BlindRotationKeyInfos, { - let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextPrepared::alloc(module, infos))); + let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc_from_infos(module, infos))); Self { data, dist: Distribution::NONE, @@ -137,8 +136,8 @@ impl BlindRotationKeyCompressed, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos))); + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos))); Self { keys: data, dist: Distribution::NONE, @@ -146,12 +145,12 @@ impl BlindRotationKeyCompressed, CGGI> { } } - pub fn generate_from_sk_scratch_space(module: &Module, infos: &A) -> usize + pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, + Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos) + GGSWCompressed::encrypt_sk_tmp_bytes(module, infos) } } @@ -169,7 +168,7 @@ impl BlindRotationKeyCompressed { DataSkGLWE: DataRef, DataSkLWE: DataRef, Module: VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/key.rs index 86dbd21..276539f 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, + Base2K, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, RingDegree, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -19,8 +19,8 @@ use crate::tfhe::blind_rotation::BlindRotationAlgo; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct BlindRotationKeyLayout { - pub n_glwe: Degree, - pub n_lwe: Degree, + pub n_glwe: RingDegree, + pub n_lwe: RingDegree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, @@ -28,11 +28,11 @@ pub struct BlindRotationKeyLayout { } impl BlindRotationKeyInfos for BlindRotationKeyLayout { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n_glwe } - fn n_lwe(&self) -> Degree { + fn n_lwe(&self) -> RingDegree { self.n_lwe } } @@ -62,7 +62,7 @@ impl LWEInfos for BlindRotationKeyLayout { self.k } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.n_glwe } } @@ -71,8 +71,8 @@ pub trait BlindRotationKeyInfos where Self: GGSWInfos, { - fn n_glwe(&self) -> Degree; - fn n_lwe(&self) -> Degree; + fn n_glwe(&self) -> RingDegree; + fn n_lwe(&self) -> RingDegree; } pub trait BlindRotationKeyAlloc { @@ -98,7 +98,7 @@ pub trait BlindRotationKeyEncryptSk { #[derive(Clone)] pub struct BlindRotationKey { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } @@ -178,12 +178,12 @@ impl WriterTo for BlindRotationKey { } impl BlindRotationKeyInfos for BlindRotationKey { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.keys.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.keys.len() as u32) } } @@ -206,7 +206,7 @@ impl LWEInfos for BlindRotationKey { self.keys[0].k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs index 51ff139..af7e40a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -8,14 +8,14 @@ use std::{fmt, marker::PhantomData}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, - layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCiphertextCompressed}, + layouts::{Base2K, Dsize, GGSWInfos, GLWEInfos, LWEInfos, RingDegree, TorusPrecision, compressed::GGSWCompressed}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; #[derive(Clone)] pub struct BlindRotationKeyCompressed { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } @@ -94,17 +94,17 @@ impl WriterTo for BlindRotationKeyCompressed } impl BlindRotationKeyInfos for BlindRotationKeyCompressed { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.keys.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.keys.len() as u32) } } impl LWEInfos for BlindRotationKeyCompressed { - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs index 8719de4..1f99ef5 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -8,8 +8,8 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGSWCiphertextPrepared, Prepare, PrepareAlloc}, + Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::{GGSWPrepared, Prepare, PrepareAlloc}, }, }; @@ -23,19 +23,19 @@ pub trait BlindRotationKeyPreparedAlloc { #[derive(PartialEq, Eq)] pub struct BlindRotationKeyPrepared { - pub(crate) data: Vec>, + pub(crate) data: Vec>, pub(crate) dist: Distribution, pub(crate) x_pow_a: Option, B>>>, pub(crate) _phantom: PhantomData, } impl BlindRotationKeyInfos for BlindRotationKeyPrepared { - fn n_glwe(&self) -> Degree { + fn n_glwe(&self) -> RingDegree { self.n() } - fn n_lwe(&self) -> Degree { - Degree(self.data.len() as u32) + fn n_lwe(&self) -> RingDegree { + RingDegree(self.data.len() as u32) } } @@ -48,7 +48,7 @@ impl LWEInfos for BlindRotationKeyP self.data[0].k() } - fn n(&self) -> Degree { + fn n(&self) -> RingDegree { self.data[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs index bd83a08..8cc262b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs @@ -14,7 +14,7 @@ pub use lut::*; pub mod tests; -use poulpy_core::layouts::{GLWECiphertext, LWECiphertext}; +use poulpy_core::layouts::{GLWE, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; pub trait BlindRotationAlgo {} @@ -27,8 +27,8 @@ pub trait BlincRotationExecute { fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 190adff..4b8131a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftSubInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply, + VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, @@ -19,20 +19,19 @@ use poulpy_hal::{ use crate::tfhe::blind_rotation::{ BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyLayout, - BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n, + BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_tmp_bytes, mod_switch_2n, }; use poulpy_core::layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWECiphertextToRef, - LWEInfos, LWEPlaintext, LWESecret, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, LWEPlaintext, LWESecret, LWEToRef, prepared::{GLWESecretPrepared, PrepareAlloc}, }; pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) where - Module: VecZnxBigAllocBytes - + VecZnxDftAllocBytes - + SvpPPolAllocBytes + Module: VecZnxBigBytesOf + + VecZnxDftBytesOf + + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes @@ -111,31 +110,31 @@ where rank: rank.into(), }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: k_res.into(), rank: rank.into(), }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe.into(), base2k: base2k.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_tmp_bytes( module, &brk_infos, )); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_dft: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_scratch_space( + let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_tmp_bytes( module, block_size, extension_factor, @@ -154,9 +153,9 @@ where scratch.borrow(), ); - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); let x: i64 = 15 % (message_modulus as i64); @@ -175,13 +174,13 @@ where let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); lut.set(module, &f_vec, log_message_modulus + 1); - let mut res: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let brk_prepared: BlindRotationKeyPrepared, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 45b9717..1799127 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -3,9 +3,9 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ ScratchAvailable, TakeMatZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, - VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, @@ -15,12 +15,12 @@ use poulpy_hal::{ }; use poulpy_core::{ - GLWEOperations, TakeGGLWE, TakeGLWECt, - layouts::{Dsize, GGLWECiphertextLayout, GGSWInfos, GLWEInfos, LWEInfos}, + GLWEOperations, TakeGGLWE, TakeGLWE, + layouts::{Dsize, GGLWELayout, GGSWInfos, GLWEInfos, LWEInfos}, }; use poulpy_core::glwe_packing; -use poulpy_core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -44,7 +44,7 @@ where + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -56,7 +56,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + VecZnxNormalize, @@ -74,8 +74,8 @@ where fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, + res: &mut GGSW, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -97,8 +97,8 @@ where &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, + res: &mut GGSW, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -122,8 +122,8 @@ pub fn circuit_bootstrap_core( to_exponent: bool, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, + res: &mut GGSW, + lwe: &LWE, log_domain: usize, extension_factor: usize, key: &CircuitBootstrappingKeyPrepared, @@ -145,7 +145,7 @@ pub fn circuit_bootstrap_core( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -156,7 +156,7 @@ pub fn circuit_bootstrap_core( + VecZnxBigNormalize + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotateInplaceTmpBytes + VecZnxRotate @@ -214,7 +214,7 @@ pub fn circuit_bootstrap_core( // TODO: separate GGSW k from output of blind rotation k let (mut res_glwe, scratch_1) = scratch.take_glwe_ct(res); - let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout { + let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -233,7 +233,7 @@ pub fn circuit_bootstrap_core( let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _; (0..dnum).for_each(|i| { - let mut tmp_glwe: GLWECiphertext<&mut [u8]> = tmp_gglwe.at_mut(i, 0); + let mut tmp_glwe: GLWE<&mut [u8]> = tmp_gglwe.at_mut(i, 0); if to_exponent { // Isolates i-th LUT and moves coefficients according to requested gap. @@ -263,12 +263,12 @@ pub fn circuit_bootstrap_core( #[allow(clippy::too_many_arguments)] fn post_process( module: &Module, - res: &mut GLWECiphertext, - a: &GLWECiphertext, + res: &mut GLWE, + a: &GLWE, 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, @@ -286,7 +286,7 @@ fn post_process( + VecZnxNegateInplace + VecZnxCopy + VecZnxSubInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VmpApplyDftToDft @@ -303,7 +303,7 @@ fn post_process( { let log_n: usize = module.log_n(); - let mut cts: HashMap>> = HashMap::new(); + let mut cts: HashMap>> = HashMap::new(); // First partial trace, vanishes all coefficients which are not multiples of gap_in // [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0] @@ -322,7 +322,7 @@ fn post_process( let steps: usize = 1 << log_domain; // TODO: from Scratch - let mut cts_vec: Vec>> = Vec::new(); + let mut cts_vec: Vec>> = Vec::new(); for i in 0..steps { if i != 0 { @@ -336,7 +336,7 @@ fn post_process( } glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); - let packed: &mut GLWECiphertext> = cts.remove(&0).unwrap(); + let packed: &mut GLWE> = cts.remove(&0).unwrap(); res.trace( module, log_n - log_gap_out, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 427cf75..1e52a76 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,15 +1,15 @@ use poulpy_core::layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GGLWETensorKey, GGLWETensorKeyLayout, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, + TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, }, @@ -24,19 +24,19 @@ use crate::tfhe::blind_rotation::{ pub trait CircuitBootstrappingKeyInfos { fn brk_infos(&self) -> BlindRotationKeyLayout; - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout; - fn tsk_infos(&self) -> GGLWETensorKeyLayout; + fn atk_infos(&self) -> AutomorphismKeyLayout; + fn tsk_infos(&self) -> TensorKeyLayout; } #[derive(Debug, Clone, Copy)] pub struct CircuitBootstrappingKeyLayout { pub layout_brk: BlindRotationKeyLayout, - pub layout_atk: GGLWEAutomorphismKeyLayout, - pub layout_tsk: GGLWETensorKeyLayout, + pub layout_atk: AutomorphismKeyLayout, + pub layout_tsk: TensorKeyLayout, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { self.layout_atk } @@ -44,7 +44,7 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { self.layout_brk } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { self.layout_tsk } } @@ -68,8 +68,8 @@ pub trait CircuitBootstrappingKeyEncryptSk { pub struct CircuitBootstrappingKey { pub(crate) brk: BlindRotationKey, - pub(crate) tsk: GGLWETensorKey>, - pub(crate) atk: HashMap>>, + pub(crate) tsk: TensorKey>, + pub(crate) atk: HashMap>>, } impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> @@ -78,7 +78,7 @@ where Module: SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -93,7 +93,7 @@ where + VecZnxSub + SvpPrepare + VecZnxSwitchRing - + SvpPPolAllocBytes + + SvpPPolBytesOf + SvpPPolAlloc + VecZnxAutomorphism, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, @@ -117,14 +117,14 @@ where assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); - let atk_infos: GGLWEAutomorphismKeyLayout = cbt_infos.atk_infos(); + let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: GGLWETensorKeyLayout = cbt_infos.tsk_infos(); + let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); - let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); + let mut auto_keys: HashMap>> = HashMap::new(); + let gal_els: Vec = GLWE::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); auto_keys.insert(*gal_el, key); }); @@ -141,7 +141,7 @@ where scratch, ); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&trk_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self { @@ -154,14 +154,14 @@ where pub struct CircuitBootstrappingKeyPrepared { pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: GGLWETensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, + pub(crate) tsk: TensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { let (_, atk) = self.atk.iter().next().expect("atk is empty"); - GGLWEAutomorphismKeyLayout { + AutomorphismKeyLayout { n: atk.n(), base2k: atk.base2k(), k: atk.k(), @@ -182,8 +182,8 @@ impl CircuitBootstrappingKeyInfo } } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { - GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { + TensorKeyLayout { n: self.tsk.n(), base2k: self.tsk.base2k(), k: self.tsk.k(), @@ -199,13 +199,13 @@ impl PrepareAlloc: VmpPMatAlloc + VmpPrepare, BlindRotationKey: PrepareAlloc, BRA, B>>, - GGLWETensorKey: PrepareAlloc, B>>, - GGLWEAutomorphismKey: PrepareAlloc, B>>, + TensorKey: 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: GGLWETensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap, B>> = HashMap::new(); + let tsk: TensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); + let mut atk: HashMap, B>> = HashMap::new(); for (key, value) in &self.atk { atk.insert(*key, value.prepare_alloc(module, scratch)); } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index 86dcf5e..5835765 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -5,7 +5,7 @@ pub mod tests; pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; @@ -13,8 +13,8 @@ pub trait CirtuitBootstrappingExecute { fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, + res: &mut GGSW, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -25,8 +25,8 @@ pub trait CirtuitBootstrappingExecute { &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, - lwe: &LWECiphertext, + res: &mut GGSW, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 40f5448..cfe238f 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -2,11 +2,11 @@ use std::time::Instant; use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, + ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, + VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, @@ -31,13 +31,11 @@ use crate::tfhe::{ }, }; -use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, LWECiphertextLayout, prepared::PrepareAlloc, -}; +use poulpy_core::layouts::{AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc}; use poulpy_core::layouts::{ - GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared}, }; pub fn test_circuit_bootstrapping_to_exponent(module: &Module) @@ -45,7 +43,7 @@ where Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -58,7 +56,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -73,7 +71,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRshInplace @@ -83,7 +81,7 @@ where + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotateInplaceTmpBytes - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -128,7 +126,7 @@ where let k_ggsw_res: usize = 4 * base2k; let rows_ggsw_res: usize = 2; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), @@ -143,7 +141,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -151,7 +149,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -161,7 +159,7 @@ where }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), @@ -179,19 +177,19 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); @@ -206,7 +204,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; @@ -236,8 +234,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - 2); ct_glwe.encrypt_sk( @@ -249,11 +247,11 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. @@ -267,7 +265,7 @@ where Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace - + VecZnxDftAllocBytes + + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace @@ -280,7 +278,7 @@ where + VecZnxAddScalarInplace + VecZnxAutomorphism + VecZnxSwitchRing - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxIdftApplyTmpA + SvpApplyDftToDft + VecZnxBigAddInplace @@ -295,7 +293,7 @@ where + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd - + SvpPPolAllocBytes + + SvpPPolBytesOf + VecZnxRotateInplace + VecZnxBigAutomorphismInplace + VecZnxRotateInplaceTmpBytes @@ -305,7 +303,7 @@ where + VecZnxCopy + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace - + VecZnxBigAllocBytes + + VecZnxBigBytesOf + VecZnxDftAddInplace + VecZnxRotate + ZnFillUniform @@ -350,7 +348,7 @@ where let k_ggsw_res: usize = 4 * base2k; let rows_ggsw_res: usize = 3; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(), @@ -365,7 +363,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -373,7 +371,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -383,7 +381,7 @@ where }, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(), @@ -401,19 +399,19 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); @@ -428,7 +426,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); @@ -449,8 +447,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - k_lwe_pt - 1); ct_glwe.encrypt_sk( @@ -462,11 +460,11 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless. From 3236e1be2c6e2321f3e0b01da733cc454b3d4975 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 16 Oct 2025 10:08:52 +0200 Subject: [PATCH 19/60] automorphism glwe --- poulpy-core/src/automorphism/glwe_ct.rs | 602 +++++++++++------------ poulpy-core/src/keyswitching/gglwe_ct.rs | 6 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 6 +- poulpy-core/src/keyswitching/glwe_ct.rs | 12 +- poulpy-core/src/keyswitching/lwe_ct.rs | 4 +- poulpy-core/src/keyswitching/mod.rs | 5 + poulpy-core/src/lib.rs | 1 + 7 files changed, 313 insertions(+), 323 deletions(-) diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 0c8b581..d989891 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -1,345 +1,331 @@ use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallInplace, VecZnxBigSubSmallNegateInplace, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, + ScratchTakeBasic, VecZnxAutomorphismInplace, VecZnxBigAutomorphismInplace, VecZnxBigSubSmallInplace, + VecZnxBigSubSmallNegateInplace, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, + layouts::{Backend, DataMut, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; +use crate::{ + GLWEKeyswitch, ScratchTakeCore, keyswitch_internal, + layouts::{ + GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + }, +}; impl GLWE> { - pub fn automorphism_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize + pub fn automorphism_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + M: GLWEAutomorphism, { - Self::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) - } - - pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize - where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - Self::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) + module.glwe_automorphism_tmp_bytes(res_infos, a_infos, key_infos) } } impl GLWE { - pub fn automorphism( - &mut self, - module: &Module, - lhs: &GLWE, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - self.keyswitch(module, lhs, &rhs.key, scratch); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_automorphism_inplace(rhs.p(), &mut self.data, i, scratch); - }) + module.glwe_automorphism(self, a, key, scratch); } - pub fn automorphism_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn automorphism_add(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - self.keyswitch_inplace(module, &rhs.key, scratch); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_automorphism_inplace(rhs.p(), &mut self.data, i, scratch); - }) + module.glwe_automorphism_add(self, a, key, scratch); } - pub fn automorphism_add( - &mut self, - module: &Module, - lhs: &GLWE, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_sub(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch(module, lhs, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = lhs.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_add_small_inplace(&mut res_big, i, &lhs.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_sub(self, a, key, scratch); } - pub fn automorphism_add_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn glwe_automorphism_sub_negate(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch_inplace(module, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = self.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_add_small_inplace(&mut res_big, i, &self.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_sub_negate(self, a, key, scratch); } - pub fn automorphism_sub_ab( - &mut self, - module: &Module, - lhs: &GLWE, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch(module, lhs, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = lhs.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_sub_small_inplace(&mut res_big, i, &lhs.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_inplace(self, key, scratch); } - pub fn automorphism_sub_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_add_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch_inplace(module, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = self.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_sub_small_inplace(&mut res_big, i, &self.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_add_inplace(self, key, scratch); } - pub fn automorphism_sub_negate( - &mut self, - module: &Module, - lhs: &GLWE, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_sub_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch(module, lhs, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = lhs.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_sub_small_negate_inplace(&mut res_big, i, &lhs.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_sub_inplace(self, key, scratch); } - pub fn automorphism_sub_negate_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_sub_negate_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) + where + M: GLWEAutomorphism, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - self.assert_keyswitch_inplace(module, &rhs.key, scratch); - } - let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self.n().into(), (self.rank() + 1).into(), rhs.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, B> = self.keyswitch_internal(module, res_dft, &rhs.key, scratch_1); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_big_automorphism_inplace(rhs.p(), &mut res_big, i, scratch_1); - module.vec_znx_big_sub_small_negate_inplace(&mut res_big, i, &self.data, i); - module.vec_znx_big_normalize( - self.base2k().into(), - &mut self.data, - i, - rhs.base2k().into(), - &res_big, - i, - scratch_1, - ); - }) + module.glwe_automorphism_sub_negate_inplace(self, key, scratch); } } + +pub trait GLWEAutomorphism +where + Self: GLWEKeyswitch + + VecZnxAutomorphismInplace + + VecZnxBigAutomorphismInplace + + VecZnxBigSubSmallInplace + + VecZnxBigSubSmallNegateInplace, +{ + fn glwe_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GLWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + { + self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } + + fn glwe_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + self.glwe_keyswitch(res, a, &key.to_ref().key, scratch); + + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_automorphism_inplace(key.p(), res.data_mut(), i, scratch); + } + } + + fn glwe_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + self.glwe_keyswitch_inplace(res, &key.to_ref().key, scratch); + + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_automorphism_inplace(key.p(), res.data_mut(), i, scratch); + } + } + + fn glwe_automorphism_add(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_add_small_inplace(&mut res_big, i, a.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } + + fn glwe_automorphism_add_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_add_small_inplace(&mut res_big, i, res.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } + + fn glwe_automorphism_sub(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_sub_small_inplace(&mut res_big, i, a.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } + + fn glwe_automorphism_sub_negate(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_sub_small_negate_inplace(&mut res_big, i, a.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } + + fn glwe_automorphism_sub_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_sub_small_inplace(&mut res_big, i, res.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } + + fn glwe_automorphism_sub_negate_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + + let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); + self.vec_znx_big_sub_small_negate_inplace(&mut res_big, i, res.data(), i); + self.vec_znx_big_normalize( + res.base2k().into(), + res.data_mut(), + i, + key.base2k().into(), + &res_big, + i, + scratch_1, + ); + } + } +} + +impl GLWEAutomorphism for Module where + Self: GLWEKeyswitch + + VecZnxAutomorphismInplace + + VecZnxBigAutomorphismInplace + + VecZnxBigSubSmallInplace + + VecZnxBigSubSmallNegateInplace +{ +} diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index edda267..363f394 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeySwitch, + keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey, GLWESwitchingKeyToRef, @@ -109,11 +109,11 @@ impl GGLWE { } } -impl GGLWEKeySwitch for Module where Self: GLWEKeySwitch {} +impl GGLWEKeySwitch for Module where Self: GLWEKeyswitch {} pub trait GGLWEKeySwitch where - Self: GLWEKeySwitch, + Self: GLWEKeyswitch, { fn gglwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index cfb4d8e..c864975 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -2,7 +2,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeySwitch, + keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, @@ -53,7 +53,7 @@ impl GGSW { pub trait GGSWKeySwitch where - Self: GLWEKeySwitch + GGSWExpandRows, + Self: GLWEKeyswitch + GGSWExpandRows, { fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize where @@ -127,5 +127,3 @@ where self.ggsw_expand_row(res, tsk, scratch); } } - -impl GGSW {} diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index 6d7bff9..de48cbf 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -21,7 +21,7 @@ impl GLWE> { R: GLWEInfos, A: GLWEInfos, B: GGLWEInfos, - M: GLWEKeySwitch, + M: GLWEKeyswitch, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) } @@ -32,7 +32,7 @@ impl GLWE { where A: GLWEToRef, B: GLWESwitchingKeyPreparedToRef, - M: GLWEKeySwitch, + M: GLWEKeyswitch, Scratch: ScratchTakeCore, { module.glwe_keyswitch(self, a, b, scratch); @@ -41,14 +41,14 @@ impl GLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where A: GLWESwitchingKeyPreparedToRef, - M: GLWEKeySwitch, + M: GLWEKeyswitch, Scratch: ScratchTakeCore, { module.glwe_keyswitch_inplace(self, a, scratch); } } -impl GLWEKeySwitch for Module where +impl GLWEKeyswitch for Module where Self: Sized + ModuleN + VecZnxDftBytesOf @@ -69,7 +69,7 @@ impl GLWEKeySwitch for Module where { } -pub trait GLWEKeySwitch +pub trait GLWEKeyswitch where Self: Sized + ModuleN @@ -239,7 +239,7 @@ impl GLWE> {} impl GLWE {} -fn keyswitch_internal( +pub(crate) fn keyswitch_internal( module: &M, mut res: VecZnxDft, a: &GLWE, diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 8546ccb..ff9fb5f 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - keyswitching::glwe_ct::GLWEKeySwitch, + keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, @@ -40,7 +40,7 @@ impl LWEKeySwitch for Module where Self: LWEKeySwitch { pub trait LWEKeySwitch where - Self: GLWEKeySwitch + GLWEAlloc, + Self: GLWEKeyswitch + GLWEAlloc, { fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where diff --git a/poulpy-core/src/keyswitching/mod.rs b/poulpy-core/src/keyswitching/mod.rs index c6a3610..b23fd47 100644 --- a/poulpy-core/src/keyswitching/mod.rs +++ b/poulpy-core/src/keyswitching/mod.rs @@ -2,3 +2,8 @@ mod gglwe_ct; mod ggsw_ct; mod glwe_ct; mod lwe_ct; + +pub use gglwe_ct::*; +pub use gglwe_ct::*; +pub use glwe_ct::*; +pub use lwe_ct::*; diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 15e6c76..ffa36a8 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -18,6 +18,7 @@ pub use conversion::*; pub use dist::*; pub use external_product::*; pub use glwe_packing::*; +pub use keyswitching::*; pub use encryption::SIGMA; From bdd00b557ff57f1dd6881c70d92f03a9b94ee94d Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 16 Oct 2025 10:52:55 +0200 Subject: [PATCH 20/60] automorphism gglwe --- poulpy-core/src/automorphism/gglwe_atk.rs | 333 ++++++++++------------ poulpy-core/src/keyswitching/gglwe_ct.rs | 22 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 8 +- poulpy-core/src/keyswitching/glwe_ct.rs | 50 ++-- poulpy-core/src/layouts/gglwe_atk.rs | 13 + poulpy-hal/src/layouts/module.rs | 39 ++- 6 files changed, 230 insertions(+), 235 deletions(-) diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 9650aa2..db57e60 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -1,198 +1,169 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, + api::VecZnxAutomorphism, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, }; -use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWE, prepared::AutomorphismKeyPrepared}; +use crate::{ + ScratchTakeCore, + automorphism::glwe_ct::GLWEAutomorphism, + layouts::{ + AutomorphismKey, AutomorphismKeyToMut, AutomorphismKeyToRef, GGLWEInfos, GLWE, GLWEInfos, + prepared::{ + AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement, SetAutomorphismGaloisElement, + }, + }, +}; impl AutomorphismKey> { - pub fn automorphism_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize + pub fn automorphism_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: AutomorphismKeyAutomorphism, { - GLWE::keyswitch_tmp_bytes( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - key_infos, - ) - } - - pub fn automorphism_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize - where - OUT: GGLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, - { - AutomorphismKey::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos) + module.automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) } } impl AutomorphismKey { - pub fn automorphism( - &mut self, - module: &Module, - lhs: &AutomorphismKey, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + where + A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + M: AutomorphismKeyAutomorphism, { - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; - - assert_eq!( - self.rank_in(), - lhs.rank_in(), - "ksk_out input rank: {} != ksk_in input rank: {}", - self.rank_in(), - lhs.rank_in() - ); - assert_eq!( - self.rank_out(), - rhs.rank_in(), - "ksk_in output rank: {} != ksk_apply input rank: {}", - self.rank_out(), - rhs.rank_in() - ); - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - assert!( - self.k() <= lhs.k(), - "output k={} cannot be greater than input k={}", - self.k(), - lhs.k() - ) - } - - let cols_out: usize = (rhs.rank_out() + 1).into(); - - let p: i64 = lhs.p(); - let p_inv: i64 = module.galois_element_inv(p); - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); - let lhs_ct: GLWE<&[u8]> = lhs.at(row_j, col_i); - - // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) - (0..cols_out).for_each(|i| { - module.vec_znx_automorphism(lhs.p(), &mut res_ct.data, i, &lhs_ct.data, i); - }); - - // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - res_ct.keyswitch_inplace(module, &rhs.key, scratch); - - // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) - (0..cols_out).for_each(|i| { - module.vec_znx_automorphism_inplace(p_inv, &mut res_ct.data, i, scratch); - }); - }); - }); - - (self.dnum().min(lhs.dnum()).into()..self.dnum().into()).for_each(|row_i| { - (0..self.rank_in().into()).for_each(|col_j| { - self.at_mut(row_i, col_j).data.zero(); - }); - }); - - self.p = (lhs.p * rhs.p) % (module.cyclotomic_order() as i64); + module.automorphism_key_automorphism(self, a, key, scratch); } - pub fn automorphism_inplace( - &mut self, - module: &Module, - rhs: &AutomorphismKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) + where + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + M: AutomorphismKeyAutomorphism, { - #[cfg(debug_assertions)] - { - assert_eq!( - self.rank_out(), - rhs.rank_in(), - "ksk_in output rank: {} != ksk_apply input rank: {}", - self.rank_out(), - rhs.rank_in() - ); - assert_eq!( - self.rank_out(), - rhs.rank_out(), - "ksk_out output rank: {} != ksk_apply output rank: {}", - self.rank_out(), - rhs.rank_out() - ); - } - - let cols_out: usize = (rhs.rank_out() + 1).into(); - - let p: i64 = self.p(); - let p_inv = module.galois_element_inv(p); - - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); - - // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) - (0..cols_out).for_each(|i| { - module.vec_znx_automorphism_inplace(p_inv, &mut res_ct.data, i, scratch); - }); - - // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - res_ct.keyswitch_inplace(module, &rhs.key, scratch); - - // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) - (0..cols_out).for_each(|i| { - module.vec_znx_automorphism_inplace(p_inv, &mut res_ct.data, i, scratch); - }); - }); - }); - - self.p = (self.p * rhs.p) % (module.cyclotomic_order() as i64); + module.automorphism_key_automorphism_inplace(self, key, scratch); + } +} + +impl AutomorphismKeyAutomorphism for Module where + Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism +{ +} + +pub trait AutomorphismKeyAutomorphism +where + Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism, +{ + fn automorphism_key_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + { + self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } + + fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, + A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + { + let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let a: &AutomorphismKey<&[u8]> = &a.to_ref(); + let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); + + assert!( + res.dnum().as_u32() <= a.dnum().as_u32(), + "res dnum: {} > a dnum: {}", + res.dnum(), + a.dnum() + ); + + assert_eq!( + res.dsize(), + a.dsize(), + "res dnum: {} != a dnum: {}", + res.dsize(), + a.dsize() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + + let p: i64 = a.p(); + let p_inv: i64 = self.galois_element_inv(p); + + for row in 0..res.dnum().as_usize() { + for col in 0..cols_out { + let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); + let a_ct: GLWE<&[u8]> = a.at(row, col); + + // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) + for i in 0..cols_out { + self.vec_znx_automorphism(a.p(), res_tmp.data_mut(), i, &a_ct.data, i); + } + + // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) + self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + + // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) + (0..cols_out).for_each(|i| { + self.vec_znx_automorphism_inplace(p_inv, res_tmp.data_mut(), i, scratch); + }); + } + } + } + + res.set_p((a.p() * key.p()) % (self.cyclotomic_order() as i64)); + } + + fn automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + where + R: AutomorphismKeyToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + { + let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); + + assert_eq!( + res.rank(), + key.rank(), + "key rank: {} != key rank: {}", + res.rank(), + key.rank() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + + let p: i64 = res.p(); + let p_inv: i64 = self.galois_element_inv(p); + + for row in 0..res.dnum().as_usize() { + for col in 0..cols_out { + let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); + + // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) + for i in 0..cols_out { + self.vec_znx_automorphism_inplace(p_inv, res_tmp.data_mut(), i, scratch); + } + + // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) + self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + + // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) + for i in 0..cols_out { + self.vec_znx_automorphism_inplace(p_inv, res_tmp.data_mut(), i, scratch); + } + } + } + } + + res.set_p((res.p() * key.p()) % (self.cyclotomic_order() as i64)); } } diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 363f394..4b92ed1 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -16,7 +16,7 @@ impl AutomorphismKey> { R: GGLWEInfos, A: GGLWEInfos, K: GGLWEInfos, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } @@ -28,7 +28,7 @@ impl AutomorphismKey { A: AutomorphismKeyToRef, B: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); } @@ -37,7 +37,7 @@ impl AutomorphismKey { where A: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); } @@ -49,7 +49,7 @@ impl GLWESwitchingKey> { R: GGLWEInfos, A: GGLWEInfos, K: GGLWEInfos, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } @@ -61,7 +61,7 @@ impl GLWESwitchingKey { A: GLWESwitchingKeyToRef, B: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); } @@ -70,7 +70,7 @@ impl GLWESwitchingKey { where A: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); } @@ -82,7 +82,7 @@ impl GGLWE> { R: GGLWEInfos, A: GGLWEInfos, K: GGLWEInfos, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } @@ -94,7 +94,7 @@ impl GGLWE { A: GGLWEToRef, B: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch(self, a, b, scratch); } @@ -103,15 +103,15 @@ impl GGLWE { where A: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGLWEKeySwitch, + M: GGLWEKeyswitch, { module.gglwe_keyswitch_inplace(self, a, scratch); } } -impl GGLWEKeySwitch for Module where Self: GLWEKeyswitch {} +impl GGLWEKeyswitch for Module where Self: GLWEKeyswitch {} -pub trait GGLWEKeySwitch +pub trait GGLWEKeyswitch where Self: GLWEKeyswitch, { diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index c864975..67f4278 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -22,7 +22,7 @@ impl GGSW> { A: GGSWInfos, K: GGLWEInfos, T: GGLWEInfos, - M: GGSWKeySwitch, + M: GGSWKeyswitch, { module.ggsw_keyswitch_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos) } @@ -35,7 +35,7 @@ impl GGSW { K: GLWESwitchingKeyPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGSWKeySwitch, + M: GGSWKeyswitch, { module.ggsw_keyswitch(self, a, key, tsk, scratch); } @@ -45,13 +45,13 @@ impl GGSW { K: GLWESwitchingKeyPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, - M: GGSWKeySwitch, + M: GGSWKeyswitch, { module.ggsw_keyswitch_inplace(self, key, tsk, scratch); } } -pub trait GGSWKeySwitch +pub trait GGSWKeyswitch where Self: GLWEKeyswitch + GGSWExpandRows, { diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index de48cbf..f82a4d1 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -16,14 +16,14 @@ use crate::{ }; impl GLWE> { - pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &B) -> usize where R: GLWEInfos, A: GLWEInfos, B: GGLWEInfos, M: GLWEKeyswitch, { - module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } @@ -89,7 +89,7 @@ where + VecZnxNormalize + VecZnxNormalizeTmpBytes, { - fn glwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize + fn glwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &B) -> usize where R: GLWEInfos, A: GLWEInfos, @@ -97,44 +97,44 @@ where { let in_size: usize = a_infos .k() - .div_ceil(b_infos.base2k()) - .div_ceil(b_infos.dsize().into()) as usize; + .div_ceil(key_infos.base2k()) + .div_ceil(key_infos.dsize().into()) as usize; let out_size: usize = res_infos.size(); - let ksk_size: usize = b_infos.size(); - let res_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + let ksk_size: usize = key_infos.size(); + let res_dft: usize = self.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = self.bytes_of_vec_znx_dft((key_infos.rank_in()).into(), in_size); let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size, in_size, - (b_infos.rank_in()).into(), - (b_infos.rank_out() + 1).into(), + (key_infos.rank_in()).into(), + (key_infos.rank_out() + 1).into(), ksk_size, - ) + self.bytes_of_vec_znx_dft((b_infos.rank_in()).into(), in_size); + ) + self.bytes_of_vec_znx_dft((key_infos.rank_in()).into(), in_size); let normalize_big: usize = self.vec_znx_big_normalize_tmp_bytes(); - if a_infos.base2k() == b_infos.base2k() { + if a_infos.base2k() == key_infos.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) - } else if b_infos.dsize() == 1 { + } else if key_infos.dsize() == 1 { // In this case, we only need one column, temporary, that we can drop once a_dft is computed. let normalize_conv: usize = VecZnx::bytes_of(self.n(), 1, in_size) + self.vec_znx_normalize_tmp_bytes(); res_dft + (((ai_dft + normalize_conv) | vmp) | normalize_big) } else { // Since we stride over a to get a_dft when dsize > 1, we need to store the full columns of a with in the base conversion. - let normalize_conv: usize = VecZnx::bytes_of(self.n(), (b_infos.rank_in()).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(self.n(), (key_infos.rank_in()).into(), in_size); res_dft + ((ai_dft + normalize_conv + (self.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } } - fn glwe_keyswitch(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch) + fn glwe_keyswitch(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where R: GLWEToMut, A: GLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + K: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); + let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( a.rank(), @@ -181,14 +181,14 @@ where }) } - fn glwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) + fn glwe_keyswitch_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - A: GLWESwitchingKeyPreparedToRef, + K: GLWESwitchingKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); + let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( res.rank(), @@ -243,7 +243,7 @@ pub(crate) fn keyswitch_internal( module: &M, mut res: VecZnxDft, a: &GLWE, - b: &GLWESwitchingKeyPrepared, + key: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) -> VecZnxBig where @@ -265,12 +265,12 @@ where Scratch: ScratchTakeCore, { let base2k_in: usize = a.base2k().into(); - let base2k_out: usize = b.base2k().into(); + let base2k_out: usize = key.base2k().into(); let cols: usize = (a.rank() + 1).into(); let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); - let pmat: &VmpPMat = &b.key.data; + let pmat: &VmpPMat = &key.key.data; - if b.dsize() == 1 { + if key.dsize() == 1 { let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); if base2k_in == base2k_out { @@ -295,7 +295,7 @@ where module.vmp_apply_dft_to_dft(&mut res, &ai_dft, pmat, scratch_1); } else { - let dsize: usize = b.dsize().into(); + let dsize: usize = key.dsize().into(); let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a_size.div_ceil(dsize)); ai_dft.data_mut().fill(0); diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index eb93bf4..3c0afc1 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -6,6 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -27,6 +28,18 @@ pub struct AutomorphismKey { pub(crate) p: i64, } +impl SetAutomorphismGaloisElement for AutomorphismKey { + fn set_p(&mut self, p: i64) { + self.p = p + } +} + +impl GetAutomorphismGaloisElement for AutomorphismKey { + fn p(&self) -> i64 { + self.p + } +} + impl AutomorphismKey { pub fn p(&self) -> i64 { self.p diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 61e312c..44fccbc 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -2,7 +2,7 @@ use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; use rand_distr::num_traits::Zero; -use crate::GALOISGENERATOR; +use crate::{GALOISGENERATOR, api::ModuleN}; #[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { @@ -75,36 +75,47 @@ impl Module { pub fn log_n(&self) -> usize { (usize::BITS - (self.n() - 1).leading_zeros()) as _ } +} - #[inline] - pub fn cyclotomic_order(&self) -> u64 { +pub trait CyclotomicOrder +where + Self: ModuleN, +{ + fn cyclotomic_order(&self) -> i64 { (self.n() << 1) as _ } +} +impl CyclotomicOrder for Module where Self: ModuleN {} + +pub trait GaloisElement +where + Self: CyclotomicOrder, +{ // Returns GALOISGENERATOR^|generator| * sign(generator) - #[inline] - pub fn galois_element(&self, generator: i64) -> i64 { + fn galois_element(&self, generator: i64) -> i64 { if generator == 0 { return 1; } - ((mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (self.cyclotomic_order() - 1)) as i64) - * generator.signum() + + let g_exp: u64 = mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (self.cyclotomic_order() - 1) as u64; + g_exp as i64 * generator.signum() } // Returns gen^-1 - #[inline] - pub fn galois_element_inv(&self, gal_el: i64) -> i64 { + fn galois_element_inv(&self, gal_el: i64) -> i64 { if gal_el == 0 { panic!("cannot invert 0") } - ((mod_exp_u64( - gal_el.unsigned_abs(), - (self.cyclotomic_order() - 1) as usize, - ) & (self.cyclotomic_order() - 1)) as i64) - * gal_el.signum() + + let g_exp: u64 = + mod_exp_u64(GALOISGENERATOR, (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; + g_exp as i64 * gal_el.signum() } } +impl GaloisElement for Module where Self: CyclotomicOrder {} + impl Drop for Module { fn drop(&mut self) { unsafe { B::destroy(self.ptr) } From 919bbfd41d4f9be02ef48cc4d584696768a6357a Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 16 Oct 2025 11:08:25 +0200 Subject: [PATCH 21/60] automorphism ggsw --- poulpy-core/src/automorphism/ggsw_ct.rs | 256 ++++++++++-------------- 1 file changed, 109 insertions(+), 147 deletions(-) diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index a3cef86..8035063 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -1,165 +1,127 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + api::ScratchAvailable, + layouts::{Backend, DataMut, Module, Scratch}, }; -use crate::layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GLWE, - prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, +use crate::{ + GGSWExpandRows, ScratchTakeCore, + automorphism::glwe_ct::GLWEAutomorphism, + layouts::{ + GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, + prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, TensorKeyPrepared, TensorKeyPreparedToRef}, + }, }; impl GGSW> { - pub fn automorphism_tmp_bytes( - module: &Module, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - tsk_infos: &TSK, + pub fn automorphism_tmp_bytes( + module: &M, + res_infos: &R, + a_infos: &A, + key_infos: &K, + tsk_infos: &T, ) -> usize where - OUT: GGSWInfos, - IN: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: - VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + M: GGSWAutomorphism, { - let out_size: usize = out_infos.size(); - let ci_dft: usize = module.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), out_size); - let ks_internal: usize = GLWE::keyswitch_tmp_bytes( - module, - &out_infos.glwe_layout(), - &in_infos.glwe_layout(), - key_infos, - ); - let expand: usize = GGSW::expand_row_tmp_bytes(module, out_infos, tsk_infos); - ci_dft + (ks_internal | expand) - } - - pub fn automorphism_inplace_tmp_bytes( - module: &Module, - out_infos: &OUT, - key_infos: &KEY, - tsk_infos: &TSK, - ) -> usize - where - OUT: GGSWInfos, - KEY: GGLWEInfos, - TSK: GGLWEInfos, - Module: - VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, - { - GGSW::automorphism_tmp_bytes(module, out_infos, out_infos, key_infos, tsk_infos) + module.ggsw_automorphism_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos) } } -impl GGSW { - pub fn automorphism( - &mut self, - module: &Module, - lhs: &GGSW, - auto_key: &AutomorphismKeyPrepared, - tensor_key: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, +impl GGSW { + pub fn automorphism(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) + where + A: GGSWToRef, + K: AutomorphismKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWAutomorphism, { - #[cfg(debug_assertions)] - { - use crate::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(self.n(), module.n() as u32); - assert_eq!(lhs.n(), module.n() as u32); - assert_eq!(auto_key.n(), module.n() as u32); - assert_eq!(tensor_key.n(), module.n() as u32); - - assert_eq!( - self.rank(), - lhs.rank(), - "ggsw_out rank: {} != ggsw_in rank: {}", - self.rank(), - lhs.rank() - ); - assert_eq!( - self.rank(), - auto_key.rank_out(), - "ggsw_in rank: {} != auto_key rank: {}", - self.rank(), - auto_key.rank_out() - ); - assert_eq!( - self.rank(), - tensor_key.rank_out(), - "ggsw_in rank: {} != tensor_key rank: {}", - self.rank(), - tensor_key.rank_out() - ); - assert!(scratch.available() >= GGSW::automorphism_tmp_bytes(module, self, lhs, auto_key, tensor_key)) - }; - - // Keyswitch the j-th row of the col 0 - (0..lhs.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .automorphism(module, &lhs.at(row_i, 0), auto_key, scratch); - }); - self.expand_row(module, tensor_key, scratch); + module.ggsw_automorphism(self, a, key, tsk, scratch); } - pub fn automorphism_inplace( - &mut self, - module: &Module, - auto_key: &AutomorphismKeyPrepared, - tensor_key: &TensorKeyPrepared, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxIdftApplyTmpA - + VecZnxNormalize, - Scratch: ScratchAvailable, + pub fn automorphism_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) + where + K: AutomorphismKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + M: GGSWAutomorphism, { - // Keyswitch the j-th row of the col 0 - (0..self.dnum().into()).for_each(|row_i| { - // Key-switch column 0, i.e. - // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2) - self.at_mut(row_i, 0) - .automorphism_inplace(module, auto_key, scratch); - }); - self.expand_row(module, tensor_key, scratch); + module.ggsw_automorphism_inplace(self, key, tsk, scratch); } } + +impl GGSWAutomorphism for Module where Self: GLWEAutomorphism + GGSWExpandRows {} + +pub trait GGSWAutomorphism +where + Self: GLWEAutomorphism + GGSWExpandRows, +{ + fn ggsw_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize + where + R: GGSWInfos, + A: GGSWInfos, + K: GGLWEInfos, + T: GGLWEInfos, + { + let out_size: usize = res_infos.size(); + let ci_dft: usize = self.bytes_of_vec_znx_dft((key_infos.rank_out() + 1).into(), out_size); + let ks_internal: usize = self.glwe_automorphism_tmp_bytes(res_infos, a_infos, key_infos); + let expand: usize = self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos); + ci_dft + (ks_internal.max(expand)) + } + + fn ggsw_automorphism(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGSWToRef, + K: AutomorphismKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + assert_eq!(res.ggsw_layout(), a.ggsw_layout()); + assert_eq!(res.glwe_layout(), a.glwe_layout()); + assert_eq!(res.lwe_layout(), a.lwe_layout()); + assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk)); + + // Keyswitch the j-th row of the col 0 + for row in 0..res.dnum().as_usize() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2) + self.glwe_automorphism(&mut res.at_mut(row, 0), &a.at(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); + } + + fn ggsw_automorphism_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + K: AutomorphismKeyPreparedToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); + let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + + // Keyswitch the j-th row of the col 0 + for row in 0..res.dnum().as_usize() { + // Key-switch column 0, i.e. + // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2) + self.glwe_automorphism_inplace(&mut res.at_mut(row, 0), key, scratch); + } + + self.ggsw_expand_row(res, tsk, scratch); + } +} + +impl GGSW {} From bbc664fa389ec780cc159cea7fe772644ad8def9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 16 Oct 2025 11:25:55 +0200 Subject: [PATCH 22/60] glwe ops --- poulpy-core/src/automorphism/mod.rs | 4 +++ poulpy-core/src/keyswitching/mod.rs | 2 +- poulpy-core/src/lib.rs | 1 + poulpy-core/src/operations/glwe.rs | 50 ++++++++++++++--------------- poulpy-hal/src/api/module.rs | 6 ++++ poulpy-hal/src/layouts/module.rs | 4 ++- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/poulpy-core/src/automorphism/mod.rs b/poulpy-core/src/automorphism/mod.rs index f985c5e..1cd7bea 100644 --- a/poulpy-core/src/automorphism/mod.rs +++ b/poulpy-core/src/automorphism/mod.rs @@ -1,3 +1,7 @@ mod gglwe_atk; mod ggsw_ct; mod glwe_ct; + +pub use gglwe_atk::*; +pub use ggsw_ct::*; +pub use glwe_ct::*; \ No newline at end of file diff --git a/poulpy-core/src/keyswitching/mod.rs b/poulpy-core/src/keyswitching/mod.rs index b23fd47..462e474 100644 --- a/poulpy-core/src/keyswitching/mod.rs +++ b/poulpy-core/src/keyswitching/mod.rs @@ -4,6 +4,6 @@ mod glwe_ct; mod lwe_ct; pub use gglwe_ct::*; -pub use gglwe_ct::*; +//pub use gglwe_ct::*; pub use glwe_ct::*; pub use lwe_ct::*; diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index ffa36a8..49c39b0 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -19,6 +19,7 @@ pub use dist::*; pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; +pub use automorphism::*; pub use encryption::SIGMA; diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index b8b32ce..021d10e 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ VecZnxNormalize, VecZnxNormalizeInplace, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSubNegateInplace, }, - layouts::{Backend, DataMut, Module, Scratch, VecZnx, ZnxZero}, + layouts::{Backend, DataMut, Scratch, VecZnx, ZnxZero}, }; use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}; @@ -19,11 +19,11 @@ where impl GLWEOperations for GLWE where GLWE: GLWEToMut + GLWEInfos {} pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { - fn add(&mut self, module: &Module, a: &A, b: &B) + fn add(&mut self, module: &M, a: &A, b: &B) where A: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos, - Module: VecZnxAdd + VecZnxCopy, + M: VecZnxAdd + VecZnxCopy, { #[cfg(debug_assertions)] { @@ -66,10 +66,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_binary(self, a, b)); } - fn add_inplace(&mut self, module: &Module, a: &A) + fn add_inplace(&mut self, module: &M, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxAddInplace, + M: VecZnxAddInplace, { #[cfg(debug_assertions)] { @@ -88,11 +88,11 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_unary(self, a)) } - fn sub(&mut self, module: &Module, a: &A, b: &B) + fn sub(&mut self, module: &M, a: &A, b: &B) where A: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos, - Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, + M: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, { #[cfg(debug_assertions)] { @@ -136,10 +136,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_binary(self, a, b)); } - fn sub_inplace_ab(&mut self, module: &Module, a: &A) + fn sub_inplace_ab(&mut self, module: &M, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxSubInplace, + M: VecZnxSubInplace, { #[cfg(debug_assertions)] { @@ -158,10 +158,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_unary(self, a)) } - fn sub_inplace_ba(&mut self, module: &Module, a: &A) + fn sub_inplace_ba(&mut self, module: &M, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxSubNegateInplace, + M: VecZnxSubNegateInplace, { #[cfg(debug_assertions)] { @@ -180,10 +180,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_unary(self, a)) } - fn rotate(&mut self, module: &Module, k: i64, a: &A) + fn rotate(&mut self, module: &M, k: i64, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxRotate, + M: VecZnxRotate, { #[cfg(debug_assertions)] { @@ -202,9 +202,9 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_unary(self, a)) } - fn rotate_inplace(&mut self, module: &Module, k: i64, scratch: &mut Scratch) + fn rotate_inplace(&mut self, module: &M, k: i64, scratch: &mut Scratch) where - Module: VecZnxRotateInplace, + M: VecZnxRotateInplace, { let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); @@ -213,10 +213,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { }); } - fn mul_xp_minus_one(&mut self, module: &Module, k: i64, a: &A) + fn mul_xp_minus_one(&mut self, module: &M, k: i64, a: &A) where A: GLWEToRef + GLWEInfos, - Module: VecZnxMulXpMinusOne, + M: VecZnxMulXpMinusOne, { #[cfg(debug_assertions)] { @@ -235,9 +235,9 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(set_k_unary(self, a)) } - fn mul_xp_minus_one_inplace(&mut self, module: &Module, k: i64, scratch: &mut Scratch) + fn mul_xp_minus_one_inplace(&mut self, module: &M, k: i64, scratch: &mut Scratch) where - Module: VecZnxMulXpMinusOneInplace, + M: VecZnxMulXpMinusOneInplace, { let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); @@ -268,9 +268,9 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_base2k(a.base2k()); } - fn rsh(&mut self, module: &Module, k: usize, scratch: &mut Scratch) + fn rsh(&mut self, module: &M, k: usize, scratch: &mut Scratch) where - Module: VecZnxRshInplace, + M: VecZnxRshInplace, { let base2k: usize = self.base2k().into(); (0..(self.rank() + 1).into()).for_each(|i| { @@ -278,10 +278,10 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { }) } - fn normalize(&mut self, module: &Module, a: &A, scratch: &mut Scratch) + fn normalize(&mut self, module: &M, a: &A, scratch: &mut Scratch) where A: GLWEToRef + GLWEInfos, - Module: VecZnxNormalize, + M: VecZnxNormalize, { #[cfg(debug_assertions)] { @@ -307,9 +307,9 @@ pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { self.set_k(a.k().min(self.k())); } - fn normalize_inplace(&mut self, module: &Module, scratch: &mut Scratch) + fn normalize_inplace(&mut self, module: &M, scratch: &mut Scratch) where - Module: VecZnxNormalizeInplace, + M: VecZnxNormalizeInplace, { let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { diff --git a/poulpy-hal/src/api/module.rs b/poulpy-hal/src/api/module.rs index 3dd6176..c2e2f1c 100644 --- a/poulpy-hal/src/api/module.rs +++ b/poulpy-hal/src/api/module.rs @@ -8,3 +8,9 @@ pub trait ModuleNew { pub trait ModuleN { fn n(&self) -> usize; } + +pub trait ModuleLogN where Self: ModuleN{ + fn log_n(&self) -> usize{ + (u64::BITS - (self.n() as u64-1).leading_zeros()) as usize + } +} \ No newline at end of file diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 44fccbc..ccefb3a 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -2,7 +2,7 @@ use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; use rand_distr::num_traits::Zero; -use crate::{GALOISGENERATOR, api::ModuleN}; +use crate::{api::{ModuleLogN, ModuleN}, GALOISGENERATOR}; #[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { @@ -86,6 +86,8 @@ where } } +impl ModuleLogN for Module where Self: ModuleN{} + impl CyclotomicOrder for Module where Self: ModuleN {} pub trait GaloisElement From 1925571492ebf2a0ecb290f164cf3fd0b398d4ce Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 16 Oct 2025 11:26:01 +0200 Subject: [PATCH 23/60] wip packing --- poulpy-core/src/glwe_packing.rs | 163 ++++++++++---------------------- 1 file changed, 50 insertions(+), 113 deletions(-) diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 7dacb97..129de7e 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -2,18 +2,13 @@ use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ModuleLogN, ModuleN, ScratchAvailable, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, DataRef, GaloisElement, Module, Scratch}, }; use crate::{ - GLWEOperations, - layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef}, GGLWEInfos, GLWEAlloc, GLWEInfos, GLWEToRef, LWEInfos, GLWE}, GLWEAutomorphism, GLWEOperations, ScratchTakeCore }; /// [GLWEPacker] enables only the fly GLWE packing @@ -43,9 +38,10 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(module: &Module, infos: &A) -> Self + pub fn alloc(module: &M, infos: &A) -> Self where A: GLWEInfos, + M: GLWEAlloc { Self { data: GLWE::alloc_from_infos(module, infos), @@ -66,9 +62,10 @@ impl GLWEPacker { /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn new(module: Module, infos: &A, log_batch: usize) -> Self + pub fn new(module: &M, infos: &A, log_batch: usize) -> Self where A: GLWEInfos, + M: GLWEAlloc { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); @@ -90,13 +87,13 @@ impl GLWEPacker { } /// Number of scratch space bytes required to call [Self::add]. - pub fn tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + pub fn tmp_bytes(module: &M, res_infos: &R, key_infos: &K) -> usize where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + K: GGLWEInfos, + M: GLWEAlloc + GLWEAutomorphism, { - pack_core_tmp_bytes(module, out_infos, key_infos) + pack_core_tmp_bytes(module, res_infos, key_infos) } pub fn galois_elements(module: &Module) -> Vec { @@ -112,37 +109,17 @@ impl GLWEPacker { /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. - pub fn add( + pub fn add( &mut self, - module: &Module, - a: Option<&GLWE>, - auto_keys: &HashMap>, + module: &M, + a: Option<&A>, + auto_keys: &HashMap, scratch: &mut Scratch, ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxCopy - + VecZnxRotateInplace - + VecZnxSub - + VecZnxNegateInplace - + VecZnxRshInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxSubInplace - + VecZnxRotate - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef, + M: GLWEAutomorphism, + Scratch: ScratchTakeCore, { assert!( (self.counter as u32) < self.accumulators[0].data.n(), @@ -177,47 +154,27 @@ impl GLWEPacker { } } -fn pack_core_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn pack_core_tmp_bytes(module: &M, res_infos: &R, key_infos: &K) -> usize where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + K: GGLWEInfos, + M: GLWEAlloc + GLWEAutomorphism, { - combine_tmp_bytes(module, out_infos, key_infos) + combine_tmp_bytes(module, res_infos, key_infos) } -fn pack_core( - module: &Module, - a: Option<&GLWE>, +fn pack_core( + module: &M, + a: Option<&A>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap>, - scratch: &mut Scratch, + auto_keys: &HashMap, + scratch: &mut Scratch, ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxCopy - + VecZnxRotateInplace - + VecZnxSub - + VecZnxNegateInplace - + VecZnxRshInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxSubInplace - + VecZnxRotate - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + A: GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef, + M: GLWEAutomorphism + ModuleLogN + VecZnxCopy, + Scratch: ScratchTakeCore, { let log_n: usize = module.log_n(); @@ -268,49 +225,29 @@ fn pack_core( } } -fn combine_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize +fn combine_tmp_bytes(module: &M, res_infos: &R, key_infos: &K) -> usize where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + K: GGLWEInfos, + M: GLWEAlloc + GLWEAutomorphism, { - GLWE::bytes_of_from_infos(module, out_infos) - + (GLWE::rsh_tmp_bytes(module.n()) | GLWE::automorphism_inplace_tmp_bytes(module, out_infos, key_infos)) + GLWE::bytes_of_from_infos(module, res_infos) + + (GLWE::rsh_tmp_bytes(module.n()) | module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) } /// [combine] merges two ciphertexts together. -fn combine( - module: &Module, +fn combine( + module: &M, acc: &mut Accumulator, - b: Option<&GLWE>, + b: Option<&B>, i: usize, - auto_keys: &HashMap>, - scratch: &mut Scratch, + auto_keys: &HashMap, + scratch: &mut Scratch, ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxCopy - + VecZnxRotateInplace - + VecZnxSub - + VecZnxNegateInplace - + VecZnxRshInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxSubInplace - + VecZnxRotate - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigAutomorphismInplace - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + B: GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef, + M: GLWEAutomorphism + GaloisElement + VecZnxRotateInplace, + Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); let a: &mut GLWE> = &mut acc.data; @@ -335,7 +272,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(a); + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); // a = a * X^-t a.rotate_inplace(module, -t, scratch_1); From d27d43759aa767292ffdf824fecc8cd4bf1a5c52 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 16:57:30 +0200 Subject: [PATCH 24/60] glwe operations --- poulpy-core/src/automorphism/mod.rs | 2 +- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 15 +- poulpy-core/src/glwe_packing.rs | 161 +++---- poulpy-core/src/glwe_trace.rs | 5 +- poulpy-core/src/keyswitching/mod.rs | 2 +- poulpy-core/src/lib.rs | 2 +- poulpy-core/src/operations/glwe.rs | 482 ++++++++++---------- poulpy-hal/src/api/module.rs | 11 +- poulpy-hal/src/layouts/module.rs | 7 +- 9 files changed, 329 insertions(+), 358 deletions(-) diff --git a/poulpy-core/src/automorphism/mod.rs b/poulpy-core/src/automorphism/mod.rs index 1cd7bea..fd10f33 100644 --- a/poulpy-core/src/automorphism/mod.rs +++ b/poulpy-core/src/automorphism/mod.rs @@ -4,4 +4,4 @@ mod glwe_ct; pub use gglwe_atk::*; pub use ggsw_ct::*; -pub use glwe_ct::*; \ No newline at end of file +pub use glwe_ct::*; diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index a7b86fa..24d02bd 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -1,19 +1,18 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ModuleN, ScratchAvailable, ScratchTakeBasic, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftAddInplace, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, + VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, DataMut, Module, Scratch, VmpPMat, ZnxInfos}, }; use crate::{ - ScratchTakeCore, + GLWECopy, ScratchTakeCore, layouts::{ GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, }, - operations::GLWEOperations, }; impl GGLWE> { @@ -39,11 +38,11 @@ impl GGSW { } } -impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + VecZnxCopy {} +impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + GLWECopy {} pub trait GGSWFromGGLWE where - Self: GGSWExpandRows + VecZnxCopy, + Self: GGSWExpandRows + GLWECopy, { fn ggsw_from_gglwe_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize where @@ -71,7 +70,7 @@ where assert_eq!(tsk.n(), self.n() as u32); for row in 0..res.dnum().into() { - res.at_mut(row, 0).copy(self, &a.at(row, 0)); + self.glwe_copy(&mut res.at_mut(row, 0), &a.at(row, 0)); } self.ggsw_expand_row(res, tsk, scratch); diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 129de7e..6fbdb1d 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -1,14 +1,13 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ModuleLogN, ModuleN, ScratchAvailable, VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes - }, + api::{ModuleLogN, VecZnxCopy, VecZnxRotateInplace}, layouts::{Backend, DataMut, DataRef, GaloisElement, Module, Scratch}, }; use crate::{ - layouts::{prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef}, GGLWEInfos, GLWEAlloc, GLWEInfos, GLWEToRef, LWEInfos, GLWE}, GLWEAutomorphism, GLWEOperations, ScratchTakeCore + GLWEAutomorphism, ScratchTakeCore, + layouts::{GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::AutomorphismKeyPreparedToRef}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -41,7 +40,7 @@ impl Accumulator { pub fn alloc(module: &M, infos: &A) -> Self where A: GLWEInfos, - M: GLWEAlloc + M: GLWEAlloc, { Self { data: GLWE::alloc_from_infos(module, infos), @@ -65,7 +64,7 @@ impl GLWEPacker { pub fn new(module: &M, infos: &A, log_batch: usize) -> Self where A: GLWEInfos, - M: GLWEAlloc + M: GLWEAlloc, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); @@ -109,13 +108,8 @@ impl GLWEPacker { /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. - pub fn add( - &mut self, - module: &M, - a: Option<&A>, - auto_keys: &HashMap, - scratch: &mut Scratch, - ) where + pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) + where A: GLWEToRef, K: AutomorphismKeyPreparedToRef, M: GLWEAutomorphism, @@ -327,110 +321,69 @@ fn combine( } } -/// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] -/// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] -pub fn glwe_packing( - module: &Module, - cts: &mut HashMap>, - log_gap_out: usize, - auto_keys: &HashMap>, - scratch: &mut Scratch, -) where - ATK: DataRef, - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotate - + VecZnxNormalize, - Scratch: ScratchAvailable, +pub trait GLWEPacking +where + Self: GLWEAutomorphism + GaloisElement + ModuleLogN, { - #[cfg(debug_assertions)] + /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] + /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] + fn glwe_pack( + &self, + cts: &mut HashMap, + log_gap_out: usize, + keys: &HashMap, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef, + Scratch: ScratchTakeCore, { - assert!(*cts.keys().max().unwrap() < module.n()) - } + #[cfg(debug_assertions)] + { + assert!(*cts.keys().max().unwrap() < self.n()) + } - let log_n: usize = module.log_n(); + let log_n: usize = self.log_n(); - (0..log_n - log_gap_out).for_each(|i| { - let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); + (0..log_n - log_gap_out).for_each(|i| { + let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); - let auto_key: &AutomorphismKeyPrepared = if i == 0 { - auto_keys.get(&-1).unwrap() - } else { - auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() - }; + let key: &K = if i == 0 { + keys.get(&-1).unwrap() + } else { + keys.get(&self.galois_element(1 << (i - 1))).unwrap() + }; - (0..t).for_each(|j| { - let mut a: Option<&mut GLWE> = cts.remove(&j); - let mut b: Option<&mut GLWE> = cts.remove(&(j + t)); + (0..t).for_each(|j| { + let mut a: Option<&mut R> = cts.remove(&j); + let mut b: Option<&mut R> = cts.remove(&(j + t)); - pack_internal(module, &mut a, &mut b, i, auto_key, scratch); + pack_internal(self, &mut a, &mut b, i, key, scratch); - if let Some(a) = a { - cts.insert(j, a); - } else if let Some(b) = b { - cts.insert(j, b); - } + if let Some(a) = a { + cts.insert(j, a); + } else if let Some(b) = b { + cts.insert(j, b); + } + }); }); - }); + } } #[allow(clippy::too_many_arguments)] -fn pack_internal( - module: &Module, - a: &mut Option<&mut GLWE>, - b: &mut Option<&mut GLWE>, +fn pack_internal( + module: &M, + a: &mut Option<&mut A>, + b: &mut Option<&mut B>, i: usize, - auto_key: &AutomorphismKeyPrepared, - scratch: &mut Scratch, + auto_key: &K, + scratch: &mut Scratch, ) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotate - + VecZnxNormalize, - Scratch: ScratchAvailable, + M: GLWEAutomorphism, + A: GLWEToMut + GLWEInfos, + B: GLWEToMut + GLWEInfos, + K: AutomorphismKeyPreparedToRef, + Scratch: ScratchTakeCore, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) // We also use the identity: AUTO(a * X^t, g) = -X^t * AUTO(a, g) @@ -446,7 +399,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(a); + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); // a = a * X^-t a.rotate_inplace(module, -t, scratch_1); diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 36aabb9..dc5582c 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -9,10 +9,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx}, }; -use crate::{ - layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, - operations::GLWEOperations, -}; +use crate::layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}; impl GLWE> { pub fn trace_galois_elements(module: &Module) -> Vec { diff --git a/poulpy-core/src/keyswitching/mod.rs b/poulpy-core/src/keyswitching/mod.rs index 462e474..7071680 100644 --- a/poulpy-core/src/keyswitching/mod.rs +++ b/poulpy-core/src/keyswitching/mod.rs @@ -4,6 +4,6 @@ mod glwe_ct; mod lwe_ct; pub use gglwe_ct::*; -//pub use gglwe_ct::*; +// pub use gglwe_ct::*; pub use glwe_ct::*; pub use lwe_ct::*; diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 49c39b0..2dcc77a 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -14,12 +14,12 @@ mod utils; pub use operations::*; pub mod layouts; +pub use automorphism::*; pub use conversion::*; pub use dist::*; pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; -pub use automorphism::*; pub use encryption::SIGMA; diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 021d10e..4ad713c 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -1,320 +1,292 @@ use poulpy_hal::{ api::{ - VecZnxAdd, VecZnxAddInplace, VecZnxCopy, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegateInplace, + ModuleN, VecZnxAdd, VecZnxAddInplace, VecZnxCopy, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSubNegateInplace, }, - layouts::{Backend, DataMut, Scratch, VecZnx, ZnxZero}, + layouts::{Backend, Module, Scratch, VecZnx, ZnxZero}, }; -use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}; +use crate::{ + ScratchTakeCore, + layouts::{GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}, +}; -impl GLWEOperations for GLWEPlaintext +pub trait GLWEAdd where - D: DataMut, - GLWEPlaintext: GLWEToMut + GLWEInfos, + Self: ModuleN + VecZnxAdd + VecZnxCopy + VecZnxAddInplace, { + fn glwe_add(&self, res: &mut R, a: &A, b: &B) + where + R: GLWEToMut, + A: GLWEToRef, + B: GLWEToRef, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &mut GLWE<&[u8]> = &mut a.to_ref(); + let b: &GLWE<&[u8]> = &b.to_ref(); + + assert_eq!(a.n(), self.n() as u32); + assert_eq!(b.n(), self.n() as u32); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.base2k(), b.base2k()); + assert!(res.rank() >= a.rank().max(b.rank())); + + let min_col: usize = (a.rank().min(b.rank()) + 1).into(); + let max_col: usize = (a.rank().max(b.rank() + 1)).into(); + let self_col: usize = (res.rank() + 1).into(); + + (0..min_col).for_each(|i| { + self.vec_znx_add(res.data_mut(), i, a.data(), i, b.data(), i); + }); + + if a.rank() > b.rank() { + (min_col..max_col).for_each(|i| { + self.vec_znx_copy(res.data_mut(), i, a.data(), i); + }); + } else { + (min_col..max_col).for_each(|i| { + self.vec_znx_copy(res.data_mut(), i, b.data(), i); + }); + } + + let size: usize = res.size(); + (max_col..self_col).for_each(|i| { + (0..size).for_each(|j| { + res.data.zero_at(i, j); + }); + }); + + res.set_base2k(a.base2k()); + res.set_k(set_k_binary(res, a, b)); + } + + fn glwe_add_inplace(&self, res: &mut R, a: &A) + where + R: GLWEToMut, + A: GLWEToRef, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.base2k(), a.base2k()); + assert!(res.rank() >= a.rank()); + + (0..(a.rank() + 1).into()).for_each(|i| { + self.vec_znx_add_inplace(res.data_mut(), i, a.data(), i); + }); + + res.set_k(set_k_unary(res, a)) + } } -impl GLWEOperations for GLWE where GLWE: GLWEToMut + GLWEInfos {} +impl GLWEAdd for Module where Self: ModuleN + VecZnxAdd + VecZnxCopy + VecZnxAddInplace {} -pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { - fn add(&mut self, module: &M, a: &A, b: &B) +pub trait GLWESub +where + Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace, +{ + fn glwe_sub(&self, res: &mut R, a: &A, b: &B) where - A: GLWEToRef + GLWEInfos, - B: GLWEToRef + GLWEInfos, - M: VecZnxAdd + VecZnxCopy, + R: GLWEToMut, + A: GLWEToRef, + B: GLWEToRef, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(b.n(), self.n()); - assert_eq!(a.base2k(), b.base2k()); - assert!(self.rank() >= a.rank().max(b.rank())); - } + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let b: &GLWE<&[u8]> = &b.to_ref(); + + assert_eq!(a.n(), self.n() as u32); + assert_eq!(b.n(), self.n() as u32); + assert_eq!(a.base2k(), b.base2k()); + assert!(res.rank() >= a.rank().max(b.rank())); let min_col: usize = (a.rank().min(b.rank()) + 1).into(); let max_col: usize = (a.rank().max(b.rank() + 1)).into(); - let self_col: usize = (self.rank() + 1).into(); - - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - let b_ref: &GLWE<&[u8]> = &b.to_ref(); + let self_col: usize = (res.rank() + 1).into(); (0..min_col).for_each(|i| { - module.vec_znx_add(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); + self.vec_znx_sub(res.data_mut(), i, a.data(), i, b.data(), i); }); if a.rank() > b.rank() { (min_col..max_col).for_each(|i| { - module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); + self.vec_znx_copy(res.data_mut(), i, a.data(), i); }); } else { (min_col..max_col).for_each(|i| { - module.vec_znx_copy(&mut self_mut.data, i, &b_ref.data, i); + self.vec_znx_copy(res.data_mut(), i, b.data(), i); + self.vec_znx_negate_inplace(res.data_mut(), i); }); } - let size: usize = self_mut.size(); + let size: usize = res.size(); (max_col..self_col).for_each(|i| { (0..size).for_each(|j| { - self_mut.data.zero_at(i, j); + res.data.zero_at(i, j); }); }); - self.set_base2k(a.base2k()); - self.set_k(set_k_binary(self, a, b)); + res.set_base2k(a.base2k()); + res.set_k(set_k_binary(res, a, b)); } - fn add_inplace(&mut self, module: &M, a: &A) + fn glwe_sub_inplace(&self, res: &mut R, a: &A) where - A: GLWEToRef + GLWEInfos, - M: VecZnxAddInplace, + R: GLWEToMut, + A: GLWEToRef, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(self.base2k(), a.base2k()); - assert!(self.rank() >= a.rank()) - } + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.base2k(), a.base2k()); + assert!(res.rank() >= a.rank()); (0..(a.rank() + 1).into()).for_each(|i| { - module.vec_znx_add_inplace(&mut self_mut.data, i, &a_ref.data, i); + self.vec_znx_sub_inplace(res.data_mut(), i, a.data(), i); }); - self.set_k(set_k_unary(self, a)) + res.set_k(set_k_unary(res, a)) } - fn sub(&mut self, module: &M, a: &A, b: &B) + fn glwe_sub_negate_inplace(&self, res: &mut R, a: &A) where - A: GLWEToRef + GLWEInfos, - B: GLWEToRef + GLWEInfos, - M: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, + R: GLWEToMut, + A: GLWEToRef, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(b.n(), self.n()); - assert_eq!(a.base2k(), b.base2k()); - assert!(self.rank() >= a.rank().max(b.rank())); - } + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); - let min_col: usize = (a.rank().min(b.rank()) + 1).into(); - let max_col: usize = (a.rank().max(b.rank() + 1)).into(); - let self_col: usize = (self.rank() + 1).into(); - - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - let b_ref: &GLWE<&[u8]> = &b.to_ref(); - - (0..min_col).for_each(|i| { - module.vec_znx_sub(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i); - }); - - if a.rank() > b.rank() { - (min_col..max_col).for_each(|i| { - module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); - }); - } else { - (min_col..max_col).for_each(|i| { - module.vec_znx_copy(&mut self_mut.data, i, &b_ref.data, i); - module.vec_znx_negate_inplace(&mut self_mut.data, i); - }); - } - - let size: usize = self_mut.size(); - (max_col..self_col).for_each(|i| { - (0..size).for_each(|j| { - self_mut.data.zero_at(i, j); - }); - }); - - self.set_base2k(a.base2k()); - self.set_k(set_k_binary(self, a, b)); - } - - fn sub_inplace_ab(&mut self, module: &M, a: &A) - where - A: GLWEToRef + GLWEInfos, - M: VecZnxSubInplace, - { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(self.base2k(), a.base2k()); - assert!(self.rank() >= a.rank()) - } - - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.base2k(), a.base2k()); + assert!(res.rank() >= a.rank()); (0..(a.rank() + 1).into()).for_each(|i| { - module.vec_znx_sub_inplace(&mut self_mut.data, i, &a_ref.data, i); + self.vec_znx_sub_negate_inplace(res.data_mut(), i, a.data(), i); }); - self.set_k(set_k_unary(self, a)) + res.set_k(set_k_unary(res, a)) } +} - fn sub_inplace_ba(&mut self, module: &M, a: &A) +pub trait GLWERotate +where + Self: ModuleN + VecZnxRotate + VecZnxRotateInplace, +{ + fn glwe_rotate(&self, k: i64, res: &mut R, a: &A) where - A: GLWEToRef + GLWEInfos, - M: VecZnxSubNegateInplace, + R: GLWEToMut, + A: GLWEToRef, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(self.base2k(), a.base2k()); - assert!(self.rank() >= a.rank()) - } + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.rank(), a.rank()); (0..(a.rank() + 1).into()).for_each(|i| { - module.vec_znx_sub_negate_inplace(&mut self_mut.data, i, &a_ref.data, i); + self.vec_znx_rotate(k, res.data_mut(), i, a.data(), i); }); - self.set_k(set_k_unary(self, a)) + res.set_base2k(a.base2k()); + res.set_k(set_k_unary(res, a)) } - fn rotate(&mut self, module: &M, k: i64, a: &A) + fn glwe_rotate_inplace(&self, k: i64, res: &mut R, scratch: &mut Scratch) where - A: GLWEToRef + GLWEInfos, - M: VecZnxRotate, + R: GLWEToMut, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(self.rank(), a.rank()) + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + + (0..(res.rank() + 1).into()).for_each(|i| { + self.vec_znx_rotate_inplace(k, res.data_mut(), i, scratch); + }); + } +} + +pub trait GLWEMulXpMinusOne +where + Self: ModuleN + VecZnxMulXpMinusOne + VecZnxMulXpMinusOneInplace, +{ + fn glwe_mul_xp_minus_one(&self, k: i64, res: &mut R, a: &A) + where + R: GLWEToMut, + A: GLWEToRef, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.rank(), a.rank()); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_mul_xp_minus_one(k, res.data_mut(), i, a.data(), i); } - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - - (0..(a.rank() + 1).into()).for_each(|i| { - module.vec_znx_rotate(k, &mut self_mut.data, i, &a_ref.data, i); - }); - - self.set_base2k(a.base2k()); - self.set_k(set_k_unary(self, a)) + res.set_base2k(a.base2k()); + res.set_k(set_k_unary(res, a)) } - fn rotate_inplace(&mut self, module: &M, k: i64, scratch: &mut Scratch) + fn glwe_mul_xp_minus_one_inplace(&self, k: i64, res: &mut R, scratch: &mut Scratch) where - M: VecZnxRotateInplace, + R: GLWEToMut, { - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - (0..(self_mut.rank() + 1).into()).for_each(|i| { - module.vec_znx_rotate_inplace(k, &mut self_mut.data, i, scratch); - }); + assert_eq!(res.n(), self.n() as u32); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_mul_xp_minus_one_inplace(k, res.data_mut(), i, scratch); + } } +} - fn mul_xp_minus_one(&mut self, module: &M, k: i64, a: &A) +pub trait GLWECopy +where + Self: ModuleN + VecZnxCopy, +{ + fn glwe_copy(&self, res: &mut R, a: &A) where - A: GLWEToRef + GLWEInfos, - M: VecZnxMulXpMinusOne, + R: GLWEToMut, + A: GLWEToRef, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), self.n()); - assert_eq!(self.rank(), a.rank()) + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.rank(), a.rank()); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_copy(res.data_mut(), i, a.data(), i); } - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - - (0..(a.rank() + 1).into()).for_each(|i| { - module.vec_znx_mul_xp_minus_one(k, &mut self_mut.data, i, &a_ref.data, i); - }); - - self.set_base2k(a.base2k()); - self.set_k(set_k_unary(self, a)) + res.set_k(a.k().min(res.max_k())); + res.set_base2k(a.base2k()); } +} - fn mul_xp_minus_one_inplace(&mut self, module: &M, k: i64, scratch: &mut Scratch) +pub trait GLWEShift +where + Self: ModuleN + VecZnxRshInplace, +{ + fn glwe_rsh(&self, k: usize, res: &mut R, scratch: &mut Scratch) where - M: VecZnxMulXpMinusOneInplace, + R: GLWEToMut, + Scratch: ScratchTakeCore, { - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - - (0..(self_mut.rank() + 1).into()).for_each(|i| { - module.vec_znx_mul_xp_minus_one_inplace(k, &mut self_mut.data, i, scratch); - }); - } - - fn copy(&mut self, module: &M, a: &A) - where - A: GLWEToRef + GLWEInfos, - M: VecZnxCopy, - { - #[cfg(debug_assertions)] - { - assert_eq!(self.n(), a.n()); - assert_eq!(self.rank(), a.rank()); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let base2k: usize = res.base2k().into(); + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_rsh_inplace(base2k, k, res.data_mut(), i, scratch); } - - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - - (0..(self_mut.rank() + 1).into()).for_each(|i| { - module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); - }); - - self.set_k(a.k().min(self.max_k())); - self.set_base2k(a.base2k()); - } - - fn rsh(&mut self, module: &M, k: usize, scratch: &mut Scratch) - where - M: VecZnxRshInplace, - { - let base2k: usize = self.base2k().into(); - (0..(self.rank() + 1).into()).for_each(|i| { - module.vec_znx_rsh_inplace(base2k, k, &mut self.to_mut().data, i, scratch); - }) - } - - fn normalize(&mut self, module: &M, a: &A, scratch: &mut Scratch) - where - A: GLWEToRef + GLWEInfos, - M: VecZnxNormalize, - { - #[cfg(debug_assertions)] - { - assert_eq!(self.n(), a.n()); - assert_eq!(self.rank(), a.rank()); - } - - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWE<&[u8]> = &a.to_ref(); - - (0..(self_mut.rank() + 1).into()).for_each(|i| { - module.vec_znx_normalize( - a.base2k().into(), - &mut self_mut.data, - i, - a.base2k().into(), - &a_ref.data, - i, - scratch, - ); - }); - self.set_base2k(a.base2k()); - self.set_k(a.k().min(self.k())); - } - - fn normalize_inplace(&mut self, module: &M, scratch: &mut Scratch) - where - M: VecZnxNormalizeInplace, - { - let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); - (0..(self_mut.rank() + 1).into()).for_each(|i| { - module.vec_znx_normalize_inplace(self_mut.base2k().into(), &mut self_mut.data, i, scratch); - }); } } @@ -324,6 +296,50 @@ impl GLWE> { } } +pub trait GLWENormalize +where + Self: ModuleN + VecZnxNormalize + VecZnxNormalizeInplace, +{ + fn glwe_normalize(&self, res: &mut R, a: &A, scratch: &mut Scratch) + where + R: GLWEToMut, + A: GLWEToRef, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(a.n(), self.n() as u32); + assert_eq!(res.rank(), a.rank()); + + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_normalize( + res.base2k().into(), + res.data_mut(), + i, + a.base2k().into(), + a.data(), + i, + scratch, + ); + } + + res.set_k(a.k().min(res.k())); + } + + fn glwe_normalize_inplace(&mut self, res: &mut R, scratch: &mut Scratch) + where + R: GLWEToMut, + Scratch: ScratchTakeCore, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + for i in 0..res.rank().as_usize() + 1 { + self.vec_znx_normalize_inplace(res.base2k().into(), res.data_mut(), i, scratch); + } + } +} + // c = op(a, b) fn set_k_binary(c: &impl GLWEInfos, a: &impl GLWEInfos, b: &impl GLWEInfos) -> TorusPrecision { // If either operands is a ciphertext diff --git a/poulpy-hal/src/api/module.rs b/poulpy-hal/src/api/module.rs index c2e2f1c..a18af44 100644 --- a/poulpy-hal/src/api/module.rs +++ b/poulpy-hal/src/api/module.rs @@ -9,8 +9,11 @@ pub trait ModuleN { fn n(&self) -> usize; } -pub trait ModuleLogN where Self: ModuleN{ - fn log_n(&self) -> usize{ - (u64::BITS - (self.n() as u64-1).leading_zeros()) as usize +pub trait ModuleLogN +where + Self: ModuleN, +{ + fn log_n(&self) -> usize { + (u64::BITS - (self.n() as u64 - 1).leading_zeros()) as usize } -} \ No newline at end of file +} diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index ccefb3a..3382774 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -2,7 +2,10 @@ use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; use rand_distr::num_traits::Zero; -use crate::{api::{ModuleLogN, ModuleN}, GALOISGENERATOR}; +use crate::{ + GALOISGENERATOR, + api::{ModuleLogN, ModuleN}, +}; #[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { @@ -86,7 +89,7 @@ where } } -impl ModuleLogN for Module where Self: ModuleN{} +impl ModuleLogN for Module where Self: ModuleN {} impl CyclotomicOrder for Module where Self: ModuleN {} From 827d257d0a2ad5914e99be27d58e9b5e5f5aaf3e Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 17:54:12 +0200 Subject: [PATCH 25/60] glwe trace --- poulpy-core/src/glwe_trace.rs | 256 ++++++++++++++++++---------------- 1 file changed, 137 insertions(+), 119 deletions(-) diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index dc5582c..48608d1 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -1,170 +1,188 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, - VecZnxRshInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx}, + api::ModuleLogN, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch, VecZnx}, }; -use crate::layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}; +use crate::{ + GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, + layouts::{ + Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + }, +}; impl GLWE> { - pub fn trace_galois_elements(module: &Module) -> Vec { - let mut gal_els: Vec = Vec::new(); - (0..module.log_n()).for_each(|i| { - if i == 0 { - gal_els.push(-1); - } else { - gal_els.push(module.galois_element(1 << (i - 1))); - } - }); - gal_els + pub fn trace_galois_elements(module: &M) -> Vec + where + M: GLWETrace, + { + module.glwe_trace_galois_elements() } - pub fn trace_tmp_bytes(module: &Module, out_infos: &OUT, in_infos: &IN, key_infos: &KEY) -> usize + pub fn trace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GLWEInfos, - IN: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + M: GLWETrace, { - let trace: usize = Self::automorphism_inplace_tmp_bytes(module, out_infos, key_infos); - if in_infos.base2k() != key_infos.base2k() { + module.glwe_automorphism_tmp_bytes(res_infos, a_infos, key_infos) + } +} + +impl GLWE { + pub fn trace( + &mut self, + module: &M, + start: usize, + end: usize, + a: &A, + keys: &HashMap, + scratch: &mut Scratch, + ) where + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + M: GLWETrace, + { + module.glwe_trace(self, start, end, a, keys, scratch); + } + + pub fn trace_inplace( + &mut self, + module: &M, + start: usize, + end: usize, + keys: &HashMap, + scratch: &mut Scratch, + ) where + K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + M: GLWETrace, + { + module.glwe_trace_inplace(self, start, end, keys, scratch); + } +} + +impl GLWETrace for Module where + Self: ModuleLogN + GaloisElement + GLWEAutomorphism + GLWEShift + GLWECopy +{ +} + +pub trait GLWETrace +where + Self: ModuleLogN + GaloisElement + GLWEAutomorphism + GLWEShift + GLWECopy, +{ + fn glwe_trace_galois_elements(&self) -> Vec { + (0..self.log_n()) + .map(|i| { + if i == 0 { + -1 + } else { + self.galois_element(1 << (i - 1)) + } + }) + .collect() + } + + fn glwe_trace_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GLWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + { + let trace: usize = self.glwe_automorphism_tmp_bytes(res_infos, a_infos, key_infos); + if a_infos.base2k() != key_infos.base2k() { let glwe_conv: usize = VecZnx::bytes_of( - module.n(), + self.n(), (key_infos.rank_out() + 1).into(), - out_infos.k().min(in_infos.k()).div_ceil(key_infos.base2k()) as usize, - ) + module.vec_znx_normalize_tmp_bytes(); + res_infos.k().min(a_infos.k()).div_ceil(key_infos.base2k()) as usize, + ) + self.vec_znx_normalize_tmp_bytes(); return glwe_conv + trace; } trace } - pub fn trace_inplace_tmp_bytes(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize + fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &HashMap, scratch: &mut Scratch) where - OUT: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEToMut, + A: GLWEToRef, + K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - Self::trace_tmp_bytes(module, out_infos, out_infos, key_infos) - } -} - -impl GLWE { - pub fn trace( - &mut self, - module: &Module, - start: usize, - end: usize, - lhs: &GLWE, - auto_keys: &HashMap>, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxCopy - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, - { - self.copy(module, lhs); - self.trace_inplace(module, start, end, auto_keys, scratch); + self.glwe_copy(res, a); + self.glwe_trace_inplace(res, start, end, keys, scratch); } - pub fn trace_inplace( - &mut self, - module: &Module, - start: usize, - end: usize, - auto_keys: &HashMap>, - scratch: &mut Scratch, - ) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxNormalizeTmpBytes - + VecZnxNormalize, - Scratch: ScratchAvailable, + fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) + where + R: GLWEToMut, + K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, { - let basek_ksk: Base2K = auto_keys - .get(auto_keys.keys().next().unwrap()) - .unwrap() - .base2k(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + + let basek_ksk: Base2K = keys.get(keys.keys().next().unwrap()).unwrap().base2k(); #[cfg(debug_assertions)] { - assert_eq!(self.n(), module.n() as u32); + assert_eq!(res.n(), self.n() as u32); assert!(start < end); - assert!(end <= module.log_n()); - for key in auto_keys.values() { - assert_eq!(key.n(), module.n() as u32); + assert!(end <= self.log_n()); + for key in keys.values() { + assert_eq!(key.n(), self.n() as u32); assert_eq!(key.base2k(), basek_ksk); - assert_eq!(key.rank_in(), self.rank()); - assert_eq!(key.rank_out(), self.rank()); + assert_eq!(key.rank_in(), res.rank()); + assert_eq!(key.rank_out(), res.rank()); } } - if self.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWELayout { - n: module.n().into(), - base2k: basek_ksk, - k: self.k(), - rank: self.rank(), - }); + if res.base2k() != basek_ksk { + let (mut self_conv, scratch_1) = scratch.take_glwe_ct( + self, + &GLWELayout { + n: self.n().into(), + base2k: basek_ksk, + k: res.k(), + rank: res.rank(), + }, + ); - for j in 0..(self.rank() + 1).into() { - module.vec_znx_normalize( + for j in 0..(res.rank() + 1).into() { + self.vec_znx_normalize( basek_ksk.into(), &mut self_conv.data, j, basek_ksk.into(), - &self.data, + res.data(), j, scratch_1, ); } for i in start..end { - self_conv.rsh(module, 1, scratch_1); + self.glwe_rsh(1, &mut self_conv, scratch_1); let p: i64 = if i == 0 { -1 } else { - module.galois_element(1 << (i - 1)) + self.galois_element(1 << (i - 1)) }; - if let Some(key) = auto_keys.get(&p) { - self_conv.automorphism_add_inplace(module, key, scratch_1); + if let Some(key) = keys.get(&p) { + self.glwe_automorphism_add_inplace(&mut self_conv, key, scratch_1); } else { - panic!("auto_keys[{p}] is empty") + panic!("keys[{p}] is empty") } } - for j in 0..(self.rank() + 1).into() { - module.vec_znx_normalize( - self.base2k().into(), - &mut self.data, + for j in 0..(res.rank() + 1).into() { + self.vec_znx_normalize( + res.base2k().into(), + res.data_mut(), j, basek_ksk.into(), &self_conv.data, @@ -174,18 +192,18 @@ impl GLWE { } } else { for i in start..end { - self.rsh(module, 1, scratch); + self.glwe_rsh(1, res, scratch); let p: i64 = if i == 0 { -1 } else { - module.galois_element(1 << (i - 1)) + self.galois_element(1 << (i - 1)) }; - if let Some(key) = auto_keys.get(&p) { - self.automorphism_add_inplace(module, key, scratch); + if let Some(key) = keys.get(&p) { + self.glwe_automorphism_add_inplace(res, key, scratch); } else { - panic!("auto_keys[{p}] is empty") + panic!("keys[{p}] is empty") } } } From 31b92f7f23b45c02d5c78ac66b32d9f7b8b62659 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 18:03:24 +0200 Subject: [PATCH 26/60] glwe packing --- poulpy-core/src/glwe_packing.rs | 68 +++++++++++++++++------------- poulpy-core/src/operations/glwe.rs | 2 +- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 6fbdb1d..5304df8 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -6,8 +6,11 @@ use poulpy_hal::{ }; use crate::{ - GLWEAutomorphism, ScratchTakeCore, - layouts::{GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::AutomorphismKeyPreparedToRef}, + GLWEAdd, GLWEAutomorphism, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + }, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -323,7 +326,14 @@ fn combine( pub trait GLWEPacking where - Self: GLWEAutomorphism + GaloisElement + ModuleLogN, + Self: GLWEAutomorphism + + GaloisElement + + ModuleLogN + + GLWERotate + + GLWESub + + GLWEShift + + GLWEAdd + + GLWENormalize, { /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] @@ -334,8 +344,8 @@ where keys: &HashMap, scratch: &mut Scratch, ) where - R: GLWEToMut, - K: AutomorphismKeyPreparedToRef, + R: GLWEToMut + GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -345,7 +355,7 @@ where let log_n: usize = self.log_n(); - (0..log_n - log_gap_out).for_each(|i| { + for i in 0..(log_n - log_gap_out){ let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); let key: &K = if i == 0 { @@ -354,7 +364,7 @@ where keys.get(&self.galois_element(1 << (i - 1))).unwrap() }; - (0..t).for_each(|j| { + for j in 0..t{ let mut a: Option<&mut R> = cts.remove(&j); let mut b: Option<&mut R> = cts.remove(&(j + t)); @@ -365,8 +375,8 @@ where } else if let Some(b) = b { cts.insert(j, b); } - }); - }); + }; + }; } } @@ -379,10 +389,10 @@ fn pack_internal( auto_key: &K, scratch: &mut Scratch, ) where - M: GLWEAutomorphism, - A: GLWEToMut + GLWEInfos, - B: GLWEToMut + GLWEInfos, - K: AutomorphismKeyPreparedToRef, + M: GLWEAutomorphism + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, + A: GLWEToMut + GLWEToRef + GLWEInfos, + B: GLWEToMut + GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, Scratch: ScratchTakeCore, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) @@ -402,42 +412,42 @@ fn pack_internal( let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); // a = a * X^-t - a.rotate_inplace(module, -t, scratch_1); + module.glwe_rotate_inplace(-t, a, scratch_1); // tmp_b = a * X^-t - b - tmp_b.sub(module, a, b); - tmp_b.rsh(module, 1, scratch_1); + module.glwe_sub(&mut tmp_b, a, b); + module.glwe_rsh(1, &mut tmp_b, scratch_1); // a = a * X^-t + b - a.add_inplace(module, b); - a.rsh(module, 1, scratch_1); + module.glwe_add_inplace(a, b); + module.glwe_rsh(1, a, scratch_1); - tmp_b.normalize_inplace(module, scratch_1); + module.glwe_normalize_inplace(&mut tmp_b, scratch_1); // tmp_b = phi(a * X^-t - b) - tmp_b.automorphism_inplace(module, auto_key, scratch_1); + module.glwe_automorphism_inplace(&mut tmp_b, auto_key, scratch_1); // a = a * X^-t + b - phi(a * X^-t - b) - a.sub_inplace_ab(module, &tmp_b); - a.normalize_inplace(module, scratch_1); + module.glwe_sub_inplace(a, &tmp_b); + module.glwe_normalize_inplace(a, scratch_1); // a = a + b * X^t - phi(a * X^-t - b) * X^t // = a + b * X^t - phi(a * X^-t - b) * - phi(X^t) // = a + b * X^t + phi(a - b * X^t) - a.rotate_inplace(module, t, scratch_1); + module.glwe_rotate_inplace(t, a, scratch_1); } else { - a.rsh(module, 1, scratch); + module.glwe_rsh(1, a, scratch); // a = a + phi(a) - a.automorphism_add_inplace(module, auto_key, scratch); + module.glwe_automorphism_add_inplace(a, auto_key, scratch); } } 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(b); - tmp_b.rotate(module, t, b); - tmp_b.rsh(module, 1, scratch_1); + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, b); + module.glwe_rotate(t, &mut tmp_b, b); + module.glwe_rsh(1, &mut tmp_b, scratch_1); // a = (b* X^t - phi(b* X^t)) - b.automorphism_sub_negate(module, &tmp_b, auto_key, scratch_1); + module.glwe_automorphism_sub_negate(b, &tmp_b, auto_key, scratch_1); } } diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 4ad713c..3e507e2 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -328,7 +328,7 @@ where res.set_k(a.k().min(res.k())); } - fn glwe_normalize_inplace(&mut self, res: &mut R, scratch: &mut Scratch) + fn glwe_normalize_inplace(&self, res: &mut R, scratch: &mut Scratch) where R: GLWEToMut, Scratch: ScratchTakeCore, From 93e0671edad6583e0a5be1322139981fcc3248b1 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 22:17:22 +0200 Subject: [PATCH 27/60] glwe packing --- poulpy-core/src/glwe_packing.rs | 246 +++++++++++++++-------------- poulpy-core/src/operations/glwe.rs | 11 +- 2 files changed, 139 insertions(+), 118 deletions(-) diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 5304df8..4b7d5b1 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -1,12 +1,13 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ModuleLogN, VecZnxCopy, VecZnxRotateInplace}, - layouts::{Backend, DataMut, DataRef, GaloisElement, Module, Scratch}, + api::ModuleLogN, + layouts::{Backend, GaloisElement, Module, Scratch}, }; use crate::{ - GLWEAdd, GLWEAutomorphism, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, + GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, + glwe_trace::GLWETrace, layouts::{ GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, @@ -40,10 +41,10 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(module: &M, infos: &A) -> Self + pub fn alloc(module: &M, infos: &A) -> Self where A: GLWEInfos, - M: GLWEAlloc, + M: GLWEPacking, { Self { data: GLWE::alloc_from_infos(module, infos), @@ -64,15 +65,15 @@ impl GLWEPacker { /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn new(module: &M, infos: &A, log_batch: usize) -> Self + pub fn alloc(module: &M, infos: &A, log_batch: usize) -> Self where A: GLWEInfos, - M: GLWEAlloc, + M: GLWEPacking, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(module, infos))); - Self { + GLWEPacker { accumulators, log_batch, counter: 0, @@ -93,13 +94,19 @@ impl GLWEPacker { where R: GLWEInfos, K: GGLWEInfos, - M: GLWEAlloc + GLWEAutomorphism, + M: GLWEPacking, { - pack_core_tmp_bytes(module, res_infos, key_infos) + module.bytes_of_glwe_from_infos(res_infos) + + module + .glwe_rsh_tmp_byte() + .max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) } - pub fn galois_elements(module: &Module) -> Vec { - GLWE::trace_galois_elements(module) + pub fn galois_elements(module: &M) -> Vec + where + M: GLWETrace, + { + module.glwe_trace_galois_elements() } /// Adds a GLWE ciphertext to the [GLWEPacker]. @@ -111,11 +118,11 @@ impl GLWEPacker { /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. - pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) + pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) where - A: GLWEToRef, - K: AutomorphismKeyPreparedToRef, - M: GLWEAutomorphism, + A: GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + M: GLWEPacking, Scratch: ScratchTakeCore, { assert!( @@ -136,14 +143,15 @@ impl GLWEPacker { } /// Flush result to`res`. - pub fn flush(&mut self, module: &Module, res: &mut GLWE) + pub fn flush(&mut self, module: &M, res: &mut R) where - Module: VecZnxCopy, + R: GLWEToMut, + M: GLWEPacking, { assert!(self.counter as u32 == self.accumulators[0].data.n()); // Copy result GLWE into res GLWE - res.copy( - module, + module.glwe_copy( + res, &self.accumulators[module.log_n() - self.log_batch - 1].data, ); @@ -151,13 +159,76 @@ impl GLWEPacker { } } -fn pack_core_tmp_bytes(module: &M, res_infos: &R, key_infos: &K) -> usize -where - R: GLWEInfos, - K: GGLWEInfos, - M: GLWEAlloc + GLWEAutomorphism, +impl GLWEPacking for Module where + Self: GLWEAutomorphism + + GaloisElement + + ModuleLogN + + GLWERotate + + GLWESub + + GLWEShift + + GLWEAdd + + GLWENormalize + + GLWECopy + + GLWEAlloc { - combine_tmp_bytes(module, res_infos, key_infos) +} + +pub trait GLWEPacking +where + Self: GLWEAutomorphism + + GaloisElement + + ModuleLogN + + GLWERotate + + GLWESub + + GLWEShift + + GLWEAdd + + GLWENormalize + + GLWECopy + + GLWEAlloc, +{ + /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] + /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] + fn glwe_pack( + &self, + cts: &mut HashMap, + log_gap_out: usize, + keys: &HashMap, + scratch: &mut Scratch, + ) where + R: GLWEToMut + GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + Scratch: ScratchTakeCore, + { + #[cfg(debug_assertions)] + { + assert!(*cts.keys().max().unwrap() < self.n()) + } + + let log_n: usize = self.log_n(); + + for i in 0..(log_n - log_gap_out) { + let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); + + let key: &K = if i == 0 { + keys.get(&-1).unwrap() + } else { + keys.get(&self.galois_element(1 << (i - 1))).unwrap() + }; + + for j in 0..t { + let mut a: Option<&mut R> = cts.remove(&j); + let mut b: Option<&mut R> = cts.remove(&(j + t)); + + pack_internal(self, &mut a, &mut b, i, key, scratch); + + if let Some(a) = a { + cts.insert(j, a); + } else if let Some(b) = b { + cts.insert(j, b); + } + } + } + } } fn pack_core( @@ -169,8 +240,16 @@ fn pack_core( scratch: &mut Scratch, ) where A: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef, - M: GLWEAutomorphism + ModuleLogN + VecZnxCopy, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + M: ModuleLogN + + GLWEAutomorphism + + GaloisElement + + GLWERotate + + GLWESub + + GLWEShift + + GLWEAdd + + GLWENormalize + + GLWECopy, Scratch: ScratchTakeCore, { let log_n: usize = module.log_n(); @@ -188,7 +267,7 @@ fn pack_core( // No previous value -> copies and sets flags accordingly if let Some(a_ref) = a { - acc_mut_ref.data.copy(module, a_ref); + module.glwe_copy(&mut acc_mut_ref.data, a_ref); acc_mut_ref.value = true } else { acc_mut_ref.value = false @@ -222,16 +301,6 @@ fn pack_core( } } -fn combine_tmp_bytes(module: &M, res_infos: &R, key_infos: &K) -> usize -where - R: GLWEInfos, - K: GGLWEInfos, - M: GLWEAlloc + GLWEAutomorphism, -{ - GLWE::bytes_of_from_infos(module, res_infos) - + (GLWE::rsh_tmp_bytes(module.n()) | module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) -} - /// [combine] merges two ciphertexts together. fn combine( module: &M, @@ -242,8 +311,9 @@ fn combine( scratch: &mut Scratch, ) where B: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef, - M: GLWEAutomorphism + GaloisElement + VecZnxRotateInplace, + M: GLWEAutomorphism + GaloisElement + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, + B: GLWEToRef + GLWEInfos, + K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); @@ -272,50 +342,50 @@ fn combine( let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); // a = a * X^-t - a.rotate_inplace(module, -t, scratch_1); + module.glwe_rotate_inplace(-t, a, scratch_1); // tmp_b = a * X^-t - b - tmp_b.sub(module, a, b); - tmp_b.rsh(module, 1, scratch_1); + module.glwe_sub(&mut tmp_b, a, b); + module.glwe_rsh(1, &mut tmp_b, scratch_1); // a = a * X^-t + b - a.add_inplace(module, b); - a.rsh(module, 1, scratch_1); + module.glwe_add_inplace(a, b); + module.glwe_rsh(1, a, scratch_1); - tmp_b.normalize_inplace(module, scratch_1); + module.glwe_normalize_inplace(&mut tmp_b, scratch_1); // tmp_b = phi(a * X^-t - b) - if let Some(key) = auto_keys.get(&gal_el) { - tmp_b.automorphism_inplace(module, key, scratch_1); + if let Some(auto_key) = auto_keys.get(&gal_el) { + module.glwe_automorphism_inplace(&mut tmp_b, auto_key, scratch_1); } else { panic!("auto_key[{gal_el}] not found"); } // a = a * X^-t + b - phi(a * X^-t - b) - a.sub_inplace_ab(module, &tmp_b); - a.normalize_inplace(module, scratch_1); + module.glwe_sub_inplace(a, &tmp_b); + module.glwe_normalize_inplace(a, scratch_1); // a = a + b * X^t - phi(a * X^-t - b) * X^t // = a + b * X^t - phi(a * X^-t - b) * - phi(X^t) // = a + b * X^t + phi(a - b * X^t) - a.rotate_inplace(module, t, scratch_1); + module.glwe_rotate_inplace(t, a, scratch_1); } else { - a.rsh(module, 1, scratch); + module.glwe_rsh(1, a, scratch); // a = a + phi(a) - if let Some(key) = auto_keys.get(&gal_el) { - a.automorphism_add_inplace(module, key, scratch); + if let Some(auto_key) = auto_keys.get(&gal_el) { + module.glwe_automorphism_add_inplace(a, auto_key, scratch); } else { panic!("auto_key[{gal_el}] not found"); } } } else if let Some(b) = b { - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(a); - tmp_b.rotate(module, 1 << (log_n - i - 1), b); - tmp_b.rsh(module, 1, scratch_1); + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(module, a); + module.glwe_rotate(t, &mut tmp_b, b); + module.glwe_rsh(1, &mut tmp_b, scratch_1); // a = (b* X^t - phi(b* X^t)) - if let Some(key) = auto_keys.get(&gal_el) { - a.automorphism_sub_negate(module, &tmp_b, key, scratch_1); + if let Some(auto_key) = auto_keys.get(&gal_el) { + module.glwe_automorphism_sub_negate(a, &tmp_b, auto_key, scratch_1); } else { panic!("auto_key[{gal_el}] not found"); } @@ -324,62 +394,6 @@ fn combine( } } -pub trait GLWEPacking -where - Self: GLWEAutomorphism - + GaloisElement - + ModuleLogN - + GLWERotate - + GLWESub - + GLWEShift - + GLWEAdd - + GLWENormalize, -{ - /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] - /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] - fn glwe_pack( - &self, - cts: &mut HashMap, - log_gap_out: usize, - keys: &HashMap, - scratch: &mut Scratch, - ) where - R: GLWEToMut + GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, - Scratch: ScratchTakeCore, - { - #[cfg(debug_assertions)] - { - assert!(*cts.keys().max().unwrap() < self.n()) - } - - let log_n: usize = self.log_n(); - - for i in 0..(log_n - log_gap_out){ - let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); - - let key: &K = if i == 0 { - keys.get(&-1).unwrap() - } else { - keys.get(&self.galois_element(1 << (i - 1))).unwrap() - }; - - for j in 0..t{ - let mut a: Option<&mut R> = cts.remove(&j); - let mut b: Option<&mut R> = cts.remove(&(j + t)); - - pack_internal(self, &mut a, &mut b, i, key, scratch); - - if let Some(a) = a { - cts.insert(j, a); - } else if let Some(b) = b { - cts.insert(j, b); - } - }; - }; - } -} - #[allow(clippy::too_many_arguments)] fn pack_internal( module: &M, diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 3e507e2..27021e7 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -277,6 +277,10 @@ pub trait GLWEShift where Self: ModuleN + VecZnxRshInplace, { + fn glwe_rsh_tmp_byte(&self) -> usize { + VecZnx::rsh_tmp_bytes(self.n()) + } + fn glwe_rsh(&self, k: usize, res: &mut R, scratch: &mut Scratch) where R: GLWEToMut, @@ -291,8 +295,11 @@ where } impl GLWE> { - pub fn rsh_tmp_bytes(n: usize) -> usize { - VecZnx::rsh_tmp_bytes(n) + pub fn rsh_tmp_bytes(module: &M) -> usize + where + M: GLWEShift, + { + module.glwe_rsh_tmp_byte() } } From 920b9252162ee5704592b75b252d1dfd8424457f Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 22:40:23 +0200 Subject: [PATCH 28/60] conversions --- poulpy-core/src/conversion/glwe_to_lwe.rs | 183 ++++++++++++---------- poulpy-core/src/conversion/lwe_to_glwe.rs | 134 ++++++++-------- 2 files changed, 177 insertions(+), 140 deletions(-) diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index b6c6ed1..49744ba 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -1,100 +1,125 @@ use poulpy_hal::{ - api::{ - VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, + api::ModuleN, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}; +use crate::{ + GLWEKeyswitch, ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, + prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, + }, +}; -impl LWE> { - pub fn from_glwe_tmp_bytes( - module: &Module, - lwe_infos: &OUT, - glwe_infos: &IN, - key_infos: &KEY, - ) -> usize +pub trait LWESampleExtract +where + Self: ModuleN, +{ + fn lwe_sample_extract(&self, res: &mut R, a: &A) where - OUT: LWEInfos, - IN: GLWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: LWEToMut, + A: GLWEToRef, { - let glwe_layout: GLWELayout = GLWELayout { - n: module.n().into(), + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + + assert!(res.n() <= a.n()); + assert_eq!(a.n(), self.n() as u32); + assert!(res.base2k() == a.base2k()); + + let min_size: usize = res.size().min(a.size()); + let n: usize = res.n().into(); + + res.data.zero(); + (0..min_size).for_each(|i| { + let data_lwe: &mut [i64] = res.data.at_mut(0, i); + data_lwe[0] = a.data.at(0, i)[0]; + data_lwe[1..].copy_from_slice(&a.data.at(1, i)[..n]); + }); + } +} + +impl LWESampleExtract for Module where Self: ModuleN {} +impl LWEFromGLWE for Module where Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract {} + +pub trait LWEFromGLWE +where + Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, +{ + fn lwe_from_glwe_tmp_bytes(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize + where + R: LWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + { + let res_infos: GLWELayout = GLWELayout { + n: self.n().into(), base2k: lwe_infos.base2k(), k: lwe_infos.k(), rank: Rank(1), }; - GLWE::bytes_of( - module.n().into(), - lwe_infos.base2k(), - lwe_infos.k(), - 1u32.into(), - ) + GLWE::keyswitch_tmp_bytes(module, &glwe_layout, glwe_infos, key_infos) - } -} - -impl LWE { - pub fn sample_extract(&mut self, a: &GLWE) { - #[cfg(debug_assertions)] - { - assert!(self.n() <= a.n()); - assert!(self.base2k() == a.base2k()); - } - - let min_size: usize = self.size().min(a.size()); - let n: usize = self.n().into(); - - self.data.zero(); - (0..min_size).for_each(|i| { - let data_lwe: &mut [i64] = self.data.at_mut(0, i); - data_lwe[0] = a.data.at(0, i)[0]; - data_lwe[1..].copy_from_slice(&a.data.at(1, i)[..n]); - }); + self.bytes_of_glwe(lwe_infos.base2k(), lwe_infos.k(), 1u32.into()) + + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) } - pub fn from_glwe( - &mut self, - module: &Module, - a: &GLWE, - ks: &GLWEToLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - DGlwe: DataRef, - DKs: DataRef, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch:, + fn lwe_from_glwe(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: LWEToMut, + A: GLWEToRef, + K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert_eq!(a.n(), module.n() as u32); - assert_eq!(ks.n(), module.n() as u32); - assert!(self.n() <= module.n() as u32); - } + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); + + assert_eq!(a.n(), self.n() as u32); + assert_eq!(key.n(), self.n() as u32); + assert!(res.n() <= self.n() as u32); let glwe_layout: GLWELayout = GLWELayout { - n: module.n().into(), - base2k: self.base2k(), - k: self.k(), + n: self.n().into(), + base2k: res.base2k(), + k: res.k(), rank: Rank(1), }; - let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(&glwe_layout); - tmp_glwe.keyswitch(module, a, &ks.0, scratch_1); - self.sample_extract(&tmp_glwe); + let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout); + self.glwe_keyswitch(&mut tmp_glwe, a, &key.0, scratch_1); + self.lwe_sample_extract(res, &tmp_glwe); + } +} + +impl LWE> { + pub fn from_glwe_tmp_bytes(module: &M, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize + where + R: LWEInfos, + A: GLWEInfos, + K: GGLWEInfos, + M: LWEFromGLWE, + { + module.lwe_from_glwe_tmp_bytes(lwe_infos, glwe_infos, key_infos) + } +} + +impl LWE { + pub fn sample_extract(&mut self, module: &M, a: &A) + where + A: GLWEToRef, + M: LWESampleExtract, + { + module.lwe_sample_extract(self, a); + } + + pub fn from_glwe(&self, module: &M, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + where + R: LWEToMut, + A: GLWEToRef, + K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + M: LWEFromGLWE, + Scratch: ScratchTakeCore, + { + module.lwe_from_glwe(res, a, key, scratch); } } diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index c4a3b88..7be6574 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -1,79 +1,67 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnx, ZnxView, ZnxViewMut, ZnxZero}, + api::ScratchTakeBasic, + layouts::{Backend, DataMut, Module, Scratch, VecZnx, ZnxView, ZnxViewMut, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}; +use crate::{ + GLWEKeyswitch, ScratchTakeCore, + layouts::{ + GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, + prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, + }, +}; -impl GLWE> { - pub fn from_lwe_tmp_bytes( - module: &Module, - glwe_infos: &OUT, - lwe_infos: &IN, - key_infos: &KEY, - ) -> usize +impl GLWEFromLWE for Module where Self: GLWEKeyswitch + GLWEAlloc {} + +pub trait GLWEFromLWE +where + Self: GLWEKeyswitch + GLWEAlloc, +{ + fn glwe_from_lwe_tmp_bytes(&self, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize where - OUT: GLWEInfos, - IN: LWEInfos, - KEY: GGLWEInfos, - Module: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GLWEInfos, + A: LWEInfos, + K: GGLWEInfos, { - let ct: usize = GLWE::bytes_of( - module.n().into(), + let ct: usize = self.bytes_of_glwe( key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), ); - let ks: usize = GLWE::keyswitch_inplace_tmp_bytes(module, glwe_infos, key_infos); + + let ks: usize = self.glwe_keyswitch_tmp_bytes(glwe_infos, glwe_infos, key_infos); if lwe_infos.base2k() == key_infos.base2k() { ct + ks } else { - let a_conv = VecZnx::bytes_of(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); + let a_conv = VecZnx::bytes_of(self.n(), 1, lwe_infos.size()) + self.vec_znx_normalize_tmp_bytes(); ct + a_conv + ks } } -} -impl GLWE { - pub fn from_lwe( - &mut self, - module: &Module, - lwe: &LWE, - ksk: &LWEToGLWESwitchingKeyPrepared, - scratch: &mut Scratch, - ) where - DLwe: DataRef, - DKsk: DataRef, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable, + fn glwe_from_lwe(&self, res: &mut R, lwe: &A, ksk: &K, scratch: &mut Scratch) + where + R: GLWEToMut, + A: LWEToRef, + K: LWEToGLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert_eq!(self.n(), module.n() as u32); - assert_eq!(ksk.n(), module.n() as u32); - assert!(lwe.n() <= module.n() as u32); - } + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let lwe: &LWE<&[u8]> = &lwe.to_ref(); + let ksk: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); - let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWELayout { - n: ksk.n(), - base2k: ksk.base2k(), - k: lwe.k(), - rank: 1u32.into(), - }); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(ksk.n(), self.n() as u32); + assert!(lwe.n() <= self.n() as u32); + + let (mut glwe, scratch_1) = scratch.take_glwe_ct( + self, + &GLWELayout { + n: ksk.n(), + base2k: ksk.base2k(), + k: lwe.k(), + rank: 1u32.into(), + }, + ); glwe.data.zero(); let n_lwe: usize = lwe.n().into(); @@ -85,14 +73,14 @@ impl GLWE { glwe.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module.n(), 1, lwe.size()); + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, lwe.size()); a_conv.zero(); for j in 0..lwe.size() { let data_lwe: &[i64] = lwe.data.at(0, j); a_conv.at_mut(0, j)[0] = data_lwe[0] } - module.vec_znx_normalize( + self.vec_znx_normalize( ksk.base2k().into(), &mut glwe.data, 0, @@ -108,7 +96,7 @@ impl GLWE { a_conv.at_mut(0, j)[..n_lwe].copy_from_slice(&data_lwe[1..]); } - module.vec_znx_normalize( + self.vec_znx_normalize( ksk.base2k().into(), &mut glwe.data, 1, @@ -119,6 +107,30 @@ impl GLWE { ); } - self.keyswitch(module, &glwe, &ksk.0, scratch_1); + self.glwe_keyswitch(res, &glwe, &ksk.0, scratch_1); + } +} + +impl GLWE> { + pub fn from_lwe_tmp_bytes(module: &M, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize + where + R: GLWEInfos, + A: LWEInfos, + K: GGLWEInfos, + M: GLWEFromLWE, + { + module.glwe_from_lwe_tmp_bytes(glwe_infos, lwe_infos, key_infos) + } +} + +impl GLWE { + pub fn from_lwe(&mut self, module: &M, lwe: &A, ksk: &K, scratch: &mut Scratch) + where + M: GLWEFromLWE, + A: LWEToRef, + K: LWEToGLWESwitchingKeyPreparedToRef, + Scratch: ScratchTakeCore, + { + module.glwe_from_lwe(self, lwe, ksk, scratch); } } From 90a25638c248750ece4eb1182fc4f359a085cfd5 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 16 Oct 2025 22:41:40 +0200 Subject: [PATCH 29/60] fix keyswitching --- poulpy-core/src/conversion/mod.rs | 2 ++ poulpy-core/src/keyswitching/lwe_ct.rs | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/poulpy-core/src/conversion/mod.rs b/poulpy-core/src/conversion/mod.rs index 9771531..8be980a 100644 --- a/poulpy-core/src/conversion/mod.rs +++ b/poulpy-core/src/conversion/mod.rs @@ -3,3 +3,5 @@ mod glwe_to_lwe; mod lwe_to_glwe; pub use gglwe_to_ggsw::*; +pub use glwe_to_lwe::*; +pub use lwe_to_glwe::*; diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index ff9fb5f..7aa5321 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + LWESampleExtract, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, @@ -40,7 +40,7 @@ impl LWEKeySwitch for Module where Self: LWEKeySwitch { pub trait LWEKeySwitch where - Self: GLWEKeyswitch + GLWEAlloc, + Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, { fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where @@ -121,6 +121,6 @@ where } self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); - res.sample_extract(&glwe_out); + self.lwe_sample_extract(res, &glwe_out); } } From a5600593cacee6fd76ea86fd83806aec0422b1bf Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Thu, 16 Oct 2025 17:42:39 -0400 Subject: [PATCH 30/60] ct, pk, sk, ksk for glwe and lwe --- poulpy-core/src/encryption/gglwe_atk.rs | 42 +------ poulpy-core/src/encryption/gglwe_ct.rs | 50 +++++++-- poulpy-core/src/encryption/gglwe_ksk.rs | 81 ++++++++++++-- poulpy-core/src/encryption/glwe_ct.rs | 143 ++++++++++++++---------- poulpy-core/src/encryption/glwe_pk.rs | 36 +++--- poulpy-core/src/encryption/lwe_ct.rs | 17 ++- poulpy-core/src/encryption/lwe_ksk.rs | 135 ++++++++++++++++++---- 7 files changed, 345 insertions(+), 159 deletions(-) diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 2e87707..41ecd61 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -6,7 +6,6 @@ use poulpy_hal::{ VecZnxSwitchRing, }, layouts::{Backend, DataMut, Module, Scratch}, - layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; @@ -17,8 +16,6 @@ use crate::{ }, }; -impl AutomorphismKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize impl AutomorphismKey> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where @@ -31,10 +28,8 @@ impl AutomorphismKey> { "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) } - pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize where A: GGLWEInfos, @@ -45,7 +40,6 @@ impl AutomorphismKey> { "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) - GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) } } @@ -63,25 +57,6 @@ pub trait GGLWEAutomorphismKeyEncryptSk { B: GLWESecretToRef; } -impl AutomorphismKey -where - Self: AutomorphismKeyToMut, -{ - pub fn encrypt_sk( -pub trait GGLWEAutomorphismKeyEncryptSk { - fn gglwe_automorphism_key_encrypt_sk( - &self, - res: &mut A, - p: i64, - sk: &B, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; -} - impl AutomorphismKey where Self: AutomorphismKeyToMut, @@ -89,14 +64,11 @@ where pub fn encrypt_sk( &mut self, module: &Module, - module: &Module, p: i64, sk: &S, - sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - scratch: &mut Scratch, ) where S: GLWESecretToRef, Module: GGLWEAutomorphismKeyEncryptSk, @@ -149,29 +121,20 @@ where { use crate::layouts::{GLWEInfos, LWEInfos}; - assert_eq!(res.n(), sk.n()); - assert_eq!(res.rank_out(), res.rank_in()); - assert_eq!(sk.rank(), res.rank_out()); assert_eq!(res.n(), sk.n()); assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), AutomorphismKey::encrypt_sk_tmp_bytes(self, res) - AutomorphismKey::encrypt_sk_tmp_bytes(self, res) ) } let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - (0..res.rank_out().into()).for_each(|i| { - self.vec_znx_automorphism( - self.galois_element_inv(p), (0..res.rank_out().into()).for_each(|i| { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -183,12 +146,9 @@ where }); } - res.key - .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); res.key .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); res.p = p; - res.p = p; } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index d0ab3a0..1f7deec 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,5 +1,6 @@ use poulpy_hal::{ api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + // oep::SvpPPolAllocBytesImpl, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; @@ -13,24 +14,34 @@ use crate::{ }; impl GGLWE> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: GGLWEEncryptSk, { - GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) - + (GLWEPlaintext::bytes_of_from_infos(module, &infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + module.gglwe_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(_module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GGLWEEncryptSk, { - unimplemented!() + module.gglwe_encrypt_sk_tmp_bytes(infos) } } pub trait GGLWEEncryptSk { + + fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn gglwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + fn gglwe_encrypt_sk( &self, res: &mut R, @@ -47,9 +58,34 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: ModuleN + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Module: ModuleN + + GLWEEncryptSk + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAddScalarInplace + + VecZnxNormalizeInplace, + // + SvpPPolAllocBytesImpl, Scratch: ScratchAvailable + ScratchTakeCore, { + fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) + + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) + } + + fn gglwe_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize + where + A: GGLWEInfos, + { + unimplemented!() + // TODO: Is this correct? + // GLWE::encrypt_pk_tmp_bytes(self, &infos.glwe_layout()) + // + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) + + } + fn gglwe_encrypt_sk( &self, res: &mut R, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index c825c73..0ba6047 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -12,27 +12,45 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, }, + encryption::gglwe_ct::GGLWEEncryptSk, }; impl GLWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + M: GLWESwitchingKeyEncryptSk, { - (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) - + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) + module.glwe_switching_key_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GLWESwitchingKeyEncryptSk, { - GGLWE::encrypt_pk_tmp_bytes(module, _infos) + module.glwe_switching_key_encrypt_pk_tmp_bytes(infos) } + + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + // { + // (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + // + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + // + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) + // } + + // pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize + // where + // A: GGLWEInfos, + // { + // GGLWE::encrypt_pk_tmp_bytes(module, _infos) + // } } impl GLWESwitchingKey { @@ -112,3 +130,50 @@ impl GLWESwitchingKey { self.sk_out_n = sk_out.n().into(); } } + + +pub trait GLWESwitchingKeyEncryptSk +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyEncryptSk for Module where + Self: ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + + fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + (GGLWE::encrypt_sk_tmp_bytes(self, infos) | ScalarZnx::bytes_of(self.n(), 1)) + + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout()) + } + + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GGLWE::encrypt_pk_tmp_bytes(self, infos) + } + +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index fa314d5..a23d7c6 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -19,25 +19,21 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GLWEEncryptSk, { - let size: usize = infos.size(); - assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.bytes_of_vec_znx_dft(1, size) + module.glwe_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + M: GLWEEncryptPk { - let size: usize = infos.size(); - assert_eq!(module.n() as u32, infos.n()); - ((module.bytes_of_vec_znx_dft(1, size) + module.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) - + module.bytes_of_svp_ppol(1) - + module.vec_znx_normalize_tmp_bytes() + module.glwe_encrypt_pk_tmp_bytes(infos) } } @@ -67,7 +63,7 @@ impl GLWE { scratch: &mut Scratch, ) where S: GLWESecretPreparedToRef, - Module: GLWEEncryptZeroSk, + Module: GLWEEncryptSk, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); } @@ -97,13 +93,20 @@ impl GLWE { scratch: &mut Scratch, ) where K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptZeroPk, + Module: GLWEEncryptPk, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); } } pub trait GLWEEncryptSk { + fn glwe_encrypt_sk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_sk( &self, res: &mut R, @@ -116,13 +119,40 @@ pub trait GLWEEncryptSk { R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; + + fn glwe_encrypt_zero_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + S: GLWESecretPreparedToRef; } impl GLWEEncryptSk for Module where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module: Sized + + ModuleN + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { + fn glwe_encrypt_sk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos, + { + let size: usize = infos.size(); + assert_eq!(self.n() as u32, infos.n()); + self.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(self.n(), 1, size) + self.bytes_of_vec_znx_dft(1, size) + } + fn glwe_encrypt_sk( &self, res: &mut R, @@ -169,26 +199,7 @@ where scratch, ); } -} -pub trait GLWEEncryptZeroSk { - fn glwe_encrypt_zero_sk( - &self, - res: &mut R, - sk: &S, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - R: GLWEToMut, - S: GLWESecretPreparedToRef; -} - -impl GLWEEncryptZeroSk for Module -where - Module: GLWEEncryptSkInternal + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - Scratch: ScratchAvailable, -{ fn glwe_encrypt_zero_sk( &self, res: &mut R, @@ -209,10 +220,10 @@ where assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); assert!( - scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), + scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(&res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", scratch.available(), - GLWE::encrypt_sk_tmp_bytes(self, &res) + self.glwe_encrypt_sk_tmp_bytes(&res) ) } @@ -230,10 +241,17 @@ where SIGMA, scratch, ); - } + } } pub trait GLWEEncryptPk { + fn glwe_encrypt_pk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos; + fn glwe_encrypt_pk( &self, res: &mut R, @@ -246,12 +264,43 @@ pub trait GLWEEncryptPk { R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; + + fn glwe_encrypt_zero_pk( + &self, + res: &mut R, + pk: &K, + source_xu: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToMut, + K: GLWEPublicKeyPreparedToRef; + + } impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal, + Module: GLWEEncryptPkInternal + + VecZnxDftBytesOf + + SvpPPolBytesOf + + VecZnxBigBytesOf + + VecZnxNormalizeTmpBytes, { + fn glwe_encrypt_pk_tmp_bytes( + &self, + infos: &A, + ) -> usize + where + A: GLWEInfos, + { + let size: usize = infos.size(); + assert_eq!(self.n() as u32, infos.n()); + ((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(self.n(), 1)) + + self.bytes_of_svp_ppol(1) + + self.vec_znx_normalize_tmp_bytes() + } + fn glwe_encrypt_pk( &self, res: &mut R, @@ -267,25 +316,7 @@ where { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); } -} -pub trait GLWEEncryptZeroPk { - fn glwe_encrypt_zero_pk( - &self, - res: &mut R, - pk: &K, - source_xu: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; -} - -impl GLWEEncryptZeroPk for Module -where - Module: GLWEEncryptPkInternal, -{ fn glwe_encrypt_zero_pk( &self, res: &mut R, diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index 9073cfe..b28f2fd 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -5,13 +5,27 @@ use poulpy_hal::{ }; use crate::{ - encryption::glwe_ct::GLWEEncryptZeroSk, + encryption::glwe_ct::{GLWEEncryptSk}, layouts::{ GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; +impl GLWEPublicKey { + pub fn generate( + &mut self, + module: &Module, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + ) where + Module: GLWEPublicKeyGenerate, + { + module.glwe_public_key_generate(self, sk, source_xa, source_xe); + } +} + pub trait GLWEPublicKeyGenerate { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where @@ -21,7 +35,7 @@ pub trait GLWEPublicKeyGenerate { impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptZeroSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) @@ -45,25 +59,11 @@ where } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(self, res)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; } -} - -impl GLWEPublicKey { - pub fn generate( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module: GLWEPublicKeyGenerate, - { - module.glwe_public_key_generate(self, sk, source_xa, source_xe); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 5a743eb..a91b2b7 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -23,10 +23,20 @@ impl LWE { } } - pub trait LWEEncryptSk where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, +{ + fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + R: LWEToMut, + P: LWEPlaintextToRef, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl; +} + +impl LWEEncryptSk for Module where + Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where @@ -93,9 +103,4 @@ where res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; }); } -} - -impl LWEEncryptSk for Module where - Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, -{ } \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index ed1c5d6..063240e 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -10,38 +10,47 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, - prepared::GLWESecretPrepared, + GLWESecretAlloc, + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, + prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, + ScratchTakeCore, }; impl LWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: LWESwitchingKeyEncrypt, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GLWESecret::bytes_of(module, Rank(1)) - + GLWESecretPrepared::bytes_of(module, Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + module.lwe_switching_key_encrypt_sk_tmp_bytes(infos) } + + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + // { + // debug_assert_eq!( + // infos.dsize().0, + // 1, + // "dsize > 1 is not supported for LWESwitchingKey" + // ); + // debug_assert_eq!( + // infos.rank_in().0, + // 1, + // "rank_in > 1 is not supported for LWESwitchingKey" + // ); + // debug_assert_eq!( + // infos.rank_out().0, + // 1, + // "rank_out > 1 is not supported for LWESwitchingKey" + // ); + // GLWESecret::bytes_of(module, Rank(1)) + // + GLWESecretPrepared::bytes_of(module, Rank(1)) + // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + // } } impl LWESwitchingKey { @@ -107,3 +116,83 @@ impl LWESwitchingKey { ); } } + +pub trait LWESwitchingKeyEncrypt +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl LWESwitchingKeyEncrypt for Module where + Self: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPrepare + + VecZnxSwitchRing + + GLWESecretAlloc + + GLWESecretPreparedAlloc +{ + fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos + { + debug_assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + debug_assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + GLWESecret::bytes_of(self, Rank(1)) + + GLWESecretPrepared::bytes_of(self, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + } +} \ No newline at end of file From 1247d3e4b724c023d1fc68710cd3dbbbc09a0df2 Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Thu, 16 Oct 2025 23:49:33 -0400 Subject: [PATCH 31/60] non-compressed finished --- poulpy-core/src/encryption/gglwe_atk.rs | 116 +++++-- poulpy-core/src/encryption/gglwe_ct.rs | 1 - poulpy-core/src/encryption/gglwe_ksk.rs | 294 ++++++++++++------ poulpy-core/src/encryption/gglwe_tsk.rs | 264 ++++++++++++---- poulpy-core/src/encryption/ggsw_ct.rs | 80 +++-- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 220 ++++++++++--- poulpy-core/src/encryption/lwe_ksk.rs | 27 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 232 +++++++++++--- 8 files changed, 917 insertions(+), 317 deletions(-) diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 41ecd61..dfd1e97 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -5,58 +5,36 @@ use poulpy_hal::{ VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, - layouts::{Backend, DataMut, Module, Scratch}, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, }, }; impl AutomorphismKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + M: GGLWEAutomorphismKeyEncryptSk { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of_from_infos(module, &infos.glwe_layout()) + module.gglwe_automorphism_key_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize + pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, + M: GGLWEAutomorphismKeyEncryptPk { - assert_eq!( - _infos.rank_in(), - _infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::encrypt_pk_tmp_bytes(module, _infos) + module.gglwe_automorphism_key_encrypt_pk_tmp_bytes(infos) } } -pub trait GGLWEAutomorphismKeyEncryptSk { - fn gglwe_automorphism_key_encrypt_sk( - &self, - res: &mut A, - p: i64, - sk: &B, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; -} - impl AutomorphismKey where Self: AutomorphismKeyToMut, @@ -77,6 +55,24 @@ where } } +pub trait GGLWEAutomorphismKeyEncryptSk { + fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn gglwe_automorphism_key_encrypt_sk( + &self, + res: &mut A, + p: i64, + sk: &B, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + A: AutomorphismKeyToMut, + B: GLWESecretToRef; +} + impl GGLWEAutomorphismKeyEncryptSk for Module where Module: ModuleN @@ -99,9 +95,22 @@ where + SvpPPolBytesOf + VecZnxAutomorphism + SvpPPolAlloc - + SvpPPolBytesOf, + + GaloisElement, Scratch: ScratchAvailable + ScratchTakeCore, { + + fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for GGLWEAutomorphismKey" + ); + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of_from_infos(self, &infos.glwe_layout()) + } + fn gglwe_automorphism_key_encrypt_sk( &self, res: &mut A, @@ -151,4 +160,47 @@ where res.p = p; } +} + + +pub trait GGLWEAutomorphismKeyEncryptPk { + fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + +} + +impl GGLWEAutomorphismKeyEncryptPk for Module +where + Module: ModuleN + + VecZnxAddScalarInplace + + VecZnxDftBytesOf + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalizeTmpBytes + + VecZnxFillUniform + + VecZnxSubInplace + + VecZnxAddInplace + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxNormalize + + VecZnxSub + + SvpPPolBytesOf + + SvpPPolAlloc, + Scratch: ScratchAvailable + ScratchTakeCore, +{ + + fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for GGLWEAutomorphismKey" + ); + GLWESwitchingKey::encrypt_pk_tmp_bytes(self, infos) + } } \ No newline at end of file diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 1f7deec..e8ae4f8 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -64,7 +64,6 @@ where + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - // + SvpPPolAllocBytesImpl, Scratch: ScratchAvailable + ScratchTakeCore, { fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 0ba6047..bd13c2b 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -12,7 +12,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, prepared::GLWESecretPrepared, }, encryption::gglwe_ct::GGLWEEncryptSk, }; @@ -26,109 +26,108 @@ impl GLWESwitchingKey> { module.glwe_switching_key_encrypt_sk_tmp_bytes(infos) } - pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWESwitchingKeyEncryptSk, + M: GLWESwitchingKeyEncryptPk, { module.glwe_switching_key_encrypt_pk_tmp_bytes(infos) } - - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, - // { - // (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) - // + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) - // + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) - // } - - // pub fn encrypt_pk_tmp_bytes(module: &Module, _infos: &A) -> usize - // where - // A: GGLWEInfos, - // { - // GGLWE::encrypt_pk_tmp_bytes(module, _infos) - // } } impl GLWESwitchingKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, sk_in: &GLWESecret, sk_out: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: ModuleN - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + M: GLWESwitchingKeyEncryptSk, + Scratch: ScratchAvailable + ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert!(sk_in.n().0 <= module.n() as u32); - assert!(sk_out.n().0 <= module.n() as u32); - assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - scratch.available(), - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) - ) - } - - let n: usize = sk_in.n().max(sk_out.n()).into(); - - let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into()); - (0..sk_in.rank().into()).for_each(|i| { - module.vec_znx_switch_ring( - &mut sk_in_tmp.as_vec_znx_mut(), - i, - &sk_in.data.as_vec_znx(), - i, - ); - }); - - let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank()); - { - let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1); - (0..sk_out.rank().into()).for_each(|i| { - module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); - module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - }); - } - - self.key.encrypt_sk( - module, - &sk_in_tmp, - &sk_out_tmp, - source_xa, - source_xe, - scratch_2, - ); - self.sk_in_n = sk_in.n().into(); - self.sk_out_n = sk_out.n().into(); + module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch); } + + + // #[allow(clippy::too_many_arguments)] + // pub fn encrypt_sk( + // &mut self, + // module: &Module, + // sk_in: &GLWESecret, + // sk_out: &GLWESecret, + // source_xa: &mut Source, + // source_xe: &mut Source, + // scratch: &mut Scratch, + // ) where + // Module: ModuleN + // + VecZnxAddScalarInplace + // + VecZnxDftBytesOf + // + VecZnxBigNormalize + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxNormalizeTmpBytes + // + VecZnxFillUniform + // + VecZnxSubInplace + // + VecZnxAddInplace + // + VecZnxNormalizeInplace + // + VecZnxAddNormal + // + VecZnxNormalize + // + VecZnxSub + // + SvpPrepare + // + VecZnxSwitchRing + // + SvpPPolBytesOf + // + SvpPPolAlloc, + // Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + // { + // #[cfg(debug_assertions)] + // { + // assert!(sk_in.n().0 <= module.n() as u32); + // assert!(sk_out.n().0 <= module.n() as u32); + // assert!( + // scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), + // "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + // scratch.available(), + // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) + // ) + // } + + // // let n: usize = sk_in.n().max(sk_out.n()).into(); + + // let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into()); + // (0..sk_in.rank().into()).for_each(|i| { + // module.vec_znx_switch_ring( + // &mut sk_in_tmp.as_vec_znx_mut(), + // i, + // &sk_in.data.as_vec_znx(), + // i, + // ); + // }); + + // let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank()); + // { + // let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1); + // (0..sk_out.rank().into()).for_each(|i| { + // module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); + // module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); + // }); + // } + + // self.key.encrypt_sk( + // module, + // &sk_in_tmp, + // &sk_out_tmp, + // source_xa, + // source_xe, + // scratch_2, + // ); + // self.sk_in_n = sk_in.n().into(); + // self.sk_out_n = sk_out.n().into(); + // } } @@ -146,9 +145,21 @@ where where A: GGLWEInfos; - fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; + fn glwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_in: &GLWESecret, + sk_out: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) + where + R: GLWESwitchingKeyToMut, + DataSkIn: DataRef, + DataSkOut: DataRef, + Scratch: ScratchAvailable + ScratchTakeCore; + } impl GLWESwitchingKeyEncryptSk for Module where @@ -157,7 +168,15 @@ impl GLWESwitchingKeyEncryptSk for Module where + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc - + GGLWEEncryptSk, + + GGLWEEncryptSk + + VecZnxSwitchRing + + SvpPrepare + + VecZnxIdftApplyConsume + + VecZnxNormalize + + VecZnxNormalizeInplace + + VecZnxAddInplace + + VecZnxAddNormal + + VecZnxSub { fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -169,11 +188,102 @@ impl GLWESwitchingKeyEncryptSk for Module where + GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout()) } + fn glwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_in: &GLWESecret, + sk_out: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) + where + R: GLWESwitchingKeyToMut, + DataSkIn: DataRef, + DataSkOut: DataRef, + Scratch: ScratchAvailable + ScratchTakeCore, + { + + let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); + let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); + let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); + + #[cfg(debug_assertions)] + { + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); + assert!( + scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + scratch.available(), + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) + ) + } + + // let n: usize = sk_in.n().max(sk_out.n()).into(); + + let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); + (0..sk_in.rank().into()).for_each(|i| { + self.vec_znx_switch_ring( + &mut sk_in_tmp.as_vec_znx_mut(), + i, + &sk_in.data.as_vec_znx(), + i, + ); + }); + + let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); + { + let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); + (0..sk_out.rank().into()).for_each(|i| { + self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); + self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); + }); + } + + res.key.encrypt_sk( + self, + &sk_in_tmp, + &sk_out_tmp, + source_xa, + source_xe, + scratch_2, + ); + res.sk_in_n = sk_in.n().into(); + res.sk_out_n = sk_out.n().into(); + } + +} + +pub trait GLWESwitchingKeyEncryptPk +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyEncryptPk for Module where + Self: ModuleN + + SvpPPolBytesOf + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + SvpPPolAlloc + + GGLWEEncryptSk, +{ + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { GGLWE::encrypt_pk_tmp_bytes(self, infos) } - } \ No newline at end of file diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 918eb8a..3f4a31b 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,112 +1,256 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, - oep::VecZnxBigAllocBytesImpl, + oep::{VecZnxAddScalarInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxDftApplyImpl, SvpApplyDftToDftImpl, VecZnxIdftApplyTmpAImpl, VecZnxBigNormalizeImpl}, source::Source, }; use crate::{ ScratchTakeCore, layouts::{ - GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, + GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, + prepared::GLWESecretPrepared, }, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, }; impl TensorKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + M: GGLWETensorKeyEncryptSk { - GLWESecretPrepared::bytes_of(module, infos.rank_out()) - + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) - + module.bytes_of_vec_znx_big(1, 1) - + module.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(module, Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + module.gglwe_tensor_key_encrypt_sk_tmp_bytes(infos) } + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + // { + // GLWESecretPrepared::bytes_of(module, infos.rank_out()) + // + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + // + module.bytes_of_vec_znx_big(1, 1) + // + module.bytes_of_vec_znx_dft(1, 1) + // + GLWESecret::bytes_of(module, Rank(1)) + // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + // } } impl TensorKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where + M: GGLWETensorKeyEncryptSk, GLWESecret: GetDist, - Module: ModuleN - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + VecZnxBigAllocBytesImpl - + VecZnxBigBytesOf - + SvpPPolAlloc, - Scratch: ScratchTakeBasic + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank_out(), sk.rank()); - assert_eq!(self.n(), sk.n()); - } + module.gglwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); + } + + // pub fn encrypt_sk( + // &mut self, + // module: &Module, + // sk: &GLWESecret, + // source_xa: &mut Source, + // source_xe: &mut Source, + // scratch: &mut Scratch, + // ) where + // GLWESecret: GetDist, + // Module: ModuleN + // + SvpApplyDftToDft + // + VecZnxIdftApplyTmpA + // + VecZnxAddScalarInplace + // + VecZnxDftBytesOf + // + VecZnxBigNormalize + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxNormalizeTmpBytes + // + VecZnxFillUniform + // + VecZnxSubInplace + // + VecZnxAddInplace + // + VecZnxNormalizeInplace + // + VecZnxAddNormal + // + VecZnxNormalize + // + VecZnxSub + // + SvpPrepare + // + VecZnxSwitchRing + // + SvpPPolBytesOf + // + VecZnxBigAllocBytesImpl + // + VecZnxBigBytesOf + // + SvpPPolAlloc, + // Scratch: ScratchTakeBasic + ScratchTakeCore, + // { + // #[cfg(debug_assertions)] + // { + // assert_eq!(self.rank_out(), sk.rank()); + // assert_eq!(self.n(), sk.n()); + // } + + // // let n: RingDegree = sk.n(); + // let rank: Rank = self.rank_out(); + + // let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank); + // sk_dft_prep.prepare(module, sk); + + // let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1); + + // (0..rank.into()).for_each(|i| { + // module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); + // }); + + // let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1); + // let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1)); + // let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1); + + // (0..rank.into()).for_each(|i| { + // (i..rank.into()).for_each(|j| { + // module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + + // module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); + // module.vec_znx_big_normalize( + // self.base2k().into(), + // &mut sk_ij.data.as_vec_znx_mut(), + // 0, + // self.base2k().into(), + // &sk_ij_big, + // 0, + // scratch_5, + // ); + + // self.at_mut(i, j) + // .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5); + // }); + // }) + // } +} + +pub trait GGLWETensorKeyEncryptSk +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf, +{ + fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: TensorKeyToMut, + S: GLWESecretToRef + GetDist; +} + +impl GGLWETensorKeyEncryptSk for Module where + Module: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf + + VecZnxAddScalarInplaceImpl + + VecZnxDftApply + + VecZnxDftApplyImpl + + SvpApplyDftToDftImpl + + GLWESwitchingKeyEncryptSk + + SvpApplyDftToDft + + VecZnxIdftApplyTmpAImpl + + VecZnxBigNormalizeImpl + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + + VecZnxAddScalarInplaceImpl + + SvpPrepare, + Scratch: ScratchTakeBasic + ScratchTakeCore, +{ + fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + GLWESecretPrepared::bytes_of(self, infos.rank_out()) + + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + self.bytes_of_vec_znx_big(1, 1) + + self.bytes_of_vec_znx_dft(1, 1) + + GLWESecret::bytes_of(self, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + } + + fn gglwe_tensor_key_encrypt_sk( + &self, + res: &mut R, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: TensorKeyToMut, + S: GLWESecretToRef + GetDist, + { + let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); // let n: RingDegree = sk.n(); - let rank: Rank = self.rank_out(); + let rank: Rank = res.rank_out(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank); - sk_dft_prep.prepare(module, sk); + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); + sk_dft_prep.prepare(self, sk); - let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1); + let sk: &GLWESecret<&[u8]> = &sk.to_ref(); + + #[cfg(debug_assertions)] + { + assert_eq!(res.rank_out(), sk.rank()); + assert_eq!(res.n(), sk.n()); + } + + let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank.into(), 1); (0..rank.into()).for_each(|i| { - module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); + self.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); }); - let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1); - let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1)); - let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1); + let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1); + let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1)); + let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1); (0..rank.into()).for_each(|i| { (i..rank.into()).for_each(|j| { - module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); - module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); - module.vec_znx_big_normalize( - self.base2k().into(), + self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); + self.vec_znx_big_normalize( + res.base2k().into(), &mut sk_ij.data.as_vec_znx_mut(), 0, - self.base2k().into(), + res.base2k().into(), &sk_ij_big, 0, scratch_5, ); - self.at_mut(i, j) - .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5); + res.at_mut(i, j) + .encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); }); - }) + }) } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 3357380..90bac6b 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -14,20 +14,48 @@ use crate::{ }; impl GGSW> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GGSWEncryptSk, { - let size = infos.size(); - GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) - + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) - + VecZnx::bytes_of(module.n(), 1, size) - + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) + module.ggsw_encrypt_sk_tmp_bytes(infos) + } + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGSWInfos, + // Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + // { + // let size = infos.size(); + // GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) + // + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + // + VecZnx::bytes_of(module.n(), 1, size) + // + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) + // } +} + +impl GGSW { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: &ScalarZnx, + sk: &GLWESecretPrepared, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + Module: GGSWEncryptSk, + { + module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } } pub trait GGSWEncryptSk { + fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; + fn ggsw_encrypt_sk( &self, res: &mut R, @@ -44,9 +72,26 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where - Module: ModuleN + GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Module: ModuleN + + GLWEEncryptSkInternal + + VecZnxAddScalarInplace + + VecZnxNormalizeInplace + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes, Scratch: ScratchTakeCore, { + + fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + let size = infos.size(); + GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) + + VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size) + + VecZnx::bytes_of(self.n(), 1, size) + + self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) + } + fn ggsw_encrypt_sk( &self, res: &mut R, @@ -104,21 +149,4 @@ where } } } -} - -impl GGSW { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGSWEncryptSk, - { - module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index cd3ffec..4ade4d9 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -4,82 +4,224 @@ use poulpy_hal::{ VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - }, + ScratchTakeBasic + }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, - source::Source, + oep::{ScratchAvailableImpl, VecZnxFillUniformImpl}, + source::Source }; use crate::{ ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut, prepared::GLWESecretPrepared, }, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, }; impl GLWEToLWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: GLWEToLWESwitchingKeyEncrypt, { - GLWESecretPrepared::bytes_of(module, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in())) + module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos) } + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + // { + // GLWESecretPrepared::bytes_of(module, infos.rank_in()) + // + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in())) + // } } impl GLWEToLWESwitchingKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, sk_lwe: &LWESecret, sk_glwe: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where + M: GLWEToLWESwitchingKeyEncrypt, DLwe: DataRef, DGlwe: DataRef, - Module: ModuleN - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { + module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } + // #[allow(clippy::too_many_arguments)] + // pub fn encrypt_sk( + // &mut self, + // module: &Module, + // sk_lwe: &LWESecret, + // sk_glwe: &GLWESecret, + // source_xa: &mut Source, + // source_xe: &mut Source, + // scratch: &mut Scratch, + // ) where + // DLwe: DataRef, + // DGlwe: DataRef, + // Module: ModuleN + // + VecZnxAutomorphismInplace + // + VecZnxAddScalarInplace + // + VecZnxDftBytesOf + // + VecZnxBigNormalize + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxNormalizeTmpBytes + // + VecZnxFillUniform + // + VecZnxSubInplace + // + VecZnxAddInplace + // + VecZnxNormalizeInplace + // + VecZnxAddNormal + // + VecZnxNormalize + // + VecZnxSub + // + SvpPrepare + // + VecZnxSwitchRing + // + SvpPPolBytesOf + // + SvpPPolAlloc, + // Scratch: ScratchAvailable + ScratchTakeCore, + // { + // #[cfg(debug_assertions)] + // { + // assert!(sk_lwe.n().0 <= module.n() as u32); + // } + + // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); + // sk_lwe_as_glwe.data.zero(); + // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); + // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + + // self.0.encrypt_sk( + // module, + // sk_glwe, + // &sk_lwe_as_glwe, + // source_xa, + // source_xe, + // scratch_1, + // ); + // } +} + +pub trait GLWEToLWESwitchingKeyEncrypt +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalize + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxSub + + VecZnxSubInplace + + VecZnxAddInplace + + SvpPrepare + + VecZnxSwitchRing + + ScratchAvailable + + ScratchAvailableImpl + + VecZnxFillUniform + + VecZnxFillUniformImpl +{ + fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_to_lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToLWESwitchingKeyToMut, + Scratch: ScratchAvailable + ScratchTakeCore; +} + +impl GLWEToLWESwitchingKeyEncrypt for Module where + Module: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalize + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxSub + + VecZnxSubInplace + + VecZnxAddInplace + + SvpPrepare + + VecZnxSwitchRing + + ScratchAvailable + + ScratchAvailableImpl + + ScratchTakeBasic + + ScratchTakeCore + + VecZnxFillUniform + + VecZnxFillUniformImpl + + GLWESwitchingKeyEncryptSk +{ + fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos + { + GLWESecretPrepared::bytes_of(self, infos.rank_in()) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) + } + + fn glwe_to_lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWEToLWESwitchingKeyToMut, + Scratch: ScratchAvailable + ScratchTakeCore, + { + + let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); + #[cfg(debug_assertions)] { - assert!(sk_lwe.n().0 <= module.n() as u32); + assert!(sk_lwe.n().0 <= self.n() as u32); } - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); + let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - self.0.encrypt_sk( - module, + res.0.encrypt_sk( + self, sk_glwe, &sk_lwe_as_glwe, source_xa, source_xe, scratch_1, - ); + ); } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 063240e..19c5fc2 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -15,6 +15,7 @@ use crate::{ GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, ScratchTakeCore, }; @@ -27,30 +28,6 @@ impl LWESwitchingKey> { module.lwe_switching_key_encrypt_sk_tmp_bytes(infos) } - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, - // { - // debug_assert_eq!( - // infos.dsize().0, - // 1, - // "dsize > 1 is not supported for LWESwitchingKey" - // ); - // debug_assert_eq!( - // infos.rank_in().0, - // 1, - // "rank_in > 1 is not supported for LWESwitchingKey" - // ); - // debug_assert_eq!( - // infos.rank_out().0, - // 1, - // "rank_out > 1 is not supported for LWESwitchingKey" - // ); - // GLWESecret::bytes_of(module, Rank(1)) - // + GLWESecretPrepared::bytes_of(module, Rank(1)) - // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - // } } impl LWESwitchingKey { @@ -171,6 +148,8 @@ impl LWESwitchingKeyEncrypt for Module where + VecZnxSwitchRing + GLWESecretAlloc + GLWESecretPreparedAlloc + + GLWESwitchingKeyEncryptSk + , { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 32c75fc..a554e24 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -4,81 +4,227 @@ use poulpy_hal::{ VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, + ScratchTakeBasic, }, layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, + oep::{ScratchAvailableImpl}, source::Source, }; use crate::{ + layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank}, ScratchTakeCore, - layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, }; impl LWEToGLWESwitchingKey> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + M: LWEToGLWESwitchingKeyEncrypt, + { + module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) + } + + // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + // where + // A: GGLWEInfos, + // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, + // { + // debug_assert_eq!( + // infos.rank_in(), + // Rank(1), + // "rank_in != 1 is not supported for LWEToGLWESwitchingKey" + // ); + // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) + // + GLWESecret::bytes_of(module, infos.rank_in()) + // } +} + +impl LWEToGLWESwitchingKey { + pub fn encrypt_sk( + &mut self, + module: &M, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + M: LWEToGLWESwitchingKeyEncrypt, + DLwe: DataRef, + DGlwe: DataRef, + Scratch: ScratchAvailable + ScratchTakeCore, + { + module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } + // #[allow(clippy::too_many_arguments)] + // pub fn encrypt_sk( + // &mut self, + // module: &Module, + // sk_lwe: &LWESecret, + // sk_glwe: &GLWESecret, + // source_xa: &mut Source, + // source_xe: &mut Source, + // scratch: &mut Scratch, + // ) where + // DLwe: DataRef, + // DGlwe: DataRef, + // Module: ModuleN + // + VecZnxAutomorphismInplace + // + VecZnxAddScalarInplace + // + VecZnxDftBytesOf + // + VecZnxBigNormalize + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxNormalizeTmpBytes + // + VecZnxFillUniform + // + VecZnxSubInplace + // + VecZnxAddInplace + // + VecZnxNormalizeInplace + // + VecZnxAddNormal + // + VecZnxNormalize + // + VecZnxSub + // + SvpPrepare + // + VecZnxSwitchRing + // + SvpPPolBytesOf + // + SvpPPolAlloc, + // Scratch: ScratchAvailable + ScratchTakeCore, + // { + // #[cfg(debug_assertions)] + // { + // use crate::layouts::LWEInfos; + + // assert!(sk_lwe.n().0 <= module.n() as u32); + // } + + // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); + // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); + // sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); + // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + + // self.0.encrypt_sk( + // module, + // &sk_lwe_as_glwe, + // sk_glwe, + // source_xa, + // source_xe, + // scratch_1, + // ); + // } +} + +pub trait LWEToGLWESwitchingKeyEncrypt +where + Self: Sized + + ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalize + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxSub + + VecZnxSubInplace + + VecZnxAddInplace + + SvpPrepare + + VecZnxSwitchRing + + ScratchAvailable + + ScratchTakeBasic + + ScratchAvailableImpl +{ + fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn lwe_to_glwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe: &LWESecret, + sk_glwe: &GLWESecret, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: LWEToGLWESwitchingKeyToMut, + Scratch: ScratchAvailable + ScratchTakeCore; +} + +impl LWEToGLWESwitchingKeyEncrypt for Module where + Module: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxAutomorphismInplace + + VecZnxAddScalarInplace + + VecZnxBigNormalize + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxNormalize + + VecZnxNormalizeInplace + + VecZnxAddNormal + + VecZnxSub + + VecZnxSubInplace + + VecZnxAddInplace + + SvpPrepare + + VecZnxSwitchRing + + ScratchAvailable + + ScratchAvailableImpl + + ScratchTakeBasic + + ScratchTakeCore + + GLWESwitchingKeyEncryptSk +{ + fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos { debug_assert_eq!( infos.rank_in(), Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - + GLWESecret::bytes_of(module, infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + + GLWESecret::bytes_of(self, infos.rank_in()) } -} -impl LWEToGLWESwitchingKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, + fn lwe_to_glwe_switching_key_encrypt_sk( + &self, + res: &mut R, sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_glwe: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, - ) where - DLwe: DataRef, - DGlwe: DataRef, - Module: ModuleN - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeCore, + scratch: &mut Scratch, + ) where + R: LWEToGLWESwitchingKeyToMut, + Scratch: ScratchAvailable + ScratchTakeCore, { + let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); + #[cfg(debug_assertions)] { use crate::layouts::LWEInfos; - assert!(sk_lwe.n().0 <= module.n() as u32); + assert!(sk_lwe.n().0 <= self.n() as u32); } - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); + let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); - module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - self.0.encrypt_sk( - module, + res.0.encrypt_sk( + self, &sk_lwe_as_glwe, sk_glwe, source_xa, @@ -86,4 +232,4 @@ impl LWEToGLWESwitchingKey { scratch_1, ); } -} +} \ No newline at end of file From e0d3ca5cea983fc4eb0d04f4f428e7eabcda8114 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Fri, 17 Oct 2025 10:51:14 +0200 Subject: [PATCH 32/60] review of encryption --- poulpy-core/src/decryption/glwe_ct.rs | 36 +-- poulpy-core/src/decryption/lwe_ct.rs | 22 +- .../src/encryption/compressed/gglwe_atk.rs | 8 +- .../src/encryption/compressed/gglwe_ksk.rs | 8 +- .../src/encryption/compressed/gglwe_tsk.rs | 16 +- poulpy-core/src/encryption/gglwe_atk.rs | 143 ++++------- poulpy-core/src/encryption/gglwe_ct.rs | 160 ++++++------ poulpy-core/src/encryption/gglwe_ksk.rs | 232 ++++-------------- poulpy-core/src/encryption/gglwe_tsk.rs | 187 +++----------- poulpy-core/src/encryption/ggsw_ct.rs | 62 ++--- poulpy-core/src/encryption/glwe_ct.rs | 153 +++++------- poulpy-core/src/encryption/glwe_pk.rs | 37 ++- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 192 +++------------ poulpy-core/src/encryption/lwe_ct.rs | 20 +- poulpy-core/src/encryption/lwe_ksk.rs | 204 ++++++--------- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 191 +++----------- poulpy-core/src/layouts/prepared/glwe_sk.rs | 4 +- 17 files changed, 513 insertions(+), 1162 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index d0ff213..99c7d0f 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -1,17 +1,14 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, - SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxBigBytesOf, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, }; -use crate::{ - layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, GLWEToMut, GLWEPlaintextToMut, - prepared::{GLWESecretPreparedToRef, GLWESecretPrepared}, - } +use crate::layouts::{ + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToMut, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }; impl GLWE> { @@ -25,12 +22,12 @@ impl GLWE> { } impl GLWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) where P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, M: GLWEDecryption, - Scratch: ScratchTakeBasic, + Scratch: ScratchTakeBasic, { module.glwe_decrypt(self, pt, sk, scratch); } @@ -48,29 +45,23 @@ where + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + + VecZnxBigNormalize, { fn glwe_decrypt_tmp_bytes(&self, infos: &A) -> usize where - A: GLWEInfos + A: GLWEInfos, { let size: usize = infos.size(); (self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size) } - fn glwe_decrypt( - &self, - res: &mut R, - pt: &mut P, - sk: &S, - scratch: &mut Scratch, - ) where + fn glwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S, scratch: &mut Scratch) + where R: GLWEToMut, P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, Scratch: ScratchTakeBasic, { - let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); @@ -117,10 +108,9 @@ where pt.base2k = res.base2k(); pt.k = pt.k().min(res.k()); } - } -impl GLWEDecryption for Module where +impl GLWEDecryption for Module where Self: ModuleN + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes @@ -132,4 +122,4 @@ impl GLWEDecryption for Module where + VecZnxBigAddSmallInplace + VecZnxBigNormalize { -} \ No newline at end of file +} diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index 1042b72..a847139 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToMut, LWESecretToRef}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut}; -impl LWE -{ +impl LWE { pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: S) where P: LWEPlaintextToMut, @@ -21,16 +20,15 @@ impl LWE pub trait LWEDecrypt where - Self: Sized + ZnNormalizeInplace + Self: Sized + ZnNormalizeInplace, { fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: S) where - R: LWEToMut, - P: LWEPlaintextToMut, - S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + R: LWEToMut, + P: LWEPlaintextToMut, + S: LWESecretToRef, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); let sk: LWESecret<&[u8]> = sk.to_ref(); @@ -60,8 +58,4 @@ where } } -impl LWEDecrypt for Module where - Self: Sized + ZnNormalizeInplace -{ - -} \ No newline at end of file +impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace {} diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 1ee58a6..2586074 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,5 +1,8 @@ use poulpy_hal::{ - api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, + VecZnxNormalizeTmpBytes, + }, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; @@ -17,7 +20,8 @@ impl AutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: + ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { assert_eq!(module.n() as u32, infos.n()); GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out()) diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 3dca15f..425a0e1 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,5 +1,8 @@ use poulpy_hal::{ - api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing}, + api::{ + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, + VecZnxNormalizeTmpBytes, VecZnxSwitchRing, + }, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, source::Source, }; @@ -18,7 +21,8 @@ impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + Module: + ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, { (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 02393f1..e165a05 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,10 +1,10 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, + VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, }, - oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, + oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; @@ -12,7 +12,7 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -21,7 +21,13 @@ impl TensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, + Module: ModuleN + + SvpPPolBytesOf + + SvpPPolAlloc + + VecZnxNormalizeTmpBytes + + VecZnxDftBytesOf + + VecZnxNormalizeTmpBytes + + VecZnxBigBytesOf, { TensorKey::encrypt_sk_tmp_bytes(module, infos) } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index dfd1e97..dc760de 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,37 +1,30 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, - }, + api::{ScratchAvailable, VecZnxAutomorphism}, layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, - }, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos}, }; impl AutomorphismKey> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptSk + M: AutomorphismKeyEncryptSk, { - module.gglwe_automorphism_key_encrypt_sk_tmp_bytes(infos) + module.automorphism_key_encrypt_sk_tmp_bytes(infos) } pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptPk + M: GGLWEAutomorphismKeyEncryptPk, { - module.gglwe_automorphism_key_encrypt_pk_tmp_bytes(infos) + module.automorphism_key_encrypt_pk_tmp_bytes(infos) } } @@ -39,9 +32,9 @@ impl AutomorphismKey where Self: AutomorphismKeyToMut, { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, p: i64, sk: &S, source_xa: &mut Source, @@ -49,57 +42,36 @@ where scratch: &mut Scratch, ) where S: GLWESecretToRef, - Module: GGLWEAutomorphismKeyEncryptSk, + M: AutomorphismKeyEncryptSk, { - module.gglwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + module.automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); } } -pub trait GGLWEAutomorphismKeyEncryptSk { - fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait AutomorphismKeyEncryptSk { + fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_automorphism_key_encrypt_sk( + fn automorphism_key_encrypt_sk( &self, - res: &mut A, + res: &mut R, p: i64, - sk: &B, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef; + R: AutomorphismKeyToMut, + S: GLWESecretToRef; } -impl GGLWEAutomorphismKeyEncryptSk for Module +impl AutomorphismKeyEncryptSk for Module where - Module: ModuleN - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + VecZnxAutomorphism - + SvpPPolAlloc - + GaloisElement, - Scratch: ScratchAvailable + ScratchTakeCore, + Module: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Scratch: ScratchTakeCore, { - - fn gglwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -108,38 +80,33 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of_from_infos(self, &infos.glwe_layout()) + self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(self, infos) } - fn gglwe_automorphism_key_encrypt_sk( + fn automorphism_key_encrypt_sk( &self, - res: &mut A, + res: &mut R, p: i64, - sk: &B, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - A: AutomorphismKeyToMut, - B: GLWESecretToRef, + R: AutomorphismKeyToMut, + S: GLWESecretToRef, { let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use crate::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(res.n(), sk.n()); - assert_eq!(res.rank_out(), res.rank_in()); - assert_eq!(sk.rank(), res.rank_out()); - assert!( - scratch.available() >= AutomorphismKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", - scratch.available(), - AutomorphismKey::encrypt_sk_tmp_bytes(self, res) - ) - } + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); + assert!( + scratch.available() >= self.automorphism_key_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", + scratch.available(), + self.automorphism_key_encrypt_sk_tmp_bytes(res) + ); let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); @@ -162,45 +129,21 @@ where } } - pub trait GGLWEAutomorphismKeyEncryptPk { - fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - } impl GGLWEAutomorphismKeyEncryptPk for Module where - Module: ModuleN - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeCore, + Module:, + Scratch: ScratchTakeCore, { - - fn gglwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize + fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::encrypt_pk_tmp_bytes(self, infos) + unimplemented!() } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index e8ae4f8..67d517d 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -1,20 +1,20 @@ use poulpy_hal::{ api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, - // oep::SvpPPolAllocBytesImpl, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ - encryption::glwe_ct::GLWEEncryptSk, layouts::GLWEInfos, ScratchTakeCore, + ScratchTakeCore, + encryption::glwe_ct::GLWEEncryptSk, + layouts::GLWEInfos, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; impl GGLWE> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, @@ -32,16 +32,31 @@ impl GGLWE> { } } -pub trait GGLWEEncryptSk { +impl GGLWE { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &M, + pt: &P, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGLWEEncryptSk, + Scratch: ScratchTakeCore, + { + module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); + } +} +pub trait GGLWEEncryptSk { fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize - where - A: GGLWEInfos; - fn gglwe_encrypt_sk( &self, res: &mut R, @@ -49,40 +64,29 @@ pub trait GGLWEEncryptSk { sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWEToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GGLWEEncryptSk for Module +impl GGLWEEncryptSk for Module where - Module: ModuleN - + GLWEEncryptSk + Module: ModuleN + + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace - + VecZnxNormalizeInplace, - Scratch: ScratchAvailable + ScratchTakeCore, + + VecZnxNormalizeInplace, + Scratch: ScratchTakeCore, { fn gglwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) - + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) - } - - fn gglwe_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize - where - A: GGLWEInfos, - { - unimplemented!() - // TODO: Is this correct? - // GLWE::encrypt_pk_tmp_bytes(self, &infos.glwe_layout()) - // + (GLWEPlaintext::bytes_of_from_infos(self, &infos.glwe_layout()) | self.vec_znx_normalize_tmp_bytes()) - + self.glwe_encrypt_sk_tmp_bytes(infos) + + GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes()) } fn gglwe_encrypt_sk( @@ -92,54 +96,49 @@ where sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWEToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref(); - - assert_eq!( - res.rank_in(), - pt.cols() as u32, - "res.rank_in(): {} != pt.cols(): {}", - res.rank_in(), - pt.cols() - ); - assert_eq!( - res.rank_out(), - sk.rank(), - "res.rank_out(): {} != sk.rank(): {}", - res.rank_out(), - sk.rank() - ); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - assert!( - scratch.available() >= GGLWE::encrypt_sk_tmp_bytes(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", - scratch.available(), - res.rank_out(), - res.size(), - GGLWE::encrypt_sk_tmp_bytes(self, res) - ); - assert!( - res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, - "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", - res.dnum(), - res.dsize(), - res.base2k(), - res.dnum().0 * res.dsize().0 * res.base2k().0, - res.k() - ); - } + assert_eq!( + res.rank_in(), + pt.cols() as u32, + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), + pt.cols() + ); + assert_eq!( + res.rank_out(), + sk.rank(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), + sk.rank() + ); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + assert!( + scratch.available() >= self.gglwe_encrypt_sk_tmp_bytes(res), + "scratch.available: {} < GGLWE::encrypt_sk_tmp_bytes(self, res.rank()={}, res.size()={}): {}", + scratch.available(), + res.rank_out(), + res.size(), + self.gglwe_encrypt_sk_tmp_bytes(res) + ); + assert!( + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() + ); let dnum: usize = res.dnum().into(); let dsize: usize = res.dsize().into(); @@ -147,7 +146,7 @@ where let rank_in: usize = res.rank_in().into(); let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); - // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns + // For each input column (i.e. rank) produces a GGLWE of rank_out+1 columns // // Example for ksk rank 2 to rank 3: // @@ -177,20 +176,3 @@ where } } } - -impl GGLWE { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGLWEEncryptSk, - { - module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); - } -} diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index bd13c2b..b55e5df 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -1,20 +1,16 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, prepared::GLWESecretPrepared, - }, encryption::gglwe_ct::GGLWEEncryptSk, + layouts::{ + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, + prepared::GLWESecretPreparedAlloc, + }, }; impl GLWESwitchingKey> { @@ -36,209 +32,102 @@ impl GLWESwitchingKey> { } impl GLWESwitchingKey { - - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: GLWESecretToRef, + S2: GLWESecretToRef, M: GLWESwitchingKeyEncryptSk, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.glwe_switching_key_encrypt_sk(self, sk_in, sk_out, source_xa, source_xe, scratch); } - - - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_in: &GLWESecret, - // sk_out: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // Module: ModuleN - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert!(sk_in.n().0 <= module.n() as u32); - // assert!(sk_out.n().0 <= module.n() as u32); - // assert!( - // scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self), - // "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - // scratch.available(), - // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self) - // ) - // } - - // // let n: usize = sk_in.n().max(sk_out.n()).into(); - - // let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(module, sk_in.rank().into()); - // (0..sk_in.rank().into()).for_each(|i| { - // module.vec_znx_switch_ring( - // &mut sk_in_tmp.as_vec_znx_mut(), - // i, - // &sk_in.data.as_vec_znx(), - // i, - // ); - // }); - - // let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(module, sk_out.rank()); - // { - // let (mut tmp, _) = scratch_2.take_scalar_znx(module, 1); - // (0..sk_out.rank().into()).for_each(|i| { - // module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); - // module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - // }); - // } - - // self.key.encrypt_sk( - // module, - // &sk_in_tmp, - // &sk_out_tmp, - // source_xa, - // source_xe, - // scratch_2, - // ); - // self.sk_in_n = sk_in.n().into(); - // self.sk_out_n = sk_out.n().into(); - // } } - -pub trait GLWESwitchingKeyEncryptSk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ +pub trait GLWESwitchingKeyEncryptSk { fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn glwe_switching_key_encrypt_sk( + fn glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) - where + ) where R: GLWESwitchingKeyToMut, - DataSkIn: DataRef, - DataSkOut: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore; - + S1: GLWESecretToRef, + S2: GLWESecretToRef, + Scratch: ScratchTakeCore; } -impl GLWESwitchingKeyEncryptSk for Module where - Self: ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk - + VecZnxSwitchRing - + SvpPrepare - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddInplace - + VecZnxAddNormal - + VecZnxSub +impl GLWESwitchingKeyEncryptSk for Module +where + Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedAlloc + VecZnxSwitchRing + SvpPrepare, + Scratch: ScratchTakeCore, { - fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - (GGLWE::encrypt_sk_tmp_bytes(self, infos) | ScalarZnx::bytes_of(self.n(), 1)) + self.gglwe_encrypt_sk_tmp_bytes(infos) + .max(ScalarZnx::bytes_of(self.n(), 1)) + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) - + GLWESecretPrepared::bytes_of_from_infos(self, &infos.glwe_layout()) + + self.bytes_of_glwe_secret_prepared_from_infos(infos) } - fn glwe_switching_key_encrypt_sk( + fn glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + sk_in: &S1, + sk_out: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) - where + ) where R: GLWESwitchingKeyToMut, - DataSkIn: DataRef, - DataSkOut: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + S1: GLWESecretToRef, + S2: GLWESecretToRef, { - let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); - #[cfg(debug_assertions)] - { - assert!(sk_in.n().0 <= self.n() as u32); - assert!(sk_out.n().0 <= self.n() as u32); - assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - scratch.available(), - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) - ) - } - - // let n: usize = sk_in.n().max(sk_out.n()).into(); + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); + assert!( + scratch.available() >= self.glwe_switching_key_encrypt_sk_tmp_bytes(res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + scratch.available(), + self.glwe_switching_key_encrypt_sk_tmp_bytes(res) + ); let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); - (0..sk_in.rank().into()).for_each(|i| { + for i in 0..sk_in.rank().into() { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), i, ); - }); + } let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); - (0..sk_out.rank().into()).for_each(|i| { + for i in 0..sk_out.rank().into() { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - }); + } } res.key.encrypt_sk( @@ -251,39 +140,20 @@ impl GLWESwitchingKeyEncryptSk for Module where ); res.sk_in_n = sk_in.n().into(); res.sk_out_n = sk_out.n().into(); - } - + } } -pub trait GLWESwitchingKeyEncryptPk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ - +pub trait GLWESwitchingKeyEncryptPk { fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; } -impl GLWESwitchingKeyEncryptPk for Module where - Self: ModuleN - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + SvpPPolAlloc - + GGLWEEncryptSk, -{ - - fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize +impl GLWESwitchingKeyEncryptPk for Module { + fn glwe_switching_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { - GGLWE::encrypt_pk_tmp_bytes(self, infos) + unimplemented!() } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 3f4a31b..f67d730 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,156 +1,54 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, - oep::{VecZnxAddScalarInplaceImpl, VecZnxBigAllocBytesImpl, VecZnxDftApplyImpl, SvpApplyDftToDftImpl, VecZnxIdftApplyTmpAImpl, VecZnxBigNormalizeImpl}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GetDist, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, - prepared::GLWESecretPrepared, - }, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut, + prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, + }, }; impl TensorKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWETensorKeyEncryptSk + M: TensorKeyEncryptSk, { - module.gglwe_tensor_key_encrypt_sk_tmp_bytes(infos) + module.tensor_key_encrypt_sk_tmp_bytes(infos) } - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + VecZnxBigBytesOf, - // { - // GLWESecretPrepared::bytes_of(module, infos.rank_out()) - // + module.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) - // + module.bytes_of_vec_znx_big(1, 1) - // + module.bytes_of_vec_znx_dft(1, 1) - // + GLWESecret::bytes_of(module, Rank(1)) - // + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - // } } impl TensorKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk: &GLWESecret, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - M: GGLWETensorKeyEncryptSk, - GLWESecret: GetDist, + M: TensorKeyEncryptSk, + S: GLWESecretToRef + GetDist, Scratch: ScratchAvailable + ScratchTakeCore, { - module.gglwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); + module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } - - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // GLWESecret: GetDist, - // Module: ModuleN - // + SvpApplyDftToDft - // + VecZnxIdftApplyTmpA - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + VecZnxBigAllocBytesImpl - // + VecZnxBigBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchTakeBasic + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert_eq!(self.rank_out(), sk.rank()); - // assert_eq!(self.n(), sk.n()); - // } - - // // let n: RingDegree = sk.n(); - // let rank: Rank = self.rank_out(); - - // let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(module, rank); - // sk_dft_prep.prepare(module, sk); - - // let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(module, rank.into(), 1); - - // (0..rank.into()).for_each(|i| { - // module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); - // }); - - // let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(module, 1, 1); - // let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(module, Rank(1)); - // let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, 1); - - // (0..rank.into()).for_each(|i| { - // (i..rank.into()).for_each(|j| { - // module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); - - // module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); - // module.vec_znx_big_normalize( - // self.base2k().into(), - // &mut sk_ij.data.as_vec_znx_mut(), - // 0, - // self.base2k().into(), - // &sk_ij_big, - // 0, - // scratch_5, - // ); - - // self.at_mut(i, j) - // .encrypt_sk(module, &sk_ij, sk, source_xa, source_xe, scratch_5); - // }); - // }) - // } } -pub trait GGLWETensorKeyEncryptSk -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxBigBytesOf, -{ - fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait TensorKeyEncryptSk { + fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -162,41 +60,33 @@ where S: GLWESecretToRef + GetDist; } -impl GGLWETensorKeyEncryptSk for Module where +impl TensorKeyEncryptSk for Module +where Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxBigBytesOf - + VecZnxAddScalarInplaceImpl - + VecZnxDftApply - + VecZnxDftApplyImpl - + SvpApplyDftToDftImpl + GLWESwitchingKeyEncryptSk + + VecZnxDftBytesOf + + VecZnxBigBytesOf + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + VecZnxDftApply + SvpApplyDftToDft - + VecZnxIdftApplyTmpAImpl - + VecZnxBigNormalizeImpl + VecZnxIdftApplyTmpA - + VecZnxBigNormalize - + VecZnxAddScalarInplaceImpl - + SvpPrepare, - Scratch: ScratchTakeBasic + ScratchTakeCore, + + VecZnxBigNormalize, + Scratch: ScratchTakeCore, { - fn gglwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_out()) - + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) - + self.bytes_of_vec_znx_big(1, 1) - + self.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(self, Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + self.bytes_of_vec_znx_big(1, 1) + + self.bytes_of_vec_znx_dft(1, 1) + + GLWESecret::bytes_of(self, Rank(1)) + + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -217,11 +107,8 @@ impl GGLWETensorKeyEncryptSk for Module where let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.rank_out(), sk.rank()); - assert_eq!(res.n(), sk.n()); - } + assert_eq!(res.rank_out(), sk.rank()); + assert_eq!(res.n(), sk.n()); let (mut sk_dft, scratch_2) = scratch_1.take_vec_znx_dft(self, rank.into(), 1); @@ -251,6 +138,6 @@ impl GGLWETensorKeyEncryptSk for Module where res.at_mut(i, j) .encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); }); - }) + }) } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 90bac6b..72b6846 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,14 +1,14 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxZero}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ SIGMA, ScratchTakeCore, - encryption::glwe_ct::GLWEEncryptSkInternal, + encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -21,31 +21,23 @@ impl GGSW> { { module.ggsw_encrypt_sk_tmp_bytes(infos) } - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGSWInfos, - // Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - // { - // let size = infos.size(); - // GLWE::encrypt_sk_tmp_bytes(module, &infos.glwe_layout()) - // + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) - // + VecZnx::bytes_of(module.n(), 1, size) - // + module.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) - // } } -impl GGSW { +impl GGSW { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: GGSWEncryptSk, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGSWEncryptSk, + Scratch: ScratchTakeCore, { module.ggsw_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } @@ -55,7 +47,7 @@ pub trait GGSWEncryptSk { fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; - + fn ggsw_encrypt_sk( &self, res: &mut R, @@ -74,19 +66,18 @@ impl GGSWEncryptSk for Module where Module: ModuleN + GLWEEncryptSkInternal - + VecZnxAddScalarInplace - + VecZnxNormalizeInplace + + GLWEEncryptSk + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes, + + VecZnxNormalizeInplace + + VecZnxAddScalarInplace, Scratch: ScratchTakeCore, { - fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { let size = infos.size(); - GLWE::encrypt_sk_tmp_bytes(self, &infos.glwe_layout()) + self.glwe_encrypt_sk_tmp_bytes(infos) + VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size) + VecZnx::bytes_of(self.n(), 1, size) + self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) @@ -109,15 +100,10 @@ where let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(pt.n(), self.n()); - assert_eq!(sk.n(), self.n() as u32); - } + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(pt.n(), self.n()); + assert_eq!(sk.n(), self.n() as u32); let k: usize = res.k().into(); let base2k: usize = res.base2k().into(); @@ -149,4 +135,4 @@ where } } } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index a23d7c6..082e53f 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -1,15 +1,16 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, - VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, ScratchTakeBasic, + ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, + VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, layouts::{Backend, DataMut, Module, ScalarZnx, Scratch, VecZnx, VecZnxBig, VecZnxToMut, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ + ScratchTakeCore, dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ @@ -19,7 +20,6 @@ use crate::{ }; impl GLWE> { - pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, @@ -31,16 +31,16 @@ impl GLWE> { pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - M: GLWEEncryptPk + M: GLWEEncryptPk, { module.glwe_encrypt_pk_tmp_bytes(infos) } } impl GLWE { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, pt: &P, sk: &S, source_xa: &mut Source, @@ -49,28 +49,30 @@ impl GLWE { ) where P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, - Module: GLWEEncryptSk, + M: GLWEEncryptSk, + Scratch: ScratchTakeCore, { module.glwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch); } - pub fn encrypt_zero_sk( + pub fn encrypt_zero_sk( &mut self, - module: &Module, + module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where S: GLWESecretPreparedToRef, - Module: GLWEEncryptSk, + M: GLWEEncryptSk, + Scratch: ScratchTakeCore, { module.glwe_encrypt_zero_sk(self, sk, source_xa, source_xe, scratch); } - pub fn encrypt_pk( + pub fn encrypt_pk( &mut self, - module: &Module, + module: &M, pt: &P, pk: &K, source_xu: &mut Source, @@ -79,31 +81,28 @@ impl GLWE { ) where P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptPk, + M: GLWEEncryptPk, { module.glwe_encrypt_pk(self, pt, pk, source_xu, source_xe, scratch); } - pub fn encrypt_zero_pk( + pub fn encrypt_zero_pk( &mut self, - module: &Module, + module: &M, pk: &K, source_xu: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where K: GLWEPublicKeyPreparedToRef, - Module: GLWEEncryptPk, + M: GLWEEncryptPk, { module.glwe_encrypt_zero_pk(self, pk, source_xu, source_xe, scratch); } } pub trait GLWEEncryptSk { - fn glwe_encrypt_sk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos; @@ -129,22 +128,15 @@ pub trait GLWEEncryptSk { scratch: &mut Scratch, ) where R: GLWEToMut, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } impl GLWEEncryptSk for Module where - Module: Sized - + ModuleN - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + GLWEEncryptSkInternal, + Module: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { - fn glwe_encrypt_sk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, { @@ -166,23 +158,20 @@ where P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let mut res: GLWE<&mut [u8]> = res.to_mut(); - let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let pt: &GLWEPlaintext<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(sk.n(), self.n() as u32); - assert_eq!(pt.n(), self.n() as u32); - assert!( - scratch.available() >= GLWE::encrypt_sk_tmp_bytes(self, &res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - GLWE::encrypt_sk_tmp_bytes(self, &res) - ) - } + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert_eq!(pt.n(), self.n() as u32); + assert!( + scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}", + scratch.available(), + self.glwe_encrypt_sk_tmp_bytes(res) + ); let cols: usize = (res.rank() + 1).into(); self.glwe_encrypt_sk_internal( @@ -191,7 +180,7 @@ where res.data_mut(), cols, false, - Some((&pt, 0)), + Some((pt, 0)), sk, source_xa, source_xe, @@ -211,21 +200,18 @@ where R: GLWEToMut, S: GLWESecretPreparedToRef, { - let mut res: GLWE<&mut [u8]> = res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(sk.n(), self.n() as u32); - assert!( - scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(&res), - "scratch.available(): {} < GLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - self.glwe_encrypt_sk_tmp_bytes(&res) - ) - } + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + assert!( + scratch.available() >= self.glwe_encrypt_sk_tmp_bytes(res), + "scratch.available(): {} < GLWE::encrypt_sk_tmp_bytes: {}", + scratch.available(), + self.glwe_encrypt_sk_tmp_bytes(res) + ); let cols: usize = (res.rank() + 1).into(); self.glwe_encrypt_sk_internal( @@ -241,14 +227,11 @@ where SIGMA, scratch, ); - } + } } pub trait GLWEEncryptPk { - fn glwe_encrypt_pk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos; @@ -274,29 +257,20 @@ pub trait GLWEEncryptPk { scratch: &mut Scratch, ) where R: GLWEToMut, - K: GLWEPublicKeyPreparedToRef; - - + K: GLWEPublicKeyPreparedToRef; } impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes, + Module: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { - fn glwe_encrypt_pk_tmp_bytes( - &self, - infos: &A, - ) -> usize + fn glwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, { let size: usize = infos.size(); assert_eq!(self.n() as u32, infos.n()); - ((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)) | ScalarZnx::bytes_of(self.n(), 1)) + ((self.bytes_of_vec_znx_dft(1, size) + self.bytes_of_vec_znx_big(1, size)).max(ScalarZnx::bytes_of(self.n(), 1))) + self.bytes_of_svp_ppol(1) + self.vec_znx_normalize_tmp_bytes() } @@ -383,15 +357,12 @@ where let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pk: &GLWEPublicKeyPrepared<&[u8], BE> = &pk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.base2k(), pk.base2k()); - assert_eq!(res.n(), pk.n()); - assert_eq!(res.rank(), pk.rank()); - if let Some((pt, _)) = pt { - assert_eq!(pt.to_ref().base2k(), pk.base2k()); - assert_eq!(pt.to_ref().n(), pk.n()); - } + assert_eq!(res.base2k(), pk.base2k()); + assert_eq!(res.n(), pk.n()); + assert_eq!(res.rank(), pk.rank()); + if let Some((pt, _)) = pt { + assert_eq!(pt.to_ref().base2k(), pk.base2k()); + assert_eq!(pt.to_ref().n(), pk.n()); } let base2k: usize = pk.base2k().into(); @@ -517,7 +488,7 @@ where assert_eq!( ct.cols(), 1, - "invalid ciphertext: compressed tag=true but #cols={} != 1", + "invalid glwe: compressed tag=true but #cols={} != 1", ct.cols() ) } diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index b28f2fd..e4fad50 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,13 +1,14 @@ use poulpy_hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, ScratchOwned}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - encryption::glwe_ct::{GLWEEncryptSk}, + Distribution, ScratchTakeCore, + encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GLWE, GLWEPublicKey, GLWEPublicKeyToMut, + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -33,37 +34,33 @@ pub trait GLWEPublicKeyGenerate { S: GLWESecretPreparedToRef; } -impl GLWEPublicKeyGenerate for Module +impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Module: GLWEEncryptSk, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where R: GLWEPublicKeyToMut, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - use crate::{Distribution, layouts::LWEInfos}; + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); - assert_eq!(res.n(), self.n() as u32); - assert_eq!(sk.n(), self.n() as u32); - - if sk.dist == Distribution::NONE { - panic!("invalid sk: SecretDistribution::NONE") - } + if sk.dist == Distribution::NONE { + panic!("invalid sk: SecretDistribution::NONE") } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 4ade4d9..94d0c62 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,23 +1,17 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, - oep::{ScratchAvailableImpl, VecZnxFillUniformImpl}, - source::Source + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, + source::Source, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, GLWEToLWESwitchingKeyToMut, - prepared::GLWESecretPrepared, - }, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, + LWESecret, LWESecretToRef, Rank, + prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, + }, }; impl GLWEToLWESwitchingKey> { @@ -28,187 +22,77 @@ impl GLWEToLWESwitchingKey> { { module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos) } - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, - // { - // GLWESecretPrepared::bytes_of(module, infos.rank_in()) - // + (GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) | GLWESecret::bytes_of(module, infos.rank_in())) - // } } impl GLWEToLWESwitchingKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where M: GLWEToLWESwitchingKeyEncrypt, - DLwe: DataRef, - DGlwe: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + S1: LWESecretToRef, + S2: GLWESecretToRef, + Scratch: ScratchTakeCore, { module.glwe_to_lwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_lwe: &LWESecret, - // sk_glwe: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // DLwe: DataRef, - // DGlwe: DataRef, - // Module: ModuleN - // + VecZnxAutomorphismInplace - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // assert!(sk_lwe.n().0 <= module.n() as u32); - // } - - // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); - // sk_lwe_as_glwe.data.zero(); - // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - - // self.0.encrypt_sk( - // module, - // sk_glwe, - // &sk_lwe_as_glwe, - // source_xa, - // source_xe, - // scratch_1, - // ); - // } } -pub trait GLWEToLWESwitchingKeyEncrypt -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + VecZnxFillUniform - + VecZnxFillUniformImpl -{ +pub trait GLWEToLWESwitchingKeyEncrypt { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn glwe_to_lwe_switching_key_encrypt_sk( + fn glwe_to_lwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWEToLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore; + S1: LWESecretToRef, + S2: GLWESecretToRef, + R: GLWEToLWESwitchingKeyToMut; } -impl GLWEToLWESwitchingKeyEncrypt for Module where - Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + ScratchTakeBasic - + ScratchTakeCore - + VecZnxFillUniform - + VecZnxFillUniformImpl - + GLWESwitchingKeyEncryptSk +impl GLWEToLWESwitchingKeyEncrypt for Module +where + Module: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { - fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) } - fn glwe_to_lwe_switching_key_encrypt_sk( + fn glwe_to_lwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, R: GLWEToLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore, { - let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); + let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); + let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); - #[cfg(debug_assertions)] - { - assert!(sk_lwe.n().0 <= self.n() as u32); - } + assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.zero(); @@ -222,6 +106,6 @@ impl GLWEToLWESwitchingKeyEncrypt for Module where source_xa, source_xe, scratch_1, - ); + ); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index a91b2b7..4a5d6ac 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -7,11 +7,10 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret, LWEToMut, LWEPlaintextToRef, LWESecretToRef}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToRef, LWESecret, LWESecretToRef, LWEToMut}, }; impl LWE { - pub fn encrypt_sk(&mut self, module: &M, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where P: LWEPlaintextToRef, @@ -23,27 +22,24 @@ impl LWE { } } -pub trait LWEEncryptSk -where - Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, -{ +pub trait LWEEncryptSk { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where R: LWEToMut, P: LWEPlaintextToRef, - S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl; + S: LWESecretToRef; } -impl LWEEncryptSk for Module where +impl LWEEncryptSk for Module +where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where R: LWEToMut, P: LWEPlaintextToRef, S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &LWEPlaintext<&[u8]> = &pt.to_ref(); @@ -101,6 +97,6 @@ impl LWEEncryptSk for Module where (0..res.size()).for_each(|i| { res.data.at_mut(0, i)[0] = tmp_znx.at(0, i)[0]; - }); + }); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 19c5fc2..348df2c 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -1,22 +1,17 @@ use poulpy_hal::{ - api::{ - ModuleN, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ + ScratchTakeCore, + encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GLWESecretAlloc, - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, + GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut, + Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, - ScratchTakeCore, }; impl LWESwitchingKey> { @@ -27,145 +22,65 @@ impl LWESwitchingKey> { { module.lwe_switching_key_encrypt_sk_tmp_bytes(infos) } - } impl LWESwitchingKey { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk_lwe_in: &LWESecret, - sk_lwe_out: &LWESecret, + module: &M, + sk_lwe_in: &S1, + sk_lwe_out: &S2, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - DIn: DataRef, - DOut: DataRef, - Module: ModuleN - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc, - Scratch: ScratchTakeCore, + S1: LWESecretToRef, + S2: LWESecretToRef, + M: LWESwitchingKeyEncrypt, { - #[cfg(debug_assertions)] - { - assert!(sk_lwe_in.n().0 <= self.n().0); - assert!(sk_lwe_out.n().0 <= self.n().0); - assert!(self.n().0 <= module.n() as u32); - } - - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(module, Rank(1)); - - sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); - sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); - module.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); - - sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); - sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); - module.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); - - self.0.encrypt_sk( - module, - &sk_in_glwe, - &sk_out_glwe, - source_xa, - source_xe, - scratch_2, - ); + module.lwe_switching_key_encrypt_sk(self, sk_lwe_in, sk_lwe_out, source_xa, source_xe, scratch); } } -pub trait LWESwitchingKeyEncrypt -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + GLWESecretAlloc - + GLWESecretPreparedAlloc -{ +pub trait LWESwitchingKeyEncrypt { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; + + fn lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe_in: &S1, + sk_lwe_out: &S2, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: LWESwitchingKeyToMut, + S1: LWESecretToRef, + S2: LWESecretToRef; } -impl LWESwitchingKeyEncrypt for Module where - Self: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + GLWESecretAlloc - + GLWESecretPreparedAlloc - + GLWESwitchingKeyEncryptSk - , +impl LWESwitchingKeyEncrypt for Module +where + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { fn lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" @@ -174,4 +89,47 @@ impl LWESwitchingKeyEncrypt for Module where + GLWESecretPrepared::bytes_of(self, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } -} \ No newline at end of file + + #[allow(clippy::too_many_arguments)] + fn lwe_switching_key_encrypt_sk( + &self, + res: &mut R, + sk_lwe_in: &S1, + sk_lwe_out: &S2, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: LWESwitchingKeyToMut, + S1: LWESecretToRef, + S2: LWESecretToRef, + { + let res: &mut LWESwitchingKey<&mut [u8]> = &mut res.to_mut(); + let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref(); + let sk_lwe_out = &sk_lwe_out.to_ref(); + + assert!(sk_lwe_in.n().0 <= res.n().0); + assert!(sk_lwe_out.n().0 <= res.n().0); + assert!(res.n() <= self.n() as u32); + + let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); + let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + + sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); + sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); + + sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); + sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); + + self.glwe_switching_key_encrypt_sk( + &mut res.0, + &sk_in_glwe, + &sk_out_glwe, + source_xa, + source_xe, + scratch_2, + ); + } +} diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a554e24..d51faf7 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,20 +1,16 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, - ScratchTakeBasic, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxView, ZnxViewMut}, - oep::{ScratchAvailableImpl}, + api::{ModuleN, VecZnxAutomorphismInplace}, + layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ - layouts::{GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank}, ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + layouts::{ + GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, + LWEToGLWESwitchingKeyToMut, Rank, + }, }; impl LWEToGLWESwitchingKey> { @@ -25,198 +21,81 @@ impl LWEToGLWESwitchingKey> { { module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) } - - // pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize - // where - // A: GGLWEInfos, - // Module: ModuleN + SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolAlloc, - // { - // debug_assert_eq!( - // infos.rank_in(), - // Rank(1), - // "rank_in != 1 is not supported for LWEToGLWESwitchingKey" - // ); - // GLWESwitchingKey::encrypt_sk_tmp_bytes(module, infos) - // + GLWESecret::bytes_of(module, infos.rank_in()) - // } } impl LWEToGLWESwitchingKey { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, M: LWEToGLWESwitchingKeyEncrypt, - DLwe: DataRef, - DGlwe: DataRef, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } - // #[allow(clippy::too_many_arguments)] - // pub fn encrypt_sk( - // &mut self, - // module: &Module, - // sk_lwe: &LWESecret, - // sk_glwe: &GLWESecret, - // source_xa: &mut Source, - // source_xe: &mut Source, - // scratch: &mut Scratch, - // ) where - // DLwe: DataRef, - // DGlwe: DataRef, - // Module: ModuleN - // + VecZnxAutomorphismInplace - // + VecZnxAddScalarInplace - // + VecZnxDftBytesOf - // + VecZnxBigNormalize - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxNormalizeTmpBytes - // + VecZnxFillUniform - // + VecZnxSubInplace - // + VecZnxAddInplace - // + VecZnxNormalizeInplace - // + VecZnxAddNormal - // + VecZnxNormalize - // + VecZnxSub - // + SvpPrepare - // + VecZnxSwitchRing - // + SvpPPolBytesOf - // + SvpPPolAlloc, - // Scratch: ScratchAvailable + ScratchTakeCore, - // { - // #[cfg(debug_assertions)] - // { - // use crate::layouts::LWEInfos; - - // assert!(sk_lwe.n().0 <= module.n() as u32); - // } - - // let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(module, Rank(1)); - // sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - // sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); - // module.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - - // self.0.encrypt_sk( - // module, - // &sk_lwe_as_glwe, - // sk_glwe, - // source_xa, - // source_xe, - // scratch_1, - // ); - // } } -pub trait LWEToGLWESwitchingKeyEncrypt -where - Self: Sized - + ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchTakeBasic - + ScratchAvailableImpl -{ +pub trait LWEToGLWESwitchingKeyEncrypt { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn lwe_to_glwe_switching_key_encrypt_sk( + fn lwe_to_glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWEToGLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore; + S1: LWESecretToRef, + S2: GLWESecretToRef, + R: LWEToGLWESwitchingKeyToMut; } -impl LWEToGLWESwitchingKeyEncrypt for Module where - Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxAutomorphismInplace - + VecZnxAddScalarInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxSub - + VecZnxSubInplace - + VecZnxAddInplace - + SvpPrepare - + VecZnxSwitchRing - + ScratchAvailable - + ScratchAvailableImpl - + ScratchTakeBasic - + ScratchTakeCore - + GLWESwitchingKeyEncryptSk +impl LWEToGLWESwitchingKeyEncrypt for Module +where + Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxAutomorphismInplace, + Scratch: ScratchTakeCore, { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { debug_assert_eq!( infos.rank_in(), Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) - + GLWESecret::bytes_of(self, infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self, infos.rank_in()) } - fn lwe_to_glwe_switching_key_encrypt_sk( + fn lwe_to_glwe_switching_key_encrypt_sk( &self, res: &mut R, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, + sk_lwe: &S1, + sk_glwe: &S2, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) where + ) where + S1: LWESecretToRef, + S2: GLWESecretToRef, R: LWEToGLWESwitchingKeyToMut, - Scratch: ScratchAvailable + ScratchTakeCore, { let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); - - #[cfg(debug_assertions)] - { - use crate::layouts::LWEInfos; + let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); + let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); - assert!(sk_lwe.n().0 <= self.n() as u32); - } + assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); @@ -232,4 +111,4 @@ impl LWEToGLWESwitchingKeyEncrypt for Module where scratch_1, ); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index ac7e11f..dd7876c 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -66,7 +66,7 @@ where fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { self.bytes_of_svp_ppol(rank.into()) } - fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_secret_prepared_from_infos(&self, infos: &A) -> usize where A: GLWEInfos, { @@ -98,7 +98,7 @@ impl GLWESecretPrepared, B> { A: GLWEInfos, M: GLWESecretPreparedAlloc, { - module.bytes_of_glwe_secret_from_infos(infos) + module.bytes_of_glwe_secret_prepared_from_infos(infos) } pub fn bytes_of(module: &M, rank: Rank) -> usize From 69d04c71bc245388af2caea3879b0d91055b12f5 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Fri, 17 Oct 2025 10:59:35 +0200 Subject: [PATCH 33/60] gglwe compressed encrypt --- .../src/encryption/compressed/gglwe_ct.rs | 141 ++++++++++-------- poulpy-core/src/encryption/ggsw_ct.rs | 41 +++-- 2 files changed, 96 insertions(+), 86 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index cca081f..98cadfb 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -1,50 +1,56 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, - ZnNormalizeInplace, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + api::{ModuleN, ScratchAvailable, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ ScratchTakeCore, - encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, + encryption::{ + SIGMA, + glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, + }, layouts::{ - GGLWE, GGLWEInfos, LWEInfos, + GGLWEInfos, GLWEPlaintextAlloc, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + prepared::GLWESecretPreparedToRef, }, }; impl GGLWECompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, seed: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: GGLWECompressedEncryptSk, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGLWECompressedEncryptSk, { module.gglwe_compressed_encrypt_sk(self, pt, sk, seed, source_xe, scratch); } } impl GGLWECompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes, + M: GGLWECompressedEncryptSk, { - GGLWE::encrypt_sk_tmp_bytes(module, infos) + module.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GGLWECompressedEncryptSk { +pub trait GGLWECompressedEncryptSk { + fn gglwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + fn gglwe_compressed_encrypt_sk( &self, res: &mut R, @@ -52,24 +58,33 @@ pub trait GGLWECompressedEncryptSk { sk: &S, seed: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWECompressedToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GGLWECompressedEncryptSk for Module +impl GGLWECompressedEncryptSk for Module where - Module: ModuleN - + GLWEEncryptSkInternal - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes + Module: ModuleN + + GLWEEncryptSkInternal + + GLWEEncryptSk + VecZnxDftBytesOf + + VecZnxNormalizeInplace + VecZnxAddScalarInplace - + ZnNormalizeInplace, - Scratch: ScratchAvailable + ScratchTakeCore, + + VecZnxNormalizeTmpBytes, + Scratch: ScratchTakeCore, { + fn gglwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.glwe_encrypt_sk_tmp_bytes(infos) + .max(self.vec_znx_normalize_tmp_bytes()) + + self.bytes_of_glwe_plaintext_from_infos(infos) + } + fn gglwe_compressed_encrypt_sk( &self, res: &mut R, @@ -77,52 +92,48 @@ where sk: &S, seed: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGLWECompressedToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - let sk = &sk.to_ref(); + let sk = &sk.to_ref(); - assert_eq!( - res.rank_in(), - pt.cols() as u32, - "res.rank_in(): {} != pt.cols(): {}", - res.rank_in(), - pt.cols() - ); - assert_eq!( - res.rank_out(), - sk.rank(), - "res.rank_out(): {} != sk.rank(): {}", - res.rank_out(), - sk.rank() - ); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - assert!( - scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - GGLWECompressed::encrypt_sk_tmp_bytes(self, res) - ); - assert!( - res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, - "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", - res.dnum(), - res.dsize(), - res.base2k(), - res.dnum().0 * res.dsize().0 * res.base2k().0, - res.k() - ); - } + assert_eq!( + res.rank_in(), + pt.cols() as u32, + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), + pt.cols() + ); + assert_eq!( + res.rank_out(), + sk.rank(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), + sk.rank() + ); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + assert!( + scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", + scratch.available(), + GGLWECompressed::encrypt_sk_tmp_bytes(self, res) + ); + assert!( + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() + ); let dnum: usize = res.dnum().into(); let dsize: usize = res.dsize().into(); diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 72b6846..a758384 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace}, - layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, VecZnx, ZnxInfos, ZnxZero}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; @@ -8,7 +8,7 @@ use crate::{ SIGMA, ScratchTakeCore, encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintextAlloc, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -43,7 +43,7 @@ impl GGSW { } } -pub trait GGSWEncryptSk { +pub trait GGSWEncryptSk { fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; @@ -55,32 +55,31 @@ pub trait GGSWEncryptSk { sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGSWToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } -impl GGSWEncryptSk for Module +impl GGSWEncryptSk for Module where - Module: ModuleN - + GLWEEncryptSkInternal - + GLWEEncryptSk + Module: ModuleN + + GLWEEncryptSkInternal + + GLWEEncryptSk + VecZnxDftBytesOf - + VecZnxNormalizeInplace - + VecZnxAddScalarInplace, - Scratch: ScratchTakeCore, + + VecZnxNormalizeInplace + + VecZnxAddScalarInplace + + VecZnxNormalizeTmpBytes, + Scratch: ScratchTakeCore, { fn ggsw_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos, { - let size = infos.size(); self.glwe_encrypt_sk_tmp_bytes(infos) - + VecZnx::bytes_of(self.n(), (infos.rank() + 1).into(), size) - + VecZnx::bytes_of(self.n(), 1, size) - + self.bytes_of_vec_znx_dft((infos.rank() + 1).into(), size) + .max(self.vec_znx_normalize_tmp_bytes()) + + self.bytes_of_glwe_plaintext_from_infos(infos) } fn ggsw_encrypt_sk( @@ -90,15 +89,15 @@ where sk: &S, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGSWToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); - let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); assert_eq!(res.rank(), sk.rank()); assert_eq!(res.n(), self.n() as u32); @@ -111,7 +110,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.glwe_layout()); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, res); for row_i in 0..res.dnum().into() { tmp_pt.data.zero(); From 957345f9eabb28d2fef4cb6361c749d9fabd8aca Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Fri, 17 Oct 2025 11:15:32 +0200 Subject: [PATCH 34/60] glwe swk + autokey compressed encrypt sk --- .../src/encryption/compressed/gglwe_atk.rs | 116 ++++++++--------- .../src/encryption/compressed/gglwe_ksk.rs | 122 +++++++++--------- .../src/encryption/compressed/gglwe_tsk.rs | 6 +- 3 files changed, 119 insertions(+), 125 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 2586074..ab5fb27 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -1,66 +1,84 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, VecZnxAutomorphism, VecZnxDftBytesOf, - VecZnxNormalizeTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + api::{ModuleN, ScratchAvailable, VecZnxAutomorphism}, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ ScratchTakeCore, - encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, + encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretAlloc, GLWESecretToRef, LWEInfos, + compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, }, }; impl AutomorphismKeyCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: - ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + M: AutomorphismKeyCompressedEncryptSk, { - assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes(module, infos) + GLWESecret::bytes_of(module, infos.rank_out()) + module.automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GGLWEAutomorphismKeyCompressedEncryptSk { - fn gglwe_automorphism_key_compressed_encrypt_sk( +impl AutomorphismKeyCompressed { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &M, + p: i64, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S: GLWESecretToRef, + M: AutomorphismKeyCompressedEncryptSk, + { + module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); + } +} + +pub trait AutomorphismKeyCompressedEncryptSk { + fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef; } -impl GGLWEAutomorphismKeyCompressedEncryptSk for Module +impl AutomorphismKeyCompressedEncryptSk for Module where - Module: ModuleN - + GGLWEKeyCompressedEncryptSk - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VecZnxAutomorphism - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + Module: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Scratch: ScratchTakeCore, { - fn gglwe_automorphism_key_compressed_encrypt_sk( + fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + self.bytes_of_glwe_secret(infos.rank()) + } + + fn automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef, @@ -68,23 +86,20 @@ where let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - #[cfg(debug_assertions)] - { - assert_eq!(res.n(), sk.n()); - assert_eq!(res.rank_out(), res.rank_in()); - assert_eq!(sk.rank(), res.rank_out()); - assert!( - scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), - "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", - scratch.available(), - AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) - ) - } + assert_eq!(res.n(), sk.n()); + assert_eq!(res.rank_out(), res.rank_in()); + assert_eq!(sk.rank(), res.rank_out()); + assert!( + scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {}", + scratch.available(), + AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) + ); let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - (0..res.rank_out().into()).for_each(|i| { + for i in 0..res.rank_out().into(){ self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -92,28 +107,11 @@ where &sk.data.as_vec_znx(), i, ); - }); + }; } - self.gglwe_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); res.p = p; } } - -impl AutomorphismKeyCompressed { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - p: i64, - sk: &GLWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGLWEAutomorphismKeyCompressedEncryptSk, - { - module.gglwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); - } -} diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 425a0e1..4ba0bcb 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -1,9 +1,6 @@ use poulpy_hal::{ - api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, - VecZnxNormalizeTmpBytes, VecZnxSwitchRing, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch}, + api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing}, + layouts::{Backend, DataMut, Module, ScalarZnx, Scratch}, source::Source, }; @@ -11,119 +8,118 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, - prepared::GLWESecretPrepared, + prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; impl GLWESwitchingKeyCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: - ModuleN + SvpPPolAlloc + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf, + M: GLWESwitchingKeyCompressedEncryptSk, { - (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) - + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) - + GLWESecretPrepared::bytes_of(module, infos.rank_out()) + module.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) } } -impl GLWESwitchingKeyCompressed { +impl GLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk_in: &GLWESecret, - sk_out: &GLWESecret, + module: &M, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - Module: GGLWEKeyCompressedEncryptSk, + S1: GLWESecretToRef, + S2: GLWESecretToRef, + M: GLWESwitchingKeyCompressedEncryptSk, { - module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); + module.glwe_switching_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch); } } -pub trait GGLWEKeyCompressedEncryptSk { - fn gglwe_key_compressed_encrypt_sk( +pub trait GLWESwitchingKeyCompressedEncryptSk { + fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + + fn glwe_switching_key_compressed_encrypt_sk( &self, res: &mut R, - sk_in: &SI, - sk_out: &SO, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWESwitchingKeyCompressedToMut, - SI: GLWESecretToRef, - SO: GLWESecretToRef; + S1: GLWESecretToRef, + S2: GLWESecretToRef; } -impl GGLWEKeyCompressedEncryptSk for Module +impl GLWESwitchingKeyCompressedEncryptSk for Module where - Module: ModuleN - + GGLWECompressedEncryptSk - + SvpPPolBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxSwitchRing - + SvpPrepare - + SvpPPolAlloc, - Scratch: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore, + Module: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, + Scratch: ScratchTakeCore, { - fn gglwe_key_compressed_encrypt_sk( + fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) + .max(ScalarZnx::bytes_of(self.n(), 1)) + + ScalarZnx::bytes_of(self.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of(self, infos.rank_out()) + } + + fn glwe_switching_key_compressed_encrypt_sk( &self, res: &mut R, - sk_in: &SI, - sk_out: &SO, + sk_in: &S1, + sk_out: &S2, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWESwitchingKeyCompressedToMut, - SI: GLWESecretToRef, - SO: GLWESecretToRef, + S1: GLWESecretToRef, + S2: GLWESecretToRef, { let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); - #[cfg(debug_assertions)] - { - use crate::layouts::GLWESwitchingKey; - - assert!(sk_in.n().0 <= self.n() as u32); - assert!(sk_out.n().0 <= self.n() as u32); - assert!( - scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res), - "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", - scratch.available(), - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res) - ) - } - - let n: usize = sk_in.n().max(sk_out.n()).into(); + assert!(sk_in.n().0 <= self.n() as u32); + assert!(sk_out.n().0 <= self.n() as u32); + assert!( + scratch.available() >= self.gglwe_compressed_encrypt_sk_tmp_bytes(res), + "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}", + scratch.available(), + self.gglwe_compressed_encrypt_sk_tmp_bytes(res) + ); let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); - (0..sk_in.rank().into()).for_each(|i| { + for i in 0..sk_in.rank().into() { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), i, &sk_in.data.as_vec_znx(), i, ); - }); + } let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); - (0..sk_out.rank().into()).for_each(|i| { + for i in 0..sk_out.rank().into() { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); - }); + } } self.gglwe_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index e165a05..5843030 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, - encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, + encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, @@ -49,7 +49,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { impl GGLWETensorKeyCompressedEncryptSk for Module where Module: ModuleN - + GGLWEKeyCompressedEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA @@ -119,7 +119,7 @@ where let (seed_xa_tmp, _) = source_xa.branch(); - self.gglwe_key_compressed_encrypt_sk( + self.glwe_switching_key_compressed_encrypt_sk( res.at_mut(i, j), &sk_ij, sk, From a282e88126eeb53c01159197a742930c73254a13 Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Fri, 17 Oct 2025 19:56:10 -0400 Subject: [PATCH 35/60] remaining in encryption + noise --- poulpy-core/src/decryption/glwe_ct.rs | 8 +- poulpy-core/src/decryption/mod.rs | 3 + .../src/encryption/compressed/gglwe_tsk.rs | 105 ++++----- .../src/encryption/compressed/ggsw_ct.rs | 95 ++++---- .../src/encryption/compressed/glwe_ct.rs | 89 ++++---- poulpy-core/src/encryption/glwe_pk.rs | 12 +- poulpy-core/src/noise/gglwe_ct.rs | 131 ++++++++--- poulpy-core/src/noise/ggsw_ct.rs | 212 ++++++++++-------- poulpy-core/src/noise/glwe_ct.rs | 180 +++++++++++---- 9 files changed, 527 insertions(+), 308 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 99c7d0f..f40d765 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToMut, LWEInfos, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToMut, GLWEToRef, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }; @@ -55,14 +55,14 @@ where (self.vec_znx_normalize_tmp_bytes() | self.bytes_of_vec_znx_dft(1, size)) + self.bytes_of_vec_znx_dft(1, size) } - fn glwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S, scratch: &mut Scratch) + fn glwe_decrypt(&self, res: &R, pt: &mut P, sk: &S, scratch: &mut Scratch) where - R: GLWEToMut, + R: GLWEToRef, P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, Scratch: ScratchTakeBasic, { - let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let res: &GLWE<&[u8]> = &res.to_ref(); let pt: &mut GLWEPlaintext<&mut [u8]> = &mut pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 8165d78..03e5fd4 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -1,2 +1,5 @@ mod glwe_ct; mod lwe_ct; + +pub use glwe_ct::*; +pub use lwe_ct::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 5843030..5f88e42 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -11,6 +11,7 @@ use poulpy_hal::{ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, + encryption::gglwe_tsk::TensorKeyEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, @@ -18,58 +19,81 @@ use crate::{ }; impl TensorKeyCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - Module: ModuleN - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VecZnxBigBytesOf, + M: GGLWETensorKeyCompressedEncryptSk, { - TensorKey::encrypt_sk_tmp_bytes(module, infos) + module.gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GGLWETensorKeyCompressedEncryptSk { +impl TensorKeyCompressed { + pub fn encrypt_sk( + &mut self, + module: &Module, + sk: &GLWESecret, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + GLWESecret: GetDist, + Module: GGLWETensorKeyCompressedEncryptSk, + { + module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + } +} + +pub trait GGLWETensorKeyCompressedEncryptSk { + + fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; + fn gglwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, S: GLWESecretToRef + GetDist; } -impl GGLWETensorKeyCompressedEncryptSk for Module +impl GGLWETensorKeyCompressedEncryptSk for Module where - Module: ModuleN - + GLWESwitchingKeyCompressedEncryptSk - + VecZnxDftApply - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxBigNormalize - + SvpPrepare - + SvpPPolAllocBytesImpl + Module: ModuleN + + GLWESwitchingKeyCompressedEncryptSk + + TensorKeyEncryptSk + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + + SvpPrepare + + SvpPPolAllocBytesImpl + SvpPPolBytesOf - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl + + VecZnxDftAllocBytesImpl + + VecZnxBigAllocBytesImpl + VecZnxDftBytesOf + VecZnxBigBytesOf, - Scratch: ScratchTakeBasic + ScratchTakeCore, + Scratch: ScratchTakeBasic + ScratchTakeCore, { + fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos + { + self.tensor_key_encrypt_sk_tmp_bytes(infos) + } + fn gglwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, S: GLWESecretToRef + GetDist, @@ -130,35 +154,4 @@ where } } } -} - -impl TensorKeyCompressed { - pub fn encrypt_sk( - &mut self, - module: &Module, - sk: &GLWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - GLWESecret: GetDist, - Module: GGLWETensorKeyCompressedEncryptSk, - { - module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); - } -} - -impl TensorKeyCompressed { - pub fn encrypt_sk( - &mut self, - module: &Module, - sk: &GLWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGLWETensorKeyCompressedEncryptSk, - { - module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 6595e15..ef122d8 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,49 +1,52 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes}, + api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; use crate::{ ScratchTakeCore, - encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, + encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GLWEInfos, LWEInfos, + GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; impl GGSWCompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GGSWCompressedEncryptSk, { - GGSW::encrypt_sk_tmp_bytes(module, infos) + module.ggsw_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GGSWCompressedEncryptSk { - fn ggsw_compressed_encrypt_sk( - &self, - res: &mut R, - pt: &P, - sk: &S, +impl GGSWCompressed { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: &ScalarZnx, + sk: &GLWESecretPrepared, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - R: GGSWCompressedToMut, - P: ScalarZnxToRef, - S: GLWESecretPreparedToRef; + Module: GGSWCompressedEncryptSk, + { + module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); + } } -impl GGSWCompressedEncryptSk for Module -where - Module: ModuleN + GLWEEncryptSkInternal + VecZnxAddScalarInplace + VecZnxNormalizeInplace, - Scratch: ScratchTakeCore, -{ + +pub trait GGSWCompressedEncryptSk { + fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; + fn ggsw_compressed_encrypt_sk( &self, res: &mut R, @@ -51,14 +54,39 @@ where sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GGSWCompressedToMut, P: ScalarZnxToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef; +} + +impl GGSWCompressedEncryptSk for Module +where + Module: ModuleN + GLWEEncryptSkInternal + GGSWEncryptSk + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Scratch: ScratchTakeCore, +{ + fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where A: GGSWInfos, + { + self.ggsw_encrypt_sk_tmp_bytes(infos) + } + + fn ggsw_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GGSWCompressedToMut, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, { let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); #[cfg(debug_assertions)] @@ -111,21 +139,4 @@ where } } } -} - -impl GGSWCompressed { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GGSWCompressedEncryptSk, - { - module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index f04a07a..0e9fca5 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ }; use crate::{ - encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, + encryption::{SIGMA, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}}, layouts::{ GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, @@ -14,34 +14,38 @@ use crate::{ }; impl GLWECompressed> { - pub fn encrypt_sk_tmp_bytes(module: &Module, infos: &A) -> usize + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GLWEInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, + M: GLWECompressedEncryptSk, { - GLWE::encrypt_sk_tmp_bytes(module, infos) + module.glwe_compressed_encrypt_sk_tmp_bytes(infos) } } -pub trait GLWECompressedEncryptSk { - fn glwe_compressed_encrypt_sk( - &self, - res: &mut R, - pt: &P, - sk: &S, +impl GLWECompressed { + #[allow(clippy::too_many_arguments)] + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: &GLWEPlaintext, + sk: &GLWESecretPrepared, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where - R: GLWECompressedToMut, - P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef; + Module: GLWECompressedEncryptSk, + { + module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); + } } -impl GLWECompressedEncryptSk for Module -where - Module: GLWEEncryptSkInternal, -{ + +pub trait GLWECompressedEncryptSk { + fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GLWEInfos; + fn glwe_compressed_encrypt_sk( &self, res: &mut R, @@ -49,11 +53,37 @@ where sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, - scratch: &mut Scratch, + scratch: &mut Scratch, ) where R: GLWECompressedToMut, P: GLWEPlaintextToRef, - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal + GLWEEncryptSk, +{ + + fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.glwe_encrypt_sk_tmp_bytes(infos) + } + + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, { let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); let mut source_xa: Source = Source::new(seed_xa); @@ -75,21 +105,4 @@ where res.seed = seed_xa; } -} - -impl GLWECompressed { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - pt: &GLWEPlaintext, - sk: &GLWESecretPrepared, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: GLWECompressedEncryptSk, - { - module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); - } -} +} \ No newline at end of file diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index e4fad50..f35457f 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -14,24 +14,24 @@ use crate::{ }; impl GLWEPublicKey { - pub fn generate( + pub fn generate( &mut self, - module: &Module, - sk: &GLWESecretPrepared, + module: &Module, + sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, ) where - Module: GLWEPublicKeyGenerate, + Module: GLWEPublicKeyGenerate, { module.glwe_public_key_generate(self, sk, source_xa, source_xe); } } -pub trait GLWEPublicKeyGenerate { +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, - S: GLWESecretPreparedToRef; + S: GLWESecretPreparedToRef; } impl GLWEPublicKeyGenerate for Module diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 516a210..e1cefbf 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,49 +1,120 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, - VecZnxSubScalarInplace, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace, }, - layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, + layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero}, + oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, }; -use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{ + GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, +}; +use crate::decryption::GLWEDecryption; impl GGLWE { - pub fn assert_noise( + + pub fn assert_noise( &self, - module: &Module, - sk: &GLWESecretPrepared, + module: &M, + sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, max_noise: f64, ) where DataSk: DataRef, DataWant: DataRef, - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxSubScalarInplace, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + M: GGLWENoise, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, { - let dsize: usize = self.dsize().into(); - let base2k: usize = self.base2k().into(); + module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); + } - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - (0..self.rank_in().into()).for_each(|col_i| { - (0..self.dnum().into()).for_each(|row_i| { - self.at(row_i, col_i) - .decrypt(module, &mut pt, sk, scratch.borrow()); + // pub fn assert_noise( + // &self, + // module: &Module, + // sk: &GLWESecretPrepared, + // pt_want: &ScalarZnx, + // max_noise: f64, + // ) where + // DataSk: DataRef, + // DataWant: DataRef, + // Module: VecZnxDftBytesOf + // + VecZnxBigBytesOf + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxBigAddInplace + // + VecZnxBigAddSmallInplace + // + VecZnxBigNormalize + // + VecZnxNormalizeTmpBytes + // + VecZnxSubScalarInplace, + // B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + // { + // let dsize: usize = self.dsize().into(); + // let base2k: usize = self.base2k().into(); - module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); + // let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); + // let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + + // (0..self.rank_in().into()).for_each(|col_i| { + // (0..self.dnum().into()).for_each(|row_i| { + // self.at(row_i, col_i) + // .decrypt(module, &mut pt, sk, scratch.borrow()); + + // module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); + + // let noise_have: f64 = pt.data.std(base2k, 0).log2(); + + // println!("noise_have: {noise_have}"); + + // assert!( + // noise_have <= max_noise, + // "noise_have: {noise_have} > max_noise: {max_noise}" + // ); + + // pt.data.zero(); + // }); + // }); + // } +} + + +pub trait GGLWENoise { + fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + R: GGLWEToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl; +} + +impl GGLWENoise for Module +where + Module: GLWEDecryption, + Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, +{ + fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + R: GGLWEToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, + { + + let res: &GGLWE<&[u8]> = &res.to_ref(); + + let dsize: usize = res.dsize().into(); + let base2k: usize = res.base2k().into(); + + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + + (0..res.rank_in().into()).for_each(|col_i| { + (0..res.dnum().into()).for_each(|row_i| { + self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow()); + + self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); let noise_have: f64 = pt.data.std(base2k, 0).log2(); @@ -58,4 +129,4 @@ impl GGLWE { }); }); } -} +} \ No newline at end of file diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 92f806b..1a00f4d 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -4,62 +4,118 @@ use poulpy_hal::{ VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + ScratchTakeBasic, }, - layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, + layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef}; +use crate::layouts::prepared::GLWESecretPreparedToRef; +use crate::decryption::GLWEDecryption; impl GGSW { - pub fn assert_noise( + pub fn assert_noise( &self, - module: &Module, - sk_prepared: &GLWESecretPrepared, + module: &M, + sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, - max_noise: F, + max_noise: F ) where DataSk: DataRef, DataScalar: DataRef, - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSubInplace, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + M: GGSWNoise, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { - let base2k: usize = self.base2k().into(); - let dsize: usize = self.dsize().into(); + module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise); + } - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); - let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); + pub fn print_noise( + &self, + module: &M, + sk_prepared: &GLWESecretPrepared, + pt_want: &ScalarZnx, + ) where + DataSk: DataRef, + DataScalar: DataRef, + M: GGSWNoise, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + { + module.ggsw_print_noise(self, sk_prepared, pt_want); + } +} - let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); +pub trait GGSWNoise { + fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + where + R: GGSWToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + F: Fn(usize) -> f64; - (0..(self.rank() + 1).into()).for_each(|col_j| { - (0..self.dnum().into()).for_each(|row_i| { - module.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); + fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) + where + R: GGSWToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; +} + +impl GGSWNoise for Module +where + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxBigAddInplace + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalizeTmpBytes + + VecZnxBigAlloc + + VecZnxDftAlloc + + VecZnxBigNormalizeTmpBytes + + VecZnxIdftApplyTmpA + + VecZnxAddScalarInplace + + VecZnxSubInplace + + GLWEDecryption, + Scratch: ScratchTakeBasic, +{ + fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) + where + R: GGSWToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + F: Fn(usize) -> f64, + { + + let res: &GGSW<&[u8]> = &res.to_ref(); + let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); + + let base2k: usize = res.base2k().into(); + let dsize: usize = res.dsize().into(); + + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); + let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); + + let mut scratch: ScratchOwned = + ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes()); + + (0..(res.rank() + 1).into()).for_each(|col_j| { + (0..res.dnum().into()).for_each(|row_i| { + self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); // mul with sk[col_j-1] if col_j > 0 { - module.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0); - module.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1); - module.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0); - module.vec_znx_big_normalize( + self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0); + self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1); + self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0); + self.vec_znx_big_normalize( base2k, &mut pt.data, 0, @@ -70,10 +126,9 @@ impl GGSW { ); } - self.at(row_i, col_j) - .decrypt(module, &mut pt_have, sk_prepared, scratch.borrow()); + self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); - module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); + self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); let noise: f64 = max_noise(col_j); @@ -81,57 +136,40 @@ impl GGSW { pt.data.zero(); }); - }); + }); } -} -impl GGSW { - pub fn print_noise( - &self, - module: &Module, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - ) where - DataSk: DataRef, - DataScalar: DataRef, - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSubInplace, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) + where + R: GGSWToRef, + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { - let base2k: usize = self.base2k().into(); - let dsize: usize = self.dsize().into(); + let res: &GGSW<&[u8]> = &res.to_ref(); + let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); - let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); + let base2k: usize = res.base2k().into(); + let dsize: usize = res.dsize().into(); - let mut scratch: ScratchOwned = - ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self) | module.vec_znx_normalize_tmp_bytes()); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); + let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); - (0..(self.rank() + 1).into()).for_each(|col_j| { - (0..self.dnum().into()).for_each(|row_i| { - module.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes()); + + (0..(res.rank() + 1).into()).for_each(|col_j| { + (0..res.dnum().into()).for_each(|row_i| { + self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); // mul with sk[col_j-1] if col_j > 0 { - module.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0); - module.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1); - module.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0); - module.vec_znx_big_normalize( + self.vec_znx_dft_apply(1, 0, &mut pt_dft, 0, &pt.data, 0); + self.svp_apply_dft_to_dft_inplace(&mut pt_dft, 0, &sk_prepared.data, col_j - 1); + self.vec_znx_idft_apply_tmpa(&mut pt_big, 0, &mut pt_dft, 0); + self.vec_znx_big_normalize( base2k, &mut pt.data, 0, @@ -142,15 +180,13 @@ impl GGSW { ); } - self.at(row_i, col_j) - .decrypt(module, &mut pt_have, sk_prepared, scratch.borrow()); - module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); + self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); + self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); println!("col: {col_j} row: {row_i}: {std_pt}"); pt.data.zero(); - // println!(">>>>>>>>>>>>>>>>"); }); }); - } -} + } +} \ No newline at end of file diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index 40b86d9..16986b5 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -2,67 +2,159 @@ use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSubInplace, + VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::{ + decryption::GLWEDecryption, + layouts::{ + GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + }, +}; impl GLWE { - pub fn noise( - &self, - module: &Module, - sk_prepared: &GLWESecretPrepared, - pt_want: &GLWEPlaintext, - scratch: &mut Scratch, - ) -> f64 + pub fn noise(&self, module: &M, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where - DataSk: DataRef, - DataPt: DataRef, - B: Backend, - Module: VecZnxDftApply - + VecZnxSubInplace - + VecZnxNormalizeInplace - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, - Scratch:, + M: GLWENoise, + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, { - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - self.decrypt(module, &mut pt_have, sk_prepared, scratch); - module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); - module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); - pt_have.data.std(self.base2k().into(), 0).log2() + module.glwe_noise(self, sk_prepared, pt_want, scratch) } + // pub fn noise( + // &self, + // module: &Module, + // sk_prepared: &GLWESecretPrepared, + // pt_want: &GLWEPlaintext, + // scratch: &mut Scratch, + // ) -> f64 + // where + // DataSk: DataRef, + // DataPt: DataRef, + // B: Backend, + // Module: VecZnxDftApply + // + VecZnxSubInplace + // + VecZnxNormalizeInplace + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxBigAddInplace + // + VecZnxBigAddSmallInplace + // + VecZnxBigNormalize, + // Scratch:, + // { + // let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); + // self.decrypt(module, &mut pt_have, sk_prepared, scratch); + // module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); + // module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); + // pt_have.data.std(self.base2k().into(), 0).log2() + // } - pub fn assert_noise( + pub fn assert_noise( &self, - module: &Module, - sk_prepared: &GLWESecretPrepared, + module: &M, + sk_prepared: &GLWESecretPrepared, pt_want: &GLWEPlaintext, max_noise: f64, ) where DataSk: DataRef, DataPt: DataRef, - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxNormalizeInplace, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + M: GLWENoise, + BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); - let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); + module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise); + } + + // pub fn assert_noise( + // &self, + // module: &Module, + // sk_prepared: &GLWESecretPrepared, + // pt_want: &GLWEPlaintext, + // max_noise: f64, + // ) where + // DataSk: DataRef, + // DataPt: DataRef, + // Module: VecZnxDftBytesOf + // + VecZnxBigBytesOf + // + VecZnxDftApply + // + SvpApplyDftToDftInplace + // + VecZnxIdftApplyConsume + // + VecZnxBigAddInplace + // + VecZnxBigAddSmallInplace + // + VecZnxBigNormalize + // + VecZnxNormalizeTmpBytes + // + VecZnxSubInplace + // + VecZnxNormalizeInplace, + // B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + // { + // let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); + // let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); + // assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); + // } +} + + +pub trait GLWENoise { + fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 + where + R: GLWEToRef, + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef; + + fn glwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + R: GLWEToRef, + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; +} + +impl GLWENoise for Module +where + Module: VecZnxDftBytesOf + + VecZnxBigBytesOf + + VecZnxDftApply + + SvpApplyDftToDftInplace + + VecZnxIdftApplyConsume + + VecZnxBigAddInplace + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + + VecZnxNormalizeTmpBytes + + VecZnxSubInplace + + VecZnxNormalizeInplace + + GLWEDecryption, + Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, +{ + fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 + where + R: GLWEToRef, + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, + { + let res_ref: &GLWE<&[u8]> = &res.to_ref(); + + let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref(); + + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res_ref); + self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch); + self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); + self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch); + pt_have.data.std(res_ref.base2k().into(), 0).log2() + } + + fn glwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + R: GLWEToRef, + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, + BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + { + let res: &GLWE<&[u8]> = &res.to_ref(); + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); + let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } -} +} \ No newline at end of file From 0b8dcb1f166e15e92c50141d70f9f7ba2635c5aa Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Sat, 18 Oct 2025 11:59:50 +0200 Subject: [PATCH 36/60] fix remaining issues before fixing tests --- .../benches/external_product_glwe_fft64.rs | 33 +++++++++------- poulpy-core/benches/keyswitch_glwe_fft64.rs | 36 ++++++++++------- poulpy-core/examples/encryption.rs | 17 ++++---- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 20 ++++++++-- poulpy-core/src/decryption/mod.rs | 2 +- .../src/encryption/compressed/gglwe_atk.rs | 6 +-- .../src/encryption/compressed/gglwe_ct.rs | 2 +- .../src/encryption/compressed/gglwe_ksk.rs | 2 +- .../src/encryption/compressed/gglwe_tsk.rs | 25 ++++++------ .../src/encryption/compressed/ggsw_ct.rs | 22 ++++++----- .../src/encryption/compressed/glwe_ct.rs | 30 +++++++------- poulpy-core/src/encryption/gglwe_atk.rs | 4 +- poulpy-core/src/encryption/gglwe_ct.rs | 2 +- poulpy-core/src/encryption/gglwe_ksk.rs | 3 +- poulpy-core/src/encryption/gglwe_tsk.rs | 8 ++-- poulpy-core/src/encryption/ggsw_ct.rs | 2 +- poulpy-core/src/encryption/glwe_ct.rs | 12 +++--- poulpy-core/src/encryption/glwe_pk.rs | 16 +++----- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/layouts/gglwe_ksk.rs | 4 +- poulpy-core/src/layouts/glwe_sk.rs | 8 +++- poulpy-core/src/layouts/mod.rs | 2 + poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 4 +- poulpy-core/src/layouts/prepared/glwe_pk.rs | 2 +- poulpy-core/src/layouts/prepared/glwe_sk.rs | 6 +-- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 2 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/noise/gglwe_ct.rs | 39 +++++++++++-------- poulpy-core/src/noise/ggsw_ct.rs | 36 ++++++++++------- poulpy-core/src/noise/glwe_ct.rs | 15 ++++--- poulpy-core/src/operations/glwe.rs | 5 +++ poulpy-hal/src/api/scratch.rs | 2 + poulpy-hal/src/delegates/module.rs | 11 +++++- 34 files changed, 224 insertions(+), 160 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 2849e3d..db78656 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ Base2K, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, RingDegree, TorusPrecision, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + prepared::{GGSWPrepared, GLWESecretPrepared}, }; use std::hint::black_box; @@ -61,9 +61,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); - let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); - let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -76,9 +76,11 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_dft.prepare(&module, &sk); ct_ggsw.encrypt_sk( &module, @@ -97,7 +99,8 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let mut ggsw_prepared: GGSWPrepared, FFT64Spqlios> = GGSWPrepared::alloc_from_infos(&module, &ct_ggsw); + ggsw_prepared.prepare(&module, &ct_ggsw, scratch.borrow()); move || { ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); @@ -162,23 +165,25 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); - let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(&module, &ggsw_layout) | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) - | GLWE::external_product_inplace_tmp_bytes(&module, &glwe_layout, &ggsw_layout), + | GLWE::external_product_tmp_bytes(&module, &glwe_layout, &glwe_layout, &ggsw_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_dft.prepare(&module, &sk); ct_ggsw.encrypt_sk( &module, @@ -197,8 +202,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); - + let mut ggsw_prepared: GGSWPrepared, FFT64Spqlios> = GGSWPrepared::alloc_from_infos(&module, &ct_ggsw); + ggsw_prepared.prepare(&module, &ct_ggsw, scratch.borrow()); move || { let scratch_borrow = scratch.borrow(); ct_glwe.external_product_inplace(&module, &ggsw_prepared, scratch_borrow); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index fc1dc69..5806b16 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, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, - GLWESwitchingKeyLayout, Rank, RingDegree, TorusPrecision, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, + GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, RingDegree, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }; use std::{hint::black_box, time::Duration}; @@ -62,9 +62,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); - let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); - let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&module, &gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) @@ -81,9 +81,11 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); + + let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_in_dft.prepare(&module, &sk_in); ksk.encrypt_sk( &module, @@ -102,7 +104,8 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: AutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); + let mut ksk_prepared: AutomorphismKeyPrepared, _> = AutomorphismKeyPrepared::alloc_from_infos(&module, &ksk); + ksk_prepared.prepare(&module, &ksk, scratch.borrow()); move || { ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); @@ -174,24 +177,26 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) | GLWE::encrypt_sk_tmp_bytes(&module, &glwe_layout) - | GLWE::keyswitch_inplace_tmp_bytes(&module, &glwe_layout, &gglwe_layout), + | GLWE::keyswitch_tmp_bytes(&module, &glwe_layout, &glwe_layout, &gglwe_layout), ); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); + let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_in_dft.prepare(&module, &sk_in); + + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( @@ -211,7 +216,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, _> = GLWESwitchingKeyPrepared::alloc_from_infos(&module, &ksk); + ksk_prepared.prepare(&module, &ksk, scratch.borrow()); move || { ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index efd838e..aae1204 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -1,9 +1,9 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ - GLWEOperations, SIGMA, + GLWESub, SIGMA, layouts::{ Base2K, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, RingDegree, TorusPrecision, - prepared::{GLWESecretPrepared, PrepareAlloc}, + prepared::GLWESecretPrepared, }, }; use poulpy_hal::{ @@ -43,9 +43,9 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -58,11 +58,12 @@ fn main() { ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret - let sk_prepared: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); + sk_prepared.prepare(&module, &sk); // Uniform plaintext module.vec_znx_fill_uniform(base2k.into(), &mut pt_want.data, 0, &mut source_xa); @@ -81,7 +82,7 @@ fn main() { ct.decrypt(&module, &mut pt_have, &sk_prepared, scratch.borrow()); // Diff between pt - Dec(Enc(pt)) - pt_want.sub_inplace_ab(&module, &pt_have); + module.glwe_sub_inplace(&mut pt_want, &pt_have); // Ideal vs. actual noise let noise_have: f64 = pt_want.data.std(base2k.into(), 0) * (ct.k().as_u32() as f64).exp2(); diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 24d02bd..3447d61 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -38,9 +38,7 @@ impl GGSW { } } -impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + GLWECopy {} - -pub trait GGSWFromGGLWE +impl GGSWFromGGLWE for Module where Self: GGSWExpandRows + GLWECopy, { @@ -77,6 +75,20 @@ where } } +pub trait GGSWFromGGLWE { + fn ggsw_from_gglwe_tmp_bytes(&self, res_infos: &R, tsk_infos: &A) -> usize + where + R: GGSWInfos, + A: GGLWEInfos; + + fn ggsw_from_gglwe(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch) + where + R: GGSWToMut, + A: GGLWEToRef, + T: TensorKeyPreparedToRef, + Scratch: ScratchTakeCore; +} + impl GGSWExpandRows for Module where Self: Sized + ModuleN @@ -99,7 +111,7 @@ impl GGSWExpandRows for Module where { } -pub(crate) trait GGSWExpandRows +pub trait GGSWExpandRows where Self: Sized + ModuleN diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 03e5fd4..0af7985 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -2,4 +2,4 @@ mod glwe_ct; mod lwe_ct; pub use glwe_ct::*; -pub use lwe_ct::*; +// pub use lwe_ct::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index ab5fb27..a3415cc 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -61,7 +61,7 @@ pub trait AutomorphismKeyCompressedEncryptSk { impl AutomorphismKeyCompressedEncryptSk for Module where - Module: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -99,7 +99,7 @@ where let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); { - for i in 0..res.rank_out().into(){ + for i in 0..res.rank_out().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -107,7 +107,7 @@ where &sk.data.as_vec_znx(), i, ); - }; + } } self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 98cadfb..638c566 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -67,7 +67,7 @@ pub trait GGLWECompressedEncryptSk { impl GGLWECompressedEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSkInternal + GLWEEncryptSk + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 4ba0bcb..7825e69 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { impl GLWESwitchingKeyCompressedEncryptSk for Module where - Module: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxSwitchRing, + Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + 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/gglwe_tsk.rs index 5f88e42..1184c03 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -1,9 +1,9 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; @@ -13,7 +13,7 @@ use crate::{ encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, encryption::gglwe_tsk::TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -29,23 +29,22 @@ impl TensorKeyCompressed> { } impl TensorKeyCompressed { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - sk: &GLWESecret, + module: &M, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - GLWESecret: GetDist, - Module: GGLWETensorKeyCompressedEncryptSk, + S: GLWESecretToRef + GetDist, + M: GGLWETensorKeyCompressedEncryptSk, { module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } pub trait GGLWETensorKeyCompressedEncryptSk { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -64,7 +63,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { impl GGLWETensorKeyCompressedEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWESwitchingKeyCompressedEncryptSk + TensorKeyEncryptSk + VecZnxDftApply @@ -82,7 +81,7 @@ where { fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where - A: GGLWEInfos + A: GGLWEInfos, { self.tensor_key_encrypt_sk_tmp_bytes(infos) } @@ -154,4 +153,4 @@ where } } } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index ef122d8..ec45398 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, source::Source, }; @@ -26,22 +26,23 @@ impl GGSWCompressed> { impl GGSWCompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &ScalarZnx, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: GGSWCompressedEncryptSk, + P: ScalarZnxToRef, + S: GLWESecretPreparedToRef, + M: GGSWCompressedEncryptSk, { module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } - pub trait GGSWCompressedEncryptSk { fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where @@ -63,11 +64,12 @@ pub trait GGSWCompressedEncryptSk { impl GGSWCompressedEncryptSk for Module where - Module: ModuleN + GLWEEncryptSkInternal + GGSWEncryptSk + VecZnxAddScalarInplace + VecZnxNormalizeInplace, + Self: ModuleN + GLWEEncryptSkInternal + GGSWEncryptSk + VecZnxAddScalarInplace + VecZnxNormalizeInplace, Scratch: ScratchTakeCore, { fn ggsw_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize - where A: GGSWInfos, + where + A: GGSWInfos, { self.ggsw_encrypt_sk_tmp_bytes(infos) } @@ -139,4 +141,4 @@ where } } } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 0e9fca5..a30001c 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,15 +1,17 @@ use poulpy_hal::{ - api::{VecZnxDftBytesOf, VecZnxNormalizeTmpBytes}, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, Module, Scratch}, source::Source, }; use crate::{ - encryption::{SIGMA, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}}, + encryption::{ + SIGMA, + glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, + }, layouts::{ - GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + GLWEInfos, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, + prepared::GLWESecretPreparedToRef, }, }; @@ -25,22 +27,23 @@ impl GLWECompressed> { impl GLWECompressed { #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, - pt: &GLWEPlaintext, - sk: &GLWESecretPrepared, + module: &M, + pt: &P, + sk: &S, seed_xa: [u8; 32], source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: GLWECompressedEncryptSk, + M: GLWECompressedEncryptSk, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, { module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } - pub trait GLWECompressedEncryptSk { fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where @@ -62,9 +65,8 @@ pub trait GLWECompressedEncryptSk { impl GLWECompressedEncryptSk for Module where - Module: GLWEEncryptSkInternal + GLWEEncryptSk, + Self: GLWEEncryptSkInternal + GLWEEncryptSk, { - fn glwe_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GLWEInfos, @@ -105,4 +107,4 @@ where res.seed = seed_xa; } -} \ No newline at end of file +} diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index dc760de..8521df3 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -68,7 +68,7 @@ pub trait AutomorphismKeyEncryptSk { impl AutomorphismKeyEncryptSk for Module where - Module: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Self: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -137,7 +137,7 @@ pub trait GGLWEAutomorphismKeyEncryptPk { impl GGLWEAutomorphismKeyEncryptPk for Module where - Module:, + Self:, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 67d517d..bb1a9c1 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -73,7 +73,7 @@ pub trait GGLWEEncryptSk { impl GGLWEEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSk + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index b55e5df..e8c49f3 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -66,8 +66,7 @@ pub trait GLWESwitchingKeyEncryptSk { ) where R: GLWESwitchingKeyToMut, S1: GLWESecretToRef, - S2: GLWESecretToRef, - Scratch: ScratchTakeCore; + S2: GLWESecretToRef; } impl GLWESwitchingKeyEncryptSk for Module diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index f67d730..d10af38 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -1,7 +1,7 @@ use poulpy_hal::{ api::{ - ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyTmpA, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, + VecZnxIdftApplyTmpA, }, layouts::{Backend, DataMut, Module, Scratch}, source::Source, @@ -37,7 +37,7 @@ impl TensorKey { ) where M: TensorKeyEncryptSk, S: GLWESecretToRef + GetDist, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchTakeCore, { module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } @@ -62,7 +62,7 @@ pub trait TensorKeyEncryptSk { impl TensorKeyEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index a758384..8964122 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -64,7 +64,7 @@ pub trait GGSWEncryptSk { impl GGSWEncryptSk for Module where - Module: ModuleN + Self: ModuleN + GLWEEncryptSkInternal + GLWEEncryptSk + VecZnxDftBytesOf diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 082e53f..5877f37 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -38,7 +38,7 @@ impl GLWE> { } impl GLWE { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &M, pt: &P, @@ -133,7 +133,7 @@ pub trait GLWEEncryptSk { impl GLWEEncryptSk for Module where - Module: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, + Self: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal, Scratch: ScratchAvailable, { fn glwe_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -262,7 +262,7 @@ pub trait GLWEEncryptPk { impl GLWEEncryptPk for Module where - Module: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, + Self: GLWEEncryptPkInternal + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes, { fn glwe_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where @@ -330,7 +330,7 @@ pub(crate) trait GLWEEncryptPkInternal { impl GLWEEncryptPkInternal for Module where - Module: SvpPrepare + Self: SvpPrepare + SvpApplyDftToDft + VecZnxIdftApplyConsume + VecZnxBigAddNormal @@ -445,7 +445,7 @@ pub(crate) trait GLWEEncryptSkInternal { impl GLWEEncryptSkInternal for Module where - Module: ModuleN + Self: ModuleN + VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply @@ -459,7 +459,7 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: ScratchAvailable + ScratchTakeBasic, + Scratch: ScratchTakeBasic, { fn glwe_encrypt_sk_internal( &self, diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index f35457f..d30fe62 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned}, + layouts::{Backend, DataMut, Module, Scratch, ScratchOwned}, source::Source, }; @@ -14,14 +14,10 @@ use crate::{ }; impl GLWEPublicKey { - pub fn generate( - &mut self, - module: &Module, - sk: &GLWESecretPrepared, - source_xa: &mut Source, - source_xe: &mut Source, - ) where - Module: GLWEPublicKeyGenerate, + pub fn generate(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source) + where + S: GLWESecretPreparedToRef, + M: GLWEPublicKeyGenerate, { module.glwe_public_key_generate(self, sk, source_xa, source_xe); } @@ -36,7 +32,7 @@ pub trait GLWEPublicKeyGenerate { impl GLWEPublicKeyGenerate for Module where - Module: GLWEEncryptSk, + Self: GLWEEncryptSk, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 94d0c62..59be47c 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -64,7 +64,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt { impl GLWEToLWESwitchingKeyEncrypt for Module where - Module: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, Scratch: ScratchTakeCore, { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index ddb4de7..e18648b 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -67,7 +67,7 @@ pub struct GLWESwitchingKey { pub(crate) sk_out_n: usize, // Degree of sk_out } -pub(crate) trait GLWESwitchingKeySetMetaData { +pub trait GLWESwitchingKeySetMetaData { fn set_sk_in_n(&mut self, sk_in_n: usize); fn set_sk_out_n(&mut self, sk_out_n: usize); } @@ -82,7 +82,7 @@ impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { } } -pub(crate) trait GLWESwtichingKeyGetMetaData { +pub trait GLWESwtichingKeyGetMetaData { fn sk_in_n(&self) -> usize; fn sk_out_n(&self) -> usize; } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 9166388..72ecd95 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, + layouts::{Base2K, GLWEInfos, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -64,6 +64,12 @@ impl LWEInfos for GLWESecret { } } +impl GetDist for GLWESecret { + fn get_dist(&self) -> Distribution { + self.dist + } +} + impl GLWEInfos for GLWESecret { fn rank(&self) -> Rank { Rank(self.data.cols() as u32) diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 09064f3..0d53307 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -17,6 +17,7 @@ mod lwe_to_glwe_ksk; pub mod compressed; pub mod prepared; +pub use compressed::*; pub use gglwe_atk::*; pub use gglwe_ct::*; pub use gglwe_ksk::*; @@ -32,6 +33,7 @@ pub use lwe_ksk::*; pub use lwe_pt::*; pub use lwe_sk::*; pub use lwe_to_glwe_ksk::*; +pub use prepared::*; use poulpy_hal::layouts::{Backend, Module}; diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 2d054c1..1ff5759 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -198,7 +198,7 @@ where impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} impl TensorKeyPrepared, B> { - fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize + pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, M: TensorKeyPrepare, @@ -208,7 +208,7 @@ impl TensorKeyPrepared, B> { } impl TensorKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, M: TensorKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index bca1826..62165c8 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -18,7 +18,7 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } -pub(crate) trait SetDist { +pub trait SetDist { fn set_dist(&mut self, dist: Distribution); } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index dd7876c..f50f2dc 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -63,7 +63,7 @@ where self.alloc_glwe_secret_prepared(infos.rank()) } - fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + fn bytes_of_glwe_secret_prepared(&self, rank: Rank) -> usize { self.bytes_of_svp_ppol(rank.into()) } fn bytes_of_glwe_secret_prepared_from_infos(&self, infos: &A) -> usize @@ -71,7 +71,7 @@ where A: GLWEInfos, { assert_eq!(self.ring_degree(), infos.n()); - self.bytes_of_glwe_secret(infos.rank()) + self.bytes_of_glwe_secret_prepared(infos.rank()) } } @@ -105,7 +105,7 @@ impl GLWESecretPrepared, B> { where M: GLWESecretPreparedAlloc, { - module.bytes_of_glwe_secret(rank) + module.bytes_of_glwe_secret_prepared(rank) } } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 55a7bf9..4c7f65e 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -172,7 +172,7 @@ impl GLWEToLWESwitchingKeyPrepared, B> { } impl GLWEToLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GLWEToLWESwitchingKeyToRef, M: GLWEToLWESwitchingKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index a857bf9..7e0b1da 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -180,7 +180,7 @@ impl LWESwitchingKeyPrepared, B> { } impl LWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWESwitchingKeyToRef, M: LWESwitchingKeyPrepare, diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 5df692e..6afb32a 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -179,7 +179,7 @@ impl LWEToGLWESwitchingKeyPrepared, B> { } impl LWEToGLWESwitchingKeyPrepared { - fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) + pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: LWEToGLWESwitchingKeyToRef, M: LWEToGLWESwitchingKeyPrepare, diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index e1cefbf..189f471 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,19 +1,16 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace, - }, - layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero}, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace}, + layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, }; +use crate::decryption::GLWEDecryption; use crate::layouts::{ - GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }; -use crate::decryption::GLWEDecryption; impl GGLWE { - pub fn assert_noise( &self, module: &M, @@ -24,12 +21,15 @@ impl GGLWE { DataSk: DataRef, DataWant: DataRef, M: GGLWENoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, + BE: Backend + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow + + VecZnxSubScalarInplaceImpl, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - // pub fn assert_noise( // &self, // module: &Module, @@ -79,7 +79,6 @@ impl GGLWE { // } } - pub trait GGLWENoise { fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) where @@ -89,10 +88,14 @@ pub trait GGLWENoise { BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl; } -impl GGLWENoise for Module +impl GGLWENoise for Module where Module: GLWEDecryption, - Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + Scratch: ScratchTakeBasic + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow, { fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) where @@ -101,9 +104,8 @@ where P: ScalarZnxToRef, BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, { - let res: &GGLWE<&[u8]> = &res.to_ref(); - + let dsize: usize = res.dsize().into(); let base2k: usize = res.base2k().into(); @@ -112,7 +114,12 @@ where (0..res.rank_in().into()).for_each(|col_i| { (0..res.dnum().into()).for_each(|row_i| { - self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_i), + &mut pt, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); @@ -129,4 +136,4 @@ where }); }); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 1a00f4d..aa270a5 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,18 +1,17 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, - VecZnxSubInplace, - ScratchTakeBasic, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, + VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef}; -use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::decryption::GLWEDecryption; +use crate::layouts::prepared::GLWESecretPreparedToRef; +use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGSW { pub fn assert_noise( @@ -20,7 +19,7 @@ impl GGSW { module: &M, sk_prepared: &GLWESecretPrepared, pt_want: &ScalarZnx, - max_noise: F + max_noise: F, ) where DataSk: DataRef, DataScalar: DataRef, @@ -91,7 +90,6 @@ where BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { - let res: &GGSW<&[u8]> = &res.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); @@ -126,7 +124,12 @@ where ); } - self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_j), + &mut pt_have, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); @@ -136,7 +139,7 @@ where pt.data.zero(); }); - }); + }); } fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) @@ -180,7 +183,12 @@ where ); } - self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow()); + self.glwe_decrypt( + &res.at(row_i, col_j), + &mut pt_have, + sk_prepared, + scratch.borrow(), + ); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0); let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); @@ -188,5 +196,5 @@ where pt.data.zero(); }); }); - } -} \ No newline at end of file + } +} diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index 16986b5..d21ae60 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,8 +1,8 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, + VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, @@ -96,7 +96,6 @@ impl GLWE { // } } - pub trait GLWENoise { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -126,7 +125,11 @@ where + VecZnxSubInplace + VecZnxNormalizeInplace + GLWEDecryption, - Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, + Scratch: ScratchTakeBasic + + ScratchOwnedAllocImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrowImpl + + ScratchOwnedBorrow, { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -157,4 +160,4 @@ where let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); } -} \ No newline at end of file +} diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 27021e7..48e67ca 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -84,6 +84,11 @@ where impl GLWEAdd for Module where Self: ModuleN + VecZnxAdd + VecZnxCopy + VecZnxAddInplace {} +impl GLWESub for Module where + Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace +{ +} + pub trait GLWESub where Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace, diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index ee4a080..fb17266 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -28,6 +28,8 @@ pub trait TakeSlice { fn take_slice(&mut self, len: usize) -> (&mut [T], &mut Self); } +impl ScratchTakeBasic for Scratch where Self: TakeSlice {} + pub trait ScratchTakeBasic where Self: TakeSlice, diff --git a/poulpy-hal/src/delegates/module.rs b/poulpy-hal/src/delegates/module.rs index 0e3a455..fa01c2f 100644 --- a/poulpy-hal/src/delegates/module.rs +++ b/poulpy-hal/src/delegates/module.rs @@ -1,5 +1,5 @@ use crate::{ - api::ModuleNew, + api::{ModuleN, ModuleNew}, layouts::{Backend, Module}, oep::ModuleNewImpl, }; @@ -12,3 +12,12 @@ where B::new_impl(n) } } + +impl ModuleN for Module +where + B: Backend, +{ + fn n(&self) -> usize { + self.n() + } +} From a84ad060866008ff38c23b79e1ca72d7b8551375 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Sat, 18 Oct 2025 12:56:30 +0200 Subject: [PATCH 37/60] fixed missing implementations --- .../benches/external_product_glwe_fft64.rs | 6 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 8 +- poulpy-core/examples/encryption.rs | 4 +- .../src/layouts/compressed/gglwe_atk.rs | 7 +- .../src/layouts/compressed/gglwe_ct.rs | 10 +- .../src/layouts/compressed/gglwe_ksk.rs | 8 +- .../src/layouts/compressed/gglwe_tsk.rs | 6 +- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 10 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 16 +- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 4 +- poulpy-core/src/layouts/compressed/lwe_ct.rs | 6 +- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 5 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 6 +- poulpy-core/src/layouts/gglwe_atk.rs | 10 +- poulpy-core/src/layouts/gglwe_ct.rs | 14 +- poulpy-core/src/layouts/gglwe_ksk.rs | 8 +- poulpy-core/src/layouts/gglwe_tsk.rs | 10 +- poulpy-core/src/layouts/ggsw_ct.rs | 14 +- poulpy-core/src/layouts/glwe_ct.rs | 14 +- poulpy-core/src/layouts/glwe_pk.rs | 14 +- poulpy-core/src/layouts/glwe_pt.rs | 14 +- poulpy-core/src/layouts/glwe_sk.rs | 14 +- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 10 +- poulpy-core/src/layouts/lwe_ct.rs | 20 +- poulpy-core/src/layouts/lwe_ksk.rs | 10 +- poulpy-core/src/layouts/lwe_pt.rs | 10 +- poulpy-core/src/layouts/lwe_sk.rs | 10 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 10 +- poulpy-core/src/layouts/mod.rs | 12 +- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 4 +- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 14 +- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 6 +- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 4 +- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 14 +- poulpy-core/src/layouts/prepared/glwe_pk.rs | 14 +- poulpy-core/src/layouts/prepared/glwe_sk.rs | 14 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 4 +- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 4 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 4 +- poulpy-core/src/lib.rs | 2 +- poulpy-core/src/tests/mod.rs | 354 +++++++++--------- poulpy-core/src/tests/serialization.rs | 63 ++-- .../benches/circuit_bootstrapping.rs | 4 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 2 +- .../ciphertexts/block_prepared.rs | 4 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 2 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 16 +- poulpy-schemes/src/tfhe/blind_rotation/key.rs | 24 +- .../src/tfhe/blind_rotation/key_compressed.rs | 10 +- .../src/tfhe/blind_rotation/key_prepared.rs | 10 +- 50 files changed, 444 insertions(+), 419 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index db78656..47b98ea 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,5 +1,5 @@ use poulpy_core::layouts::{ - Base2K, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, prepared::{GGSWPrepared, GLWESecretPrepared}, }; use std::hint::black_box; @@ -28,7 +28,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: RingDegree = RingDegree(module.n() as u32); + let n: Degree = Degree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct_in: TorusPrecision = p.k_ct_in; let k_ct_out: TorusPrecision = p.k_ct_out; @@ -140,7 +140,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: RingDegree = RingDegree(module.n() as u32); + let n: Degree = Degree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe: TorusPrecision = p.k_ct; let k_ggsw: TorusPrecision = p.k_ggsw; diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 5806b16..d603b60 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,6 +1,6 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, - GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, RingDegree, TorusPrecision, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }; use std::{hint::black_box, time::Duration}; @@ -29,7 +29,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: RingDegree = RingDegree(module.n() as u32); + let n: Degree = Degree(module.n() as u32); let base2k: Base2K = p.base2k; let k_glwe_in: TorusPrecision = p.k_ct_in; let k_glwe_out: TorusPrecision = p.k_ct_out; @@ -151,7 +151,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { fn runner(p: Params) -> impl FnMut() { let module: Module = Module::::new(1 << p.log_n); - let n: RingDegree = RingDegree(module.n() as u32); + let n: Degree = Degree(module.n() as u32); let base2k: Base2K = p.base2k; let k_ct: TorusPrecision = p.k_ct; let k_ksk: TorusPrecision = p.k_ksk; diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index aae1204..43a2b0e 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -2,7 +2,7 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios; use poulpy_core::{ GLWESub, SIGMA, layouts::{ - Base2K, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -16,7 +16,7 @@ fn main() { // Ring degree let log_n: usize = 10; - let n: RingDegree = RingDegree(1 << log_n); + let n: Degree = Degree(1 << log_n); // Base-2-k (implicit digit decomposition) let base2k: Base2K = Base2K(14); diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 61ba79a..276b695 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, AutomorphismKeyToMut, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, - TorusPrecision, + AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -22,7 +21,7 @@ pub struct AutomorphismKeyCompressed { } impl LWEInfos for AutomorphismKeyCompressed { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } @@ -80,6 +79,8 @@ impl fmt::Display for AutomorphismKeyCompressed { } } +impl AutomorphismKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + pub trait AutomorphismKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 9216ec4..235cc4d 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -24,8 +24,8 @@ pub struct GGLWECompressed { } impl LWEInfos for GGLWECompressed { - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -88,7 +88,7 @@ impl fmt::Display for GGLWECompressed { pub trait GGLWECompressedAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_gglwe_compressed( &self, @@ -183,7 +183,7 @@ where } } -impl GGLWECompressedAlloc for Module where Self: GetRingDegree {} +impl GGLWECompressedAlloc for Module where Self: GetDegree {} impl GGLWECompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index fb963c5..d9574c4 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, LWEInfos, - Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, + LWEInfos, Rank, TorusPrecision, compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -19,7 +19,7 @@ pub struct GLWESwitchingKeyCompressed { } impl LWEInfos for GLWESwitchingKeyCompressed { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } @@ -81,6 +81,8 @@ impl fmt::Display for GLWESwitchingKeyCompressed { } } +impl GLWESwitchingKeyCompressedAlloc for Module where Self: GGLWECompressedAlloc {} + pub trait GLWESwitchingKeyCompressedAlloc where Self: GGLWECompressedAlloc, diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index ff2b8b8..d0202cc 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToMut, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -19,7 +19,7 @@ pub struct TensorKeyCompressed { } impl LWEInfos for TensorKeyCompressed { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.keys[0].n() } @@ -82,6 +82,8 @@ impl fmt::Display for TensorKeyCompressed { } } +impl TensorKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + pub trait TensorKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index adad621..454dae3 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -23,8 +23,8 @@ pub struct GGSWCompressed { } impl LWEInfos for GGSWCompressed { - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -76,9 +76,11 @@ impl FillUniform for GGSWCompressed { } } +impl GGSWCompressedAlloc for Module where Self: GetDegree {} + pub trait GGSWCompressedAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_ggsw_compressed( &self, diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index e558cf7..00f125a 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,9 +6,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{ - Base2K, GLWE, GLWEInfos, GLWEToMut, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, -}; +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -34,8 +32,8 @@ impl LWEInfos for GLWECompressed { self.data.size() } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } } impl GLWEInfos for GLWECompressed { @@ -72,7 +70,7 @@ impl FillUniform for GLWECompressed { pub trait GLWECompressedAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { GLWECompressed { @@ -113,7 +111,7 @@ where } } -impl GLWECompressedAlloc for Module where Self: GetRingDegree {} +impl GLWECompressedAlloc for Module where Self: GetDegree {} impl GLWECompressed> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -169,7 +167,7 @@ impl WriterTo for GLWECompressed { pub trait GLWEDecompress where - Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy, + Self: GetDegree + VecZnxFillUniform + VecZnxCopy, { fn decompress_glwe(&self, res: &mut R, other: &O) where @@ -203,7 +201,7 @@ where } } -impl GLWEDecompress for Module where Self: GetRingDegree + VecZnxFillUniform + VecZnxCopy {} +impl GLWEDecompress for Module where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} impl GLWE { pub fn decompress(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 5bfa3eb..afdb493 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, RingDegree, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, @@ -26,7 +26,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index 0393758..c21a6dd 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, LWE, LWEInfos, LWEToMut, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision}; #[derive(PartialEq, Eq, Clone)] pub struct LWECompressed { @@ -28,8 +28,8 @@ impl LWEInfos for LWECompressed { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index cf6ae9a..7e1b0cf 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, RingDegree, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, @@ -25,7 +24,7 @@ impl LWEInfos for LWESwitchingKeyCompressed { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 33fcf49..545f126 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, RingDegree, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, @@ -17,7 +17,7 @@ use std::fmt; pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } @@ -86,6 +86,8 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } +impl LWEToGLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + pub trait LWEToGLWESwitchingKeyCompressedAlloc where Self: GLWESwitchingKeyCompressedAlloc, diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 3c0afc1..b4b61c6 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -14,7 +14,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct AutomorphismKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -47,7 +47,7 @@ impl AutomorphismKey { } impl LWEInfos for AutomorphismKey { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } @@ -97,7 +97,7 @@ impl LWEInfos for AutomorphismKeyLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 3b95d63..666293a 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -37,7 +37,7 @@ pub trait SetGGLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWELayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -55,7 +55,7 @@ impl LWEInfos for GGLWELayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } @@ -101,8 +101,8 @@ impl LWEInfos for GGLWE { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { @@ -193,7 +193,7 @@ impl GGLWE { pub trait GGLWEAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_gglwe( &self, @@ -293,7 +293,7 @@ where } } -impl GGLWEAlloc for Module where Self: GetRingDegree {} +impl GGLWEAlloc for Module where Self: GetDegree {} impl GGLWE> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index e18648b..466f8ac 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, RingDegree, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -13,7 +13,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESwitchingKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -23,7 +23,7 @@ pub struct GLWESwitchingKeyLayout { } impl LWEInfos for GLWESwitchingKeyLayout { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -98,7 +98,7 @@ impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { } impl LWEInfos for GLWESwitchingKey { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 6ef67b8..10523b1 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -13,7 +13,7 @@ use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct TensorKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -27,7 +27,7 @@ pub struct TensorKey { } impl LWEInfos for TensorKey { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.keys[0].n() } @@ -69,7 +69,7 @@ impl GGLWEInfos for TensorKey { } impl LWEInfos for TensorKeyLayout { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index aac4a20..a4d091d 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use std::fmt; -use crate::layouts::{Base2K, Dnum, Dsize, GLWE, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -28,7 +28,7 @@ where #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGSWLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -45,7 +45,7 @@ impl LWEInfos for GGSWLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } @@ -74,8 +74,8 @@ pub struct GGSW { } impl LWEInfos for GGSW { - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -152,11 +152,11 @@ impl GGSW { } } -impl GGSWAlloc for Module where Self: GetRingDegree {} +impl GGSWAlloc for Module where Self: GetDegree {} pub trait GGSWAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { let size: usize = k.0.div_ceil(base2k.0) as usize; diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 2361e3c..edf7b4c 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -32,14 +32,14 @@ pub trait SetGLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWELayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } impl LWEInfos for GLWELayout { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -96,8 +96,8 @@ impl LWEInfos for GLWE { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { @@ -148,7 +148,7 @@ impl FillUniform for GLWE { pub trait GLWEAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { GLWE { @@ -185,7 +185,7 @@ where } } -impl GLWEAlloc for Module where Self: GetRingDegree {} +impl GLWEAlloc for Module where Self: GetDegree {} impl GLWE> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 16fc6ea..80b0a9d 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -4,7 +4,7 @@ use poulpy_hal::layouts::{ use crate::{ dist::Distribution, - layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -18,7 +18,7 @@ pub struct GLWEPublicKey { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, @@ -43,8 +43,8 @@ impl LWEInfos for GLWEPublicKey { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { @@ -67,7 +67,7 @@ impl LWEInfos for GLWEPublicKeyLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -84,7 +84,7 @@ impl GLWEInfos for GLWEPublicKeyLayout { pub trait GLWEPublicKeyAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { GLWEPublicKey { @@ -122,7 +122,7 @@ where } } -impl GLWEPublicKeyAlloc for Module where Self: GetRingDegree {} +impl GLWEPublicKeyAlloc for Module where Self: GetDegree {} impl GLWEPublicKey> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index 3d5ce86..e3d7290 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -3,12 +3,12 @@ use std::fmt; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetRingDegree, LWEInfos, Rank, RingDegree, SetGLWEInfos, TorusPrecision, + Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, } @@ -22,7 +22,7 @@ impl LWEInfos for GLWEPlaintextLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } @@ -62,8 +62,8 @@ impl LWEInfos for GLWEPlaintext { self.data.size() } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } } @@ -87,7 +87,7 @@ impl fmt::Display for GLWEPlaintext { pub trait GLWEPlaintextAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { GLWEPlaintext { @@ -124,7 +124,7 @@ where } } -impl GLWEPlaintextAlloc for Module where Self: GetRingDegree {} +impl GLWEPlaintextAlloc for Module where Self: GetDegree {} impl GLWEPlaintext> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 72ecd95..51b2f0d 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -8,12 +8,12 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, GLWEInfos, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWESecretLayout { - pub n: RingDegree, + pub n: Degree, pub rank: Rank, } @@ -26,7 +26,7 @@ impl LWEInfos for GLWESecretLayout { TorusPrecision(0) } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -55,8 +55,8 @@ impl LWEInfos for GLWESecret { TorusPrecision(0) } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { @@ -78,7 +78,7 @@ impl GLWEInfos for GLWESecret { pub trait GLWESecretAlloc where - Self: GetRingDegree, + Self: GetDegree, { fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { GLWESecret { @@ -106,7 +106,7 @@ where } } -impl GLWESecretAlloc for Module where Self: GetRingDegree {} +impl GLWESecretAlloc for Module where Self: GetDegree {} impl GLWESecret> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 671f018..3aea241 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,15 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEToLWEKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_in: Rank, @@ -20,7 +20,7 @@ pub struct GLWEToLWEKeyLayout { } impl LWEInfos for GLWEToLWEKeyLayout { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -70,7 +70,7 @@ impl LWEInfos for GLWEToLWESwitchingKey { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 0c8831f..aed2807 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { - fn n(&self) -> RingDegree; + fn n(&self) -> Degree; fn k(&self) -> TorusPrecision; fn max_k(&self) -> TorusPrecision { TorusPrecision(self.k().0 * self.size() as u32) @@ -34,7 +34,7 @@ pub trait SetLWEInfos { #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWELayout { - pub n: RingDegree, + pub n: Degree, pub k: TorusPrecision, pub base2k: Base2K, } @@ -48,7 +48,7 @@ impl LWEInfos for LWELayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } @@ -67,8 +67,8 @@ impl LWEInfos for LWE { fn k(&self) -> TorusPrecision { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32 - 1) + fn n(&self) -> Degree { + Degree(self.data.n() as u32 - 1) } fn size(&self) -> usize { @@ -126,7 +126,7 @@ where } pub trait LWEAlloc { - fn alloc_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> LWE> { + fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, @@ -141,7 +141,7 @@ pub trait LWEAlloc { self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_lwe(&self, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize { + fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } @@ -164,7 +164,7 @@ impl LWE> { module.alloc_lwe_from_infos(infos) } - pub fn alloc(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> Self + pub fn alloc(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self where M: LWEAlloc, { @@ -179,7 +179,7 @@ impl LWE> { module.bytes_of_lwe_from_infos(infos) } - pub fn bytes_of(module: &M, n: RingDegree, base2k: Base2K, k: TorusPrecision) -> usize + pub fn bytes_of(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize where M: LWEAlloc, { diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 2ff0391..e449da6 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -6,20 +6,20 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, } impl LWEInfos for LWESwitchingKeyLayout { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } @@ -68,7 +68,7 @@ impl LWEInfos for LWESwitchingKey { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index 6ffd650..01ae19b 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -2,7 +2,7 @@ use std::fmt; use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; -use crate::layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEPlaintextLayout { @@ -19,8 +19,8 @@ impl LWEInfos for LWEPlaintextLayout { self.k } - fn n(&self) -> RingDegree { - RingDegree(0) + fn n(&self) -> Degree { + Degree(0) } fn size(&self) -> usize { @@ -43,8 +43,8 @@ impl LWEInfos for LWEPlaintext { self.k } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32 - 1) + fn n(&self) -> Degree { + Degree(self.data.n() as u32 - 1) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index d593ad5..b5f019f 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{Base2K, LWEInfos, RingDegree, TorusPrecision}, + layouts::{Base2K, Degree, LWEInfos, TorusPrecision}, }; pub struct LWESecret { @@ -14,7 +14,7 @@ pub struct LWESecret { } pub trait LWESecretAlloc { - fn alloc_lwe_secret(&self, n: RingDegree) -> LWESecret> { + fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, @@ -25,7 +25,7 @@ pub trait LWESecretAlloc { impl LWESecretAlloc for Module {} impl LWESecret> { - pub fn alloc(module: &M, n: RingDegree) -> Self + pub fn alloc(module: &M, n: Degree) -> Self where M: LWESecretAlloc, { @@ -55,8 +55,8 @@ impl LWEInfos for LWESecret { TorusPrecision(0) } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 72b7514..1427447 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,13 +6,13 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEToGLWESwitchingKeyLayout { - pub n: RingDegree, + pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank_out: Rank, @@ -28,7 +28,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n } } @@ -69,7 +69,7 @@ impl LWEInfos for LWEToGLWESwitchingKey { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 0d53307..abafe7e 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -37,12 +37,12 @@ pub use prepared::*; use poulpy_hal::layouts::{Backend, Module}; -pub trait GetRingDegree { - fn ring_degree(&self) -> RingDegree; +pub trait GetDegree { + fn ring_degree(&self) -> Degree; } -impl GetRingDegree for Module { - fn ring_degree(&self) -> RingDegree { +impl GetDegree for Module { + fn ring_degree(&self) -> Degree { Self::n(&self).into() } } @@ -204,14 +204,14 @@ macro_rules! newtype_u32 { }; } -newtype_u32!(RingDegree); +newtype_u32!(Degree); newtype_u32!(TorusPrecision); newtype_u32!(Base2K); newtype_u32!(Dnum); newtype_u32!(Rank); newtype_u32!(Dsize); -impl RingDegree { +impl Degree { pub fn log2(&self) -> usize { let n: usize = self.0 as usize; (usize::BITS - (n - 1).leading_zeros()) as _ diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 7c8d0b9..d09b2e6 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - AutomorphismKeyToRef, Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, + AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -15,7 +15,7 @@ pub struct AutomorphismKeyPrepared { } impl LWEInfos for AutomorphismKeyPrepared { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index fd6033a..98d45b5 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -16,8 +16,8 @@ pub struct GGLWEPrepared { } impl LWEInfos for GGLWEPrepared { - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -59,7 +59,7 @@ impl GGLWEInfos for GGLWEPrepared { pub trait GGLWEPreparedAlloc where - Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_gglwe_prepared( &self, @@ -149,7 +149,7 @@ where } } -impl GGLWEPreparedAlloc for Module where Module: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGLWEPreparedAlloc for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGLWEPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -201,7 +201,7 @@ impl GGLWEPrepared, B> { pub trait GGLWEPrepare where - Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize where @@ -233,7 +233,7 @@ where } } -impl GGLWEPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} +impl GGLWEPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} impl GGLWEPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 2154b3e..d8ab0ca 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,8 +1,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, GLWESwtichingKeyGetMetaData, - LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, + GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; @@ -34,7 +34,7 @@ impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPre } impl LWEInfos for GLWESwitchingKeyPrepared { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.key.n() } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 1ff5759..29f6e28 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TensorKey, TensorKeyToRef, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -14,7 +14,7 @@ pub struct TensorKeyPrepared { } impl LWEInfos for TensorKeyPrepared { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.keys[0].n() } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index 6365818..551660b 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] @@ -16,8 +16,8 @@ pub struct GGSWPrepared { } impl LWEInfos for GGSWPrepared { - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn base2k(&self) -> Base2K { @@ -51,7 +51,7 @@ impl GGSWInfos for GGSWPrepared { pub trait GGSWPreparedAlloc where - Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf, + Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf, { fn alloc_ggsw_prepared( &self, @@ -135,7 +135,7 @@ where } } -impl GGSWPreparedAlloc for Module where Self: GetRingDegree + VmpPMatAlloc + VmpPMatBytesOf {} +impl GGSWPreparedAlloc for Module where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf {} impl GGSWPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -177,7 +177,7 @@ impl GGSWPrepared { pub trait GGSWPrepare where - Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare, + Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize where @@ -207,7 +207,7 @@ where } } -impl GGSWPrepare for Module where Self: GetRingDegree + VmpPrepareTmpBytes + VmpPrepare {} +impl GGSWPrepare for Module where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare {} impl GGSWPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 62165c8..5212a11 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -5,9 +5,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{ - Base2K, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, - }, + layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] @@ -41,8 +39,8 @@ impl LWEInfos for GLWEPublicKeyPrepared { self.data.size() } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } } @@ -54,7 +52,7 @@ impl GLWEInfos for GLWEPublicKeyPrepared { pub trait GLWEPublicKeyPreparedAlloc where - Self: GetRingDegree + VecZnxDftAlloc + VecZnxDftBytesOf, + Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf, { fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { @@ -120,7 +118,7 @@ impl GLWEPublicKeyPrepared, B> { pub trait GLWEPublicKeyPrepare where - Self: GetRingDegree + VecZnxDftApply, + Self: GetDegree + VecZnxDftApply, { fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where @@ -146,7 +144,7 @@ where } } -impl GLWEPublicKeyPrepare for Module where Self: GetRingDegree + VecZnxDftApply {} +impl GLWEPublicKeyPrepare for Module where Self: GetDegree + VecZnxDftApply {} impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index f50f2dc..5eb9e07 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - Base2K, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, prepared::SetDist, }, }; @@ -31,8 +31,8 @@ impl LWEInfos for GLWESecretPrepared { TorusPrecision(0) } - fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + fn n(&self) -> Degree { + Degree(self.data.n() as u32) } fn size(&self) -> usize { @@ -47,7 +47,7 @@ impl GLWEInfos for GLWESecretPrepared { pub trait GLWESecretPreparedAlloc where - Self: GetRingDegree + SvpPPolBytesOf + SvpPPolAlloc, + Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc, { fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { @@ -75,7 +75,7 @@ where } } -impl GLWESecretPreparedAlloc for Module where Self: GetRingDegree + SvpPPolBytesOf + SvpPPolAlloc {} +impl GLWESecretPreparedAlloc for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc {} impl GLWESecretPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -110,8 +110,8 @@ impl GLWESecretPrepared, B> { } impl GLWESecretPrepared { - pub fn n(&self) -> RingDegree { - RingDegree(self.data.n() as u32) + pub fn n(&self) -> Degree { + Degree(self.data.n() as u32) } pub fn rank(&self) -> Rank { diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 4c7f65e..cf1d875 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -20,7 +20,7 @@ impl LWEInfos for GLWEToLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 7e0b1da..83508d0 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -20,7 +20,7 @@ impl LWEInfos for LWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 6afb32a..43b006c 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, prepared::{ GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, @@ -21,7 +21,7 @@ impl LWEInfos for LWEToGLWESwitchingKeyPrepared { self.0.k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.0.n() } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 2dcc77a..88a0ac5 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -25,4 +25,4 @@ pub use encryption::SIGMA; pub use scratch::*; -// pub mod tests; +pub mod tests; diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 23b489b..5dcd276 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -1,182 +1,182 @@ -pub mod test_suite; +// pub mod test_suite; #[cfg(test)] mod serialization; -#[allow(unused_imports)] -use poulpy_hal::backend_test_suite; +//#[allow(unused_imports)] +// use poulpy_hal::backend_test_suite; -#[cfg(test)] -backend_test_suite!( - mod cpu_spqlios, - backend = poulpy_backend::cpu_spqlios::FFT64Spqlios, - size = 1<<8, - tests = { - // GLWE Encryption - glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, - glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, - glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, - glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, - // GLWE Keyswitch - glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, - glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, - // GLWE Automorphism - glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, - glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, - // GLWE External Product - glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, - glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, - // GLWE Trace - glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, - glwe_packing => crate::tests::test_suite::test_glwe_packing, - // GGLWE Encryption - gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, - gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, - gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, - gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, - gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, - gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, - // GGLWE Keyswitching - gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, - gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, - // GGLWE External Product - gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, - gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, - // GGLWE Automorphism - gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, - gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, - // GGSW Encryption - ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, - ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, - // GGSW Keyswitching - ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, - ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, - // GGSW External Product - ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, - ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, - // GGSW Automorphism - ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, - ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, - // LWE - lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, - glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, - lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, - } -); - -#[cfg(test)] -backend_test_suite!( - mod cpu_ref, - backend = poulpy_backend::cpu_fft64_ref::FFT64Ref, - size = 1<<8, - tests = { - // GLWE Encryption - glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, - glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, - glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, - glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, - // GLWE Keyswitch - glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, - glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, - // GLWE Automorphism - glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, - glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, - // GLWE External Product - glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, - glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, - // GLWE Trace - glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, - glwe_packing => crate::tests::test_suite::test_glwe_packing, - // GGLWE Encryption - gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, - gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, - gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, - gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, - gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, - gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, - // GGLWE Keyswitching - gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, - gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, - // GGLWE External Product - gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, - gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, - // GGLWE Automorphism - gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, - gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, - // GGSW Encryption - ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, - ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, - // GGSW Keyswitching - ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, - ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, - // GGSW External Product - ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, - ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, - // GGSW Automorphism - ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, - ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, - // LWE - lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, - glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, - lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, - } -); - -#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] -#[cfg(test)] -backend_test_suite!( - mod cpu_avx, - backend = poulpy_backend::cpu_fft64_avx::FFT64Avx, - size = 1<<8, - tests = { - // GLWE Encryption - glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, - glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, - glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, - glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, - // GLWE Keyswitch - glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, - glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, - // GLWE Automorphism - glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, - glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, - // GLWE External Product - glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, - glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, - // GLWE Trace - glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, - glwe_packing => crate::tests::test_suite::test_glwe_packing, - // GGLWE Encryption - gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, - gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, - gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, - gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, - gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, - gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, - // GGLWE Keyswitching - gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, - gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, - // GGLWE External Product - gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, - gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, - // GGLWE Automorphism - gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, - gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, - // GGSW Encryption - ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, - ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, - // GGSW Keyswitching - ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, - ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, - // GGSW External Product - ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, - ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, - // GGSW Automorphism - ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, - ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, - // LWE - lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, - glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, - lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, - } -); +// #[cfg(test)] +// backend_test_suite!( +// mod cpu_spqlios, +// backend = poulpy_backend::cpu_spqlios::FFT64Spqlios, +// size = 1<<8, +// tests = { +// GLWE Encryption +// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, +// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, +// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, +// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +// GLWE Keyswitch +// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +// GLWE Automorphism +// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +// GLWE External Product +// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +// GLWE Trace +// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +// glwe_packing => crate::tests::test_suite::test_glwe_packing, +// GGLWE Encryption +// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, +// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, +// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +// GGLWE Keyswitching +// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +// GGLWE External Product +// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +// GGLWE Automorphism +// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +// GGSW Encryption +// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +// GGSW Keyswitching +// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +// GGSW External Product +// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +// GGSW Automorphism +// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +// LWE +// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +// } +// ); +// +// #[cfg(test)] +// backend_test_suite!( +// mod cpu_ref, +// backend = poulpy_backend::cpu_fft64_ref::FFT64Ref, +// size = 1<<8, +// tests = { +// GLWE Encryption +// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, +// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, +// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, +// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +// GLWE Keyswitch +// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +// GLWE Automorphism +// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +// GLWE External Product +// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +// GLWE Trace +// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +// glwe_packing => crate::tests::test_suite::test_glwe_packing, +// GGLWE Encryption +// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, +// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, +// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +// GGLWE Keyswitching +// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +// GGLWE External Product +// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +// GGLWE Automorphism +// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +// GGSW Encryption +// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +// GGSW Keyswitching +// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +// GGSW External Product +// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +// GGSW Automorphism +// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +// LWE +// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +// } +// ); +// +// #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] +// #[cfg(test)] +// backend_test_suite!( +// mod cpu_avx, +// backend = poulpy_backend::cpu_fft64_avx::FFT64Avx, +// size = 1<<8, +// tests = { +// GLWE Encryption +// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, +// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, +// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, +// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +// GLWE Keyswitch +// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +// GLWE Automorphism +// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +// GLWE External Product +// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +// GLWE Trace +// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +// glwe_packing => crate::tests::test_suite::test_glwe_packing, +// GGLWE Encryption +// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, +// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, +// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +// GGLWE Keyswitching +// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +// GGLWE External Product +// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +// GGLWE Automorphism +// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +// GGSW Encryption +// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +// GGSW Keyswitching +// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +// GGSW External Product +// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +// GGSW Automorphism +// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +// LWE +// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +// } +// ); diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 3c4e564..c770311 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,4 +1,5 @@ -use poulpy_hal::test_suite::serialization::test_reader_writer_interface; +use poulpy_backend::FFT64Ref; +use poulpy_hal::{api::ModuleNew, layouts::Module, test_suite::serialization::test_reader_writer_interface}; use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, @@ -20,123 +21,143 @@ const DSIZE: Dsize = Dsize(1); #[test] fn glwe_serialization() { - let original: GLWE> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GLWE> = GLWE::alloc(&module, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GLWECompressed> = GLWECompressed::alloc(&module, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: LWE> = LWE::alloc(&module, N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: LWECompressed> = LWECompressed::alloc(&module, BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GGLWE> = GGLWE::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let original: GGLWECompressed> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GGLWECompressed> = GGLWECompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { + let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + GLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let original: AutomorphismKey> = AutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: AutomorphismKey> = AutomorphismKey::alloc_with(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: TensorKey> = TensorKey::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { + let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWEToLWESwitchingKeyCompressed> = - GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_compressed_serialization() { + let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: LWEToGLWESwitchingKeyCompressed> = - LWEToGLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + LWEToGLWESwitchingKeyCompressed::alloc_with(&module, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_serialization() { - let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(&module, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(&module, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GGSW> = GGSW::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let original: GGSWCompressed> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let module: Module = Module::::new(N_GLWE.as_u32() as u64); + let original: GGSWCompressed> = GGSWCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index c2ce4ad..a1f39c6 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -188,8 +188,8 @@ where // Scratch space (4MB) let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let n_glwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_glwe(); - let n_lwe: poulpy_core::layouts::RingDegree = params.cbt_infos.layout_brk.n_lwe(); + let n_glwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_glwe(); + let n_lwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_lwe(); let rank: poulpy_core::layouts::Rank = params.cbt_infos.layout_brk.rank; let module: Module = Module::::new(n_glwe.as_u32() as u64); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index de4e30d..70bd910 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -38,7 +38,7 @@ impl LWEInfos for FheUintBlocks { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::RingDegree { + fn n(&self) -> poulpy_core::layouts::Degree { self.blocks[0].n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 2f4bc44..c0cd09f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -225,7 +225,7 @@ impl LWEInfos for FheUintBlocksPrep< self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::RingDegree { + fn n(&self) -> poulpy_core::layouts::Degree { self.blocks[0].n() } } @@ -256,7 +256,7 @@ impl LWEInfos for FheUintBlocksPrepDebug { self.blocks[0].k() } - fn n(&self) -> poulpy_core::layouts::RingDegree { + fn n(&self) -> poulpy_core::layouts::Degree { self.blocks[0].n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index 38ea57d..a3a779a 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -87,7 +87,7 @@ impl LWEInfos for FheUintWord { self.0.k() } - fn n(&self) -> poulpy_core::layouts::RingDegree { + fn n(&self) -> poulpy_core::layouts::Degree { self.0.n() } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 924d53a..90175fb 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,6 +1,6 @@ #[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, RingDegree, TensorKeyLayout, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, TensorKeyLayout, TorusPrecision, }; @@ -26,7 +26,7 @@ pub(crate) const TEST_RANK: u32 = 2; #[cfg(test)] pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { - n: RingDegree(TEST_N_GLWE), + n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GLWE), rank: Rank(TEST_RANK), @@ -34,7 +34,7 @@ pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { #[cfg(test)] pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { - n: RingDegree(TEST_N_GLWE), + n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GGSW), rank: Rank(TEST_RANK), @@ -46,15 +46,15 @@ pub(crate) static TEST_GGSW_INFOS: GGSWLayout = GGSWLayout { pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { cbt: CircuitBootstrappingKeyLayout { layout_brk: BlindRotationKeyLayout { - n_glwe: RingDegree(TEST_N_GLWE), - n_lwe: RingDegree(TEST_N_LWE), + n_glwe: Degree(TEST_N_GLWE), + n_lwe: Degree(TEST_N_LWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), dnum: Dnum(3), rank: Rank(TEST_RANK), }, layout_atk: AutomorphismKeyLayout { - n: RingDegree(TEST_N_GLWE), + n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), @@ -62,7 +62,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dsize: Dsize(1), }, layout_tsk: TensorKeyLayout { - n: RingDegree(TEST_N_GLWE), + n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), rank: Rank(TEST_RANK), @@ -71,7 +71,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { }, }, ks: GLWEToLWEKeyLayout { - n: RingDegree(TEST_N_GLWE), + n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(39), rank_in: Rank(TEST_RANK), diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/key.rs index 276539f..d9bb0a9 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, layouts::{ - Base2K, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -19,8 +19,8 @@ use crate::tfhe::blind_rotation::BlindRotationAlgo; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct BlindRotationKeyLayout { - pub n_glwe: RingDegree, - pub n_lwe: RingDegree, + pub n_glwe: Degree, + pub n_lwe: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub dnum: Dnum, @@ -28,11 +28,11 @@ pub struct BlindRotationKeyLayout { } impl BlindRotationKeyInfos for BlindRotationKeyLayout { - fn n_glwe(&self) -> RingDegree { + fn n_glwe(&self) -> Degree { self.n_glwe } - fn n_lwe(&self) -> RingDegree { + fn n_lwe(&self) -> Degree { self.n_lwe } } @@ -62,7 +62,7 @@ impl LWEInfos for BlindRotationKeyLayout { self.k } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.n_glwe } } @@ -71,8 +71,8 @@ pub trait BlindRotationKeyInfos where Self: GGSWInfos, { - fn n_glwe(&self) -> RingDegree; - fn n_lwe(&self) -> RingDegree; + fn n_glwe(&self) -> Degree; + fn n_lwe(&self) -> Degree; } pub trait BlindRotationKeyAlloc { @@ -178,12 +178,12 @@ impl WriterTo for BlindRotationKey { } impl BlindRotationKeyInfos for BlindRotationKey { - fn n_glwe(&self) -> RingDegree { + fn n_glwe(&self) -> Degree { self.n() } - fn n_lwe(&self) -> RingDegree { - RingDegree(self.keys.len() as u32) + fn n_lwe(&self) -> Degree { + Degree(self.keys.len() as u32) } } @@ -206,7 +206,7 @@ impl LWEInfos for BlindRotationKey { self.keys[0].k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs index af7e40a..784d332 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, - layouts::{Base2K, Dsize, GGSWInfos, GLWEInfos, LWEInfos, RingDegree, TorusPrecision, compressed::GGSWCompressed}, + layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; @@ -94,17 +94,17 @@ impl WriterTo for BlindRotationKeyCompressed } impl BlindRotationKeyInfos for BlindRotationKeyCompressed { - fn n_glwe(&self) -> RingDegree { + fn n_glwe(&self) -> Degree { self.n() } - fn n_lwe(&self) -> RingDegree { - RingDegree(self.keys.len() as u32) + fn n_lwe(&self) -> Degree { + Degree(self.keys.len() as u32) } } impl LWEInfos for BlindRotationKeyCompressed { - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.keys[0].n() } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs index 1f99ef5..2001083 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -8,7 +8,7 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, RingDegree, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{GGSWPrepared, Prepare, PrepareAlloc}, }, }; @@ -30,12 +30,12 @@ pub struct BlindRotationKeyPrepared } impl BlindRotationKeyInfos for BlindRotationKeyPrepared { - fn n_glwe(&self) -> RingDegree { + fn n_glwe(&self) -> Degree { self.n() } - fn n_lwe(&self) -> RingDegree { - RingDegree(self.data.len() as u32) + fn n_lwe(&self) -> Degree { + Degree(self.data.len() as u32) } } @@ -48,7 +48,7 @@ impl LWEInfos for BlindRotationKeyP self.data[0].k() } - fn n(&self) -> RingDegree { + fn n(&self) -> Degree { self.data[0].n() } From ca3997a4c9b085f22e2fec2c56858b2119a7dc29 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Sat, 18 Oct 2025 13:12:01 +0200 Subject: [PATCH 38/60] wip test glwe encryption --- poulpy-core/src/decryption/glwe_ct.rs | 8 +- poulpy-core/src/encryption/compressed/mod.rs | 7 ++ poulpy-core/src/encryption/mod.rs | 13 ++ poulpy-core/src/lib.rs | 1 + poulpy-core/src/noise/gglwe_ct.rs | 4 +- poulpy-core/src/noise/ggsw_ct.rs | 4 +- poulpy-core/src/noise/glwe_ct.rs | 4 +- poulpy-core/src/tests/mod.rs | 2 +- .../tests/test_suite/encryption/glwe_ct.rs | 115 +++++------------- .../src/tests/test_suite/encryption/mod.rs | 16 +-- poulpy-core/src/tests/test_suite/mod.rs | 18 +-- 11 files changed, 82 insertions(+), 110 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index f40d765..3c64fda 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -15,7 +15,7 @@ impl GLWE> { pub fn decrypt_tmp_bytes(module: &M, a_infos: &A) -> usize where A: GLWEInfos, - M: GLWEDecryption, + M: GLWEDecrypt, { module.glwe_decrypt_tmp_bytes(a_infos) } @@ -26,14 +26,14 @@ impl GLWE { where P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, - M: GLWEDecryption, + M: GLWEDecrypt, Scratch: ScratchTakeBasic, { module.glwe_decrypt(self, pt, sk, scratch); } } -pub trait GLWEDecryption +pub trait GLWEDecrypt where Self: Sized + ModuleN @@ -110,7 +110,7 @@ where } } -impl GLWEDecryption for Module where +impl GLWEDecrypt for Module where Self: ModuleN + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes diff --git a/poulpy-core/src/encryption/compressed/mod.rs b/poulpy-core/src/encryption/compressed/mod.rs index e763db4..a2f7b9a 100644 --- a/poulpy-core/src/encryption/compressed/mod.rs +++ b/poulpy-core/src/encryption/compressed/mod.rs @@ -4,3 +4,10 @@ mod gglwe_ksk; mod gglwe_tsk; mod ggsw_ct; mod glwe_ct; + +pub use gglwe_atk::*; +pub use gglwe_ct::*; +pub use gglwe_ksk::*; +pub use gglwe_tsk::*; +pub use ggsw_ct::*; +pub use glwe_ct::*; diff --git a/poulpy-core/src/encryption/mod.rs b/poulpy-core/src/encryption/mod.rs index fb9a459..9cc3a4b 100644 --- a/poulpy-core/src/encryption/mod.rs +++ b/poulpy-core/src/encryption/mod.rs @@ -11,5 +11,18 @@ mod lwe_ct; mod lwe_ksk; mod lwe_to_glwe_ksk; +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 const SIGMA: f64 = 3.2; pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA; diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 88a0ac5..8c45893 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -17,6 +17,7 @@ pub mod layouts; pub use automorphism::*; pub use conversion::*; pub use dist::*; +pub use encryption::*; pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 189f471..48c2df8 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, }; -use crate::decryption::GLWEDecryption; +use crate::decryption::GLWEDecrypt; use crate::layouts::{ GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, @@ -90,7 +90,7 @@ pub trait GGLWENoise { impl GGLWENoise for Module where - Module: GLWEDecryption, + Module: GLWEDecrypt, Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index aa270a5..13531e3 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::decryption::GLWEDecryption; +use crate::decryption::GLWEDecrypt; use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; @@ -79,7 +79,7 @@ where + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace - + GLWEDecryption, + + GLWEDecrypt, Scratch: ScratchTakeBasic, { fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index d21ae60..b89bdc6 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ }; use crate::{ - decryption::GLWEDecryption, + decryption::GLWEDecrypt, layouts::{ GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, @@ -124,7 +124,7 @@ where + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace - + GLWEDecryption, + + GLWEDecrypt, Scratch: ScratchTakeBasic + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 5dcd276..6d94982 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -1,4 +1,4 @@ -// pub mod test_suite; +pub mod test_suite; #[cfg(test)] mod serialization; 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 ed2c1e3..73d1679 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -1,60 +1,38 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, + SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, + VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, + VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, + VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, }, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GLWEEncryptSk, GLWESub, ScratchTakeCore, + decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, - compressed::{Decompress, GLWECompressed}, - prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, LWEInfos, + compressed::GLWECompressed, + prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, - operations::GLWEOperations, }; -pub fn test_glwe_encrypt_sk(module: &Module) +pub fn test_glwe_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + SvpApplyDftToDft - + VecZnxBigAddNormal + Module: GLWEAlloc + + GLWEEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWESub, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -76,21 +54,22 @@ where k: k_pt.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), - ); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + + sk_prepared.prepare(module, &sk); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); @@ -105,7 +84,7 @@ where ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); - pt_want.sub_inplace_ab(module, &pt_have); + module.glwe_sub_inplace(&mut pt_want, &pt_have); let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2(); let noise_want: f64 = SIGMA; @@ -138,15 +117,6 @@ where + VecZnxNormalize + VecZnxSub + VecZnxCopy, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, { let base2k: usize = 8; let k_ct: usize = 54; @@ -239,15 +209,6 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, { let base2k: usize = 8; let k_ct: usize = 54; @@ -268,9 +229,8 @@ where let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos), - ); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -316,15 +276,6 @@ where + VecZnxDftAlloc + SvpApplyDftToDft + VecZnxBigAddNormal, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, { let base2k: usize = 8; let k_ct: usize = 54; diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index d871177..33fd5a1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,11 +1,11 @@ -mod gglwe_atk; -mod gglwe_ct; -mod ggsw_ct; +// mod gglwe_atk; +// mod gglwe_ct; +// mod ggsw_ct; mod glwe_ct; -mod glwe_tsk; +// mod glwe_tsk; -pub use gglwe_atk::*; -pub use gglwe_ct::*; -pub use ggsw_ct::*; +// pub use gglwe_atk::*; +// pub use gglwe_ct::*; +// pub use ggsw_ct::*; pub use glwe_ct::*; -pub use glwe_tsk::*; +// pub use glwe_tsk::*; diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 624b9c3..54157bd 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,12 +1,12 @@ -pub mod automorphism; +// pub mod automorphism; pub mod encryption; -pub mod external_product; -pub mod keyswitch; +// pub mod external_product; +// pub mod keyswitch; -mod conversion; -mod packing; -mod trace; +// mod conversion; +// mod packing; +// mod trace; -pub use conversion::*; -pub use packing::*; -pub use trace::*; +// pub use conversion::*; +// pub use packing::*; +// pub use trace::*; From d6e9805a8f051bd43289dbe9fc4702b5967762ed Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Sun, 19 Oct 2025 08:48:32 +0200 Subject: [PATCH 39/60] glwe encryption + reenable associated tests --- poulpy-core/src/tests/mod.rs | 31 ++-- .../tests/test_suite/encryption/glwe_ct.rs | 164 +++++++----------- 2 files changed, 78 insertions(+), 117 deletions(-) diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 6d94982..37d39aa 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -3,20 +3,20 @@ pub mod test_suite; #[cfg(test)] mod serialization; -//#[allow(unused_imports)] -// use poulpy_hal::backend_test_suite; +#[allow(unused_imports)] +use poulpy_hal::backend_test_suite; -// #[cfg(test)] -// backend_test_suite!( -// mod cpu_spqlios, -// backend = poulpy_backend::cpu_spqlios::FFT64Spqlios, -// size = 1<<8, -// tests = { -// GLWE Encryption -// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, -// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, -// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, -// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +#[cfg(test)] +backend_test_suite!( + mod cpu_spqlios, + backend = poulpy_backend::cpu_spqlios::FFT64Spqlios, + size = 1<<8, + tests = { + //GLWE Encryption + glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, + lwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, + glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, + glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch // glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, // glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, @@ -61,9 +61,8 @@ mod serialization; // lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, // lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, -// } -// ); -// +} +); // #[cfg(test)] // backend_test_suite!( // mod cpu_ref, 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 73d1679..4d912fe 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -1,22 +1,16 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, - SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - GLWEEncryptSk, GLWESub, ScratchTakeCore, + GLWECompressedEncryptSk, GLWEEncryptPk, GLWEEncryptSk, GLWEPublicKeyGenerate, GLWESub, ScratchTakeCore, decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, GLWESecretPrepare, - GLWESecretPreparedAlloc, LWEInfos, + GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, + GLWEPublicKeyPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, compressed::GLWECompressed, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, @@ -67,8 +61,8 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); @@ -93,30 +87,17 @@ where } } -pub fn test_glwe_compressed_encrypt_sk(module: &Module) +pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + SvpApplyDftToDft - + VecZnxBigAddNormal + Module: GLWEAlloc + + GLWECompressedEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxCopy, + + GLWESub, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -138,10 +119,10 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -151,9 +132,11 @@ where GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); @@ -168,12 +151,12 @@ where scratch.borrow(), ); - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); - pt_want.sub_inplace_ab(module, &pt_have); + module.glwe_sub_inplace(&mut pt_want, &pt_have); let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2(); let noise_want: f64 = SIGMA; @@ -186,29 +169,17 @@ where } } -pub fn test_glwe_encrypt_zero_sk(module: &Module) +pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + SvpApplyDftToDft - + VecZnxBigAddNormal + Module: GLWEAlloc + + GLWEEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, + + GLWESub, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -223,7 +194,7 @@ where rank: rank.into(), }; - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); @@ -232,11 +203,13 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); + + let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); ct.encrypt_zero_sk( module, @@ -251,31 +224,20 @@ where } } -pub fn test_glwe_encrypt_pk(module: &Module) +pub fn test_glwe_encrypt_pk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GLWEAlloc + + GLWEEncryptPk + + GLWEPublicKeyPrepare + + GLWEPublicKeyPreparedAlloc + + GLWEPublicKeyGenerate + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VecZnxDftAlloc - + SvpApplyDftToDft - + VecZnxBigAddNormal, + + GLWESub, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -290,31 +252,31 @@ where rank: rank.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xu: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) - | GLWE::decrypt_tmp_bytes(module, &glwe_infos) - | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos), - ); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(&glwe_infos); + let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); + + let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos); pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let pk_prepared: GLWEPublicKeyPrepared, B> = pk.prepare_alloc(module, scratch.borrow()); + let mut pk_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc_from_infos(module, &glwe_infos); + pk_prepared.prepare(module, &pk); ct.encrypt_pk( module, @@ -327,7 +289,7 @@ where ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); - pt_want.sub_inplace_ab(module, &pt_have); + module.glwe_sub_inplace(&mut pt_want, &pt_have); let noise_have: f64 = pt_want.data.std(base2k, 0).log2(); let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * SIGMA * SIGMA).sqrt()).log2() - (k_ct as f64); From a706b759d6192de09f4d86de1aa78a7409a8cefe Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Sun, 19 Oct 2025 18:11:04 +0200 Subject: [PATCH 40/60] fixed encryption --- poulpy-core/src/dist.rs | 8 + .../src/encryption/compressed/gglwe_ct.rs | 139 +++++++++--------- .../src/encryption/compressed/gglwe_tsk.rs | 13 +- .../src/encryption/compressed/ggsw_ct.rs | 86 +++++------ .../src/encryption/compressed/glwe_ct.rs | 42 +++--- poulpy-core/src/encryption/gglwe_tsk.rs | 10 +- poulpy-core/src/encryption/glwe_ct.rs | 2 + poulpy-core/src/encryption/glwe_pk.rs | 42 +++--- .../src/layouts/compressed/gglwe_ct.rs | 19 +++ poulpy-core/src/layouts/compressed/ggsw_ct.rs | 20 +++ poulpy-core/src/layouts/compressed/glwe_ct.rs | 20 +++ poulpy-core/src/layouts/glwe_pk.rs | 23 +-- poulpy-core/src/layouts/glwe_sk.rs | 9 +- poulpy-core/src/layouts/prepared/glwe_pk.rs | 23 +-- poulpy-core/src/layouts/prepared/glwe_sk.rs | 26 ++-- poulpy-core/src/tests/mod.rs | 2 +- .../tests/test_suite/encryption/glwe_ct.rs | 1 + 17 files changed, 289 insertions(+), 196 deletions(-) diff --git a/poulpy-core/src/dist.rs b/poulpy-core/src/dist.rs index 415b0b5..c754278 100644 --- a/poulpy-core/src/dist.rs +++ b/poulpy-core/src/dist.rs @@ -1,5 +1,13 @@ use std::io::{Read, Result, Write}; +pub trait GetDistribution { + fn dist(&self) -> &Distribution; +} + +pub trait GetDistributionMut { + fn dist_mut(&mut self) -> &mut Distribution; +} + #[derive(Clone, Copy, Debug)] pub enum Distribution { TernaryFixed(usize), // Ternary with fixed Hamming weight diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 638c566..fbac0f6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GGLWEInfos, GLWEPlaintextAlloc, LWEInfos, + GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -60,7 +60,7 @@ pub trait GGLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -94,80 +94,85 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); - let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; - let sk = &sk.to_ref(); + { + let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - assert_eq!( - res.rank_in(), - pt.cols() as u32, - "res.rank_in(): {} != pt.cols(): {}", - res.rank_in(), - pt.cols() - ); - assert_eq!( - res.rank_out(), - sk.rank(), - "res.rank_out(): {} != sk.rank(): {}", - res.rank_out(), - sk.rank() - ); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - assert!( - scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), - "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", - scratch.available(), - GGLWECompressed::encrypt_sk_tmp_bytes(self, res) - ); - assert!( - res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, - "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", - res.dnum(), - res.dsize(), - res.base2k(), - res.dnum().0 * res.dsize().0 * res.base2k().0, - res.k() - ); + assert_eq!( + res.rank_in(), + pt.cols() as u32, + "res.rank_in(): {} != pt.cols(): {}", + res.rank_in(), + pt.cols() + ); + assert_eq!( + res.rank_out(), + sk.rank(), + "res.rank_out(): {} != sk.rank(): {}", + res.rank_out(), + sk.rank() + ); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + assert!( + scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res), + "scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}", + scratch.available(), + GGLWECompressed::encrypt_sk_tmp_bytes(self, res) + ); + assert!( + res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, + "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}", + res.dnum(), + res.dsize(), + res.base2k(), + res.dnum().0 * res.dsize().0 * res.base2k().0, + res.k() + ); - let dnum: usize = res.dnum().into(); - let dsize: usize = res.dsize().into(); - let base2k: usize = res.base2k().into(); - let rank_in: usize = res.rank_in().into(); - let cols: usize = (res.rank_out() + 1).into(); + let dnum: usize = res.dnum().into(); + let dsize: usize = res.dsize().into(); + let base2k: usize = res.base2k().into(); + let rank_in: usize = res.rank_in().into(); + let cols: usize = (res.rank_out() + 1).into(); - let mut source_xa = Source::new(seed); + let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res); - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|d_i| { - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - tmp_pt.data.zero(); // zeroes for next iteration - self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); - self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(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 + tmp_pt.data.zero(); // zeroes for next iteration + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1); - let (seed, mut source_xa_tmp) = source_xa.branch(); - res.seed[col_i * dnum + d_i] = seed; + let (seed, mut source_xa_tmp) = source_xa.branch(); + seeds[col_i * dnum + d_i] = seed; - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.at_mut(d_i, col_i).data, - cols, - true, - Some((&tmp_pt, 0)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scrach_1, - ); - }); - }); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(d_i, col_i).data, + cols, + true, + Some((&tmp_pt, 0)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scrach_1, + ); + } + } + } + + res.seed_mut().copy_from_slice(&seeds); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 1184c03..9a37d17 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -9,11 +9,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, - encryption::gglwe_tsk::TensorKeyEncryptSk, + GetDistribution, ScratchTakeCore, + encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk}, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; @@ -37,7 +36,7 @@ impl TensorKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, M: GGLWETensorKeyCompressedEncryptSk, { module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); @@ -58,7 +57,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef + GetDist; + S: GLWESecretToRef + GetDistribution; } impl GGLWETensorKeyCompressedEncryptSk for Module @@ -95,7 +94,7 @@ where scratch: &mut Scratch, ) where R: TensorKeyCompressedToMut, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, { let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index ec45398..4526faf 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWInfos, GLWEInfos, LWEInfos, + GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -83,62 +83,66 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; - #[cfg(debug_assertions)] { - use poulpy_hal::layouts::ZnxInfos; + let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } + #[cfg(debug_assertions)] + { + use poulpy_hal::layouts::ZnxInfos; - let base2k: usize = res.base2k().into(); - let rank: usize = res.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = res.dsize().into(); + assert_eq!(res.rank(), sk.rank()); + assert_eq!(res.n(), sk.n()); + assert_eq!(pt.n() as u32, sk.n()); + } - let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); - let mut source = Source::new(seed_xa); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); - res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols]; + let mut source = Source::new(seed_xa); - for row_i in 0..res.dnum().into() { - tmp_pt.data.zero(); + for row_i in 0..res.dnum().into() { + tmp_pt.data.zero(); - // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt - self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); - self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); + // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt + self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0); + self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1); - for col_j in 0..rank + 1 { - // rlwe encrypt of vec_znx_pt into vec_znx_ct + for col_j in 0..rank + 1 { + // rlwe encrypt of vec_znx_pt into vec_znx_ct - let (seed, mut source_xa_tmp) = source.branch(); + let (seed, mut source_xa_tmp) = source.branch(); - res.seed[row_i * cols + col_j] = seed; + seeds[row_i * cols + col_j] = seed; - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.at_mut(row_i, col_j).data, - cols, - true, - Some((&tmp_pt, col_j)), - sk, - &mut source_xa_tmp, - source_xe, - SIGMA, - scratch_1, - ); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.at_mut(row_i, col_j).data, + cols, + true, + Some((&tmp_pt, col_j)), + sk, + &mut source_xa_tmp, + source_xe, + SIGMA, + scratch_1, + ); + } } } + + res.seed_mut().copy_from_slice(&seeds); } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index a30001c..e2b1414 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -9,7 +9,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GLWEInfos, GLWEPlaintextToRef, LWEInfos, + GLWECompressedSeedMut, GLWEInfos, GLWEPlaintextToRef, LWEInfos, compressed::{GLWECompressed, GLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -58,7 +58,7 @@ pub trait GLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECompressedToMut, + R: GLWECompressedToMut + GLWECompressedSeedMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -83,28 +83,30 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECompressedToMut, + R: GLWECompressedToMut + GLWECompressedSeedMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); - let mut source_xa: Source = Source::new(seed_xa); - let cols: usize = (res.rank() + 1).into(); + { + let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); + let mut source_xa: Source = Source::new(seed_xa); + let cols: usize = (res.rank() + 1).into(); - self.glwe_encrypt_sk_internal( - res.base2k().into(), - res.k().into(), - &mut res.data, - cols, - true, - Some((pt, 0)), - sk, - &mut source_xa, - source_xe, - SIGMA, - scratch, - ); + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.data, + cols, + true, + Some((pt, 0)), + sk, + &mut source_xa, + source_xe, + SIGMA, + scratch, + ); + } - res.seed = seed_xa; + res.seed_mut().copy_from_slice(&seed_xa); } } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index d10af38..6867b55 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -8,10 +8,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + GetDistribution, ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -36,7 +36,7 @@ impl TensorKey { scratch: &mut Scratch, ) where M: TensorKeyEncryptSk, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, Scratch: ScratchTakeCore, { module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); @@ -57,7 +57,7 @@ pub trait TensorKeyEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDist; + S: GLWESecretToRef + GetDistribution; } impl TensorKeyEncryptSk for Module @@ -95,7 +95,7 @@ where scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDist, + S: GLWESecretToRef + GetDistribution, { let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 5877f37..0e684ac 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -510,6 +510,8 @@ where // ct[i] = uniform (+ pt) self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); + println!("vec_znx_fill_uniform: {}", ct); + let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size); // ci = ct[i] - pt diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index d30fe62..f4a8c35 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ }; use crate::{ - Distribution, ScratchTakeCore, + Distribution, GetDistribution, GetDistributionMut, ScratchTakeCore, encryption::glwe_ct::GLWEEncryptSk, layouts::{ GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, @@ -16,7 +16,7 @@ use crate::{ impl GLWEPublicKey { pub fn generate(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - S: GLWESecretPreparedToRef, + S: GLWESecretPreparedToRef + GetDistribution, M: GLWEPublicKeyGenerate, { module.glwe_public_key_generate(self, sk, source_xa, source_xe); @@ -26,8 +26,8 @@ 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, - S: GLWESecretPreparedToRef; + R: GLWEPublicKeyToMut + GetDistributionMut, + S: GLWESecretPreparedToRef + GetDistribution; } impl GLWEPublicKeyGenerate for Module @@ -38,25 +38,27 @@ where { fn glwe_public_key_generate(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where - R: GLWEPublicKeyToMut, - S: GLWESecretPreparedToRef, + R: GLWEPublicKeyToMut + GetDistributionMut, + S: GLWESecretPreparedToRef + GetDistribution, { - let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + { + 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); - assert_eq!(sk.n(), self.n() as u32); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); - if sk.dist == Distribution::NONE { - panic!("invalid sk: SecretDistribution::NONE") + if sk.dist == Distribution::NONE { + panic!("invalid sk: SecretDistribution::NONE") + } + + // Its ok to allocate scratch space here since pk is usually generated only once. + let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); + + let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); + + tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); } - - // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); - - let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); - - tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); - res.dist = sk.dist; + *res.dist_mut() = *sk.dist(); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 235cc4d..13d13f9 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -23,6 +23,25 @@ pub struct GGLWECompressed { pub(crate) seed: Vec<[u8; 32]>, } +pub trait GGLWECompressedSeedMut { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>; +} + +impl GGLWECompressedSeedMut for GGLWECompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.seed + } +} + +pub trait GGLWECompressedSeed { + fn seed(&self) -> &Vec<[u8; 32]>; +} + +impl GGLWECompressedSeed for GGLWECompressed { + fn seed(&self) -> &Vec<[u8; 32]> { + &self.seed + } +} impl LWEInfos for GGLWECompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index 454dae3..d12a85b 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -22,6 +22,26 @@ pub struct GGSWCompressed { pub(crate) seed: Vec<[u8; 32]>, } +pub trait GGSWCompressedSeedMut { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>; +} + +impl GGSWCompressedSeedMut for GGSWCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.seed + } +} + +pub trait GGSWCompressedSeed { + fn seed(&self) -> &Vec<[u8; 32]>; +} + +impl GGSWCompressedSeed for GGSWCompressed { + fn seed(&self) -> &Vec<[u8; 32]> { + &self.seed + } +} + impl LWEInfos for GGSWCompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 00f125a..98115b6 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -19,6 +19,26 @@ pub struct GLWECompressed { pub(crate) seed: [u8; 32], } +pub trait GLWECompressedSeedMut { + fn seed_mut(&mut self) -> &mut [u8; 32]; +} + +impl GLWECompressedSeedMut for GLWECompressed { + fn seed_mut(&mut self) -> &mut [u8; 32] { + &mut self.seed + } +} + +pub trait GLWECompressedSeed { + fn seed(&self) -> &[u8; 32]; +} + +impl GLWECompressedSeed for GLWECompressed { + fn seed(&self) -> &[u8; 32] { + &self.seed + } +} + impl LWEInfos for GLWECompressed { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index 80b0a9d..b601e82 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -3,6 +3,7 @@ use poulpy_hal::layouts::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; @@ -16,6 +17,18 @@ pub struct GLWEPublicKey { pub(crate) dist: Distribution, } +impl GetDistributionMut for GLWEPublicKey { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist + } +} + +impl GetDistribution for GLWEPublicKey { + fn dist(&self) -> &Distribution { + &self.dist + } +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPublicKeyLayout { pub n: Degree, @@ -24,16 +37,6 @@ pub struct GLWEPublicKeyLayout { pub rank: Rank, } -pub trait GetDist { - fn get_dist(&self) -> Distribution; -} - -impl GetDist for GLWEPublicKey { - fn get_dist(&self) -> Distribution { - self.dist - } -} - impl LWEInfos for GLWEPublicKey { fn base2k(&self) -> Base2K { self.base2k diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 51b2f0d..2d4ade1 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -7,8 +7,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -64,9 +65,9 @@ impl LWEInfos for GLWESecret { } } -impl GetDist for GLWESecret { - fn get_dist(&self) -> Distribution { - self.dist +impl GetDistribution for GLWESecret { + fn dist(&self) -> &Distribution { + &self.dist } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 5212a11..f186d22 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -4,8 +4,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] @@ -16,13 +17,15 @@ pub struct GLWEPublicKeyPrepared { pub(crate) dist: Distribution, } -pub trait SetDist { - fn set_dist(&mut self, dist: Distribution); +impl GetDistribution for GLWEPublicKeyPrepared { + fn dist(&self) -> &Distribution { + &self.dist + } } -impl SetDist for GLWEPublicKeyPrepared { - fn set_dist(&mut self, dist: Distribution) { - self.dist = dist +impl GetDistributionMut for GLWEPublicKeyPrepared { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist } } @@ -122,8 +125,8 @@ where { fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where - R: GLWEPublicKeyPreparedToMut + SetDist, - O: GLWEPublicKeyToRef + GetDist, + R: GLWEPublicKeyPreparedToMut + GetDistributionMut, + O: GLWEPublicKeyToRef + GetDistribution, { { let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); @@ -140,7 +143,7 @@ where } } - res.set_dist(other.get_dist()); + *res.dist_mut() = *other.dist(); } } @@ -149,7 +152,7 @@ impl GLWEPublicKeyPrepare for Module where Self: GetDegree + V impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) where - O: GLWEPublicKeyToRef + GetDist, + O: GLWEPublicKeyToRef + GetDistribution, M: GLWEPublicKeyPrepare, { module.prepare_glwe_public_key(self, other); diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 5eb9e07..3633e3f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -4,11 +4,9 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision, - prepared::SetDist, - }, + layouts::{Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, LWEInfos, Rank, TorusPrecision}, }; pub struct GLWESecretPrepared { @@ -16,9 +14,15 @@ pub struct GLWESecretPrepared { pub(crate) dist: Distribution, } -impl SetDist for GLWESecretPrepared { - fn set_dist(&mut self, dist: Distribution) { - self.dist = dist +impl GetDistribution for GLWESecretPrepared { + fn dist(&self) -> &Distribution { + &self.dist + } +} + +impl GetDistributionMut for GLWESecretPrepared { + fn dist_mut(&mut self) -> &mut Distribution { + &mut self.dist } } @@ -125,8 +129,8 @@ where { fn prepare_glwe_secret(&self, res: &mut R, other: &O) where - R: GLWESecretPreparedToMut + SetDist, - O: GLWESecretToRef + GetDist, + R: GLWESecretPreparedToMut + GetDistributionMut, + O: GLWESecretToRef + GetDistribution, { { let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut(); @@ -137,7 +141,7 @@ where } } - res.set_dist(other.get_dist()); + *res.dist_mut() = *other.dist(); } } @@ -147,7 +151,7 @@ impl GLWESecretPrepared { pub fn prepare(&mut self, module: &M, other: &O) where M: GLWESecretPrepare, - O: GLWESecretToRef + GetDist, + O: GLWESecretToRef + GetDistribution, { module.prepare_glwe_secret(self, other); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 37d39aa..874a879 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -14,7 +14,7 @@ backend_test_suite!( tests = { //GLWE Encryption glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, - lwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, + glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch 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 4d912fe..970002d 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -104,6 +104,7 @@ where let k_pt: usize = 30; for rank in 1_usize..3 { + println!("rank: {}", rank); let n: usize = module.n(); let glwe_infos: GLWELayout = GLWELayout { From 1ba54b68c7a4cffa7984b0405e2029a40a0c32bf Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Sun, 19 Oct 2025 21:13:52 -0400 Subject: [PATCH 41/60] gglwe tests --- poulpy-core/src/encryption/glwe_ct.rs | 2 +- poulpy-core/src/noise/gglwe_ct.rs | 73 ++------- poulpy-core/src/noise/mod.rs | 4 + poulpy-core/src/tests/mod.rs | 4 +- .../tests/test_suite/encryption/gglwe_ct.rs | 151 ++++++------------ .../tests/test_suite/encryption/glwe_ct.rs | 90 +++++------ .../src/tests/test_suite/encryption/mod.rs | 4 +- 7 files changed, 113 insertions(+), 215 deletions(-) diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index 0e684ac..a0328d3 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -510,7 +510,7 @@ where // ct[i] = uniform (+ pt) self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); - println!("vec_znx_fill_uniform: {}", ct); + // println!("vec_znx_fill_uniform: {}", ct); let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size); diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 48c2df8..687b657 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,7 +1,6 @@ use poulpy_hal::{ - api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace}, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace}, layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, }; use crate::decryption::GLWEDecrypt; @@ -11,7 +10,7 @@ use crate::layouts::{ }; impl GGLWE { - pub fn assert_noise( + pub fn assert_noise( &self, module: &M, sk_prepared: &GLWESecretPrepared, @@ -21,62 +20,12 @@ impl GGLWE { DataSk: DataRef, DataWant: DataRef, M: GGLWENoise, - BE: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrow - + VecZnxSubScalarInplaceImpl, + Scratch: ScratchTakeBasic, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - // pub fn assert_noise( - // &self, - // module: &Module, - // sk: &GLWESecretPrepared, - // pt_want: &ScalarZnx, - // max_noise: f64, - // ) where - // DataSk: DataRef, - // DataWant: DataRef, - // Module: VecZnxDftBytesOf - // + VecZnxBigBytesOf - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxBigAddInplace - // + VecZnxBigAddSmallInplace - // + VecZnxBigNormalize - // + VecZnxNormalizeTmpBytes - // + VecZnxSubScalarInplace, - // B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - // { - // let dsize: usize = self.dsize().into(); - // let base2k: usize = self.base2k().into(); - // let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); - // let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - - // (0..self.rank_in().into()).for_each(|col_i| { - // (0..self.dnum().into()).for_each(|row_i| { - // self.at(row_i, col_i) - // .decrypt(module, &mut pt, sk, scratch.borrow()); - - // module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i); - - // let noise_have: f64 = pt.data.std(base2k, 0).log2(); - - // println!("noise_have: {noise_have}"); - - // assert!( - // noise_have <= max_noise, - // "noise_have: {noise_have} > max_noise: {max_noise}" - // ); - - // pt.data.zero(); - // }); - // }); - // } } pub trait GGLWENoise { @@ -85,24 +34,22 @@ pub trait GGLWENoise { R: GGLWEToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl; + Scratch: ScratchTakeBasic; } impl GGLWENoise for Module where - Module: GLWEDecrypt, - Scratch: ScratchTakeBasic - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrow, + Module: GLWEDecrypt + VecZnxFillUniform + VecZnxSubScalarInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeBasic, { fn gglwe_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64) where R: GGLWEToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow + VecZnxSubScalarInplaceImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeBasic, { let res: &GGLWE<&[u8]> = &res.to_ref(); @@ -125,7 +72,7 @@ where let noise_have: f64 = pt.data.std(base2k, 0).log2(); - println!("noise_have: {noise_have}"); + // println!("noise_have: {noise_have}"); assert!( noise_have <= max_noise, diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index 25e2b9b..aace6ad 100644 --- a/poulpy-core/src/noise/mod.rs +++ b/poulpy-core/src/noise/mod.rs @@ -2,6 +2,10 @@ mod gglwe_ct; mod ggsw_ct; mod glwe_ct; +pub use gglwe_ct::*; +pub use ggsw_ct::*; +pub use glwe_ct::*; + #[allow(clippy::too_many_arguments)] #[allow(dead_code)] pub(crate) fn var_noise_gglwe_product( diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 874a879..9cad53d 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -30,8 +30,8 @@ backend_test_suite!( // glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, // glwe_packing => crate::tests::test_suite::test_glwe_packing, // GGLWE Encryption -// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, -// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, // gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, // gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, // gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, 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 5e09781..ba19219 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -1,66 +1,32 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSubScalarInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, }, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - encryption::SIGMA, - layouts::{ - GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey, - compressed::{Decompress, GLWESwitchingKeyCompressed}, - prepared::{GLWESecretPrepared, PrepareAlloc}, - }, + decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ + prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress + }, noise::GGLWENoise, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore }; -pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) +pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GGLWEEncryptSk + + GGLWEPrepare + + GGLWEPreparedAlloc + + GGLWEKeyswitch + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 54; @@ -71,7 +37,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout { + let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -81,23 +47,24 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); ksk.encrypt_sk( module, @@ -115,44 +82,23 @@ where } } -pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) +pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGLWEEncryptSk + + GGLWEPrepare + + GGLWEPreparedAlloc + + GGLWEKeyswitch + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + + GLWESwitchingKeyDecompress + + GGLWENoise + + VecZnxFillUniform, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 54; @@ -163,7 +109,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let gglwe_infos: GGLWECiphertextLayout = GGLWECiphertextLayout { + let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -174,22 +120,23 @@ where }; let mut ksk_compressed: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos); + GLWESwitchingKeyCompressed::alloc_from_infos(module, &gglwe_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(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_tmp_bytes( module, &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); let seed_xa = [1u8; 32]; @@ -202,7 +149,7 @@ where scratch.borrow(), ); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key 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 970002d..1d9d701 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -16,17 +16,17 @@ use crate::{ }, }; -pub fn test_glwe_encrypt_sk(module: &Module) +pub fn test_glwe_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + Module: GLWEAlloc + + GLWEEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform + GLWESub, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -56,13 +56,13 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); @@ -87,24 +87,24 @@ where } } -pub fn test_glwe_compressed_encrypt_sk(module: &Module) +pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWECompressedEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + Module: GLWEAlloc + + GLWECompressedEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform + GLWESub, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; let k_pt: usize = 30; for rank in 1_usize..3 { - println!("rank: {}", rank); + // println!("rank: {}", rank); let n: usize = module.n(); let glwe_infos: GLWELayout = GLWELayout { @@ -129,14 +129,14 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); @@ -170,17 +170,17 @@ where } } -pub fn test_glwe_encrypt_zero_sk(module: &Module) +pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + Module: GLWEAlloc + + GLWEEncryptSk + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform + GLWESub, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -201,13 +201,13 @@ where let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); @@ -225,20 +225,20 @@ where } } -pub fn test_glwe_encrypt_pk(module: &Module) +pub fn test_glwe_encrypt_pk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptPk - + GLWEPublicKeyPrepare - + GLWEPublicKeyPreparedAlloc - + GLWEPublicKeyGenerate - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare + Module: GLWEAlloc + + GLWEEncryptPk + + GLWEPublicKeyPrepare + + GLWEPublicKeyPreparedAlloc + + GLWEPublicKeyGenerate + + GLWEDecrypt + + GLWESecretPreparedAlloc + + GLWESecretPrepare + VecZnxFillUniform + GLWESub, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k_ct: usize = 54; @@ -262,13 +262,13 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xu: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_prepared: GLWESecretPrepared, B> = GLWESecretPrepared::alloc(module, rank.into()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos); @@ -276,7 +276,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut pk_prepared: GLWEPublicKeyPrepared, B> = GLWEPublicKeyPrepared::alloc_from_infos(module, &glwe_infos); + let mut pk_prepared: GLWEPublicKeyPrepared, BE> = GLWEPublicKeyPrepared::alloc_from_infos(module, &glwe_infos); pk_prepared.prepare(module, &pk); ct.encrypt_pk( diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index 33fd5a1..2ed1147 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,11 +1,11 @@ // mod gglwe_atk; -// mod gglwe_ct; +mod gglwe_ct; // mod ggsw_ct; mod glwe_ct; // mod glwe_tsk; // pub use gglwe_atk::*; -// pub use gglwe_ct::*; +pub use gglwe_ct::*; // pub use ggsw_ct::*; pub use glwe_ct::*; // pub use glwe_tsk::*; From d68c0a2baf608847f4a9178bfd5a22c8c7052cdb Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Sun, 19 Oct 2025 22:57:00 -0400 Subject: [PATCH 42/60] gglwe atk key --- .../src/layouts/compressed/gglwe_atk.rs | 9 +- poulpy-core/src/noise/mod.rs | 4 +- poulpy-core/src/tests/mod.rs | 4 +- .../tests/test_suite/encryption/gglwe_atk.rs | 148 ++++++------------ .../src/tests/test_suite/encryption/mod.rs | 4 +- 5 files changed, 59 insertions(+), 110 deletions(-) diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 276b695..8c57529 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -20,6 +20,12 @@ pub struct AutomorphismKeyCompressed { pub(crate) p: i64, } +impl GetAutomorphismGaloisElement for AutomorphismKeyCompressed { + fn p(&self) -> i64 { + self.p + } +} + impl LWEInfos for AutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() @@ -37,6 +43,7 @@ impl LWEInfos for AutomorphismKeyCompressed { self.key.size() } } + impl GLWEInfos for AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() @@ -199,7 +206,7 @@ where } } -impl AutomorphismKeyDecompress for Module where Self: AutomorphismKeyDecompress {} +impl AutomorphismKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} impl AutomorphismKey where diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index aace6ad..cb2cefe 100644 --- a/poulpy-core/src/noise/mod.rs +++ b/poulpy-core/src/noise/mod.rs @@ -3,8 +3,8 @@ mod ggsw_ct; mod glwe_ct; pub use gglwe_ct::*; -pub use ggsw_ct::*; -pub use glwe_ct::*; +// pub use ggsw_ct::*; +// pub use glwe_ct::*; #[allow(clippy::too_many_arguments)] #[allow(dead_code)] diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 9cad53d..b04779e 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -32,8 +32,8 @@ backend_test_suite!( // GGLWE Encryption gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, -// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, -// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, +gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, +gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, // gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, // gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching 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 bd164dc..81b727f 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,71 +1,38 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, + VecZnxAutomorphism,VecZnxFillUniform, }, + layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement}, source::Source, }; use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, - compressed::{AutomorphismKeyCompressed, Decompress}, - prepared::{GLWESecretPrepared, PrepareAlloc}, + compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress }, + noise::GGLWENoise, + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore }; -pub fn test_gglwe_automorphisk_key_encrypt_sk(module: &Module) +pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GGLWEEncryptSk + + GGLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + + GLWESwitchingKeyDecompress + + GGLWENoise + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + VecZnxAutomorphism, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -84,17 +51,17 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -118,7 +85,8 @@ where i, ); }); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + sk_out_prepared.prepare(module, &sk_out); atk.key .key @@ -127,48 +95,21 @@ where } } -pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk(module: &Module) +pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace + Module: GGLWEEncryptSk + + GGLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyAlloc + + GLWESwitchingKeyEncryptSk + + GLWESwitchingKeyCompressedEncryptSk + + AutomorphismKeyDecompress + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + VecZnxFillUniform + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -187,16 +128,16 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(module, &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(AutomorphismKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -215,9 +156,10 @@ where i, ); }); - let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index 2ed1147..a23cb64 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,10 +1,10 @@ -// mod gglwe_atk; +mod gglwe_atk; mod gglwe_ct; // mod ggsw_ct; mod glwe_ct; // mod glwe_tsk; -// pub use gglwe_atk::*; +pub use gglwe_atk::*; pub use gglwe_ct::*; // pub use ggsw_ct::*; pub use glwe_ct::*; From 60fbd3e625f2a3284c665e6b26940a224088fbce Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Mon, 20 Oct 2025 00:40:43 -0400 Subject: [PATCH 43/60] glwe tsk test --- poulpy-core/src/decryption/glwe_ct.rs | 6 +- poulpy-core/src/tests/mod.rs | 2 +- .../tests/test_suite/encryption/glwe_tsk.rs | 175 ++++++------------ .../src/tests/test_suite/encryption/mod.rs | 4 +- 4 files changed, 67 insertions(+), 120 deletions(-) diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 3c64fda..6dc7f5a 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -3,7 +3,7 @@ use poulpy_hal::{ ModuleN, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, - layouts::{Backend, DataMut, DataViewMut, Module, Scratch}, + layouts::{Backend, DataRef, DataViewMut, Module, Scratch}, }; use crate::layouts::{ @@ -21,8 +21,8 @@ impl GLWE> { } } -impl GLWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) +impl GLWE { + pub fn decrypt(&self, module: &M, pt: &mut P, sk: &S, scratch: &mut Scratch) where P: GLWEPlaintextToMut, S: GLWESecretPreparedToRef, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index b04779e..8bdf034 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -34,7 +34,7 @@ gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_ggl gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, -// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, // gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching // gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, 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 da05ba1..c7f5e2e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,68 +1,31 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, - }, - layouts::{Backend, Module, ScratchOwned, VecZnxDft}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, - source::Source, + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing + }, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source }; use crate::{ - encryption::SIGMA, - layouts::{ - Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, - compressed::{Decompress, TensorKeyCompressed}, - prepared::{GLWESecretPrepared, PrepareAlloc}, - }, + decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ + prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout + }, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk }; -pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) +pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VecZnxDftAlloc - + SvpApplyDftToDft - + VecZnxBigAlloc - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSwitchRing + Module: TensorKeyEncryptSk + + TensorKeyAlloc + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxBigNormalize + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k: usize = 54; @@ -80,20 +43,21 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); tensor_key.encrypt_sk( module, @@ -103,12 +67,12 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); - let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); @@ -128,10 +92,11 @@ where scratch.borrow(), ); for row_i in 0..dnum { - tensor_key - .at(i, j) - .at(row_i, 0) - .decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); + let ct = tensor_key + .at(i, j) + .at(row_i, 0); + + ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, 0); @@ -143,46 +108,27 @@ where } } -pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) +pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VecZnxDftAlloc - + SvpApplyDftToDft - + VecZnxBigAlloc - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSwitchRing - + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: TensorKeyEncryptSk + + TensorKeyAlloc + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGLWETensorKeyCompressedEncryptSk + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxSubScalarInplace + + VecZnxFillUniform + + VecZnxCopy + + VecZnxSwitchRing, + // + VecZnxNormalizeInplaceImpl + // + VecZnxNormalizeImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k = 8; let k = 54; @@ -199,33 +145,34 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(module, &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(TensorKeyCompressed::encrypt_sk_tmp_bytes( module, &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); let seed_xa: [u8; 32] = [1u8; 32]; tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); - let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i); diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index a23cb64..afc5599 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -2,10 +2,10 @@ mod gglwe_atk; mod gglwe_ct; // mod ggsw_ct; mod glwe_ct; -// mod glwe_tsk; +mod glwe_tsk; pub use gglwe_atk::*; pub use gglwe_ct::*; // pub use ggsw_ct::*; pub use glwe_ct::*; -// pub use glwe_tsk::*; +pub use glwe_tsk::*; From 53bc78f421a95531c5239d588efda7ac6152152d Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 10:32:02 +0200 Subject: [PATCH 44/60] Removed traits on structs not dependent on Module API --- .../benches/external_product_glwe_fft64.rs | 14 +- poulpy-core/benches/keyswitch_glwe_fft64.rs | 16 +-- poulpy-core/examples/encryption.rs | 8 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 14 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 9 +- .../src/encryption/compressed/gglwe_atk.rs | 4 +- .../src/encryption/compressed/gglwe_ct.rs | 4 +- poulpy-core/src/encryption/gglwe_atk.rs | 2 +- poulpy-core/src/encryption/gglwe_ct.rs | 3 +- poulpy-core/src/encryption/gglwe_tsk.rs | 2 +- poulpy-core/src/encryption/ggsw_ct.rs | 4 +- poulpy-core/src/encryption/glwe_pk.rs | 2 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 2 +- poulpy-core/src/encryption/lwe_ksk.rs | 2 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/glwe_packing.rs | 19 +-- poulpy-core/src/keyswitching/lwe_ct.rs | 12 +- .../src/layouts/compressed/gglwe_atk.rs | 96 +++---------- .../src/layouts/compressed/gglwe_ct.rs | 101 ++++---------- .../src/layouts/compressed/gglwe_ksk.rs | 90 +++--------- .../src/layouts/compressed/gglwe_tsk.rs | 92 +++---------- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 86 +++--------- poulpy-core/src/layouts/compressed/glwe_ct.rs | 75 ++-------- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 122 ++++++----------- poulpy-core/src/layouts/compressed/lwe_ct.rs | 58 ++------ poulpy-core/src/layouts/compressed/lwe_ksk.rs | 129 +++++++----------- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 116 ++++++---------- poulpy-core/src/layouts/gglwe_atk.rs | 76 +++-------- poulpy-core/src/layouts/gglwe_ct.rs | 113 ++++----------- poulpy-core/src/layouts/gglwe_ksk.rs | 124 +++++------------ poulpy-core/src/layouts/gglwe_tsk.rs | 95 ++++--------- poulpy-core/src/layouts/ggsw_ct.rs | 83 ++++------- poulpy-core/src/layouts/glwe_ct.rs | 78 +++-------- poulpy-core/src/layouts/glwe_pk.rs | 79 +++-------- poulpy-core/src/layouts/glwe_pt.rs | 79 +++-------- poulpy-core/src/layouts/glwe_sk.rs | 72 +++------- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 125 +++++++---------- poulpy-core/src/layouts/lwe_ct.rs | 60 ++------ poulpy-core/src/layouts/lwe_ksk.rs | 127 +++++++---------- poulpy-core/src/layouts/lwe_pt.rs | 39 ++---- poulpy-core/src/layouts/lwe_sk.rs | 17 +-- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 127 +++++++---------- poulpy-core/src/noise/gglwe_ct.rs | 28 ++-- poulpy-core/src/noise/ggsw_ct.rs | 77 ++++------- poulpy-core/src/noise/glwe_ct.rs | 107 ++------------- poulpy-core/src/tests/serialization.rs | 63 +++------ .../tests/test_suite/encryption/gglwe_atk.rs | 38 ++---- .../tests/test_suite/encryption/gglwe_ct.rs | 42 +++--- .../tests/test_suite/encryption/glwe_ct.rs | 62 ++++----- .../tests/test_suite/encryption/glwe_tsk.rs | 73 +++++----- 50 files changed, 858 insertions(+), 2010 deletions(-) diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 47b98ea..900eda1 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -61,9 +61,9 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); - let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); - let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -76,7 +76,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); @@ -165,8 +165,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&module, &ggsw_layout); - let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( @@ -179,7 +179,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index d603b60..66fb932 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -62,9 +62,9 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&module, &gglwe_atk_layout); - let mut ct_in: GLWE> = GLWE::alloc_from_infos(&module, &glwe_in_layout); - let mut ct_out: GLWE> = GLWE::alloc_from_infos(&module, &glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_atk_layout) @@ -81,7 +81,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); @@ -177,8 +177,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&module, &gglwe_layout); - let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(&module, &gglwe_layout) @@ -190,13 +190,13 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let mut sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = GLWESecretPrepared::alloc(&module, rank); sk_in_dft.prepare(&module, &sk_in); - let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_layout); + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index 43a2b0e..5ee8c8e 100644 --- a/poulpy-core/examples/encryption.rs +++ b/poulpy-core/examples/encryption.rs @@ -43,9 +43,9 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWE> = GLWE::alloc_from_infos(&module, &glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&module, &glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -58,7 +58,7 @@ fn main() { ); // Generate secret-key - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&module, &glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 49744ba..17ca1bf 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, + GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, }, }; @@ -40,11 +40,11 @@ where } impl LWESampleExtract for Module where Self: ModuleN {} -impl LWEFromGLWE for Module where Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract {} +impl LWEFromGLWE for Module where Self: GLWEKeyswitch + LWESampleExtract {} pub trait LWEFromGLWE where - Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch + LWESampleExtract, { fn lwe_from_glwe_tmp_bytes(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize where @@ -59,8 +59,12 @@ where rank: Rank(1), }; - self.bytes_of_glwe(lwe_infos.base2k(), lwe_infos.k(), 1u32.into()) - + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) + GLWE::bytes_of( + self.n().into(), + lwe_infos.base2k(), + lwe_infos.k(), + 1u32.into(), + ) + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos) } fn lwe_from_glwe(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index 7be6574..22f38ed 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -6,16 +6,16 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, + GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, }, }; -impl GLWEFromLWE for Module where Self: GLWEKeyswitch + GLWEAlloc {} +impl GLWEFromLWE for Module where Self: GLWEKeyswitch {} pub trait GLWEFromLWE where - Self: GLWEKeyswitch + GLWEAlloc, + Self: GLWEKeyswitch, { fn glwe_from_lwe_tmp_bytes(&self, glwe_infos: &R, lwe_infos: &A, key_infos: &K) -> usize where @@ -23,7 +23,8 @@ where A: LWEInfos, K: GGLWEInfos, { - let ct: usize = self.bytes_of_glwe( + let ct: usize = GLWE::bytes_of( + self.n().into(), key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index a3415cc..668a133 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretAlloc, GLWESecretToRef, LWEInfos, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, }, }; @@ -68,7 +68,7 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + self.bytes_of_glwe_secret(infos.rank()) + self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank()) } fn automorphism_key_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index fbac0f6..1081525 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, }, layouts::{ - GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos, + GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintext, GLWESecretPrepared, LWEInfos, compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::GLWESecretPreparedToRef, }, @@ -82,7 +82,7 @@ where { self.glwe_encrypt_sk_tmp_bytes(infos) .max(self.vec_znx_normalize_tmp_bytes()) - + self.bytes_of_glwe_plaintext_from_infos(infos) + + GLWEPlaintext::bytes_of_from_infos(infos) } fn gglwe_compressed_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 8521df3..ea6096a 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -80,7 +80,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(self, infos) + self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos) } fn automorphism_key_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index bb1a9c1..ba4ae8e 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -85,8 +85,7 @@ where where A: GGLWEInfos, { - self.glwe_encrypt_sk_tmp_bytes(infos) - + GLWEPlaintext::bytes_of_from_infos(self, infos).max(self.vec_znx_normalize_tmp_bytes()) + self.glwe_encrypt_sk_tmp_bytes(infos) + GLWEPlaintext::bytes_of_from_infos(infos).max(self.vec_znx_normalize_tmp_bytes()) } fn gglwe_encrypt_sk( diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 6867b55..2a4792d 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -82,7 +82,7 @@ where + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_dft(1, 1) - + GLWESecret::bytes_of(self, Rank(1)) + + GLWESecret::bytes_of(self.n().into(), Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index 8964122..bc67822 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ SIGMA, ScratchTakeCore, encryption::glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintextAlloc, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -79,7 +79,7 @@ where { self.glwe_encrypt_sk_tmp_bytes(infos) .max(self.vec_znx_normalize_tmp_bytes()) - + self.bytes_of_glwe_plaintext_from_infos(infos) + + GLWEPlaintext::bytes_of_from_infos(infos) } fn ggsw_encrypt_sk( diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index f4a8c35..58b5b8a 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -55,7 +55,7 @@ where // Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res)); - let mut tmp: GLWE> = GLWE::alloc_from_infos(self, res); + let mut tmp: GLWE> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); } diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 59be47c..bd7c271 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -72,7 +72,7 @@ where A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self, infos.rank_in())) + + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in())) } fn glwe_to_lwe_switching_key_encrypt_sk( diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 348df2c..215efee 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -85,7 +85,7 @@ where 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::bytes_of(self, Rank(1)) + GLWESecret::bytes_of(self.n().into(), Rank(1)) + GLWESecretPrepared::bytes_of(self, Rank(1)) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index d51faf7..a66c95d 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -75,7 +75,7 @@ where Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self, infos.rank_in()) + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) } fn lwe_to_glwe_switching_key_encrypt_sk( diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 4b7d5b1..bcd9e45 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -9,7 +9,7 @@ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, }, }; @@ -41,13 +41,12 @@ impl Accumulator { /// * `base2k`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. - pub fn alloc(module: &M, infos: &A) -> Self + pub fn alloc(infos: &A) -> Self where A: GLWEInfos, - M: GLWEPacking, { Self { - data: GLWE::alloc_from_infos(module, infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, } @@ -59,20 +58,18 @@ impl GLWEPacker { /// /// # Arguments /// - /// * `module`: static backend FFT tables. /// * `log_batch`: packs coefficients which are multiples of X^{N/2^log_batch}. /// i.e. with `log_batch=0` only the constant coefficient is packed /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts /// can be packed. - pub fn alloc(module: &M, infos: &A, log_batch: usize) -> Self + pub fn alloc(infos: &A, log_batch: usize) -> Self where A: GLWEInfos, - M: GLWEPacking, { let mut accumulators: Vec = Vec::::new(); let log_n: usize = infos.n().log2(); - (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(module, infos))); + (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(infos))); GLWEPacker { accumulators, log_batch, @@ -96,7 +93,7 @@ impl GLWEPacker { K: GGLWEInfos, M: GLWEPacking, { - module.bytes_of_glwe_from_infos(res_infos) + GLWE::bytes_of_from_infos(res_infos) + module .glwe_rsh_tmp_byte() .max(module.glwe_automorphism_tmp_bytes(res_infos, res_infos, key_infos)) @@ -169,7 +166,6 @@ impl GLWEPacking for Module where + GLWEAdd + GLWENormalize + GLWECopy - + GLWEAlloc { } @@ -183,8 +179,7 @@ where + GLWEShift + GLWEAdd + GLWENormalize - + GLWECopy - + GLWEAlloc, + + GLWECopy, { /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 7aa5321..8a529c7 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -7,7 +7,7 @@ use crate::{ LWESampleExtract, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, layouts::{ - GGLWEInfos, GLWE, GLWEAlloc, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, + GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, }, }; @@ -40,7 +40,7 @@ impl LWEKeySwitch for Module where Self: LWEKeySwitch { pub trait LWEKeySwitch where - Self: GLWEKeyswitch + GLWEAlloc + LWESampleExtract, + Self: GLWEKeyswitch + LWESampleExtract, { fn lwe_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where @@ -51,21 +51,21 @@ where let max_k: TorusPrecision = a_infos.k().max(res_infos.k()); let glwe_a_infos: GLWELayout = GLWELayout { - n: self.ring_degree(), + n: self.n().into(), base2k: a_infos.base2k(), k: max_k, rank: Rank(1), }; let glwe_res_infos: GLWELayout = GLWELayout { - n: self.ring_degree(), + n: self.n().into(), base2k: res_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_in: usize = GLWE::bytes_of_from_infos(self, &glwe_a_infos); - let glwe_out: usize = GLWE::bytes_of_from_infos(self, &glwe_res_infos); + let glwe_in: usize = GLWE::bytes_of_from_infos(&glwe_a_infos); + let glwe_out: usize = GLWE::bytes_of_from_infos(&glwe_res_infos); let ks: usize = self.glwe_keyswitch_tmp_bytes(&glwe_res_infos, &glwe_a_infos, key_infos); glwe_in + glwe_out + ks diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 8c57529..5a40e5d 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; @@ -86,32 +85,34 @@ impl fmt::Display for AutomorphismKeyCompressed { } } -impl AutomorphismKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} +impl AutomorphismKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait AutomorphismKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_automorphism_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKeyCompressed> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKeyCompressed { - key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), + key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } - fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.alloc_automorphism_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -120,61 +121,8 @@ where ) } - fn bytes_of_automorphism_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) - } - - fn bytes_of_automorphism_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!(infos.rank_in(), infos.rank_out()); - self.bytes_of_automorphism_key_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl AutomorphismKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.alloc_automorphism_key_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: AutomorphismKeyCompressedAlloc, - { - module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyCompressedAlloc, - { - module.bytes_of_automorphism_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: AutomorphismKeyCompressedAlloc, - { - module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 13d13f9..dcc9886 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -105,19 +105,23 @@ impl fmt::Display for GGLWECompressed { } } -pub trait GGLWECompressedAlloc -where - Self: GetDegree, -{ - fn alloc_gglwe_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWECompressed> { +impl GGLWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -134,7 +138,7 @@ where GGLWECompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), 1, @@ -148,22 +152,21 @@ where } } - fn alloc_gglwe_compressed_from_infos(&self, infos: &A) -> GGLWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!(infos.n(), self.ring_degree()); - self.alloc_gglwe_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), - infos.rank_out(), infos.dnum(), infos.dsize(), ) } - fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -179,69 +182,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), 1, k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_gglwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!(infos.n(), self.ring_degree()); - self.bytes_of_gglwe_compressed( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGLWECompressedAlloc for Module where Self: GetDegree {} - -impl GGLWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.alloc_gglwe_compressed_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GGLWECompressedAlloc, - { - module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWECompressedAlloc, - { - module.bytes_of_gglwe_compressed_from_infos(infos) - } - - pub fn byte_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGLWECompressedAlloc, - { - module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) - } } impl GGLWECompressed { diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index d9574c4..3b94141 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, LWEInfos, Rank, TorusPrecision, - compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, + compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -81,33 +81,13 @@ impl fmt::Display for GLWESwitchingKeyCompressed { } } -impl GLWESwitchingKeyCompressedAlloc for Module where Self: GGLWECompressedAlloc {} - -pub trait GLWESwitchingKeyCompressedAlloc -where - Self: GGLWECompressedAlloc, -{ - fn alloc_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKeyCompressed> { - GLWESwitchingKeyCompressed { - key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } - } - - fn alloc_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWESwitchingKeyCompressed> +impl GLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_glwe_switching_key_compressed( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -117,62 +97,24 @@ where ) } - fn bytes_of_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { + GLWESwitchingKeyCompressed { + key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } } - fn bytes_of_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - self.bytes_of_gglwe_compressed_from_infos(infos) - } -} - -impl GLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_switching_key_compressed_from_infos(infos) + GGLWECompressed::bytes_of_from_infos(infos) } - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize +where { + GGLWECompressed::bytes_of(n, base2k, k, rank_in, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index d0202cc..4ec8a42 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -82,38 +81,36 @@ impl fmt::Display for TensorKeyCompressed { } } -impl TensorKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} +impl TensorKeyCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait TensorKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_tensor_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> 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 { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_compressed_from_infos(&self, infos: &A) -> TensorKeyCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" - ); - self.alloc_tensor_key_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -122,54 +119,9 @@ where ) } - fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) - } - - fn bytes_of_tensor_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_tensor_key_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl TensorKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.alloc_tensor_key_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: TensorKeyCompressedAlloc, - { - module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyCompressedAlloc, - { - module.bytes_of_tensor_key_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: TensorKeyCompressedAlloc, - { - module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize) + pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index d12a85b..ca4446f 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -96,20 +96,22 @@ impl FillUniform for GGSWCompressed { } } -impl GGSWCompressedAlloc for Module where Self: GetDegree {} +impl GGSWCompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGSWInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait GGSWCompressedAlloc -where - Self: GetDegree, -{ - fn alloc_ggsw_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGSWCompressed> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; assert!( size as u32 > dsize.0, @@ -126,7 +128,7 @@ where GGSWCompressed { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), 1, @@ -140,11 +142,12 @@ where } } - fn alloc_ggsw_compressed_from_infos(&self, infos: &A) -> GGSWCompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGSWInfos, { - self.alloc_ggsw_compressed( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -153,7 +156,7 @@ where ) } - fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; assert!( size as u32 > dsize.0, @@ -169,58 +172,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), 1, k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_ggsw_compressed_key_from_infos(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw_compressed( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSWCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.alloc_ggsw_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: GGSWCompressedAlloc, - { - module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWCompressedAlloc, - { - module.bytes_of_ggsw_compressed_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGSWCompressedAlloc, - { - module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) - } } impl GGSWCompressed { diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 98115b6..1b05575 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -88,17 +88,17 @@ impl FillUniform for GLWECompressed { } } -pub trait GLWECompressedAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed> { +impl GLWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWECompressed { - data: VecZnx::alloc( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, rank, @@ -106,62 +106,15 @@ where } } - fn alloc_glwe_compressed_from_infos(&self, infos: &A) -> GLWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - assert_eq!(self.ring_degree(), infos.n()); - self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - assert_eq!(self.ring_degree(), infos.n()); - self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) - } -} - -impl GLWECompressedAlloc for Module where Self: GetDegree {} - -impl GLWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.alloc_glwe_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWECompressedAlloc, - { - module.alloc_glwe_compressed(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWECompressedAlloc, - { - module.bytes_of_glwe_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: GLWECompressedAlloc, - { - module.bytes_of_glwe_compressed(base2k, k) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index afdb493..08508b1 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -9,8 +9,7 @@ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; @@ -88,96 +87,61 @@ impl WriterTo for GLWEToLWESwitchingKeyCompressed { } } -pub trait GLWEToLWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_glwe_to_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> GLWEToLWESwitchingKeyCompressed> { - GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) - } - - fn alloc_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } - - fn bytes_of_glwe_to_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1)) - } - - fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" - ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } -} - -impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, { - module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + rank_in, + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyCompressedAlloc, { - module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize - where - M: GLWEToLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index c21a6dd..ce4c000 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ct.rs @@ -62,8 +62,15 @@ impl FillUniform for LWECompressed { } } -pub trait LWECompressedAlloc { - fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed> { +impl LWECompressed> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.base2k(), infos.k()) + } + + pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self { LWECompressed { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, @@ -72,57 +79,16 @@ pub trait LWECompressedAlloc { } } - fn alloc_lwe_compressed_from_infos(&self, infos: &A) -> LWECompressed> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: LWEInfos, { - self.alloc_lwe_compressed(infos.base2k(), infos.k()) + Self::bytes_of(infos.base2k(), infos.k()) } - fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + pub fn bytes_of(base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize) } - - fn bytes_of_lwe_compressed_from_infos(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) - } -} - -impl LWECompressedAlloc for Module {} - -impl LWECompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWECompressedAlloc, - { - module.alloc_lwe_compressed(base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: LWEInfos, - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWECompressedAlloc, - { - module.bytes_of_lwe_compressed(base2k, k) - } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 7e1b0cf..3738b92 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -6,8 +6,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use std::fmt; @@ -85,99 +84,65 @@ impl WriterTo for LWESwitchingKeyCompressed { } } -pub trait LWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_lwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - ) -> LWESwitchingKeyCompressed> { - LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKeyCompressed" - ); - self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) - } - - fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKeyCompressed" - ); - self.bytes_of_glwe_switching_key_compressed_from_infos(infos) - } -} - -impl LWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - impl LWESwitchingKeyCompressed> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, { - module.alloc_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" + ); + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + Rank(1), + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWESwitchingKeyCompressedAlloc, { - module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index 545f126..ea0ca05 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -7,8 +7,7 @@ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, TorusPrecision, compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, - GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, }, }; use std::fmt; @@ -86,90 +85,61 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } -impl LWEToGLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} - -pub trait LWEToGLWESwitchingKeyCompressedAlloc -where - Self: GLWESwitchingKeyCompressedAlloc, -{ - fn alloc_lwe_to_glwe_switching_key_compressed( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_out: Rank, - dnum: Dnum, - ) -> LWEToGLWESwitchingKeyCompressed> { - LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) - } - - fn alloc_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } - - fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" - ); - self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) - } -} - impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, { - module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + LWEToGLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc( + n, + base2k, + k, + Rank(1), + rank_out, + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyCompressedAlloc, { - module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + GLWESwitchingKeyCompressed::bytes_of_from_infos(infos) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWEToGLWESwitchingKeyCompressedAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index b4b61c6..5d0f5f7 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, + LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -144,31 +144,13 @@ impl fmt::Display for AutomorphismKey { } } -impl AutomorphismKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - -pub trait AutomorphismKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_automorphism_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> AutomorphismKey> { - AutomorphismKey { - key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), - p: 0, - } - } - - fn alloc_automorphism_key_from_infos(&self, infos: &A) -> AutomorphismKey> +impl AutomorphismKey> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_automorphism_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -177,11 +159,14 @@ where ) } - fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { + AutomorphismKey { + key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize), + p: 0, + } } - fn bytes_of_automorphism_key_from_infos(&self, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { @@ -190,7 +175,8 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKey" ); - self.bytes_of_automorphism_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -198,37 +184,9 @@ where infos.dsize(), ) } -} -impl AutomorphismKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.alloc_automorphism_key_from_infos(infos) - } - - pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: AutomorphismKeyAlloc, - { - module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: AutomorphismKeyAlloc, - { - module.bytes_of_automorphism_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: AutomorphismKeyAlloc, - { - module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 666293a..a491186 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -1,11 +1,9 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, - }, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -191,19 +189,23 @@ impl GGLWE { } } -pub trait GGLWEAlloc -where - Self: GetDegree, -{ - fn alloc_gglwe( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GGLWE> { +impl GGLWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -220,7 +222,7 @@ where GGLWE { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), @@ -232,11 +234,12 @@ where } } - fn alloc_glwe_from_infos(&self, infos: &A) -> GGLWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { - self.alloc_gglwe( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -246,8 +249,8 @@ where ) } - fn bytes_of_gglwe( - &self, + pub fn bytes_of( + n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -270,77 +273,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_gglwe_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_gglwe( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGLWEAlloc for Module where Self: GetDegree {} - -impl GGLWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.alloc_glwe_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GGLWEAlloc, - { - module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GGLWEAlloc, - { - module.bytes_of_gglwe_from_infos(infos) - } - - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize - where - M: GGLWEAlloc, - { - module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } } pub trait GGLWEToMut { diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 466f8ac..b451951 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -1,11 +1,10 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -163,31 +162,13 @@ impl FillUniform for GLWESwitchingKey { } } -pub trait GLWESwitchingKeyAlloc -where - Self: GGLWEAlloc, -{ - fn alloc_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> GLWESwitchingKey> { - GLWESwitchingKey { - key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } - } - - fn alloc_glwe_switching_key_from_infos(&self, infos: &A) -> GLWESwitchingKey> +impl GLWESwitchingKey> { + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, { - self.alloc_glwe_switching_key( + Self::alloc( + infos.n(), infos.base2k(), infos.k(), infos.rank_in(), @@ -197,8 +178,31 @@ where ) } - fn bytes_of_glwe_switching_key( - &self, + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { + GLWESwitchingKey { + key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } + } + + pub fn bytes_of_from_infos(infos: &A) -> usize + where + A: GGLWEInfos, + { + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn bytes_of( + n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -206,71 +210,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) - } - - fn bytes_of_glwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - self.bytes_of_glwe_switching_key( - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GLWESwitchingKeyAlloc for Module where Self: GGLWEAlloc {} - -impl GLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.alloc_glwe_switching_key_from_infos(infos) - } - - pub fn alloc( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> Self - where - M: GLWESwitchingKeyAlloc, - { - module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: GLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key_from_infos(infos) - } - - pub fn bytes_of( - module: &M, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - rank_out: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize - where - M: GLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + GGLWE::bytes_of(n, base2k, k, rank_in, rank_out, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 10523b1..240b482 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -130,20 +130,36 @@ impl fmt::Display for TensorKey { } } -pub trait TensorKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey> { +impl TensorKey> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for GGLWETensorKey" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); TensorKey { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_from_infos(&self, infos: &A) -> TensorKey> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, { @@ -152,7 +168,8 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - self.alloc_tensor_key( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -161,61 +178,9 @@ where ) } - fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize) - } - - fn bytes_of_tensor_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" - ); - self.bytes_of_tensor_key( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - -impl TensorKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.alloc_tensor_key_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: TensorKeyAlloc, - { - module.alloc_tensor_key(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGLWEInfos, - M: TensorKeyAlloc, - { - module.bytes_of_tensor_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: TensorKeyAlloc, - { - module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) + pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) } } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index a4d091d..a4109d0 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -1,12 +1,10 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, - }, + layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, source::Source, }; use std::fmt; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where @@ -152,13 +150,22 @@ impl GGSW { } } -impl GGSWAlloc for Module where Self: GetDegree {} +impl GGSW> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GGSWInfos, + { + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } -pub trait GGSWAlloc -where - Self: GetDegree, -{ - fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW> { + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -175,7 +182,7 @@ where GGSW { data: MatZnx::alloc( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -187,11 +194,12 @@ where } } - fn alloc_ggsw_from_infos(&self, infos: &A) -> GGSW> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGSWInfos, { - self.alloc_ggsw( + Self::bytes_of( + infos.n(), infos.base2k(), infos.k(), infos.rank(), @@ -200,7 +208,7 @@ where ) } - fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -216,58 +224,13 @@ where ); MatZnx::bytes_of( - self.ring_degree().into(), + n.into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } - - fn bytes_of_ggsw_from_infos(&self, infos: &A) -> usize - where - A: GGSWInfos, - { - self.bytes_of_ggsw( - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } -} - -impl GGSW> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.alloc_ggsw_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self - where - M: GGSWAlloc, - { - module.alloc_ggsw(base2k, k, rank, dnum, dsize) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GGSWInfos, - M: GGSWAlloc, - { - module.bytes_of_ggsw_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize - where - M: GGSWAlloc, - { - module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) - } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index edf7b4c..d47f8cd 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -1,12 +1,11 @@ use poulpy_hal::{ layouts::{ - Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, - WriterTo, ZnxInfos, + Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, }, source::Source, }; -use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -146,76 +145,31 @@ impl FillUniform for GLWE { } } -pub trait GLWEAlloc -where - Self: GetDegree, -{ - fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { +impl GLWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWE { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, } } - fn alloc_glwe_from_infos(&self, infos: &A) -> GLWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl GLWEAlloc for Module where Self: GetDegree {} - -impl GLWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEAlloc, - { - module.alloc_glwe_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEAlloc, - { - module.alloc_glwe(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEAlloc, - { - module.bytes_of_glwe_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - M: GLWEAlloc, - { - module.bytes_of_glwe(base2k, k, rank) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index b601e82..2231389 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/poulpy-core/src/layouts/glwe_pk.rs @@ -1,11 +1,9 @@ -use poulpy_hal::layouts::{ - Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, -}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; use crate::{ GetDistribution, GetDistributionMut, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -85,77 +83,32 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -pub trait GLWEPublicKeyAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { +impl GLWEPublicKey> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { GLWEPublicKey { - data: VecZnx::alloc( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, k, dist: Distribution::NONE, } } - fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank()) } - fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - (rank + 1).into(), - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_public_key_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) - } -} - -impl GLWEPublicKeyAlloc for Module where Self: GetDegree {} - -impl GLWEPublicKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEPublicKeyAlloc, - { - module.alloc_glwe_public_key_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self - where - M: GLWEPublicKeyAlloc, - { - module.alloc_glwe_public_key(base2k, k, rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEPublicKeyAlloc, - { - module.bytes_of_glwe_public_key_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize - where - M: GLWEPublicKeyAlloc, - { - module.bytes_of_glwe_public_key(base2k, k, rank) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index e3d7290..3261d3d 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/poulpy-core/src/layouts/glwe_pt.rs @@ -1,10 +1,8 @@ use std::fmt; -use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; -use crate::layouts::{ - Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, -}; +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEPlaintextLayout { @@ -85,76 +83,31 @@ impl fmt::Display for GLWEPlaintext { } } -pub trait GLWEPlaintextAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { +impl GLWEPlaintext> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { GLWEPlaintext { - data: VecZnx::alloc( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ), + data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, } } - fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_plaintext(infos.base2k(), infos.k()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::bytes_of( - self.ring_degree().into(), - 1, - k.0.div_ceil(base2k.0) as usize, - ) - } - - fn bytes_of_glwe_plaintext_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl GLWEPlaintextAlloc for Module where Self: GetDegree {} - -impl GLWEPlaintext> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWEPlaintextAlloc, - { - module.alloc_glwe_plaintext_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: GLWEPlaintextAlloc, - { - module.alloc_glwe_plaintext(base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWEPlaintextAlloc, - { - module.bytes_of_glwe_plaintext_from_infos(infos) - } - - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision) -> usize - where - M: GLWEPlaintextAlloc, - { - module.bytes_of_glwe_plaintext(base2k, k) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize) } } diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index 2d4ade1..b99a78e 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/poulpy-core/src/layouts/glwe_sk.rs @@ -1,15 +1,12 @@ use poulpy_hal::{ - layouts::{ - Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, - ZnxZero, - }, + layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero}, source::Source, }; use crate::{ GetDistribution, dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -77,67 +74,30 @@ impl GLWEInfos for GLWESecret { } } -pub trait GLWESecretAlloc -where - Self: GetDegree, -{ - fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { +impl GLWESecret> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: GLWEInfos, + { + Self::alloc(infos.n(), infos.rank()) + } + + pub fn alloc(n: Degree, rank: Rank) -> Self { GLWESecret { - data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()), + data: ScalarZnx::alloc(n.into(), rank.into()), dist: Distribution::NONE, } } - fn alloc_glwe_secret_from_infos(&self, infos: &A) -> GLWESecret> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GLWEInfos, { - self.alloc_glwe_secret(infos.rank()) + Self::bytes_of(infos.n(), infos.rank()) } - fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { - ScalarZnx::bytes_of(self.ring_degree().into(), rank.into()) - } - - fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize - where - A: GLWEInfos, - { - self.bytes_of_glwe_secret(infos.rank()) - } -} - -impl GLWESecretAlloc for Module where Self: GetDegree {} - -impl GLWESecret> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: GLWEInfos, - M: GLWESecretAlloc, - { - module.alloc_glwe_secret_from_infos(infos) - } - - pub fn alloc(module: &M, rank: Rank) -> Self - where - M: GLWESecretAlloc, - { - module.alloc_glwe_secret(rank) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: GLWEInfos, - M: GLWESecretAlloc, - { - module.bytes_of_glwe_secret_from_infos(infos) - } - - pub fn bytes_of(module: &M, rank: Rank) -> usize - where - M: GLWESecretAlloc, - { - module.bytes_of_glwe_secret(rank) + pub fn bytes_of(n: Degree, rank: Rank) -> usize { + ScalarZnx::bytes_of(n.into(), rank.into()) } } diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 3aea241..45fa6df 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -1,11 +1,11 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; use std::fmt; @@ -132,90 +132,67 @@ impl WriterTo for GLWEToLWESwitchingKey { } } -pub trait GLWEToLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_glwe_to_lwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> GLWEToLWESwitchingKey> { - GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) - } - - fn alloc_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } - - fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) - } -} - -impl GLWEToLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl GLWEToLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, { - module.alloc_glwe_to_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self - where - M: GLWEToLWESwitchingKeyAlloc, - { - module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { + GLWEToLWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + rank_in, + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyAlloc, { - module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + ) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize - where - M: GLWEToLWESwitchingKeyAlloc, - { - module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index aed2807..0900d78 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -1,7 +1,7 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, source::Source, }; @@ -125,8 +125,15 @@ where } } -pub trait LWEAlloc { - fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { +impl LWE> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.n(), infos.base2k(), infos.k()) + } + + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, @@ -134,57 +141,16 @@ pub trait LWEAlloc { } } - fn alloc_lwe_from_infos(&self, infos: &A) -> LWE> + pub fn bytes_of_from_infos(infos: &A) -> usize where A: LWEInfos, { - self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) + Self::bytes_of(infos.n(), infos.base2k(), infos.k()) } - fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } - - fn bytes_of_lwe_from_infos(&self, infos: &A) -> usize - where - A: LWEInfos, - { - self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) - } -} - -impl LWEAlloc for Module {} - -impl LWE> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWEAlloc, - { - module.alloc_lwe_from_infos(infos) - } - - pub fn alloc(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEAlloc, - { - module.alloc_lwe(n, base2k, k) - } - - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize - where - A: LWEInfos, - M: LWEAlloc, - { - module.bytes_of_lwe_from_infos(infos) - } - - pub fn bytes_of(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize - where - M: LWEAlloc, - { - module.bytes_of_lwe(n, base2k, k) - } } pub trait LWEToRef { diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index e449da6..25ce059 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -101,94 +101,65 @@ impl GGLWEInfos for LWESwitchingKey { } } -pub trait LWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { - LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) - } - - fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) - } - - fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) - } - - fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) - } -} - -impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl LWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWESwitchingKeyAlloc, { - module.alloc_lwe_switching_key_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self - where - M: LWESwitchingKeyAlloc, - { - module.alloc_lwe_switching_key(base2k, k, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { + LWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + Rank(1), + Rank(1), + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWESwitchingKeyAlloc, { - module.bytes_of_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum()) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize - where - M: LWESwitchingKeyAlloc, - { - module.bytes_of_lwe_switching_key(base2k, k, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index 01ae19b..966ceb5 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/poulpy-core/src/layouts/lwe_pt.rs @@ -1,6 +1,6 @@ use std::fmt; -use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; +use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos}; use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision}; @@ -52,40 +52,21 @@ impl LWEInfos for LWEPlaintext { } } -pub trait LWEPlaintextAlloc { - fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext> { +impl LWEPlaintext> { + pub fn alloc_from_infos(infos: &A) -> Self + where + A: LWEInfos, + { + Self::alloc(infos.base2k(), infos.k()) + } + + pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self { LWEPlaintext { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } - - fn alloc_lwe_plaintext_from_infos(&self, infos: &A) -> LWEPlaintext> - where - A: LWEInfos, - { - self.alloc_lwe_plaintext(infos.base2k(), infos.k()) - } -} - -impl LWEPlaintextAlloc for Module {} - -impl LWEPlaintext> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self - where - A: LWEInfos, - M: LWEPlaintextAlloc, - { - module.alloc_lwe_plaintext_from_infos(infos) - } - - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision) -> Self - where - M: LWEPlaintextAlloc, - { - module.alloc_lwe_plaintext(base2k, k) - } } impl fmt::Display for LWEPlaintext { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index b5f019f..78ad9c7 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -13,8 +13,8 @@ pub struct LWESecret { pub(crate) dist: Distribution, } -pub trait LWESecretAlloc { - fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { +impl LWESecret> { + pub fn alloc(n: Degree) -> Self { LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, @@ -22,17 +22,6 @@ pub trait LWESecretAlloc { } } -impl LWESecretAlloc for Module {} - -impl LWESecret> { - pub fn alloc(module: &M, n: Degree) -> Self - where - M: LWESecretAlloc, - { - module.alloc_lwe_secret(n) - } -} - impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 1427447..26fb7f9 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -1,13 +1,13 @@ use std::fmt; use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, - GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -131,91 +131,68 @@ impl WriterTo for LWEToGLWESwitchingKey { } } -pub trait LWEToGLWESwitchingKeyAlloc -where - Self: GLWESwitchingKeyAlloc, -{ - fn alloc_lwe_to_glwe_switching_key( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_out: Rank, - dnum: Dnum, - ) -> LWEToGLWESwitchingKey> { - LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) - } - - fn alloc_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKey> - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - - self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } - - fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { - self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) - } - - fn bytes_of_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> usize - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) - } -} - -impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} - impl LWEToGLWESwitchingKey> { - pub fn alloc_from_infos(module: &M, infos: &A) -> Self + pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, { - module.alloc_lwe_to_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + + Self::alloc( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self - where - M: LWEToGLWESwitchingKeyAlloc, - { - module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { + LWEToGLWESwitchingKey(GLWESwitchingKey::alloc( + n, + base2k, + k, + Rank(1), + rank_out, + dnum, + Dsize(1), + )) } - pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize + pub fn bytes_of_from_infos(infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyAlloc, { - module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + Self::bytes_of( + infos.n(), + infos.base2k(), + infos.k(), + infos.rank_out(), + infos.dnum(), + ) } - pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize - where - M: LWEToGLWESwitchingKeyAlloc, - { - module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 687b657..dc32d57 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -1,31 +1,21 @@ use poulpy_hal::{ api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxFillUniform, VecZnxSubScalarInplace}, - layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, + layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero}, }; use crate::decryption::GLWEDecrypt; -use crate::layouts::{ - GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, -}; +use crate::layouts::{GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, prepared::GLWESecretPreparedToRef}; impl GGLWE { - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - max_noise: f64, - ) where - DataSk: DataRef, - DataWant: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGLWENoise, Scratch: ScratchTakeBasic, { module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - } pub trait GGLWENoise { @@ -34,7 +24,7 @@ pub trait GGLWENoise { R: GGLWEToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - Scratch: ScratchTakeBasic; + Scratch: ScratchTakeBasic; } impl GGLWENoise for Module @@ -49,7 +39,7 @@ where S: GLWESecretPreparedToRef, P: ScalarZnxToRef, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeBasic, + Scratch: ScratchAvailable + ScratchTakeBasic, { let res: &GGLWE<&[u8]> = &res.to_ref(); @@ -57,7 +47,7 @@ where let base2k: usize = res.base2k().into(); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); (0..res.rank_in().into()).for_each(|col_i| { (0..res.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 13531e3..7adeeca 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -1,12 +1,9 @@ use poulpy_hal::{ api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, + ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAlloc, + VecZnxBigNormalize, VecZnxDftAlloc, VecZnxDftApply, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace, }, - layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, + layouts::{Backend, DataRef, Module, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero}, }; use crate::decryption::GLWEDecrypt; @@ -14,32 +11,21 @@ use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl GGSW { - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - max_noise: F, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: F) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGSWNoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { module.ggsw_assert_noise(self, sk_prepared, pt_want, max_noise); } - pub fn print_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &ScalarZnx, - ) where - DataSk: DataRef, - DataScalar: DataRef, + pub fn print_noise(&self, module: &M, sk_prepared: &S, pt_want: &P) + where + S: GLWESecretPreparedToRef, + P: ScalarZnxToRef, M: GGSWNoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { module.ggsw_print_noise(self, sk_prepared, pt_want); } @@ -51,43 +37,31 @@ pub trait GGSWNoise { R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64; fn ggsw_print_noise(&self, res: &R, sk_prepared: &S, pt_want: &P) where R: GGSWToRef, S: GLWESecretPreparedToRef, - P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; + P: ScalarZnxToRef; } impl GGSWNoise for Module where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc + Module: GLWEDecrypt + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA + + VecZnxBigAlloc + VecZnxAddScalarInplace - + VecZnxSubInplace - + GLWEDecrypt, + + VecZnxIdftApplyTmpA + + VecZnxSubInplace, Scratch: ScratchTakeBasic, + ScratchOwned: ScratchOwnedBorrow + ScratchOwnedAlloc, { fn ggsw_assert_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) where R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, F: Fn(usize) -> f64, { let res: &GGSW<&[u8]> = &res.to_ref(); @@ -96,8 +70,8 @@ where let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); @@ -147,7 +121,6 @@ where R: GGSWToRef, S: GLWESecretPreparedToRef, P: ScalarZnxToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { let res: &GGSW<&[u8]> = &res.to_ref(); let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref(); @@ -155,16 +128,16 @@ where let base2k: usize = res.base2k().into(); let dsize: usize = res.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res); let mut pt_dft: VecZnxDft, BE> = self.vec_znx_dft_alloc(1, res.size()); let mut pt_big: VecZnxBig, BE> = self.vec_znx_big_alloc(1, res.size()); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res) | self.vec_znx_normalize_tmp_bytes()); - (0..(res.rank() + 1).into()).for_each(|col_j| { - (0..res.dnum().into()).for_each(|row_i| { + for col_j in 0..(res.rank() + 1).into() { + for row_i in 0..res.dnum().into() { self.vec_znx_add_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, 0); // mul with sk[col_j-1] @@ -194,7 +167,7 @@ where let std_pt: f64 = pt_have.data.std(base2k, 0).log2(); println!("col: {col_j} row: {row_i}: {std_pt}"); pt.data.zero(); - }); - }); + } + } } } diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index b89bdc6..7c6979e 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -1,19 +1,12 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxNormalizeInplace, VecZnxSubInplace}, layouts::{Backend, DataRef, Module, Scratch, ScratchOwned}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::{ + ScratchTakeCore, decryption::GLWEDecrypt, - layouts::{ - GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, - prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, - }, + layouts::{GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, prepared::GLWESecretPreparedToRef}, }; impl GLWE { @@ -25,75 +18,15 @@ impl GLWE { { module.glwe_noise(self, sk_prepared, pt_want, scratch) } - // pub fn noise( - // &self, - // module: &Module, - // sk_prepared: &GLWESecretPrepared, - // pt_want: &GLWEPlaintext, - // scratch: &mut Scratch, - // ) -> f64 - // where - // DataSk: DataRef, - // DataPt: DataRef, - // B: Backend, - // Module: VecZnxDftApply - // + VecZnxSubInplace - // + VecZnxNormalizeInplace - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxBigAddInplace - // + VecZnxBigAddSmallInplace - // + VecZnxBigNormalize, - // Scratch:, - // { - // let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, self); - // self.decrypt(module, &mut pt_have, sk_prepared, scratch); - // module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); - // module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch); - // pt_have.data.std(self.base2k().into(), 0).log2() - // } - pub fn assert_noise( - &self, - module: &M, - sk_prepared: &GLWESecretPrepared, - pt_want: &GLWEPlaintext, - max_noise: f64, - ) where - DataSk: DataRef, - DataPt: DataRef, + pub fn assert_noise(&self, module: &M, sk_prepared: &S, pt_want: &P, max_noise: f64) + where + S: GLWESecretPreparedToRef, + P: GLWEPlaintextToRef, M: GLWENoise, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { module.glwe_assert_noise(self, sk_prepared, pt_want, max_noise); } - - // pub fn assert_noise( - // &self, - // module: &Module, - // sk_prepared: &GLWESecretPrepared, - // pt_want: &GLWEPlaintext, - // max_noise: f64, - // ) where - // DataSk: DataRef, - // DataPt: DataRef, - // Module: VecZnxDftBytesOf - // + VecZnxBigBytesOf - // + VecZnxDftApply - // + SvpApplyDftToDftInplace - // + VecZnxIdftApplyConsume - // + VecZnxBigAddInplace - // + VecZnxBigAddSmallInplace - // + VecZnxBigNormalize - // + VecZnxNormalizeTmpBytes - // + VecZnxSubInplace - // + VecZnxNormalizeInplace, - // B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - // { - // let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, self)); - // let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); - // assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); - // } } pub trait GLWENoise { @@ -107,29 +40,14 @@ pub trait GLWENoise { where R: GLWEToRef, S: GLWESecretPreparedToRef, - P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow; + P: GLWEPlaintextToRef; } impl GLWENoise for Module where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxNormalizeInplace - + GLWEDecrypt, - Scratch: ScratchTakeBasic - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrowImpl - + ScratchOwnedBorrow, + Module: GLWEDecrypt + VecZnxSubInplace + VecZnxNormalizeInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { fn glwe_noise(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch) -> f64 where @@ -141,7 +59,7 @@ where let pt_want: &GLWEPlaintext<&[u8]> = &pt_want.to_ref(); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self, res_ref); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(res_ref); self.glwe_decrypt(res, &mut pt_have, sk_prepared, scratch); self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); self.vec_znx_normalize_inplace(res_ref.base2k().into(), &mut pt_have.data, 0, scratch); @@ -153,7 +71,6 @@ where R: GLWEToRef, S: GLWESecretPreparedToRef, P: GLWEPlaintextToRef, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchOwnedBorrow, { let res: &GLWE<&[u8]> = &res.to_ref(); let mut scratch: ScratchOwned = ScratchOwned::alloc(self.glwe_decrypt_tmp_bytes(res)); diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index c770311..ddaaf9c 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,5 +1,4 @@ -use poulpy_backend::FFT64Ref; -use poulpy_hal::{api::ModuleNew, layouts::Module, test_suite::serialization::test_reader_writer_interface}; +use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, @@ -21,143 +20,123 @@ const DSIZE: Dsize = Dsize(1); #[test] fn glwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWE> = GLWE::alloc(&module, BASE2K, K, RANK); + let original: GLWE> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWECompressed> = GLWECompressed::alloc(&module, BASE2K, K, RANK); + let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWE> = LWE::alloc(&module, N_LWE, BASE2K, K); + let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWECompressed> = LWECompressed::alloc(&module, BASE2K, K); + let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGLWE> = GGLWE::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGLWECompressed> = GGLWECompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, RANK, DNUM, DSIZE); + GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKey> = AutomorphismKey::alloc_with(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: TensorKey> = TensorKey::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: GLWEToLWESwitchingKeyCompressed> = - GLWEToLWESwitchingKeyCompressed::alloc(&module, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(&module, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); let original: LWEToGLWESwitchingKeyCompressed> = - LWEToGLWESwitchingKeyCompressed::alloc_with(&module, BASE2K, K, RANK, DNUM); + LWEToGLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKey> = LWESwitchingKey::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(&module, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGSW> = GGSW::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let module: Module = Module::::new(N_GLWE.as_u32() as u64); - let original: GGSWCompressed> = GGSWCompressed::alloc(&module, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } 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 81b727f..487f1f7 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -1,21 +1,17 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, - VecZnxAutomorphism,VecZnxFillUniform, - }, - layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement}, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxFillUniform}, + layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, - GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, GLWESwitchingKeyDecompress, compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared, }, noise::GGLWENoise, - GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore }; pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) @@ -24,7 +20,6 @@ where + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -32,7 +27,7 @@ where + VecZnxFillUniform + VecZnxAutomorphism, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -51,17 +46,15 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -101,7 +94,6 @@ where + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + AutomorphismKeyDecompress @@ -109,7 +101,7 @@ where + VecZnxFillUniform + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: ScratchAvailable + ScratchTakeCore, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_ksk: usize = 60; @@ -128,16 +120,14 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc_from_infos(module, &atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::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(AutomorphismKey::encrypt_sk_tmp_bytes( - module, &atk_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -159,7 +149,7 @@ where let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(module, &atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key 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 ba19219..06167d1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -1,15 +1,19 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress - }, noise::GGLWENoise, GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore + GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, + decryption::GLWEDecrypt, + encryption::SIGMA, + layouts::{ + GGLWELayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyCompressed, + GLWESwitchingKeyDecompress, + prepared::{GGLWEPrepare, GGLWEPreparedAlloc, GLWESecretPrepared}, + }, + noise::GGLWENoise, }; pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) @@ -21,20 +25,19 @@ where + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + VecZnxFillUniform + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { + let n = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; for rank_in in 1_usize..3 { for rank_out in 1_usize..3 { for di in 1_usize..dsize + 1 { - let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let gglwe_infos: GGLWELayout = GGLWELayout { @@ -47,21 +50,19 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes( - module, - &gglwe_infos, - )); + let mut scratch: ScratchOwned = + ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_infos)); - let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out); @@ -91,7 +92,6 @@ where + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare - + GLWESwitchingKeyAlloc + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -100,13 +100,13 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { + let n: usize = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; for rank_in in 1_usize..3 { for rank_out in 1_usize..3 { for di in 1_usize..dsize + 1 { - let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); let gglwe_infos: GGLWELayout = GGLWELayout { @@ -120,7 +120,7 @@ where }; let mut ksk_compressed: GLWESwitchingKeyCompressed> = - GLWESwitchingKeyCompressed::alloc_from_infos(module, &gglwe_infos); + GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -130,10 +130,10 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc(module, rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc(module, rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); sk_out_prepared.prepare(module, &sk_out); @@ -149,7 +149,7 @@ where scratch.borrow(), ); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(module, &gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key 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 1d9d701..0f07c32 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, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, - GLWEPublicKeyPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPrepare, GLWEPublicKeyPreparedAlloc, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, compressed::GLWECompressed, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, @@ -18,13 +18,8 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + VecZnxFillUniform - + GLWESub, + Module: + GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -48,9 +43,9 @@ where k: k_pt.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -59,7 +54,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); @@ -89,8 +84,7 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: GLWEAlloc - + GLWECompressedEncryptSk + Module: GLWECompressedEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -120,10 +114,10 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(module, &glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -133,7 +127,7 @@ where GLWECompressed::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); @@ -152,7 +146,7 @@ where scratch.borrow(), ); - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); @@ -172,13 +166,8 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptSk - + GLWEDecrypt - + GLWESecretPreparedAlloc - + GLWESecretPrepare - + VecZnxFillUniform - + GLWESub, + Module: + GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedAlloc + GLWESecretPrepare + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -195,7 +184,7 @@ where rank: rank.into(), }; - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); @@ -204,13 +193,13 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module, @@ -227,8 +216,7 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where - Module: GLWEAlloc - + GLWEEncryptPk + Module: GLWEEncryptPk + GLWEPublicKeyPrepare + GLWEPublicKeyPreparedAlloc + GLWEPublicKeyGenerate @@ -253,9 +241,9 @@ where rank: rank.into(), }; - let mut ct: GLWE> = GLWE::alloc_from_infos(module, &glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -265,13 +253,13 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_pk_tmp_bytes(module, &glwe_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(module, &glwe_infos); + let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc_from_infos(&glwe_infos); pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); 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 c7f5e2e..647f68e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -1,19 +1,26 @@ use poulpy_hal::{ api::{ - ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing - }, layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, oep::{VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl}, source::Source + ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize, + VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, + VecZnxSwitchRing, + }, + layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, + source::Source, }; use crate::{ - decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - prepared::GLWESecretPrepared, Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyAlloc, TensorKeyCompressed, TensorKeyLayout - }, GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk + GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + decryption::GLWEDecrypt, + encryption::SIGMA, + layouts::{ + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, TensorKey, TensorKeyCompressed, + TensorKeyLayout, prepared::GLWESecretPrepared, + }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + TensorKeyAlloc + GLWESecretPrepare + GLWESecretPreparedAlloc + GLWEDecrypt @@ -43,18 +50,15 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes( - module, - &tensor_key_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); @@ -67,11 +71,11 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { @@ -92,9 +96,7 @@ where scratch.borrow(), ); for row_i in 0..dnum { - let ct = tensor_key - .at(i, j) - .at(row_i, 0); + let ct = tensor_key.at(i, j).at(row_i, 0); ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow()); @@ -111,20 +113,19 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + TensorKeyAlloc - + GLWESecretPrepare - + GLWESecretPreparedAlloc - + GGLWETensorKeyCompressedEncryptSk - + GLWEDecrypt - + VecZnxDftAlloc - + VecZnxBigAlloc - + VecZnxDftApply - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxSubScalarInplace - + VecZnxFillUniform - + VecZnxCopy - + VecZnxSwitchRing, + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGLWETensorKeyCompressedEncryptSk + + GLWEDecrypt + + VecZnxDftAlloc + + VecZnxBigAlloc + + VecZnxDftApply + + SvpApplyDftToDft + + VecZnxIdftApplyTmpA + + VecZnxSubScalarInplace + + VecZnxFillUniform + + VecZnxCopy + + VecZnxSwitchRing, // + VecZnxNormalizeInplaceImpl // + VecZnxNormalizeImpl, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -145,7 +146,7 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -155,7 +156,7 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(module, &tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_prepared.prepare(module, &sk); @@ -164,14 +165,14 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(module, &tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(module, &tensor_key_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret> = GLWESecret::alloc(module, 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { From 8dc4946736bd2936a3b0e96d26f083d6d542fac9 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 10:39:00 +0200 Subject: [PATCH 45/60] ggsw encrypt --- poulpy-core/src/lib.rs | 1 + poulpy-core/src/noise/mod.rs | 4 +- .../tests/test_suite/encryption/ggsw_ct.rs | 90 +++++-------------- .../src/tests/test_suite/encryption/mod.rs | 4 +- 4 files changed, 29 insertions(+), 70 deletions(-) diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 8c45893..dae6157 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -21,6 +21,7 @@ pub use encryption::*; pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; +pub use noise::*; pub use encryption::SIGMA; diff --git a/poulpy-core/src/noise/mod.rs b/poulpy-core/src/noise/mod.rs index cb2cefe..aace6ad 100644 --- a/poulpy-core/src/noise/mod.rs +++ b/poulpy-core/src/noise/mod.rs @@ -3,8 +3,8 @@ mod ggsw_ct; mod glwe_ct; pub use gglwe_ct::*; -// pub use ggsw_ct::*; -// pub use glwe_ct::*; +pub use ggsw_ct::*; +pub use glwe_ct::*; #[allow(clippy::too_many_arguments)] #[allow(dead_code)] 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 7620709..529a42f 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -1,32 +1,23 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, - compressed::{Decompress, GGSWCompressed}, - prepared::{GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, compressed::GGSWCompressed, + prepared::GLWESecretPrepared, }, }; -pub fn test_ggsw_encrypt_sk(module: &Module) +pub fn test_ggsw_encrypt_sk(module: &Module) where - ScratchOwned: ScratchOwnedAlloc, - Module: SvpPrepare, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, + Module: GGSWEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise, { let base2k: usize = 12; let k: usize = 54; @@ -36,7 +27,7 @@ where let n: usize = module.n(); let dnum: usize = (k - di * base2k) / (di * base2k); - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -55,11 +46,13 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ct.encrypt_sk( module, @@ -77,46 +70,12 @@ where } } -pub fn test_ggsw_compressed_encrypt_sk(module: &Module) +pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, + Module: + GGSWCompressedEncryptSk + GLWESecretPreparedAlloc + GLWESecretPrepare + GGSWNoise + GGSWDecompress, { let base2k: usize = 12; let k: usize = 54; @@ -126,7 +85,7 @@ where let n: usize = module.n(); let dnum: usize = (k - di * base2k) / (di * base2k); - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -144,14 +103,13 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes( - module, - &ggsw_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); let seed_xa: [u8; 32] = [1u8; 32]; diff --git a/poulpy-core/src/tests/test_suite/encryption/mod.rs b/poulpy-core/src/tests/test_suite/encryption/mod.rs index afc5599..d871177 100644 --- a/poulpy-core/src/tests/test_suite/encryption/mod.rs +++ b/poulpy-core/src/tests/test_suite/encryption/mod.rs @@ -1,11 +1,11 @@ mod gglwe_atk; mod gglwe_ct; -// mod ggsw_ct; +mod ggsw_ct; mod glwe_ct; mod glwe_tsk; pub use gglwe_atk::*; pub use gglwe_ct::*; -// pub use ggsw_ct::*; +pub use ggsw_ct::*; pub use glwe_ct::*; pub use glwe_tsk::*; From 0c894c19db60841eebfa74f103c3b82d106fe0dc Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 11:07:29 +0200 Subject: [PATCH 46/60] glwe + ggsw keyswitch --- poulpy-core/src/keyswitching/gglwe_ct.rs | 6 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 4 +- poulpy-core/src/keyswitching/mod.rs | 2 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 145 +++++---------- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 176 ++++++------------ .../src/tests/test_suite/keyswitch/glwe_ct.rs | 115 ++---------- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 77 ++------ poulpy-core/src/tests/test_suite/mod.rs | 2 +- 8 files changed, 146 insertions(+), 381 deletions(-) diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 4b92ed1..912d803 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ }; impl AutomorphismKey> { - pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos, @@ -44,7 +44,7 @@ impl AutomorphismKey { } impl GLWESwitchingKey> { - pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos, @@ -77,7 +77,7 @@ impl GLWESwitchingKey { } impl GGLWE> { - pub fn keyswitch_inplace_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos, diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 67f4278..5e49d62 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -1,4 +1,4 @@ -use poulpy_hal::layouts::{Backend, DataMut, Scratch, VecZnx}; +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, @@ -51,6 +51,8 @@ impl GGSW { } } +impl GGSWKeyswitch for Module where Self: GLWEKeyswitch + GGSWExpandRows {} + pub trait GGSWKeyswitch where Self: GLWEKeyswitch + GGSWExpandRows, diff --git a/poulpy-core/src/keyswitching/mod.rs b/poulpy-core/src/keyswitching/mod.rs index 7071680..d7621db 100644 --- a/poulpy-core/src/keyswitching/mod.rs +++ b/poulpy-core/src/keyswitching/mod.rs @@ -4,6 +4,6 @@ mod glwe_ct; mod lwe_ct; pub use gglwe_ct::*; -// pub use gglwe_ct::*; +pub use ggsw_ct::*; pub use glwe_ct::*; pub use lwe_ct::*; 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 c667848..dad69c0 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -1,68 +1,31 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGLWEKeyswitch, GGLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, - prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, }; -pub fn test_gglwe_switching_key_keyswitch(module: &Module) +pub fn test_gglwe_switching_key_keyswitch(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWESwitchingKeyEncryptSk + + GGLWEKeyswitch + + GLWESecretPrepare + + GLWESwitchingKeyPreparedAlloc + + GLWESecretPreparedAlloc + + GLWESwitchingKeyPrepare + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -118,12 +81,12 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( + let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_tmp_bytes( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_tmp_bytes( module, &gglwe_s0s1_infos, &gglwe_s0s2_infos, @@ -138,7 +101,10 @@ where let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s1s2.into()); sk2.fill_ternary_prob(0.5, &mut source_xs); - let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); + + let mut sk2_prepared: GLWESecretPrepared, BE> = + GLWESecretPrepared::alloc(module, rank_out_s1s2.into()); + sk2_prepared.prepare(module, &sk2); // gglwe_{s1}(s0) = s0 -> s1 gglwe_s0s1.encrypt_sk( @@ -160,8 +126,9 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = - gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); + let mut gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &gglwe_s1s2); + gglwe_s1s2_prepared.prepare(module, &gglwe_s1s2, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) gglwe_s0s2.keyswitch( @@ -194,45 +161,17 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module) +pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxSubScalarInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWESwitchingKeyEncryptSk + + GGLWEKeyswitch + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWESwitchingKeyPrepare + + GGLWENoise + + GLWESwitchingKeyPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -273,13 +212,14 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( + let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s0s1_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_s1s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_tmp_bytes( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_tmp_bytes( module, &gglwe_s0s1_infos, + &gglwe_s0s1_infos, &gglwe_s1s2_infos, )); @@ -293,7 +233,9 @@ where let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk2.fill_ternary_prob(var_xs, &mut source_xs); - let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); + + let mut sk2_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk2_prepared.prepare(module, &sk2); // gglwe_{s1}(s0) = s0 -> s1 gglwe_s0s1.encrypt_sk( @@ -315,8 +257,9 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = - gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); + let mut gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &gglwe_s1s2); + gglwe_s1s2_prepared.prepare(module, &gglwe_s1s2, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow()); 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 4d3d556..08d268c 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -1,74 +1,34 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAddInplace, VecZnxDftAlloc, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned}, source::Source, }; use crate::{ + GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, TensorKey, TensorKeyLayout, - prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc, TensorKeyPrepared}, + GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_keyswitch(module: &Module) +pub fn test_ggsw_keyswitch(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxBigAlloc - + VecZnxDftAlloc, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + TensorKeyEncryptSk + + GGSWKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + TensorKeyPreparedAlloc + + GLWESwitchingKeyPreparedAlloc + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 54; @@ -84,7 +44,7 @@ where let dsize_in: usize = 1; - let ggsw_in_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_in_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +53,7 @@ where rank: rank.into(), }; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -131,7 +91,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) @@ -148,11 +108,15 @@ where let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); + + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_out_prepared.prepare(module, &sk_out); ksk.encrypt_sk( module, @@ -175,14 +139,14 @@ where ggsw_in.encrypt_sk( module, &pt_scalar, - &sk_in_dft, + &sk_in_prepared, &mut source_xa, &mut source_xe, scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); ggsw_out.keyswitch( module, @@ -214,50 +178,19 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_keyswitch_inplace(module: &Module) +pub fn test_ggsw_keyswitch_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxBigAlloc - + VecZnxDftAlloc, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + TensorKeyEncryptSk + + GGSWKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + TensorKeyPreparedAlloc + + GLWESwitchingKeyPreparedAlloc + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 54; @@ -272,7 +205,7 @@ where let dsize_in: usize = 1; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -309,22 +242,31 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) - | GGSW::keyswitch_inplace_tmp_bytes(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), + | GGSW::keyswitch_tmp_bytes( + module, + &ggsw_out_infos, + &ggsw_out_infos, + &ksk_apply_infos, + &tsk_infos, + ), ); let var_xs: f64 = 0.5; let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_out_prepared.prepare(module, &sk_out); ksk.encrypt_sk( module, @@ -347,14 +289,14 @@ where ggsw_out.encrypt_sk( module, &pt_scalar, - &sk_in_dft, + &sk_in_prepared, &mut source_xa, &mut source_xe, scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); 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 2ea6e75..6cbe7c3 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -1,67 +1,25 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + ScratchTakeCore, encryption::SIGMA, layouts::{ GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, - prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_glwe_keyswitch(module: &Module) +pub fn test_glwe_keyswitch(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module:, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 45; @@ -111,7 +69,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), @@ -119,11 +77,11 @@ where let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); + let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); ksk.encrypt_sk( module, @@ -143,7 +101,7 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -166,44 +124,11 @@ where } } -pub fn test_glwe_keyswitch_inplace(module: &Module) +pub fn test_glwe_keyswitch_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module:, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 45; @@ -243,7 +168,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), @@ -251,11 +176,11 @@ where let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); + let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); key_apply.encrypt_sk( module, @@ -275,7 +200,7 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, BE> = key_apply.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); 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 130c356..0722b98 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -1,68 +1,21 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned, ZnxView}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView}, source::Source, }; -use crate::layouts::{ - LWE, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, - prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, +use crate::{ + ScratchTakeCore, + layouts::{ + LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::LWESwitchingKeyPrepared, + }, }; -pub fn test_lwe_keyswitch(module: &Module) +pub fn test_lwe_keyswitch(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + ZnNormalizeInplace - + ZnFillUniform - + ZnAddNormal - + VecZnxCopy, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module:, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let n: usize = module.n(); let base2k: usize = 17; @@ -86,19 +39,19 @@ where dnum: dnum.into(), }; - let lwe_in_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_in_infos: LWELayout = LWELayout { n: n_lwe_in.into(), base2k: base2k.into(), k: k_lwe_ct.into(), }; - let lwe_out_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_out_infos: LWELayout = LWELayout { n: n_lwe_out.into(), k: k_lwe_ct.into(), base2k: base2k.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( LWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) | LWE::keyswitch_tmp_bytes(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), ); @@ -136,7 +89,7 @@ where let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); - let ksk_prepared: LWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: LWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 54157bd..9737a54 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,7 +1,7 @@ // pub mod automorphism; pub mod encryption; // pub mod external_product; -// pub mod keyswitch; +pub mod keyswitch; // mod conversion; // mod packing; From 252eda36fe0c0e5e5c8b630d7f333b4862b12c6c Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 15:32:52 +0200 Subject: [PATCH 47/60] keyswitch tests --- poulpy-core/src/automorphism/gglwe_atk.rs | 104 +++++++++--------- poulpy-core/src/automorphism/ggsw_ct.rs | 19 ++-- poulpy-core/src/automorphism/glwe_ct.rs | 57 +++++----- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 6 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 12 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 12 +- poulpy-core/src/decryption/lwe_ct.rs | 20 ++-- poulpy-core/src/decryption/mod.rs | 2 +- .../src/encryption/compressed/gglwe_atk.rs | 47 +++++--- .../src/encryption/compressed/gglwe_ksk.rs | 24 ++-- .../src/encryption/compressed/gglwe_tsk.rs | 50 +++++---- .../src/encryption/compressed/ggsw_ct.rs | 37 +++---- poulpy-core/src/encryption/gglwe_atk.rs | 45 +++++--- poulpy-core/src/encryption/gglwe_ksk.rs | 16 +-- poulpy-core/src/encryption/gglwe_tsk.rs | 25 +++-- poulpy-core/src/encryption/glwe_ct.rs | 14 +-- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 43 +++++--- poulpy-core/src/encryption/lwe_ct.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 13 +-- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 36 +++--- poulpy-core/src/external_product/gglwe_atk.rs | 8 +- poulpy-core/src/external_product/gglwe_ksk.rs | 8 +- poulpy-core/src/glwe_packing.rs | 13 +-- poulpy-core/src/glwe_trace.rs | 12 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 36 +++--- poulpy-core/src/keyswitching/ggsw_ct.rs | 16 ++- poulpy-core/src/keyswitching/glwe_ct.rs | 34 +++--- poulpy-core/src/keyswitching/lwe_ct.rs | 14 +-- .../src/layouts/compressed/gglwe_atk.rs | 53 ++++++--- .../src/layouts/compressed/gglwe_ct.rs | 3 +- .../src/layouts/compressed/gglwe_ksk.rs | 96 ++++++++-------- .../src/layouts/compressed/gglwe_tsk.rs | 46 +++++--- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 4 +- poulpy-core/src/layouts/compressed/glwe_ct.rs | 1 - .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 42 +++---- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 41 +++---- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 42 +++---- poulpy-core/src/layouts/gglwe_atk.rs | 41 ++----- poulpy-core/src/layouts/gglwe_ksk.rs | 86 ++++++--------- poulpy-core/src/layouts/gglwe_tsk.rs | 23 ++-- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 40 +++---- poulpy-core/src/layouts/lwe_ksk.rs | 50 +++++---- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 40 +++---- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 59 ++++------ poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 78 ++++++------- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 39 +++---- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 50 ++++----- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 50 ++++----- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 50 ++++----- poulpy-core/src/lib.rs | 1 + poulpy-core/src/scratch.rs | 32 +++--- poulpy-core/src/tests/mod.rs | 20 ++-- .../tests/test_suite/encryption/gglwe_atk.rs | 13 ++- .../tests/test_suite/encryption/gglwe_ct.rs | 2 +- .../tests/test_suite/encryption/glwe_tsk.rs | 12 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 28 +++-- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 69 ++++++++---- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 15 ++- poulpy-hal/src/layouts/module.rs | 6 +- poulpy-hal/src/layouts/znx_base.rs | 4 +- 60 files changed, 918 insertions(+), 945 deletions(-) diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index db57e60..b768b81 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,10 +7,8 @@ use crate::{ ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - AutomorphismKey, AutomorphismKeyToMut, AutomorphismKeyToRef, GGLWEInfos, GLWE, GLWEInfos, - prepared::{ - AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement, SetAutomorphismGaloisElement, - }, + AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }, }; @@ -29,8 +27,8 @@ impl AutomorphismKey> { impl AutomorphismKey { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where - A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: AutomorphismKeyAutomorphism, { @@ -39,7 +37,7 @@ impl AutomorphismKey { pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: AutomorphismKeyAutomorphism, { @@ -67,35 +65,34 @@ where fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, - A: AutomorphismKeyToRef + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, + A: GGLWEToRef + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { + assert!( + res.dnum().as_u32() <= a.dnum().as_u32(), + "res dnum: {} > a dnum: {}", + res.dnum(), + a.dnum() + ); + + assert_eq!( + res.dsize(), + a.dsize(), + "res dnum: {} != a dnum: {}", + res.dsize(), + a.dsize() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + + let p: i64 = a.p(); + let p_inv: i64 = self.galois_element_inv(p); + { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); - let a: &AutomorphismKey<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); - - assert!( - res.dnum().as_u32() <= a.dnum().as_u32(), - "res dnum: {} > a dnum: {}", - res.dnum(), - a.dnum() - ); - - assert_eq!( - res.dsize(), - a.dsize(), - "res dnum: {} != a dnum: {}", - res.dsize(), - a.dsize() - ); - - let cols_out: usize = (key.rank_out() + 1).into(); - - let p: i64 = a.p(); - let p_inv: i64 = self.galois_element_inv(p); + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GGLWE<&[u8]> = &a.to_ref(); for row in 0..res.dnum().as_usize() { for col in 0..cols_out { @@ -104,11 +101,11 @@ where // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) for i in 0..cols_out { - self.vec_znx_automorphism(a.p(), res_tmp.data_mut(), i, &a_ct.data, i); + self.vec_znx_automorphism(p, res_tmp.data_mut(), i, &a_ct.data, i); } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch); // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) (0..cols_out).for_each(|i| { @@ -118,32 +115,29 @@ where } } - res.set_p((a.p() * key.p()) % (self.cyclotomic_order() as i64)); + res.set_p((p * key.p()) % (self.cyclotomic_order() as i64)); } fn automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement + GetAutomorphismGaloisElement + GGLWEInfos, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { + assert_eq!( + res.rank(), + key.rank(), + "key rank: {} != key rank: {}", + res.rank(), + key.rank() + ); + + let cols_out: usize = (key.rank_out() + 1).into(); + let p: i64 = res.p(); + let p_inv: i64 = self.galois_element_inv(p); + { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], _> = &key.to_ref(); - - assert_eq!( - res.rank(), - key.rank(), - "key rank: {} != key rank: {}", - res.rank(), - key.rank() - ); - - let cols_out: usize = (key.rank_out() + 1).into(); - - let p: i64 = res.p(); - let p_inv: i64 = self.galois_element_inv(p); - + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); for row in 0..res.dnum().as_usize() { for col in 0..cols_out { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); @@ -154,7 +148,7 @@ where } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) - self.glwe_keyswitch_inplace(&mut res_tmp, &key.key, scratch); + self.glwe_keyswitch_inplace(&mut res_tmp, key, scratch); // Applies back the automorphism X^{-k}: (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) to (-pi^{-1}_{k'+k}(s)a + s, a) for i in 0..cols_out { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 8035063..58b8b53 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -7,8 +7,8 @@ use crate::{ GGSWExpandRows, ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, - prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, TensorKeyPrepared, TensorKeyPreparedToRef}, + GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetAutomorphismGaloisElement, + 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: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + 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: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, @@ -78,18 +78,16 @@ where where R: GGSWToMut, A: GGSWToRef, - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); - assert_eq!(res.ggsw_layout(), a.ggsw_layout()); - assert_eq!(res.glwe_layout(), a.glwe_layout()); - assert_eq!(res.lwe_layout(), a.lwe_layout()); + assert_eq!(res.dsize(), a.dsize()); + assert!(res.dnum() <= a.dnum()); assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk)); // Keyswitch the j-th row of the col 0 @@ -105,12 +103,11 @@ where fn ggsw_automorphism_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - K: AutomorphismKeyPreparedToRef, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); // Keyswitch the j-th row of the col 0 diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index d989891..5d973b6 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -9,8 +9,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, keyswitch_internal, layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPrepared, AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, }, }; @@ -31,7 +30,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism(self, a, key, scratch); @@ -41,7 +40,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add(self, a, key, scratch); @@ -51,7 +50,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub(self, a, key, scratch); @@ -61,7 +60,7 @@ impl GLWE { where M: GLWEAutomorphism, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate(self, a, key, scratch); @@ -70,7 +69,7 @@ impl GLWE { pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_inplace(self, key, scratch); @@ -79,7 +78,7 @@ impl GLWE { pub fn automorphism_add_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_add_inplace(self, key, scratch); @@ -88,7 +87,7 @@ impl GLWE { pub fn automorphism_sub_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_inplace(self, key, scratch); @@ -97,7 +96,7 @@ impl GLWE { pub fn automorphism_sub_negate_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_automorphism_sub_negate_inplace(self, key, scratch); @@ -125,10 +124,10 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { - self.glwe_keyswitch(res, a, &key.to_ref().key, scratch); + self.glwe_keyswitch(res, a, key, scratch); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -140,10 +139,10 @@ where fn glwe_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { - self.glwe_keyswitch_inplace(res, &key.to_ref().key, scratch); + self.glwe_keyswitch_inplace(res, key, scratch); let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -156,15 +155,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -184,14 +182,13 @@ where fn glwe_automorphism_add_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -212,15 +209,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -241,15 +237,14 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, a, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -269,14 +264,13 @@ where fn glwe_automorphism_sub_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); @@ -296,14 +290,13 @@ where fn glwe_automorphism_sub_negate_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GetAutomorphismGaloisElement + GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let key: &AutomorphismKeyPrepared<&[u8], BE> = &key.to_ref(); let (res_dft, scratch_1) = scratch.take_vec_znx_dft(self, (res.rank() + 1).into(), key.size()); // TODO: optimise size - let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, &key.key, scratch_1); + let mut res_big: VecZnxBig<_, BE> = keyswitch_internal(self, res_dft, res, key, scratch_1); for i in 0..res.rank().as_usize() + 1 { self.vec_znx_big_automorphism_inplace(key.p(), &mut res_big, i, scratch_1); diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 3447d61..7ad73e6 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -119,10 +119,6 @@ where + VmpApplyDftToDftTmpBytes + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigBytesOf - + VecZnxNormalizeTmpBytes + VecZnxDftApply + VecZnxDftCopy + VmpApplyDftToDft @@ -236,7 +232,7 @@ where // = // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2) for col_i in 1..cols { - let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).key.data; // Selects Enc(s[i]s[j]) + let pmat: &VmpPMat<&[u8], BE> = &tsk.at(col_i - 1, col_j - 1).data; // Selects Enc(s[i]s[j]) // Extracts a[i] and multipies with Enc(s[i]s[j]) for di in 0..dsize { diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 17ca1bf..276010d 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank, - prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank}, }; pub trait LWESampleExtract @@ -71,12 +68,11 @@ where where R: LWEToMut, A: GLWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let key: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); assert_eq!(a.n(), self.n() as u32); assert_eq!(key.n(), self.n() as u32); @@ -90,7 +86,7 @@ where }; let (mut tmp_glwe, scratch_1) = scratch.take_glwe_ct(self, &glwe_layout); - self.glwe_keyswitch(&mut tmp_glwe, a, &key.0, scratch_1); + self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1); self.lwe_sample_extract(res, &tmp_glwe); } } @@ -120,7 +116,7 @@ impl LWE { where R: LWEToMut, A: GLWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef + GGLWEInfos, + K: GGLWEPreparedToRef + GGLWEInfos, M: LWEFromGLWE, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index 22f38ed..7d72ef5 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ GLWEKeyswitch, ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef, - prepared::{LWEToGLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, LWE, LWEInfos, LWEToRef}, }; impl GLWEFromLWE for Module where Self: GLWEKeyswitch {} @@ -43,12 +40,11 @@ where where R: GLWEToMut, A: LWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let lwe: &LWE<&[u8]> = &lwe.to_ref(); - let ksk: &LWEToGLWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); assert_eq!(res.n(), self.n() as u32); assert_eq!(ksk.n(), self.n() as u32); @@ -108,7 +104,7 @@ where ); } - self.glwe_keyswitch(res, &glwe, &ksk.0, scratch_1); + self.glwe_keyswitch(res, &glwe, ksk, scratch_1); } } @@ -129,7 +125,7 @@ impl GLWE { where M: GLWEFromLWE, A: LWEToRef, - K: LWEToGLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { module.glwe_from_lwe(self, lwe, ksk, scratch); diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index a847139..edd727b 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -1,33 +1,39 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnNormalizeInplace}, layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWEPlaintextToMut, LWESecret, LWESecretToRef, LWEToMut}; impl LWE { - pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: S) + pub fn decrypt(&mut self, module: &M, pt: &mut P, sk: &S) where P: LWEPlaintextToMut, S: LWESecretToRef, M: LWEDecrypt, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { module.lwe_decrypt(self, pt, sk); } } -pub trait LWEDecrypt +pub trait LWEDecrypt { + fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S) + where + R: LWEToMut, + P: LWEPlaintextToMut, + S: LWESecretToRef; +} + +impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { - fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: S) + fn lwe_decrypt(&self, res: &mut R, pt: &mut P, sk: &S) where R: LWEToMut, P: LWEPlaintextToMut, S: LWESecretToRef, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let pt: &mut LWEPlaintext<&mut [u8]> = &mut pt.to_mut(); @@ -57,5 +63,3 @@ where pt.k = crate::layouts::TorusPrecision(res.k().0.min(pt.size() as u32 * res.base2k().0)); } } - -impl LWEDecrypt for Module where Self: Sized + ZnNormalizeInplace {} diff --git a/poulpy-core/src/decryption/mod.rs b/poulpy-core/src/decryption/mod.rs index 0af7985..03e5fd4 100644 --- a/poulpy-core/src/decryption/mod.rs +++ b/poulpy-core/src/decryption/mod.rs @@ -2,4 +2,4 @@ mod glwe_ct; mod lwe_ct; pub use glwe_ct::*; -// pub use lwe_ct::*; +pub use lwe_ct::*; diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 668a133..d6aa85f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -5,11 +5,10 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, - encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, + GGLWECompressedEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut}, + GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, + GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, compressed::AutomorphismKeyCompressed, }, }; @@ -34,7 +33,7 @@ impl AutomorphismKeyCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - S: GLWESecretToRef, + S: GLWESecretToRef + GLWEInfos, M: AutomorphismKeyCompressedEncryptSk, { module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); @@ -55,20 +54,28 @@ pub trait AutomorphismKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyCompressedToMut, - S: GLWESecretToRef; + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + S: GLWESecretToRef + GLWEInfos; } impl AutomorphismKeyCompressedEncryptSk for Module where - Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk, + Self: ModuleN + + GaloisElement + + VecZnxAutomorphism + + GGLWECompressedEncryptSk + + GLWESecretPreparedAlloc + + GLWESecretPrepare, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of(self.n().into(), infos.rank()) + assert_eq!(self.n() as u32, infos.n()); + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) + .max(GLWESecret::bytes_of_from_infos(infos)) + + GLWESecretPrepared::bytes_of_from_infos(self, infos) } fn automorphism_key_compressed_encrypt_sk( @@ -80,12 +87,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyCompressedToMut, - S: GLWESecretToRef, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetAutomorphismGaloisElement + GGLWEInfos, + S: GLWESecretToRef + GLWEInfos, { - let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); - assert_eq!(res.n(), sk.n()); assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); @@ -96,9 +101,9 @@ where AutomorphismKeyCompressed::encrypt_sk_tmp_bytes(self, res) ); - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); - + let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { + let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); for i in 0..res.rank_out().into() { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -108,10 +113,18 @@ where i, ); } + sk_out_prepared.prepare(self, &sk_out); } - self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1); + self.gglwe_compressed_encrypt_sk( + res, + &sk.data, + &sk_out_prepared, + seed_xa, + source_xe, + scratch_1, + ); - res.p = p; + res.set_p(p); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index 7825e69..7067250 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -8,8 +8,9 @@ use crate::{ ScratchTakeCore, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, + GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, + GLWESwitchingKeyDegreesMut, LWEInfos, + compressed::GLWESwitchingKeyCompressed, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -57,7 +58,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyCompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef; } @@ -86,11 +87,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyCompressedToMut, + R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef, { - let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); @@ -122,15 +122,9 @@ where } } - self.gglwe_compressed_encrypt_sk( - &mut res.key, - &sk_in_tmp, - &sk_out_tmp, - seed_xa, - source_xe, - scratch_2, - ); - res.sk_in_n = sk_in.n().into(); - res.sk_out_n = sk_out.n().into(); + self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2); + + *res.input_degree() = sk_in.n(); + *res.output_degree() = sk_out.n(); } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index 9a37d17..dbbbcb5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -4,16 +4,15 @@ use poulpy_hal::{ VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, }, layouts::{Backend, DataMut, Module, Scratch}, - oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl}, source::Source, }; use crate::{ - GetDistribution, ScratchTakeCore, - encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk}, + GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, + encryption::gglwe_tsk::TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, - compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, Rank, + TensorKeyCompressedAtMut, compressed::TensorKeyCompressed, }, }; @@ -23,7 +22,7 @@ impl TensorKeyCompressed> { A: GGLWEInfos, M: GGLWETensorKeyCompressedEncryptSk, { - module.gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) + module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -39,16 +38,16 @@ impl TensorKeyCompressed { S: GLWESecretToRef + GetDistribution, M: GGLWETensorKeyCompressedEncryptSk, { - module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } pub trait GGLWETensorKeyCompressedEncryptSk { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -56,36 +55,40 @@ pub trait GGLWETensorKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyCompressedToMut, + D: DataMut, + R: TensorKeyCompressedAtMut + GGLWEInfos, S: GLWESecretToRef + GetDistribution; } impl GGLWETensorKeyCompressedEncryptSk for Module where Self: ModuleN - + GLWESwitchingKeyCompressedEncryptSk + + GGLWECompressedEncryptSk + TensorKeyEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA + VecZnxBigNormalize + SvpPrepare - + SvpPPolAllocBytesImpl + SvpPPolBytesOf - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl + VecZnxDftBytesOf - + VecZnxBigBytesOf, + + VecZnxBigBytesOf + + GLWESecretPreparedAlloc, Scratch: ScratchTakeBasic + ScratchTakeCore, { - fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.tensor_key_encrypt_sk_tmp_bytes(infos) + GLWESecretPrepared::bytes_of(self, infos.rank_out()) + + self.bytes_of_vec_znx_dft(infos.rank_out().into(), 1) + + self.bytes_of_vec_znx_big(1, 1) + + self.bytes_of_vec_znx_dft(1, 1) + + GLWESecret::bytes_of(self.n().into(), Rank(1)) + + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } - fn gglwe_tensor_key_encrypt_sk( + fn tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -93,11 +96,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyCompressedToMut, + D: DataMut, + R: GGLWEInfos + TensorKeyCompressedAtMut, S: GLWESecretToRef + GetDistribution, { - let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out()); sk_dft_prep.prepare(self, sk); @@ -141,10 +143,10 @@ where let (seed_xa_tmp, _) = source_xa.branch(); - self.glwe_switching_key_compressed_encrypt_sk( + self.gglwe_compressed_encrypt_sk( res.at_mut(i, j), - &sk_ij, - sk, + &sk_ij.data, + &sk_dft_prep, seed_xa_tmp, source_xe, scratch_5, diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index 4526faf..9d242c8 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -1,6 +1,6 @@ use poulpy_hal::{ api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace}, - layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero}, + layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxInfos, ZnxZero}, source::Source, }; @@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut + GGSWCompressedSeedMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -83,30 +83,29 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCompressedToMut + GGSWCompressedSeedMut, + R: GGSWCompressedToMut + GGSWCompressedSeedMut + GGSWInfos, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()]; + let base2k: usize = res.base2k().into(); + let rank: usize = res.rank().into(); + let cols: usize = rank + 1; + let dsize: usize = res.dsize().into(); + + let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); + let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); + + assert_eq!(res.rank(), sk.rank()); + assert_eq!(pt.n(), self.n()); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + + let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.dnum().as_usize() * (res.rank().as_usize() + 1)]; { let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); - let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); - let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); - #[cfg(debug_assertions)] - { - use poulpy_hal::layouts::ZnxInfos; - - assert_eq!(res.rank(), sk.rank()); - assert_eq!(res.n(), sk.n()); - assert_eq!(pt.n() as u32, sk.n()); - } - - let base2k: usize = res.base2k().into(); - let rank: usize = res.rank().into(); - let cols: usize = rank + 1; - let dsize: usize = res.dsize().into(); + println!("res.seed: {:?}", res.seed); let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout()); diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index ea6096a..2b9cc06 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -1,13 +1,15 @@ use poulpy_hal::{ - api::{ScratchAvailable, VecZnxAutomorphism}, + api::{ScratchAvailable, SvpPPolBytesOf, VecZnxAutomorphism}, layouts::{Backend, DataMut, GaloisElement, Module, Scratch}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, - layouts::{AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos}, + GGLWEEncryptSk, ScratchTakeCore, + layouts::{ + AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepare, GLWESecretPrepared, + GLWESecretPreparedAlloc, GLWESecretToRef, LWEInfos, SetAutomorphismGaloisElement, + }, }; impl AutomorphismKey> { @@ -30,7 +32,7 @@ impl AutomorphismKey> { impl AutomorphismKey where - Self: AutomorphismKeyToMut, + Self: GGLWEToRef, { pub fn encrypt_sk( &mut self, @@ -62,13 +64,18 @@ pub trait AutomorphismKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyToMut, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, S: GLWESecretToRef; } impl AutomorphismKeyEncryptSk for Module where - Self: GLWESwitchingKeyEncryptSk + VecZnxAutomorphism + GaloisElement, + Self: GGLWEEncryptSk + + VecZnxAutomorphism + + GaloisElement + + SvpPPolBytesOf + + GLWESecretPrepare + + GLWESecretPreparedAlloc, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -80,7 +87,10 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - self.glwe_switching_key_encrypt_sk_tmp_bytes(infos) + GLWESecret::bytes_of_from_infos(infos) + GLWESecretPrepared::bytes_of_from_infos(self, infos) + + self + .gglwe_encrypt_sk_tmp_bytes(infos) + .max(GLWESecret::bytes_of_from_infos(infos)) } fn automorphism_key_encrypt_sk( @@ -92,10 +102,9 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: AutomorphismKeyToMut, + R: GGLWEToMut + SetAutomorphismGaloisElement + GGLWEInfos, S: GLWESecretToRef, { - let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); assert_eq!(res.n(), sk.n()); @@ -108,9 +117,10 @@ where self.automorphism_key_encrypt_sk_tmp_bytes(res) ); - let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank()); + let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { + let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); (0..res.rank_out().into()).for_each(|i| { self.vec_znx_automorphism( self.galois_element_inv(p), @@ -120,12 +130,19 @@ where i, ); }); + sk_out_prepared.prepare(self, &sk_out); } - res.key - .encrypt_sk(self, sk, &sk_out, source_xa, source_xe, scratch_1); + self.gglwe_encrypt_sk( + res, + &sk.data, + &sk_out_prepared, + source_xa, + source_xe, + scratch_1, + ); - res.p = p; + res.set_p(p); } } diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index e8c49f3..2699e4a 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::gglwe_ct::GGLWEEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyToMut, LWEInfos, + GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, prepared::GLWESecretPreparedAlloc, }, }; @@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef; } @@ -93,11 +93,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: GLWESecretToRef, S2: GLWESecretToRef, { - let res: &mut GLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); @@ -129,16 +128,17 @@ where } } - res.key.encrypt_sk( - self, + self.gglwe_encrypt_sk( + res, &sk_in_tmp, &sk_out_tmp, source_xa, source_xe, scratch_2, ); - res.sk_in_n = sk_in.n().into(); - res.sk_out_n = sk_out.n().into(); + + *res.input_degree() = sk_in.n(); + *res.output_degree() = sk_out.n(); } } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 2a4792d..10f404a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -8,10 +8,9 @@ use poulpy_hal::{ }; use crate::{ - GetDistribution, ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut, prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -63,7 +62,7 @@ pub trait TensorKeyEncryptSk { impl TensorKeyEncryptSk for Module where Self: ModuleN - + GLWESwitchingKeyEncryptSk + + GGLWEEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf + GLWESecretPreparedAlloc @@ -83,7 +82,7 @@ where + self.bytes_of_vec_znx_big(1, 1) + self.bytes_of_vec_znx_dft(1, 1) + GLWESecret::bytes_of(self.n().into(), Rank(1)) - + GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + + GGLWE::encrypt_sk_tmp_bytes(self, infos) } fn tensor_key_encrypt_sk( @@ -102,8 +101,8 @@ where // let n: RingDegree = sk.n(); let rank: Rank = res.rank_out(); - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); - sk_dft_prep.prepare(self, sk); + let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); + sk_prepared.prepare(self, sk); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); @@ -122,7 +121,7 @@ where (0..rank.into()).for_each(|i| { (i..rank.into()).for_each(|j| { - self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_dft_prep.data, j, &sk_dft, i); + self.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i); self.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0); self.vec_znx_big_normalize( @@ -135,8 +134,14 @@ where scratch_5, ); - res.at_mut(i, j) - .encrypt_sk(self, &sk_ij, sk, source_xa, source_xe, scratch_5); + res.at_mut(i, j).encrypt_sk( + self, + &sk_ij.data, + &sk_prepared, + source_xa, + source_xe, + scratch_5, + ); }); }) } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index a0328d3..85aa6e1 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/poulpy-core/src/encryption/glwe_ct.rs @@ -79,7 +79,7 @@ impl GLWE { source_xe: &mut Source, scratch: &mut Scratch, ) where - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, M: GLWEEncryptPk, { @@ -245,7 +245,7 @@ pub trait GLWEEncryptPk { scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef; fn glwe_encrypt_zero_pk( @@ -285,7 +285,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal(res, Some((pt, 0)), pk, source_xu, source_xe, scratch); @@ -324,7 +324,7 @@ pub(crate) trait GLWEEncryptPkInternal { scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef; } @@ -351,7 +351,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut, - P: GLWEPlaintextToRef, + P: GLWEPlaintextToRef + GLWEInfos, K: GLWEPublicKeyPreparedToRef, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); @@ -361,8 +361,8 @@ where assert_eq!(res.n(), pk.n()); assert_eq!(res.rank(), pk.rank()); if let Some((pt, _)) = pt { - assert_eq!(pt.to_ref().base2k(), pk.base2k()); - assert_eq!(pt.to_ref().n(), pk.n()); + assert_eq!(pt.base2k(), pk.base2k()); + assert_eq!(pt.n(), pk.n()); } let base2k: usize = pk.base2k().into(); diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index bd7c271..71b96ff 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -1,14 +1,13 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAutomorphismInplace}, + api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut, ZnxZero}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPrepare, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, @@ -59,12 +58,17 @@ pub trait GLWEToLWESwitchingKeyEncrypt { ) where S1: LWESecretToRef, S2: GLWESecretToRef, - R: GLWEToLWESwitchingKeyToMut; + R: GGLWEToMut; } impl GLWEToLWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedAlloc + VecZnxAutomorphismInplace, + Self: ModuleN + + GGLWEEncryptSk + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + VecZnxAutomorphismInplaceTmpBytes + + GLWESecretPrepare, Scratch: ScratchTakeCore, { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -72,7 +76,8 @@ where A: GGLWEInfos, { GLWESecretPrepared::bytes_of(self, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) | GLWESecret::bytes_of(self.n().into(), infos.rank_in())) + + GGLWE::encrypt_sk_tmp_bytes(self, infos) + .max(GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + self.vec_znx_automorphism_inplace_tmp_bytes()) } fn glwe_to_lwe_switching_key_encrypt_sk( @@ -86,23 +91,27 @@ where ) where S1: LWESecretToRef, S2: GLWESecretToRef, - R: GLWEToLWESwitchingKeyToMut, + R: GGLWEToMut, { - let res: &mut GLWEToLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); assert!(sk_lwe.n().0 <= self.n() as u32); - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); - sk_lwe_as_glwe.data.zero(); - sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); - self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); + let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1)); - res.0.encrypt_sk( - self, - sk_glwe, - &sk_lwe_as_glwe, + { + let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + sk_lwe_as_glwe.data.zero(); + sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); + self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_lwe_as_glwe_prep.prepare(self, &sk_lwe_as_glwe); + } + + self.gglwe_encrypt_sk( + res, + &sk_glwe.data, + &sk_lwe_as_glwe_prep, source_xa, source_xe, scratch_1, diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 4a5d6ac..7651e8d 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -1,7 +1,6 @@ use poulpy_hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace}, layouts::{Backend, DataMut, Module, ScratchOwned, Zn, ZnxView, ZnxViewMut}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, source::Source, }; @@ -16,7 +15,6 @@ impl LWE { P: LWEPlaintextToRef, S: LWESecretToRef, M: LWEEncryptSk, - BE: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { module.lwe_encrypt_sk(self, pt, sk, source_xa, source_xe); } @@ -33,7 +31,7 @@ pub trait LWEEncryptSk { impl LWEEncryptSk for Module where Self: Sized + ZnFillUniform + ZnAddNormal + ZnNormalizeInplace, - BE: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, { fn lwe_encrypt_sk(&self, res: &mut R, pt: &P, sk: &S, source_xa: &mut Source, source_xe: &mut Source) where diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 215efee..50e08a1 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -8,8 +8,8 @@ use crate::{ ScratchTakeCore, encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, layouts::{ - GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, LWESwitchingKeyToMut, - Rank, + GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef, + LWESwitchingKey, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedAlloc}, }, }; @@ -56,7 +56,7 @@ pub trait LWESwitchingKeyEncrypt { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: LWESecretToRef, S2: LWESecretToRef; } @@ -100,13 +100,12 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: LWESwitchingKeyToMut, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut + GGLWEInfos, S1: LWESecretToRef, S2: LWESecretToRef, { - let res: &mut LWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe_in: &LWESecret<&[u8]> = &sk_lwe_in.to_ref(); - let sk_lwe_out = &sk_lwe_out.to_ref(); + let sk_lwe_out: &LWESecret<&[u8]> = &sk_lwe_out.to_ref(); assert!(sk_lwe_in.n().0 <= res.n().0); assert!(sk_lwe_out.n().0 <= res.n().0); @@ -124,7 +123,7 @@ where self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); self.glwe_switching_key_encrypt_sk( - &mut res.0, + res, &sk_in_glwe, &sk_out_glwe, source_xa, diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a66c95d..a9856cc 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -1,15 +1,14 @@ use poulpy_hal::{ - api::{ModuleN, VecZnxAutomorphismInplace}, + api::{ModuleN, VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes}, layouts::{Backend, DataMut, Module, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ - ScratchTakeCore, - encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk, + GGLWEEncryptSk, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, - LWEToGLWESwitchingKeyToMut, Rank, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedAlloc, GLWESecretPreparedToRef, LWEInfos, LWESecret, + LWESecretToRef, LWEToGLWESwitchingKey, Rank, }, }; @@ -34,7 +33,7 @@ impl LWEToGLWESwitchingKey { scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, + S2: GLWESecretPreparedToRef, M: LWEToGLWESwitchingKeyEncrypt, Scratch: ScratchTakeCore, { @@ -57,13 +56,17 @@ pub trait LWEToGLWESwitchingKeyEncrypt { scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, - R: LWEToGLWESwitchingKeyToMut; + S2: GLWESecretPreparedToRef, + R: GGLWEToMut; } impl LWEToGLWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + VecZnxAutomorphismInplace, + Self: ModuleN + + GGLWEEncryptSk + + VecZnxAutomorphismInplace + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize @@ -75,7 +78,8 @@ where Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::encrypt_sk_tmp_bytes(self, infos) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + GLWESecret::bytes_of(self.n().into(), infos.rank_in()) + + GGLWE::encrypt_sk_tmp_bytes(self, infos).max(self.vec_znx_automorphism_inplace_tmp_bytes()) } fn lwe_to_glwe_switching_key_encrypt_sk( @@ -88,12 +92,10 @@ where scratch: &mut Scratch, ) where S1: LWESecretToRef, - S2: GLWESecretToRef, - R: LWEToGLWESwitchingKeyToMut, + S2: GLWESecretPreparedToRef, + R: GGLWEToMut, { - let res: &mut LWEToGLWESwitchingKey<&mut [u8]> = &mut res.to_mut(); let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); - let sk_glwe: &GLWESecret<&[u8]> = &sk_glwe.to_ref(); assert!(sk_lwe.n().0 <= self.n() as u32); @@ -102,9 +104,9 @@ where sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); - res.0.encrypt_sk( - self, - &sk_lwe_as_glwe, + self.gglwe_encrypt_sk( + res, + &sk_lwe_as_glwe.data, sk_glwe, source_xa, source_xe, diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index 871eab8..2943ce3 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Scratch}; use crate::{ ScratchTakeCore, external_product::gglwe_ksk::GGLWEExternalProduct, - layouts::{AutomorphismKey, AutomorphismKeyToRef, GGLWEInfos, GGSWInfos, prepared::GGSWPreparedToRef}, + layouts::{AutomorphismKey, GGLWEInfos, GGLWEToRef, GGSWInfos, prepared::GGSWPreparedToRef}, }; impl AutomorphismKey> { @@ -28,11 +28,11 @@ impl AutomorphismKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, - A: AutomorphismKeyToRef, + A: GGLWEToRef, B: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product(&mut self.key.key, &a.to_ref().key.key, b, scratch); + module.gglwe_external_product(self, a, b, scratch); } pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) @@ -41,6 +41,6 @@ impl AutomorphismKey { A: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product_inplace(&mut self.key.key, a, scratch); + module.gglwe_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 5bb4557..c6ee4a6 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::{ GLWEExternalProduct, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GLWEInfos, GLWESwitchingKey, prepared::{GGSWPrepared, GGSWPreparedToRef}, }, }; @@ -116,11 +116,11 @@ impl GLWESwitchingKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, - A: GLWESwitchingKeyToRef, + A: GGLWEToRef, B: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product(&mut self.key, &a.to_ref().key, b, scratch); + module.gglwe_external_product(self, a, b, scratch); } pub fn external_product_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) @@ -129,6 +129,6 @@ impl GLWESwitchingKey { A: GGSWPreparedToRef, Scratch: ScratchTakeCore, { - module.gglwe_external_product_inplace(&mut self.key, a, scratch); + module.gglwe_external_product_inplace(self, a, scratch); } } diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index bcd9e45..548f4d4 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -9,8 +9,7 @@ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, prepared::GetAutomorphismGaloisElement, }, }; @@ -118,7 +117,7 @@ impl GLWEPacker { pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) where A: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, M: GLWEPacking, Scratch: ScratchTakeCore, { @@ -191,7 +190,7 @@ where scratch: &mut Scratch, ) where R: GLWEToMut + GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -235,7 +234,7 @@ fn pack_core( scratch: &mut Scratch, ) where A: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, M: ModuleLogN + GLWEAutomorphism + GaloisElement @@ -308,7 +307,7 @@ fn combine( B: GLWEToRef + GLWEInfos, M: GLWEAutomorphism + GaloisElement + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, B: GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); @@ -401,7 +400,7 @@ fn pack_internal( M: GLWEAutomorphism + GLWERotate + GLWESub + GLWEShift + GLWEAdd + GLWENormalize, A: GLWEToMut + GLWEToRef + GLWEInfos, B: GLWEToMut + GLWEToRef + GLWEInfos, - K: AutomorphismKeyPreparedToRef + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { // Goal is to evaluate: a = a + b*X^t + phi(a - b*X^t)) diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 48608d1..a1c2573 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -8,8 +8,8 @@ use poulpy_hal::{ use crate::{ GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, layouts::{ - Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{AutomorphismKeyPreparedToRef, GetAutomorphismGaloisElement}, + Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, LWEInfos, + prepared::GetAutomorphismGaloisElement, }, }; @@ -43,7 +43,7 @@ impl GLWE { scratch: &mut Scratch, ) where A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -58,7 +58,7 @@ impl GLWE { keys: &HashMap, scratch: &mut Scratch, ) where - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -110,7 +110,7 @@ where where R: GLWEToMut, A: GLWEToRef, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { self.glwe_copy(res, a); @@ -120,7 +120,7 @@ where fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) where R: GLWEToMut, - K: AutomorphismKeyPreparedToRef + GGLWEInfos + GetAutomorphismGaloisElement, + K: GGLWEPreparedToRef + GetAutomorphismGaloisElement + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 912d803..3b6495a 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -3,11 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - AutomorphismKey, AutomorphismKeyToRef, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWESwitchingKey, - GLWESwitchingKeyToRef, - prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, - }, + layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, }; impl AutomorphismKey> { @@ -25,21 +21,21 @@ impl AutomorphismKey> { impl AutomorphismKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where - A: AutomorphismKeyToRef, - B: GLWESwitchingKeyPreparedToRef, + A: GGLWEToRef + GGLWEToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); + module.gglwe_keyswitch(self, a, b, scratch); } pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); + module.gglwe_keyswitch_inplace(self, a, scratch); } } @@ -58,21 +54,21 @@ impl GLWESwitchingKey> { impl GLWESwitchingKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where - A: GLWESwitchingKeyToRef, - B: GLWESwitchingKeyPreparedToRef, + A: GGLWEToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); + module.gglwe_keyswitch(self, a, b, scratch); } pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { - module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); + module.gglwe_keyswitch_inplace(self, a, scratch); } } @@ -92,7 +88,7 @@ impl GGLWE { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GGLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { @@ -101,7 +97,7 @@ impl GGLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: GGLWEKeyswitch, { @@ -128,12 +124,11 @@ where where R: GGLWEToMut, A: GGLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let a: &GGLWE<&[u8]> = &a.to_ref(); - let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &b.to_ref(); assert_eq!( res.rank_in(), @@ -180,11 +175,10 @@ where fn gglwe_keyswitch_inplace(&self, res: &mut R, a: &A, scratch: &mut Scratch) where R: GGLWEToMut, - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); - let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &a.to_ref(); assert_eq!( res.rank_out(), diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 5e49d62..7682315 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -3,10 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, - prepared::{GLWESwitchingKeyPreparedToRef, TensorKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef}, }; impl GGSW> { @@ -32,7 +29,7 @@ impl GGSW { pub fn keyswitch(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where A: GGSWToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, @@ -42,7 +39,7 @@ impl GGSW { pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, @@ -93,14 +90,15 @@ where where R: GGSWToMut, A: GGSWToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - assert_eq!(res.ggsw_layout(), a.ggsw_layout()); + assert!(res.dnum() <= a.dnum()); + assert_eq!(res.dsize(), a.dsize()); for row in 0..a.dnum().into() { // Key-switch column 0, i.e. @@ -114,7 +112,7 @@ where fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, T: TensorKeyPreparedToRef, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index f82a4d1..be81ebf 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -4,15 +4,12 @@ use poulpy_hal::{ VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeTmpBytes, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, - layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, + layouts::{Backend, DataMut, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; use crate::{ ScratchTakeCore, - layouts::{ - GGLWEInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, - prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPrepared, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos}, }; impl GLWE> { @@ -31,7 +28,7 @@ impl GLWE { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GLWEToRef, - B: GLWESwitchingKeyPreparedToRef, + B: GGLWEPreparedToRef, M: GLWEKeyswitch, Scratch: ScratchTakeCore, { @@ -40,7 +37,7 @@ impl GLWE { pub fn keyswitch_inplace(&mut self, module: &M, a: &A, scratch: &mut Scratch) where - A: GLWESwitchingKeyPreparedToRef, + A: GGLWEPreparedToRef, M: GLWEKeyswitch, Scratch: ScratchTakeCore, { @@ -129,12 +126,12 @@ where where R: GLWEToMut, A: GLWEToRef, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let a: &GLWE<&[u8]> = &a.to_ref(); - let b: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); + let b: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( a.rank(), @@ -184,11 +181,11 @@ where fn glwe_keyswitch_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GLWEToMut, - K: GLWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let a: &GLWESwitchingKeyPrepared<&[u8], BE> = &key.to_ref(); + let a: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); assert_eq!( res.rank(), @@ -239,17 +236,17 @@ impl GLWE> {} impl GLWE {} -pub(crate) fn keyswitch_internal( +pub(crate) fn keyswitch_internal( module: &M, mut res: VecZnxDft, - a: &GLWE, - key: &GLWESwitchingKeyPrepared, + a: &A, + key: &K, scratch: &mut Scratch, ) -> VecZnxBig where DR: DataMut, - DA: DataRef, - DB: DataRef, + A: GLWEToRef, + K: GGLWEPreparedToRef, M: ModuleN + VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes @@ -264,11 +261,14 @@ where + VecZnxNormalize, Scratch: ScratchTakeCore, { + let a: &GLWE<&[u8]> = &a.to_ref(); + let key: &GGLWEPrepared<&[u8], BE> = &key.to_ref(); + let base2k_in: usize = a.base2k().into(); let base2k_out: usize = key.base2k().into(); let cols: usize = (a.rank() + 1).into(); let a_size: usize = (a.size() * base2k_in).div_ceil(base2k_out); - let pmat: &VmpPMat = &key.key.data; + let pmat: &VmpPMat<&[u8], BE> = &key.data; if key.dsize() == 1 { let (mut ai_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols - 1, a.size()); diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 8a529c7..e73747a 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -6,10 +6,7 @@ use poulpy_hal::{ use crate::{ LWESampleExtract, ScratchTakeCore, keyswitching::glwe_ct::GLWEKeyswitch, - layouts::{ - GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision, - prepared::{LWESwitchingKeyPrepared, LWESwitchingKeyPreparedToRef}, - }, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWELayout, LWE, LWEInfos, LWEToMut, LWEToRef, Rank, TorusPrecision}, }; impl LWE> { @@ -28,7 +25,7 @@ impl LWE { pub fn keyswitch(&mut self, module: &M, a: &A, ksk: &K, scratch: &mut Scratch) where A: LWEToRef, - K: LWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, M: LWEKeySwitch, { @@ -36,7 +33,7 @@ impl LWE { } } -impl LWEKeySwitch for Module where Self: LWEKeySwitch {} +impl LWEKeySwitch for Module where Self: GLWEKeyswitch + LWESampleExtract {} pub trait LWEKeySwitch where @@ -75,12 +72,11 @@ where where R: LWEToMut, A: LWEToRef, - K: LWESwitchingKeyPreparedToRef, + K: GGLWEPreparedToRef + GGLWEInfos, Scratch: ScratchTakeCore, { let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); let a: &LWE<&[u8]> = &a.to_ref(); - let ksk: &LWESwitchingKeyPrepared<&[u8], BE> = &ksk.to_ref(); assert!(res.n().as_usize() <= self.n()); assert!(a.n().as_usize() <= self.n()); @@ -120,7 +116,7 @@ where glwe_in.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } - self.glwe_keyswitch(&mut glwe_out, &glwe_in, &ksk.0, scratch_1); + self.glwe_keyswitch(&mut glwe_out, &glwe_in, ksk, scratch_1); self.lwe_sample_extract(res, &glwe_out); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 5a40e5d..1f15bfc 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, + GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef, + GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -15,7 +14,7 @@ use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKeyCompressed { - pub(crate) key: GLWESwitchingKeyCompressed, + pub(crate) key: GGLWECompressed, pub(crate) p: i64, } @@ -102,7 +101,7 @@ impl AutomorphismKeyCompressed> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKeyCompressed { - key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), + key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -122,7 +121,7 @@ impl AutomorphismKeyCompressed> { } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize) + GGLWECompressed::bytes_of(n, base2k, k, rank, dnum, dsize) } } @@ -142,19 +141,19 @@ impl WriterTo for AutomorphismKeyCompressed { pub trait AutomorphismKeyDecompress where - Self: GLWESwitchingKeyDecompress, + Self: GGLWEDecompress, { fn decompress_automorphism_key(&self, res: &mut R, other: &O) where - R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + R: GGLWEToMut + SetAutomorphismGaloisElement, + O: GGLWECompressedToRef + GetAutomorphismGaloisElement, { - self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); + self.decompress_gglwe(res, other); res.set_p(other.p()); } } -impl AutomorphismKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} +impl AutomorphismKeyDecompress for Module where Self: GLWEDecompress {} impl AutomorphismKey where @@ -162,20 +161,32 @@ where { pub fn decompress(&mut self, module: &M, other: &O) where - O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + O: GGLWECompressedToRef + GetAutomorphismGaloisElement, M: AutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } } +impl GGLWECompressedToRef for AutomorphismKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.key.to_ref() + } +} + +impl GGLWECompressedToMut for AutomorphismKeyCompressed { + 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 - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, + GLWECompressed: GLWECompressedToRef, { fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { AutomorphismKeyCompressed { @@ -191,7 +202,7 @@ pub trait AutomorphismKeyCompressedToMut { impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, + GLWECompressed: GLWECompressedToMut, { fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { AutomorphismKeyCompressed { @@ -200,3 +211,15 @@ where } } } + +impl GGLWECompressedSeedMut for AutomorphismKeyCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.key.seed + } +} + +impl SetAutomorphismGaloisElement for AutomorphismKeyCompressed { + fn set_p(&mut self, p: i64) { + self.p = p + } +} diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index dcc9886..0fb0382 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -258,7 +258,8 @@ where let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let other: &GGLWECompressed<&[u8]> = &other.to_ref(); - assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + assert_eq!(res.dsize(), other.dsize()); + assert!(res.dnum() <= other.dnum()); let rank_in: usize = res.rank_in().into(); let dnum: usize = res.dnum().into(); diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 3b94141..82fb797 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, - LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWECompressedSeedMut, GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -14,8 +14,34 @@ use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKeyCompressed { pub(crate) key: GGLWECompressed, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out +} + +impl GGLWECompressedSeedMut for GLWESwitchingKeyCompressed { + fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> { + &mut self.key.seed + } +} + +impl GLWESwitchingKeyDegrees for GLWESwitchingKeyCompressed { + fn output_degree(&self) -> &Degree { + &self.output_degree + } + + fn input_degree(&self) -> &Degree { + &self.input_degree + } +} + +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKeyCompressed { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree + } + + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree + } } impl LWEInfos for GLWESwitchingKeyCompressed { @@ -76,7 +102,7 @@ impl fmt::Display for GLWESwitchingKeyCompressed { write!( f, "(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}", - self.sk_in_n, self.sk_out_n, self.key.data + self.input_degree, self.output_degree, self.key.data ) } } @@ -100,8 +126,8 @@ impl GLWESwitchingKeyCompressed> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { GLWESwitchingKeyCompressed { key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -120,16 +146,16 @@ where { impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.sk_in_n = reader.read_u64::()? as usize; - self.sk_out_n = reader.read_u64::()? as usize; + self.input_degree = Degree(reader.read_u32::()? as u32); + self.output_degree = Degree(reader.read_u32::()? as u32); self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_u64::(self.sk_in_n as u64)?; - writer.write_u64::(self.sk_out_n as u64)?; + writer.write_u32::(self.input_degree.into())?; + writer.write_u32::(self.output_degree.into())?; self.key.write_to(writer) } } @@ -140,13 +166,13 @@ where { fn decompress_glwe_switching_key(&self, res: &mut R, other: &O) where - R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); - self.decompress_gglwe(&mut res.to_mut().key, &other.key); - res.set_sk_in_n(other.sk_in_n); - res.set_sk_out_n(other.sk_out_n); + self.decompress_gglwe(res, other); + + *res.input_degree() = *other.input_degree(); + *res.output_degree() = *other.output_degree(); } } @@ -155,43 +181,21 @@ impl GLWESwitchingKeyDecompress for Module where Self: GGLWEDecom impl GLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: GLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: GLWESwitchingKeyDecompress, { module.decompress_glwe_switching_key(self, other); } } -pub trait GLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed -where - GGLWECompressed: GGLWECompressedToMut, -{ - fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { - GLWESwitchingKeyCompressed { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_mut(), - } +impl GGLWECompressedToMut for GLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.key.to_mut() } } -pub trait GLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; -} - -impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed -where - GGLWECompressed: GGLWECompressedToRef, -{ - fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { - GLWESwitchingKeyCompressed { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_ref(), - } +impl GGLWECompressedToRef for GLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 4ec8a42..1e9e312 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -4,17 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, + GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct TensorKeyCompressed { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKeyCompressed { @@ -67,7 +65,7 @@ impl FillUniform for TensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GGLWECompressed| key.fill_uniform(log_bound, source)) } } @@ -100,7 +98,7 @@ impl TensorKeyCompressed> { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyCompressed { keys: (0..pairs) - .map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GGLWECompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -121,7 +119,7 @@ impl TensorKeyCompressed> { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) + pairs * GGLWECompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize) } } @@ -151,8 +149,26 @@ impl WriterTo for TensorKeyCompressed { } } -impl TensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed { +pub trait TensorKeyCompressedAtRef { + fn at(&self, i: usize, j: usize) -> &GGLWECompressed; +} + +impl TensorKeyCompressedAtRef for TensorKeyCompressed { + fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed { + if i > j { + std::mem::swap(&mut i, &mut j); + }; + let rank: usize = self.rank_out().into(); + &self.keys[i * rank + j - (i * (i + 1) / 2)] + } +} + +pub trait TensorKeyCompressedAtMut { + fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed; +} + +impl TensorKeyCompressedAtMut for TensorKeyCompressed { + fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -163,7 +179,7 @@ impl TensorKeyCompressed { pub trait TensorKeyDecompress where - Self: GLWESwitchingKeyDecompress, + Self: GGLWEDecompress, { fn decompress_tensor_key(&self, res: &mut R, other: &O) where @@ -182,12 +198,12 @@ where ); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.decompress_glwe_switching_key(a, b); + self.decompress_gglwe(a, b); } } } -impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} +impl TensorKeyDecompress for Module where Self: GGLWEDecompress {} impl TensorKey { pub fn decompress(&mut self, module: &M, other: &O) @@ -205,7 +221,7 @@ pub trait TensorKeyCompressedToMut { impl TensorKeyCompressedToMut for TensorKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, + GGLWECompressed: GGLWECompressedToMut, { fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { TensorKeyCompressed { @@ -220,7 +236,7 @@ pub trait TensorKeyCompressedToRef { impl TensorKeyCompressedToRef for TensorKeyCompressed where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, + GGLWECompressed: GGLWECompressedToRef, { fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { TensorKeyCompressed { diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index ca4446f..14fdd7a 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -138,7 +138,7 @@ impl GGSWCompressed> { base2k, dsize, rank, - seed: Vec::new(), + seed: vec![[0u8; 32]; dnum.as_usize() * (rank.as_usize() + 1)], } } @@ -260,7 +260,7 @@ where } } -impl GGSWDecompress for Module where Self: GGSWDecompress {} +impl GGSWDecompress for Module where Self: GLWEDecompress {} impl GGSW { pub fn decompress(&mut self, module: &M, other: &O) diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 1b05575..fa61b81 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -158,7 +158,6 @@ where self.ring_degree() ); - assert_eq!(res.lwe_layout(), other.lwe_layout()); assert_eq!(res.glwe_layout(), other.glwe_layout()); let mut source: Source = Source::new(other.seed); diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 08508b1..6ac325c 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,11 +6,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, - TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; #[derive(PartialEq, Eq, Clone)] @@ -151,10 +149,10 @@ where { fn decompress_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O) where - R: GLWEToLWESwitchingKeyToMut, - O: GLWEToLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -163,35 +161,21 @@ impl GLWEToLWESwitchingKeyDecompress for Module where Self: GLWES impl GLWEToLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: GLWEToLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: GLWEToLWESwitchingKeyDecompress, { module.decompress_glwe_to_lwe_switching_key(self, other); } } -pub trait GLWEToLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; -} - -impl GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { - GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { - GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 3738b92..764d423 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -4,10 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; use std::fmt; @@ -152,10 +151,10 @@ where { fn decompress_lwe_switching_key(&self, res: &mut R, other: &O) where - R: LWESwitchingKeyToMut, - O: LWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -164,35 +163,21 @@ impl LWESwitchingKeyDecompress for Module where Self: GLWESwitchi impl LWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: LWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: LWESwitchingKeyDecompress, { module.decompress_lwe_switching_key(self, other); } } -pub trait LWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; -} - -impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { - LWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for LWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait LWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; -} - -impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { - LWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for LWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index ea0ca05..7a724c9 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -4,11 +4,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, - TorusPrecision, - compressed::{ - GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, - }, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress}, }; use std::fmt; @@ -149,10 +147,10 @@ where { fn decompress_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O) where - R: LWEToGLWESwitchingKeyToMut, - O: LWEToGLWESwitchingKeyCompressedToRef, + R: GGLWEToMut + GLWESwitchingKeyDegreesMut, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, { - self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + self.decompress_glwe_switching_key(res, other); } } @@ -161,35 +159,21 @@ impl LWEToGLWESwitchingKeyDecompress for Module where Self: GLWES impl LWEToGLWESwitchingKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: LWEToGLWESwitchingKeyCompressedToRef, + O: GGLWECompressedToRef + GLWESwitchingKeyDegrees, M: LWEToGLWESwitchingKeyDecompress, { module.decompress_lwe_to_glwe_switching_key(self, other); } } -pub trait LWEToGLWESwitchingKeyCompressedToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; -} - -impl LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, -{ - fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { - LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) +impl GGLWECompressedToRef for LWEToGLWESwitchingKeyCompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyCompressedToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; -} - -impl LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed -where - GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, -{ - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { - LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) +impl GGLWECompressedToMut for LWEToGLWESwitchingKeyCompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + self.0.to_mut() } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index 5d0f5f7..b705056 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, - LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -24,7 +23,7 @@ pub struct AutomorphismKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct AutomorphismKey { - pub(crate) key: GLWESwitchingKey, + pub(crate) key: GGLWE, pub(crate) p: i64, } @@ -161,7 +160,7 @@ impl AutomorphismKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { AutomorphismKey { - key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize), + key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -186,39 +185,19 @@ impl AutomorphismKey> { } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize) + GGLWE::bytes_of(n, base2k, k, rank, rank, dnum, dsize) } } -pub trait AutomorphismKeyToMut { - fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; -} - -impl AutomorphismKeyToMut for AutomorphismKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> { - AutomorphismKey { - key: self.key.to_mut(), - p: self.p, - } +impl GGLWEToMut for AutomorphismKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.key.to_mut() } } -pub trait AutomorphismKeyToRef { - fn to_ref(&self) -> AutomorphismKey<&[u8]>; -} - -impl AutomorphismKeyToRef for AutomorphismKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> AutomorphismKey<&[u8]> { - AutomorphismKey { - p: self.p, - key: self.key.to_ref(), - } +impl GGLWEToRef for AutomorphismKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index b451951..1e67222 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -62,37 +62,37 @@ impl GGLWEInfos for GLWESwitchingKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKey { pub(crate) key: GGLWE, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out } -pub trait GLWESwitchingKeySetMetaData { - fn set_sk_in_n(&mut self, sk_in_n: usize); - fn set_sk_out_n(&mut self, sk_out_n: usize); +pub trait GLWESwitchingKeyDegrees { + fn input_degree(&self) -> &Degree; + fn output_degree(&self) -> &Degree; } -impl GLWESwitchingKeySetMetaData for GLWESwitchingKey { - fn set_sk_in_n(&mut self, sk_in_n: usize) { - self.sk_in_n = sk_in_n +impl GLWESwitchingKeyDegrees for GLWESwitchingKey { + fn output_degree(&self) -> &Degree { + &self.output_degree } - fn set_sk_out_n(&mut self, sk_out_n: usize) { - self.sk_out_n = sk_out_n + fn input_degree(&self) -> &Degree { + &self.input_degree } } -pub trait GLWESwtichingKeyGetMetaData { - fn sk_in_n(&self) -> usize; - fn sk_out_n(&self) -> usize; +pub trait GLWESwitchingKeyDegreesMut { + fn input_degree(&mut self) -> &mut Degree; + fn output_degree(&mut self) -> &mut Degree; } -impl GLWESwtichingKeyGetMetaData for GLWESwitchingKey { - fn sk_in_n(&self) -> usize { - self.sk_in_n +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKey { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree } - fn sk_out_n(&self) -> usize { - self.sk_out_n + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree } } @@ -149,8 +149,8 @@ impl fmt::Display for GLWESwitchingKey { write!( f, "(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}", - self.sk_in_n, - self.sk_out_n, + self.input_degree, + self.output_degree, self.key.data() ) } @@ -181,8 +181,8 @@ impl GLWESwitchingKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self { GLWESwitchingKey { key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -214,37 +214,15 @@ impl GLWESwitchingKey> { } } -pub trait GLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; -} - -impl GLWESwitchingKeyToMut for GLWESwitchingKey -where - GGLWE: GGLWEToMut, -{ - fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { - GLWESwitchingKey { - key: self.key.to_mut(), - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - } +impl GGLWEToMut for GLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.key.to_mut() } } -pub trait GLWESwitchingKeyToRef { - fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; -} - -impl GLWESwitchingKeyToRef for GLWESwitchingKey -where - GGLWE: GGLWEToRef, -{ - fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { - GLWESwitchingKey { - key: self.key.to_ref(), - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - } +impl GGLWEToRef for GLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.key.to_ref() } } @@ -262,16 +240,16 @@ impl GLWESwitchingKey { impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.sk_in_n = reader.read_u64::()? as usize; - self.sk_out_n = reader.read_u64::()? as usize; + self.input_degree = Degree(reader.read_u32::()? as u32); + self.output_degree = Degree(reader.read_u32::()? as u32); self.key.read_from(reader) } } impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { - writer.write_u64::(self.sk_in_n as u64)?; - writer.write_u64::(self.sk_out_n as u64)?; + writer.write_u32::(self.input_degree.into())?; + writer.write_u32::(self.output_degree.into())?; self.key.write_to(writer) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index 240b482..7f1bb4c 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -4,8 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -23,7 +22,7 @@ pub struct TensorKeyLayout { #[derive(PartialEq, Eq, Clone)] pub struct TensorKey { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKey { @@ -116,7 +115,7 @@ impl FillUniform for TensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GLWESwitchingKey| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GGLWE| key.fill_uniform(log_bound, source)) } } @@ -154,7 +153,7 @@ impl TensorKey> { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); TensorKey { keys: (0..pairs) - .map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -180,13 +179,13 @@ impl TensorKey> { pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GGLWE::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize) } } impl TensorKey { - // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { + // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -196,8 +195,8 @@ impl TensorKey { } impl TensorKey { - // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { + // Returns a reference to GGLWE_{s}(s[i] * s[j]) + pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -238,7 +237,7 @@ pub trait TensorKeyToRef { impl TensorKeyToRef for TensorKey where - GLWESwitchingKey: GLWESwitchingKeyToRef, + GGLWE: GGLWEToRef, { fn to_ref(&self) -> TensorKey<&[u8]> { TensorKey { @@ -253,7 +252,7 @@ pub trait TensorKeyToMut { impl TensorKeyToMut for TensorKey where - GLWESwitchingKey: GLWESwitchingKeyToMut, + GGLWE: GGLWEToMut, { fn to_mut(&mut self) -> TensorKey<&mut [u8]> { TensorKey { diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 45fa6df..1bb28ff 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; use std::fmt; @@ -196,28 +196,24 @@ impl GLWEToLWESwitchingKey> { } } -pub trait GLWEToLWESwitchingKeyToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; -} - -impl GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> { - GLWEToLWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for GLWEToLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; -} - -impl GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { - GLWEToLWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for GLWEToLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 25ce059..2ae6032 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -193,28 +193,34 @@ impl WriterTo for LWESwitchingKey { } } -pub trait LWESwitchingKeyToRef { - fn to_ref(&self) -> LWESwitchingKey<&[u8]>; -} - -impl LWESwitchingKeyToRef for LWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> LWESwitchingKey<&[u8]> { - LWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for LWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait LWESwitchingKeyToMut { - fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; -} - -impl LWESwitchingKeyToMut for LWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { - LWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for LWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree + } +} + +impl GLWESwitchingKeyDegrees for LWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree } } diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 26fb7f9..dbe95dd 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)] @@ -196,28 +196,24 @@ impl LWEToGLWESwitchingKey> { } } -pub trait LWEToGLWESwitchingKeyToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; -} - -impl LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToRef, -{ - fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> { - LWEToGLWESwitchingKey(self.0.to_ref()) +impl GGLWEToRef for LWEToGLWESwitchingKey { + fn to_ref(&self) -> GGLWE<&[u8]> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; -} - -impl LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey -where - GLWESwitchingKey: GLWESwitchingKeyToMut, -{ - fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { - LWEToGLWESwitchingKey(self.0.to_mut()) +impl GGLWEToMut for LWEToGLWESwitchingKey { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index d09b2e6..6f3d4b9 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -1,16 +1,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, + GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct AutomorphismKeyPrepared { - pub(crate) key: GLWESwitchingKeyPrepared, + pub(crate) key: GGLWEPrepared, pub(crate) p: i64, } @@ -78,7 +75,7 @@ impl GGLWEInfos for AutomorphismKeyPrepared { pub trait AutomorphismKeyPreparedAlloc where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn alloc_automorphism_key_prepared( &self, @@ -89,7 +86,7 @@ where dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, B> { - key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), + key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -120,7 +117,7 @@ where dnum: Dnum, dsize: Dsize, ) -> usize { - self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) + self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize) } fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize @@ -130,7 +127,7 @@ where assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" + "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); self.bytes_of_automorphism_key_prepared( infos.base2k(), @@ -142,7 +139,7 @@ where } } -impl AutomorphismKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} +impl AutomorphismKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} impl AutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -178,26 +175,26 @@ impl AutomorphismKeyPrepared, B> { pub trait PrepareAutomorphismKey where - Self: GLWESwitchingKeyPrepare, + Self: GGLWEPrepare, { fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.prepare_glwe_switching_key_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: AutomorphismKeyPreparedToMut + SetAutomorphismGaloisElement, - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + R: GGLWEPreparedToMut + SetAutomorphismGaloisElement, + O: GGLWEToRef + GetAutomorphismGaloisElement, { - self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_gglwe(res, other, scratch); res.set_p(other.p()); } } -impl PrepareAutomorphismKey for Module where Module: GLWESwitchingKeyPrepare {} +impl PrepareAutomorphismKey for Module where Module: GGLWEPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize @@ -211,35 +208,21 @@ impl AutomorphismKeyPrepared, B> { impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, + O: GGLWEToRef + GetAutomorphismGaloisElement, M: PrepareAutomorphismKey, { module.prepare_automorphism_key(self, other, scratch); } } -pub trait AutomorphismKeyPreparedToMut { - fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; -} - -impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { - fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { - AutomorphismKeyPrepared { - p: self.p, - key: self.key.to_mut(), - } +impl GGLWEPreparedToMut for AutomorphismKeyPrepared { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.key.to_mut() } } -pub trait AutomorphismKeyPreparedToRef { - fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; -} - -impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { - fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { - AutomorphismKeyPrepared { - p: self.p, - key: self.key.to_ref(), - } +impl GGLWEPreparedToRef for AutomorphismKeyPrepared { + fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { + self.key.to_ref() } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index d8ab0ca..930f3e5 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,35 +1,35 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef, - GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, + LWEInfos, Rank, TorusPrecision, prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] pub struct GLWESwitchingKeyPrepared { pub(crate) key: GGLWEPrepared, - pub(crate) sk_in_n: usize, // Degree of sk_in - pub(crate) sk_out_n: usize, // Degree of sk_out + pub(crate) input_degree: Degree, // Degree of sk_in + pub(crate) output_degree: Degree, // Degree of sk_out } -impl GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared { - fn set_sk_in_n(&mut self, sk_in_n: usize) { - self.sk_in_n = sk_in_n +impl GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared { + fn output_degree(&self) -> &Degree { + &self.output_degree } - fn set_sk_out_n(&mut self, sk_out_n: usize) { - self.sk_out_n = sk_out_n + fn input_degree(&self) -> &Degree { + &self.input_degree } } -impl GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared { - fn sk_in_n(&self) -> usize { - self.sk_in_n +impl GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared { + fn output_degree(&mut self) -> &mut Degree { + &mut self.output_degree } - fn sk_out_n(&self) -> usize { - self.sk_out_n + fn input_degree(&mut self) -> &mut Degree { + &mut self.input_degree } } @@ -90,8 +90,8 @@ where ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, B> { key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), } } @@ -199,12 +199,12 @@ where fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeySetMetaData, - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); - res.set_sk_in_n(other.sk_in_n()); - res.set_sk_out_n(other.sk_out_n()); + self.prepare_gglwe(res, other, scratch); + *res.input_degree() = *other.input_degree(); + *res.output_degree() = *other.output_degree(); } } @@ -213,7 +213,7 @@ impl GLWESwitchingKeyPrepare for Module where Self: GGLWEPrepa impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: GLWESwitchingKeyPrepare, { module.prepare_glwe_switching(self, other, scratch); @@ -229,30 +229,20 @@ impl GLWESwitchingKeyPrepared, B> { } } -pub trait GLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { - fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { - GLWESwitchingKeyPrepared { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_mut(), - } +impl GGLWEPreparedToRef for GLWESwitchingKeyPrepared +where + GGLWEPrepared: GGLWEPreparedToRef, +{ + fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> { + self.key.to_ref() } } -pub trait GLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; -} - -impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { - fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { - GLWESwitchingKeyPrepared { - sk_in_n: self.sk_in_n, - sk_out_n: self.sk_out_n, - key: self.key.to_ref(), - } +impl GGLWEPreparedToMut for GLWESwitchingKeyPrepared +where + GGLWEPrepared: GGLWEPreparedToMut, +{ + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> { + self.key.to_mut() } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 29f6e28..7b07ce7 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,16 +1,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, + GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, }; #[derive(PartialEq, Eq)] pub struct TensorKeyPrepared { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, } impl LWEInfos for TensorKeyPrepared { @@ -57,7 +54,7 @@ impl GGLWEInfos for TensorKeyPrepared { pub trait TensorKeyPreparedAlloc where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GGLWEPreparedAlloc, { fn alloc_tensor_key_prepared( &self, @@ -70,7 +67,7 @@ where let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyPrepared { keys: (0..pairs) - .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } @@ -82,7 +79,7 @@ where assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" + "rank_in != rank_out is not supported for TensorKeyPrepared" ); self.alloc_tensor_key_prepared( infos.base2k(), @@ -95,7 +92,7 @@ where fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) + pairs * self.bytes_of_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize) } fn bytes_of_tensor_key_prepared_from_infos(&self, infos: &A) -> usize @@ -112,7 +109,7 @@ where } } -impl TensorKeyPreparedAlloc for Module where Module: GLWESwitchingKeyPreparedAlloc {} +impl TensorKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} impl TensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self @@ -147,8 +144,8 @@ impl TensorKeyPrepared, B> { } impl TensorKeyPrepared { - // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { + // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -158,8 +155,8 @@ impl TensorKeyPrepared { } impl TensorKeyPrepared { - // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { + // Returns a reference to GGLWE_{s}(s[i] * s[j]) + pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -170,13 +167,13 @@ impl TensorKeyPrepared { pub trait TensorKeyPrepare where - Self: GLWESwitchingKeyPrepare, + Self: GGLWEPrepare, { fn prepare_tensor_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.prepare_glwe_switching_key_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -190,12 +187,12 @@ where assert_eq!(res.keys.len(), other.keys.len()); for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { - self.prepare_glwe_switching(a, b, scratch); + self.prepare_gglwe(a, b, scratch); } } } -impl TensorKeyPrepare for Module where Self: GLWESwitchingKeyPrepare {} +impl TensorKeyPrepare for Module where Self: GGLWEPrepare {} impl TensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize @@ -223,7 +220,7 @@ pub trait TensorKeyPreparedToMut { impl TensorKeyPreparedToMut for TensorKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GGLWEPrepared: GGLWEPreparedToMut, { fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { TensorKeyPrepared { @@ -238,7 +235,7 @@ pub trait TensorKeyPreparedToRef { impl TensorKeyPreparedToRef for TensorKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GGLWEPrepared: GGLWEPreparedToRef, { fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { TensorKeyPrepared { diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index cf1d875..716eeff 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -152,10 +150,10 @@ where fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: GLWEToLWESwitchingKeyPreparedToMut, - O: GLWEToLWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -174,35 +172,37 @@ impl GLWEToLWESwitchingKeyPrepared, B> { impl GLWEToLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: GLWEToLWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: GLWEToLWESwitchingKeyPrepare, { module.prepare_glwe_to_lwe_switching_key(self, other, scratch); } } -pub trait GLWEToLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>; -} - -impl GLWEToLWESwitchingKeyPreparedToRef for GLWEToLWESwitchingKeyPrepared +impl GGLWEPreparedToRef for GLWEToLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> { - GLWEToLWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait GLWEToLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl GLWEToLWESwitchingKeyPreparedToMut for GLWEToLWESwitchingKeyPrepared +impl GGLWEPreparedToMut for GLWEToLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> { - GLWEToLWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 83508d0..0319f77 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; #[derive(PartialEq, Eq)] @@ -160,10 +158,10 @@ where } fn prepare_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: LWESwitchingKeyPreparedToMut, - O: LWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -182,35 +180,37 @@ impl LWESwitchingKeyPrepared, B> { impl LWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: LWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: LWESwitchingKeyPrepare, { module.prepare_lwe_switching_key(self, other, scratch); } } -pub trait LWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>; -} - -impl LWESwitchingKeyPreparedToRef for LWESwitchingKeyPrepared +impl GGLWEPreparedToRef for LWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GGLWEPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> { - LWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait LWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl LWESwitchingKeyPreparedToMut for LWESwitchingKeyPrepared +impl GGLWEPreparedToMut for LWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GGLWEPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> { - LWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 43b006c..c863c3f 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -1,11 +1,9 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision, - prepared::{ - GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut, - GLWESwitchingKeyPreparedToRef, - }, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, + GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, + prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. @@ -159,10 +157,10 @@ where fn prepare_lwe_to_glwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: LWEToGLWESwitchingKeyPreparedToMut, - O: LWEToGLWESwitchingKeyToRef, + R: GGLWEPreparedToMut + GLWESwitchingKeyDegreesMut, + O: GGLWEToRef + GLWESwitchingKeyDegrees, { - self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(res, other, scratch); } } @@ -181,35 +179,37 @@ impl LWEToGLWESwitchingKeyPrepared, B> { impl LWEToGLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: LWEToGLWESwitchingKeyToRef, + O: GGLWEToRef + GLWESwitchingKeyDegrees, M: LWEToGLWESwitchingKeyPrepare, { module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } } -pub trait LWEToGLWESwitchingKeyPreparedToRef { - fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>; -} - -impl LWEToGLWESwitchingKeyPreparedToRef for LWEToGLWESwitchingKeyPrepared +impl GGLWEPreparedToRef for LWEToGLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, + GLWESwitchingKeyPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> { - LWEToGLWESwitchingKeyPrepared(self.0.to_ref()) + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + self.0.to_ref() } } -pub trait LWEToGLWESwitchingKeyPreparedToMut { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>; -} - -impl LWEToGLWESwitchingKeyPreparedToMut for LWEToGLWESwitchingKeyPrepared +impl GGLWEPreparedToMut for LWEToGLWESwitchingKeyPrepared where - GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> { - LWEToGLWESwitchingKeyPrepared(self.0.to_mut()) + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + self.0.to_mut() + } +} + +impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKeyPrepared { + fn input_degree(&mut self) -> &mut Degree { + &mut self.0.input_degree + } + + fn output_degree(&mut self) -> &mut Degree { + &mut self.0.output_degree } } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index dae6157..78fb717 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -16,6 +16,7 @@ pub use operations::*; pub mod layouts; pub use automorphism::*; pub use conversion::*; +pub use decryption::*; pub use dist::*; pub use encryption::*; pub use external_product::*; diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index d1e95f9..a479080 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, - GLWESwitchingKey, Rank, TensorKey, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, + GLWESecret, GLWESwitchingKey, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared, @@ -258,8 +258,8 @@ where ( GLWESwitchingKey { key: data, - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), }, scratch, ) @@ -279,8 +279,8 @@ where ( GLWESwitchingKeyPrepared { key: data, - sk_in_n: 0, - sk_out_n: 0, + input_degree: Degree(0), + output_degree: Degree(0), }, scratch, ) @@ -292,7 +292,7 @@ where M: ModuleN, { assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_glwe_switching_key(module, infos); + let (data, scratch) = self.take_gglwe(module, infos); (AutomorphismKey { key: data, p: 0 }, scratch) } @@ -306,7 +306,7 @@ where M: ModuleN + VmpPMatBytesOf, { assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_gglwe_switching_key_prepared(module, infos); + let (data, scratch) = self.take_gglwe_prepared(module, infos); (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } @@ -321,21 +321,21 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); + let mut ksk_infos: GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_glwe_switching_key(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); scratch = s; keys.push(gglwe); } @@ -354,21 +354,21 @@ where "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Self = self; - let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); + let mut ksk_infos: GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_gglwe_switching_key_prepared(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos); scratch = s; keys.push(gglwe); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 8bdf034..61fbcb1 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -18,8 +18,8 @@ backend_test_suite!( glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch -// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, -// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, // GLWE Automorphism // glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, // glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, @@ -35,10 +35,10 @@ gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryptio gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, -// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching -// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, -// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, // GGLWE External Product // gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, // gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, @@ -46,11 +46,11 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_ // gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, // gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // GGSW Encryption -// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, -// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, // GGSW Keyswitching -// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, -// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, // GGSW External Product // ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, // ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, @@ -58,7 +58,7 @@ gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_ // ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, // ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE -// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, // lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, } 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 487f1f7..4f84616 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -5,7 +5,8 @@ use poulpy_hal::{ }; use crate::{ - GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, + AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, + GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPrepare, @@ -16,7 +17,7 @@ use crate::{ pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: GGLWEEncryptSk + Module: AutomorphismKeyEncryptSk + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -82,7 +83,6 @@ where sk_out_prepared.prepare(module, &sk_out); atk.key - .key .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); } } @@ -90,7 +90,7 @@ where pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: GGLWEEncryptSk + Module: AutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch + GLWESecretPreparedAlloc + GLWESecretPrepare @@ -125,7 +125,9 @@ where let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKeyCompressed::encrypt_sk_tmp_bytes( + module, &atk_infos, + )); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -153,7 +155,6 @@ where atk.decompress(module, &atk_compressed); atk.key - .key .assert_noise(module, &sk_out_prepared, &sk.data, SIGMA); } } 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 06167d1..60eaf0d 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -31,7 +31,7 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { - let n = module.n(); + let n: usize = module.n(); let base2k: usize = 12; let k_ksk: usize = 54; let dsize: usize = k_ksk / base2k; 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 647f68e..48573db 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace, VecZnxSwitchRing, }, - layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxDft}, + layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft}, source::Source, }; @@ -73,8 +73,8 @@ where let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); - let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); + let mut sk_ij_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big: VecZnxBig, BE> = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); @@ -126,8 +126,6 @@ where + VecZnxFillUniform + VecZnxCopy + VecZnxSwitchRing, - // + VecZnxNormalizeInplaceImpl - // + VecZnxNormalizeImpl, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -170,8 +168,8 @@ where let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); - let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); - let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); + let mut sk_ij_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(1, 1); + let mut sk_ij_big: VecZnxBig, BE> = module.vec_znx_big_alloc(1, 1); let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, BE> = module.vec_znx_dft_alloc(rank, 1); 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 08d268c..9646909 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -9,7 +9,8 @@ use crate::{ encryption::SIGMA, layouts::{ GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, + GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, + TensorKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, @@ -26,7 +27,9 @@ where + GLWESecretPrepare + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise, + + GGSWNoise + + GLWESwitchingKeyPrepare + + TensorKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -145,8 +148,12 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); - let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); + + let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch( module, @@ -188,7 +195,9 @@ where + GLWESecretPrepare + TensorKeyPreparedAlloc + GLWESwitchingKeyPreparedAlloc - + GGSWNoise, + + GGSWNoise + + GLWESwitchingKeyPrepare + + TensorKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -259,6 +268,7 @@ where let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); sk_in_prepared.prepare(module, &sk_in); @@ -295,8 +305,12 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); - let tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); + + let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); 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 6cbe7c3..146ea32 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -1,14 +1,15 @@ use poulpy_hal::{ - api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform}, layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - ScratchTakeCore, + GLWEEncryptSk, GLWEKeyswitch, GLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKey, + GLWESwitchingKeyLayout, GLWESwitchingKeyPrepare, GLWESwitchingKeyPreparedAlloc, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -17,7 +18,15 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_glwe_keyswitch(module: &Module) where - Module:, + Module: VecZnxFillUniform + + GLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + GLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyPrepare + + GLWESwitchingKeyPreparedAlloc + + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -48,7 +57,7 @@ where rank: rank_out.into(), }; - let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { + let ksk: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -58,7 +67,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk); let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); @@ -70,18 +79,22 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) - | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &key_apply), + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ksk), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); + + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_in.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); ksk.encrypt_sk( module, @@ -101,7 +114,9 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -126,7 +141,15 @@ where pub fn test_glwe_keyswitch_inplace(module: &Module) where - Module:, + Module: VecZnxFillUniform + + GLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + GLWEKeyswitch + + GLWESecretPreparedAlloc + + GLWESecretPrepare + + GLWESwitchingKeyPrepare + + GLWESwitchingKeyPreparedAlloc + + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -148,7 +171,7 @@ where rank: rank.into(), }; - let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { + let ksk_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -158,7 +181,7 @@ where rank_out: rank.into(), }; - let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_infos); let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); @@ -169,20 +192,24 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &key_apply_infos) + GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | GLWE::keyswitch_inplace_tmp_bytes(module, &glwe_out_infos, &key_apply_infos), + | GLWE::keyswitch_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ksk_infos), ); let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let sk_in_prepared: GLWESecretPrepared, BE> = sk_in.prepare_alloc(module, scratch.borrow()); + + let mut sk_in_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_in_prepared.prepare(module, &sk_in); let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, BE> = sk_out.prepare_alloc(module, scratch.borrow()); - key_apply.encrypt_sk( + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_out_prepared.prepare(module, &sk_out); + + ksk.encrypt_sk( module, &sk_in, &sk_out, @@ -200,7 +227,9 @@ where scratch.borrow(), ); - let ksk_prepared: GLWESwitchingKeyPrepared, BE> = key_apply.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWESwitchingKeyPrepared, BE> = + GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); 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 0722b98..a709747 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -5,15 +5,21 @@ use poulpy_hal::{ }; use crate::{ - ScratchTakeCore, + LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore, layouts::{ - LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::LWESwitchingKeyPrepared, + LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPrepare, + LWESwitchingKeyPreparedAlloc, prepared::LWESwitchingKeyPrepared, }, }; pub fn test_lwe_keyswitch(module: &Module) where - Module:, + Module: LWEKeySwitch + + LWESwitchingKeyEncrypt + + LWEEncryptSk + + LWESwitchingKeyPreparedAlloc + + LWEDecrypt + + LWESwitchingKeyPrepare, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -89,7 +95,8 @@ where let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); - let ksk_prepared: LWESwitchingKeyPrepared, BE> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: LWESwitchingKeyPrepared, BE> = LWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 3382774..6653f90 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, marker::PhantomData, ptr::NonNull}; +use std::{fmt::{Debug, Display}, marker::PhantomData, ptr::NonNull}; use rand_distr::num_traits::Zero; @@ -9,8 +9,8 @@ use crate::{ #[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { - type ScalarBig: Copy + Zero + Display; - type ScalarPrep: Copy + Zero + Display; + type ScalarBig: Copy + Zero + Display + Debug; + type ScalarPrep: Copy + Zero + Display + Debug; type Handle: 'static; fn layout_prep_word_count() -> usize; fn layout_big_word_count() -> usize; diff --git a/poulpy-hal/src/layouts/znx_base.rs b/poulpy-hal/src/layouts/znx_base.rs index 7ca75b2..45e30d4 100644 --- a/poulpy-hal/src/layouts/znx_base.rs +++ b/poulpy-hal/src/layouts/znx_base.rs @@ -1,3 +1,5 @@ +use std::fmt::{Debug, Display}; + use crate::{ layouts::{Backend, Data, DataMut, DataRef}, source::Source, @@ -48,7 +50,7 @@ pub trait DataViewMut: DataView { } pub trait ZnxView: ZnxInfos + DataView { - type Scalar: Copy + Zero; + type Scalar: Copy + Zero + Display + Debug; /// Returns a non-mutable pointer to the underlying coefficients array. fn as_ptr(&self) -> *const Self::Scalar { From b91918dcaff4605916ebf93ce54408f3479f077e Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 16:10:49 +0200 Subject: [PATCH 48/60] external product tests --- poulpy-core/src/external_product/gglwe_ksk.rs | 8 +- poulpy-core/src/external_product/ggsw_ct.rs | 8 +- poulpy-core/src/tests/mod.rs | 12 +- .../test_suite/external_product/gglwe_ksk.rs | 158 +++++------------- .../test_suite/external_product/ggsw_ct.rs | 158 +++++------------- .../test_suite/external_product/glwe_ct.rs | 138 +++++---------- poulpy-core/src/tests/test_suite/mod.rs | 2 +- 7 files changed, 145 insertions(+), 339 deletions(-) diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index c6ee4a6..f79ff6e 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -95,13 +95,7 @@ where impl GGLWEExternalProduct for Module where Self: GLWEExternalProduct {} impl GLWESwitchingKey> { - pub fn external_product_tmp_bytes( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGLWEInfos, A: GGLWEInfos, diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index d3a59a6..f9659fe 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -97,13 +97,7 @@ where impl GGSWExternalProduct for Module where Self: GLWEExternalProduct {} impl GGSW> { - pub fn external_product_tmp_bytes( - &self, - module: &M, - res_infos: &R, - a_infos: &A, - b_infos: &B, - ) -> usize + pub fn external_product_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize where R: GGSWInfos, A: GGSWInfos, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 61fbcb1..2281caf 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -24,8 +24,8 @@ glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswit // glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, // glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // GLWE External Product -// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, -// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace // glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, // glwe_packing => crate::tests::test_suite::test_glwe_packing, @@ -40,8 +40,8 @@ gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption:: gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, // GGLWE External Product -// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, -// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, // GGLWE Automorphism // gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, // gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, @@ -52,8 +52,8 @@ ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_co ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, // GGSW External Product -// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, -// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, // GGSW Automorphism // ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, // ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, 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 a369978..d5f13fb 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 @@ -1,72 +1,34 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_external_product(module: &Module) +pub fn test_gglwe_switching_key_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGLWEExternalProduct + + GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxRotateInplace + + GGSWPrepare + + GGSWPreparedAlloc + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -101,7 +63,7 @@ where rank_out: rank_out.into(), }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -120,14 +82,9 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_in_infos) - | GLWESwitchingKey::external_product_tmp_bytes( - module, - &gglwe_out_infos, - &gglwe_in_infos, - &ggsw_infos, - ) + | GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); @@ -142,7 +99,9 @@ where let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe_in.encrypt_sk( @@ -163,7 +122,8 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw); + ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); @@ -207,48 +167,19 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) +pub fn test_gglwe_switching_key_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGLWEExternalProduct + + GGSWEncryptSk + + GLWESwitchingKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxRotateInplace + + GGSWPrepare + + GGSWPreparedAlloc + + GGLWENoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -273,7 +204,7 @@ where rank_out: rank_out.into(), }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -291,9 +222,9 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_out_infos) - | GLWESwitchingKey::external_product_inplace_tmp_bytes(module, &gglwe_out_infos, &ggsw_infos) + | GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_out_infos, &ggsw_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos), ); @@ -308,7 +239,9 @@ where let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); - let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); + + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank_out.into()); + sk_out_prepared.prepare(module, &sk_out); // gglwe_{s1}(s0) = s0 -> s1 ct_gglwe.encrypt_sk( @@ -329,7 +262,8 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let mut ct_rgsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw); + ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); 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 84a2f68..de080a3 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 @@ -1,74 +1,32 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGSWEncryptSk, GGSWExternalProduct, GGSWNoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_external_product(module: &Module) +pub fn test_ggsw_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + GGSWExternalProduct + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGSWPrepare + + GGSWPreparedAlloc + + VecZnxRotateInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -84,7 +42,7 @@ where let dnum_in: usize = k_in.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_in_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_in_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +51,7 @@ where rank: rank.into(), }; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -102,7 +60,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -127,7 +85,7 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), @@ -135,7 +93,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -155,7 +115,8 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); @@ -190,50 +151,18 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_external_product_inplace(module: &Module) +pub fn test_ggsw_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VecZnxRotateInplace - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + GGSWExternalProduct + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GGSWPrepare + + GGSWPreparedAlloc + + VecZnxRotateInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -247,7 +176,7 @@ where let dnum_in: usize = k_out.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -256,7 +185,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -281,15 +210,17 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) - | GGSW::external_product_inplace_tmp_bytes(module, &ggsw_out_infos, &ggsw_apply_infos), + | GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -309,7 +240,8 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_rhs_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); 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 60026b8..6c9de79 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 @@ -1,66 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxRotateInplace}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned, ZnxViewMut}, source::Source, }; use crate::{ + GGSWEncryptSk, GLWEEncryptSk, GLWEExternalProduct, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_glwe_external_product(module: &Module) +pub fn test_glwe_external_product(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: GGSWEncryptSk + + GGSWPrepare + + GGSWPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEExternalProduct + + GLWEEncryptSk + + GLWENoise + + VecZnxRotateInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 45; @@ -87,7 +56,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -115,7 +84,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos) | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), @@ -123,7 +92,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -143,7 +114,8 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); @@ -176,43 +148,20 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_glwe_external_product_inplace(module: &Module) +pub fn test_glwe_external_product_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: GGSWEncryptSk + + GGSWPrepare + + GGSWPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VecZnxRotateInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEExternalProduct + + GLWEEncryptSk + + GLWENoise + + VecZnxRotateInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -231,7 +180,7 @@ where rank: rank.into(), }; - let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_apply_infos: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_ggsw.into(), @@ -258,15 +207,17 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | GLWE::external_product_inplace_tmp_bytes(module, &glwe_out_infos, &ggsw_apply_infos), + | GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_prepared.prepare(module, &sk); ggsw_apply.encrypt_sk( module, @@ -286,7 +237,8 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let mut ct_ggsw_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply); + ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 9737a54..b0bfff4 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,6 +1,6 @@ // pub mod automorphism; pub mod encryption; -// pub mod external_product; +pub mod external_product; pub mod keyswitch; // mod conversion; From 1e2478852e684fdbefbeaf61b42089dc641312b4 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 16:31:25 +0200 Subject: [PATCH 49/60] automorphism tests --- poulpy-core/src/automorphism/gglwe_atk.rs | 8 +- poulpy-core/src/encryption/gglwe_atk.rs | 4 +- poulpy-core/src/encryption/gglwe_ct.rs | 1 - poulpy-core/src/layouts/prepared/gglwe_atk.rs | 8 +- poulpy-core/src/tests/mod.rs | 12 +- .../test_suite/automorphism/gglwe_atk.rs | 151 ++++----------- .../tests/test_suite/automorphism/ggsw_ct.rs | 179 ++++++------------ .../tests/test_suite/automorphism/glwe_ct.rs | 141 +++++--------- poulpy-core/src/tests/test_suite/mod.rs | 2 +- poulpy-hal/src/layouts/module.rs | 2 +- 10 files changed, 154 insertions(+), 354 deletions(-) diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index b768b81..3c9c9f3 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -86,6 +86,7 @@ where ); let cols_out: usize = (key.rank_out() + 1).into(); + let cols_in: usize = key.rank_in().into(); let p: i64 = a.p(); let p_inv: i64 = self.galois_element_inv(p); @@ -95,7 +96,7 @@ where let a: &GGLWE<&[u8]> = &a.to_ref(); for row in 0..res.dnum().as_usize() { - for col in 0..cols_out { + for col in 0..cols_in { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); let a_ct: GLWE<&[u8]> = a.at(row, col); @@ -133,18 +134,19 @@ where ); let cols_out: usize = (key.rank_out() + 1).into(); + let cols_in: usize = key.rank_in().into(); let p: i64 = res.p(); let p_inv: i64 = self.galois_element_inv(p); { let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); for row in 0..res.dnum().as_usize() { - for col in 0..cols_out { + for col in 0..cols_in { let mut res_tmp: GLWE<&mut [u8]> = res.at_mut(row, col); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) for i in 0..cols_out { - self.vec_znx_automorphism_inplace(p_inv, res_tmp.data_mut(), i, scratch); + self.vec_znx_automorphism_inplace(p, res_tmp.data_mut(), i, scratch); } // Key-switch (-sa + pi_{k}(s), a) to (-pi^{-1}_{k'}(s)a + pi_{k}(s), a) diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 2b9cc06..b7469a2 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -121,7 +121,7 @@ where { let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); - (0..res.rank_out().into()).for_each(|i| { + for i in 0..res.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), @@ -129,7 +129,7 @@ where &sk.data.as_vec_znx(), i, ); - }); + } sk_out_prepared.prepare(self, &sk_out); } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index ba4ae8e..fa80541 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -156,7 +156,6 @@ where // // (-(a*s) + s0, a) // (-(b*s) + s1, b) - for col_i in 0..rank_in { for row_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/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 6f3d4b9..244ab0a 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -173,7 +173,7 @@ impl AutomorphismKeyPrepared, B> { } } -pub trait PrepareAutomorphismKey +pub trait AutomorphismKeyPrepare where Self: GGLWEPrepare, { @@ -194,12 +194,12 @@ where } } -impl PrepareAutomorphismKey for Module where Module: GGLWEPrepare {} +impl AutomorphismKeyPrepare for Module where Module: GGLWEPrepare {} impl AutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: PrepareAutomorphismKey, + M: AutomorphismKeyPrepare, { module.prepare_automorphism_key_tmp_bytes(self) } @@ -209,7 +209,7 @@ impl AutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GetAutomorphismGaloisElement, - M: PrepareAutomorphismKey, + M: AutomorphismKeyPrepare, { module.prepare_automorphism_key(self, other, scratch); } diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 2281caf..f24ff84 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -21,8 +21,8 @@ backend_test_suite!( glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, // GLWE Automorphism -// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, -// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // GLWE External Product glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, @@ -43,8 +43,8 @@ gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::te gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, // GGLWE Automorphism -// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, -// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // GGSW Encryption ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, @@ -55,8 +55,8 @@ ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswit ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, // GGSW Automorphism -// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, -// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, // glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, 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 37b5403..5a7bad6 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -1,71 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxSubScalarInplace}, + layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyAutomorphism, AutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGLWEInfos, GLWEPlaintext, + GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_automorphism_key_automorphism(module: &Module) +pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + Module: AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + AutomorphismKeyAutomorphism + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + SvpPPolBytesOf - + VecZnxDftBytesOf - + VecZnxNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpApplyDftToDftInplace - + VecZnxAddScalarInplace - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxSwitchRing - + SvpPPolAlloc - + VecZnxBigAddInplace - + VecZnxSubScalarInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + GaloisElement + + VecZnxSubScalarInplace + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -119,7 +83,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) | AutomorphismKey::automorphism_tmp_bytes( @@ -153,7 +117,7 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -180,7 +144,8 @@ where ); } - let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto); + sk_auto_dft.prepare(module, &sk_auto); (0..auto_key_out.rank_in().into()).for_each(|col_i| { (0..auto_key_out.dnum().into()).for_each(|row_i| { @@ -222,61 +187,20 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) +pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize + Module: AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + AutomorphismKeyAutomorphism + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxAutomorphismInplace - + VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes + + GaloisElement + VecZnxSubScalarInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxDftImpl - + TakeVecZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl, + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEDecrypt, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -318,10 +242,10 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) - | AutomorphismKey::automorphism_inplace_tmp_bytes(module, &auto_key, &auto_key_apply), + | AutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); @@ -347,7 +271,7 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -370,7 +294,8 @@ where ); } - let sk_auto_dft: GLWESecretPrepared, B> = sk_auto.prepare_alloc(module, scratch.borrow()); + let mut sk_auto_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto); + sk_auto_dft.prepare(module, &sk_auto); (0..auto_key.rank_in().into()).for_each(|col_i| { (0..auto_key.dnum().into()).for_each(|row_i| { 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 7c2a427..4a2b665 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -1,79 +1,36 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace}, + layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared}, + AutomorphismKey, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare, + GLWESecretPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, TensorKeyPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; -pub fn test_ggsw_automorphism(module: &Module) +pub fn test_ggsw_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxFillUniform - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpApplyDftToDft - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxAutomorphism, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + GGSWAutomorphism + + TensorKeyPrepare + + TensorKeyPreparedAlloc + + TensorKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 54; @@ -92,7 +49,7 @@ where let dsize_in: usize = 1; - let ggsw_in_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_in_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -101,7 +58,7 @@ where rank: rank.into(), }; - let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -138,7 +95,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct_in) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) @@ -149,7 +106,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module, @@ -178,11 +137,12 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, BE> = + TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -217,56 +177,22 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_ggsw_automorphism_inplace(module: &Module) +pub fn test_ggsw_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAddScalarInplace - + VecZnxCopy - + VecZnxSubInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigAddSmallInplace - + VecZnxDftCopy - + VecZnxDftAddInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VecZnxFillUniform - + SvpApplyDftToDft - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxAutomorphism, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + Module: GGSWEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPreparedAlloc + + AutomorphismKeyPrepare + + GGSWAutomorphism + + TensorKeyPrepare + + TensorKeyPreparedAlloc + + TensorKeyEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + VecZnxAutomorphismInplace + + GGSWNoise, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 54; @@ -282,7 +208,7 @@ where let dnum_in: usize = k_out.div_euclid(base2k * di); let dsize_in: usize = 1; - let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_out_layout: GGSWLayout = GGSWLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -318,18 +244,20 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) - | GGSW::automorphism_inplace_tmp_bytes(module, &ct, &auto_key, &tensor_key), + | GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); auto_key.encrypt_sk( module, @@ -358,11 +286,12 @@ where scratch.borrow(), ); - let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + let mut auto_key_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, BE> = + TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); 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 02afcb8..22d913d 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -1,69 +1,35 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace, VecZnxFillUniform}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ + AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, + GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, }; -pub fn test_glwe_automorphism(module: &Module) +pub fn test_glwe_automorphism(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GLWEEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEDecrypt + + GLWEAutomorphism + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWENoise + + VecZnxAutomorphismInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_in: usize = 60; @@ -111,7 +77,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct_out) | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) @@ -120,7 +86,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module, @@ -140,7 +108,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, B> = + let mut autokey_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); @@ -167,46 +135,21 @@ where } #[allow(clippy::too_many_arguments)] -pub fn test_glwe_automorphism_inplace(module: &Module) +pub fn test_glwe_automorphism_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes + Module: GLWEEncryptSk + + GLWESecretPrepare + + GLWESecretPreparedAlloc + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxAddScalarInplace - + VecZnxAutomorphismInplace - + VmpPMatAlloc - + VmpPrepare, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + GLWEDecrypt + + GLWEAutomorphism + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWENoise + + VecZnxAutomorphismInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 12; let k_out: usize = 60; @@ -245,16 +188,18 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct) | GLWE::encrypt_sk_tmp_bytes(module, &ct) - | GLWE::automorphism_inplace_tmp_bytes(module, &ct, &autokey), + | GLWE::automorphism_tmp_bytes(module, &ct, &ct, &autokey), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_prepared.prepare(module, &sk); autokey.encrypt_sk( module, @@ -274,7 +219,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: AutomorphismKeyPrepared, B> = + let mut autokey_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index b0bfff4..11af37a 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -1,4 +1,4 @@ -// pub mod automorphism; +pub mod automorphism; pub mod encryption; pub mod external_product; pub mod keyswitch; diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 6653f90..10b04b9 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -114,7 +114,7 @@ where } let g_exp: u64 = - mod_exp_u64(GALOISGENERATOR, (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; + mod_exp_u64(gal_el.unsigned_abs(), (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; g_exp as i64 * gal_el.signum() } } From d4c936e04c756fe5f5e8627f9e6723bb9572dfab Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 17:20:34 +0200 Subject: [PATCH 50/60] conversions tests --- poulpy-core/src/conversion/glwe_to_lwe.rs | 5 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 8 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 8 +- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 12 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 12 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 10 ++ poulpy-core/src/tests/mod.rs | 4 +- .../src/tests/test_suite/conversion.rs | 161 ++++++------------ poulpy-core/src/tests/test_suite/mod.rs | 4 +- 9 files changed, 99 insertions(+), 125 deletions(-) diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 276010d..072f3d5 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -112,14 +112,13 @@ impl LWE { module.lwe_sample_extract(self, a); } - pub fn from_glwe(&self, module: &M, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + pub fn from_glwe(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where - R: LWEToMut, A: GLWEToRef, K: GGLWEPreparedToRef + GGLWEInfos, M: LWEFromGLWE, Scratch: ScratchTakeCore, { - module.lwe_from_glwe(res, a, key, scratch); + module.lwe_from_glwe(self, a, key, scratch); } } diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index 71b96ff..28a9778 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -17,7 +17,7 @@ impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyEncrypt, + M: GLWEToLWESwitchingKeyEncryptSk, { module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos) } @@ -33,7 +33,7 @@ impl GLWEToLWESwitchingKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - M: GLWEToLWESwitchingKeyEncrypt, + M: GLWEToLWESwitchingKeyEncryptSk, S1: LWESecretToRef, S2: GLWESecretToRef, Scratch: ScratchTakeCore, @@ -42,7 +42,7 @@ impl GLWEToLWESwitchingKey { } } -pub trait GLWEToLWESwitchingKeyEncrypt { +pub trait GLWEToLWESwitchingKeyEncryptSk { fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -61,7 +61,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt { R: GGLWEToMut; } -impl GLWEToLWESwitchingKeyEncrypt for Module +impl GLWEToLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index a9856cc..ddf6264 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -16,7 +16,7 @@ impl LWEToGLWESwitchingKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyEncrypt, + M: LWEToGLWESwitchingKeyEncryptSk, { module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos) } @@ -34,14 +34,14 @@ impl LWEToGLWESwitchingKey { ) where S1: LWESecretToRef, S2: GLWESecretPreparedToRef, - M: LWEToGLWESwitchingKeyEncrypt, + M: LWEToGLWESwitchingKeyEncryptSk, Scratch: ScratchTakeCore, { module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } } -pub trait LWEToGLWESwitchingKeyEncrypt { +pub trait LWEToGLWESwitchingKeyEncryptSk { fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; @@ -60,7 +60,7 @@ pub trait LWEToGLWESwitchingKeyEncrypt { R: GGLWEToMut; } -impl LWEToGLWESwitchingKeyEncrypt for Module +impl LWEToGLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index 1bb28ff..bc3ee4b 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; @@ -217,3 +217,13 @@ impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey { &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for GLWEToLWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index dbe95dd..caa676d 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision, }; @@ -217,3 +217,13 @@ impl GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey { &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for LWEToGLWESwitchingKey { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 716eeff..26d617f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -206,3 +206,13 @@ impl GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKe &mut self.0.output_degree } } + +impl GLWESwitchingKeyDegrees for GLWEToLWESwitchingKeyPrepared { + fn input_degree(&self) -> &Degree { + &self.0.input_degree + } + + fn output_degree(&self) -> &Degree { + &self.0.output_degree + } +} diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index f24ff84..595c480 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -59,8 +59,8 @@ ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorph ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, -// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, -// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, } ); // #[cfg(test)] diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 972e0c6..610b61e 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -1,68 +1,33 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned, ZnxView}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView}, source::Source, }; -use crate::layouts::{ - Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, - LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, - prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, +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, + prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared}, + }, }; -pub fn test_lwe_to_glwe(module: &Module) +pub fn test_lwe_to_glwe(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + ZnNormalizeInplace - + ZnFillUniform - + ZnAddNormal, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEFromLWE + + LWEToGLWESwitchingKeyEncryptSk + + GLWEDecrypt + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + LWEEncryptSk + + LWEToGLWESwitchingKeyPreparedAlloc + + LWEToGLWESwitchingKeyPrepare, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22); @@ -89,13 +54,13 @@ where rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos) | GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), @@ -104,7 +69,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); @@ -122,7 +88,7 @@ where ksk.encrypt_sk( module, &sk_lwe, - &sk_glwe, + &sk_glwe_prepared, &mut source_xa, &mut source_xe, scratch.borrow(), @@ -130,7 +96,9 @@ where let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); - let ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared, BE> = + LWEToGLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); @@ -140,46 +108,20 @@ where assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); } -pub fn test_glwe_to_lwe(module: &Module) +pub fn test_glwe_to_lwe(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + ZnNormalizeInplace, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEFromLWE + + GLWEToLWESwitchingKeyEncryptSk + + GLWEEncryptSk + + LWEDecrypt + + GLWEDecrypt + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWEToLWESwitchingKeyEncryptSk + + GLWEToLWESwitchingKeyPrepare + + GLWEToLWESwitchingKeyPreparedAlloc, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let n_glwe: Degree = Degree(module.n() as u32); let n_lwe: Degree = Degree(22); @@ -202,7 +144,7 @@ where rank, }; - let lwe_infos: LWECiphertextLayout = LWECiphertextLayout { + let lwe_infos: LWELayout = LWELayout { n: n_lwe, base2k: Base2K(17), k: TorusPrecision(34), @@ -212,7 +154,7 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos) | LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), @@ -221,7 +163,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe); + sk_glwe_prepared.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe); sk_lwe.fill_ternary_prob(0.5, &mut source_xs); @@ -230,7 +173,7 @@ where let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); - let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos); + let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt, @@ -240,7 +183,7 @@ where scratch.borrow(), ); - let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc_from_infos(&glwe_to_lwe_infos); ksk.encrypt_sk( module, @@ -253,7 +196,9 @@ where let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); - let ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared, BE> = + GLWEToLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); + ksk_prepared.prepare(module, &ksk, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index 11af37a..e3b8e28 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -3,10 +3,10 @@ pub mod encryption; pub mod external_product; pub mod keyswitch; -// mod conversion; +mod conversion; // mod packing; // mod trace; -// pub use conversion::*; +pub use conversion::*; // pub use packing::*; // pub use trace::*; From 749552801cc12e7847ff09d6f702381c3ad95905 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 17:29:23 +0200 Subject: [PATCH 51/60] packing test --- poulpy-core/src/operations/glwe.rs | 8 ++ poulpy-core/src/tests/mod.rs | 2 +- poulpy-core/src/tests/test_suite/mod.rs | 4 +- poulpy-core/src/tests/test_suite/packing.rs | 99 ++++++--------------- 4 files changed, 40 insertions(+), 73 deletions(-) diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 48e67ca..417e5f2 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -179,6 +179,8 @@ where } } +impl GLWERotate for Module where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace {} + pub trait GLWERotate where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace, @@ -253,6 +255,8 @@ where } } +impl GLWECopy for Module where Self: ModuleN + VecZnxCopy {} + pub trait GLWECopy where Self: ModuleN + VecZnxCopy, @@ -278,6 +282,8 @@ where } } +impl GLWEShift for Module where Self: ModuleN + VecZnxRshInplace {} + pub trait GLWEShift where Self: ModuleN + VecZnxRshInplace, @@ -308,6 +314,8 @@ impl GLWE> { } } +impl GLWENormalize for Module where Self: ModuleN + VecZnxNormalize + VecZnxNormalizeInplace {} + pub trait GLWENormalize where Self: ModuleN + VecZnxNormalize + VecZnxNormalizeInplace, diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 595c480..291a94a 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -28,7 +28,7 @@ glwe_external_product => crate::tests::test_suite::external_product::test_glwe_e glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace // glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, -// glwe_packing => crate::tests::test_suite::test_glwe_packing, +glwe_packing => crate::tests::test_suite::test_glwe_packing, // GGLWE Encryption gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index e3b8e28..f6ee65e 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -4,9 +4,9 @@ pub mod external_product; pub mod keyswitch; mod conversion; -// mod packing; +mod packing; // mod trace; pub use conversion::*; -// pub use packing::*; +pub use packing::*; // pub use trace::*; diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index fdfbd57..7e132b2 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -1,78 +1,34 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use crate::{ - GLWEOperations, GLWEPacker, + AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, + GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; -pub fn test_glwe_packing(module: &Module) +pub fn test_glwe_packing(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxAutomorphism - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxNegateInplace - + VecZnxRshInplace - + VecZnxRotateInplace - + VecZnxBigNormalize - + VecZnxDftApply - + VecZnxRotate - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxAutomorphismInplace - + VecZnxCopy, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + Module: GLWEEncryptSk + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + + GLWEPacking + + GLWESecretPrepare + + GLWESecretPreparedAlloc + + GLWESub + + GLWEDecrypt + + GLWERotate, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); @@ -104,7 +60,7 @@ where dnum: dnum.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), @@ -112,7 +68,9 @@ where let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_dft.prepare(module, &sk); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; @@ -124,7 +82,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = 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( @@ -135,13 +93,14 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let mut atk_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp); + atk_prepared.prepare(module, &tmp, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); let log_batch: usize = 0; - let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); + let mut packer: GLWEPacker = GLWEPacker::alloc(&glwe_out_infos, log_batch); let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); @@ -166,7 +125,7 @@ where scratch.borrow(), ); - pt.rotate_inplace(module, -(1 << log_batch), scratch.borrow()); // X^-batch * pt + module.glwe_rotate_inplace(-(1 << log_batch), &mut pt, scratch.borrow()); // X^-batch * pt if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); @@ -190,7 +149,7 @@ where res.decrypt(module, &mut pt, &sk_dft, scratch.borrow()); - pt.sub_inplace_ab(module, &pt_want); + module.glwe_sub_inplace(&mut pt, &pt_want); let noise_have: f64 = pt.std().log2(); From 77b49ea5309d57d77aa31e9ac9045dfda99a757c Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Mon, 20 Oct 2025 17:34:30 +0200 Subject: [PATCH 52/60] trace test --- poulpy-core/src/tests/mod.rs | 182 +++++++++++----------- poulpy-core/src/tests/test_suite/mod.rs | 4 +- poulpy-core/src/tests/test_suite/trace.rs | 89 ++++------- poulpy-hal/src/layouts/module.rs | 12 +- 4 files changed, 129 insertions(+), 158 deletions(-) diff --git a/poulpy-core/src/tests/mod.rs b/poulpy-core/src/tests/mod.rs index 291a94a..dd16db0 100644 --- a/poulpy-core/src/tests/mod.rs +++ b/poulpy-core/src/tests/mod.rs @@ -27,7 +27,7 @@ glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_a glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace -// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, glwe_packing => crate::tests::test_suite::test_glwe_packing, // GGLWE Encryption gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, @@ -63,119 +63,119 @@ glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, } ); -// #[cfg(test)] -// backend_test_suite!( -// mod cpu_ref, -// backend = poulpy_backend::cpu_fft64_ref::FFT64Ref, -// size = 1<<8, -// tests = { -// GLWE Encryption -// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, -// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, -// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, -// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +#[cfg(test)] +backend_test_suite!( + mod cpu_ref, + backend = poulpy_backend::cpu_fft64_ref::FFT64Ref, + size = 1<<8, + tests = { + //GLWE Encryption + glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, + glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, + glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, + glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch -// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, -// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, + glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, // GLWE Automorphism -// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, -// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // GLWE External Product -// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, -// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace -// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, -// glwe_packing => crate::tests::test_suite::test_glwe_packing, +glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +glwe_packing => crate::tests::test_suite::test_glwe_packing, // GGLWE Encryption -// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, -// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, -// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, -// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, -// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, -// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, +gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, +gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching -// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, -// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, // GGLWE External Product -// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, -// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, // GGLWE Automorphism -// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, -// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // GGSW Encryption -// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, -// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, // GGSW Keyswitching -// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, -// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, // GGSW External Product -// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, -// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, // GGSW Automorphism -// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, -// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE -// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, -// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, -// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, -// } -// ); -// -// #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] -// #[cfg(test)] -// backend_test_suite!( -// mod cpu_avx, -// backend = poulpy_backend::cpu_fft64_avx::FFT64Avx, -// size = 1<<8, -// tests = { -// GLWE Encryption -// glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, -// glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, -// glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, -// glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, +lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +} + ); + +#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] +#[cfg(test)] +backend_test_suite!( + mod cpu_avx, + backend = poulpy_backend::cpu_fft64_avx::FFT64Avx, + size = 1<<8, + tests = { + //GLWE Encryption + glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, + glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk, + glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk, + glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk, // GLWE Keyswitch -// glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, -// glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, +glwe_keyswitch => crate::tests::test_suite::keyswitch::test_glwe_keyswitch, +glwe_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_glwe_keyswitch_inplace, // GLWE Automorphism -// glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, -// glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, +glwe_automorphism => crate::tests::test_suite::automorphism::test_glwe_automorphism, +glwe_automorphism_inplace => crate::tests::test_suite::automorphism::test_glwe_automorphism_inplace, // GLWE External Product -// glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, -// glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, +glwe_external_product => crate::tests::test_suite::external_product::test_glwe_external_product, +glwe_external_product_inplace => crate::tests::test_suite::external_product::test_glwe_external_product_inplace, // GLWE Trace -// glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, -// glwe_packing => crate::tests::test_suite::test_glwe_packing, +glwe_trace_inplace => crate::tests::test_suite::test_glwe_trace_inplace, +glwe_packing => crate::tests::test_suite::test_glwe_packing, // GGLWE Encryption -// gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, -// gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, -// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, -// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, -// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, -// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, +gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, +gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, +gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk, +gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk, +gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, +gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // GGLWE Keyswitching -// gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, -// gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, +gglwe_switching_key_keyswitch => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch, +gglwe_switching_key_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_gglwe_switching_key_keyswitch_inplace, // GGLWE External Product -// gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, -// gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, +gglwe_switching_key_external_product => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product, +gglwe_switching_key_external_product_inplace => crate::tests::test_suite::external_product::test_gglwe_switching_key_external_product_inplace, // GGLWE Automorphism -// gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, -// gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, +gglwe_automorphism_key_automorphism => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism, +gglwe_automorphism_key_automorphism_inplace => crate::tests::test_suite::automorphism::test_gglwe_automorphism_key_automorphism_inplace, // GGSW Encryption -// ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, -// ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, +ggsw_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_encrypt_sk, +ggsw_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_ggsw_compressed_encrypt_sk, // GGSW Keyswitching -// ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, -// ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, +ggsw_keyswitch => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch, +ggsw_keyswitch_inplace => crate::tests::test_suite::keyswitch::test_ggsw_keyswitch_inplace, // GGSW External Product -// ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, -// ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, +ggsw_external_product => crate::tests::test_suite::external_product::test_ggsw_external_product, +ggsw_external_product_inplace => crate::tests::test_suite::external_product::test_ggsw_external_product_inplace, // GGSW Automorphism -// ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, -// ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, +ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorphism, +ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace, // LWE -// lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, -// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, -// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, -// } -// ); +lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch, +glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe, +lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe, +} + ); diff --git a/poulpy-core/src/tests/test_suite/mod.rs b/poulpy-core/src/tests/test_suite/mod.rs index f6ee65e..624b9c3 100644 --- a/poulpy-core/src/tests/test_suite/mod.rs +++ b/poulpy-core/src/tests/test_suite/mod.rs @@ -5,8 +5,8 @@ pub mod keyswitch; mod conversion; mod packing; -// mod trace; +mod trace; pub use conversion::*; pub use packing::*; -// pub use trace::*; +pub use trace::*; diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 932b401..395a961 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -1,76 +1,38 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigAddInplace, - VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, - }, - layouts::{Backend, Module, ScratchOwned, ZnxView, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, - }, + api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxNormalizeInplace, VecZnxSubInplace}, + layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView, ZnxViewMut}, source::Source, }; use crate::{ + AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, encryption::SIGMA, + glwe_trace::GLWETrace, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWEInfos, - prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout, + GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, LWEInfos, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::var_noise_gglwe_product, }; -pub fn test_glwe_trace_inplace(module: &Module) +pub fn test_glwe_trace_inplace(module: &Module) where - Module: VecZnxDftBytesOf - + VecZnxAutomorphism - + VecZnxBigAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRshInplace - + VecZnxRotateInplace - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume + Module: GLWETrace + + GLWEEncryptSk + + GLWEDecrypt + + AutomorphismKeyEncryptSk + + AutomorphismKeyPrepare + + AutomorphismKeyPreparedAlloc + VecZnxFillUniform + + GLWESecretPrepare + + GLWESecretPreparedAlloc + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxBigBytesOf - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxNormalizeTmpBytes - + VecZnxAddScalarInplace - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxBigNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxCopy, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl, + + VecZnxNormalizeInplace, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchAvailable + ScratchTakeCore, { let base2k: usize = 8; let k: usize = 54; @@ -106,16 +68,18 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc( + let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) - | GLWE::trace_inplace_tmp_bytes(module, &glwe_out_infos, &key_infos), + | GLWE::trace_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &key_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); - let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); + + let mut sk_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk); + sk_dft.prepare(module, &sk); let mut data_want: Vec = vec![0i64; n]; @@ -134,7 +98,7 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, B>> = 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| { @@ -146,7 +110,8 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let mut atk_prepared: AutomorphismKeyPrepared, BE> = AutomorphismKeyPrepared::alloc_from_infos(module, &tmp); + atk_prepared.prepare(module, &tmp, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index 10b04b9..bc074dd 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -1,4 +1,8 @@ -use std::{fmt::{Debug, Display}, marker::PhantomData, ptr::NonNull}; +use std::{ + fmt::{Debug, Display}, + marker::PhantomData, + ptr::NonNull, +}; use rand_distr::num_traits::Zero; @@ -113,8 +117,10 @@ where panic!("cannot invert 0") } - let g_exp: u64 = - mod_exp_u64(gal_el.unsigned_abs(), (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1) as u64; + let g_exp: u64 = mod_exp_u64( + gal_el.unsigned_abs(), + (self.cyclotomic_order() - 1) as usize, + ) & (self.cyclotomic_order() - 1) as u64; g_exp as i64 * gal_el.signum() } } From db3b20b8f6cedab32186d0f0e994cc30fa9b861d Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 21 Oct 2025 10:17:52 +0200 Subject: [PATCH 53/60] Traits reduction, file + structs renaming --- poulpy-core/benches/keyswitch_glwe_fft64.rs | 5 +- poulpy-core/src/automorphism/gglwe_atk.rs | 29 ++-- poulpy-core/src/automorphism/ggsw_ct.rs | 10 +- poulpy-core/src/automorphism/glwe_ct.rs | 36 ++-- poulpy-core/src/conversion/glwe_to_lwe.rs | 2 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 2 +- .../src/decryption/{glwe_ct.rs => glwe.rs} | 0 .../src/decryption/{lwe_ct.rs => lwe.rs} | 0 poulpy-core/src/decryption/mod.rs | 8 +- .../compressed/{gglwe_ct.rs => gglwe.rs} | 7 +- .../compressed/{ggsw_ct.rs => ggsw.rs} | 4 +- ...{gglwe_atk.rs => glwe_automorphism_key.rs} | 23 +-- .../src/encryption/compressed/glwe_ct.rs | 5 +- .../{gglwe_ksk.rs => glwe_switching_key.rs} | 7 +- .../{gglwe_tsk.rs => glwe_tensor_key.rs} | 13 +- poulpy-core/src/encryption/compressed/mod.rs | 20 +-- .../src/encryption/{gglwe_ct.rs => gglwe.rs} | 5 +- .../src/encryption/{ggsw_ct.rs => ggsw.rs} | 5 +- .../src/encryption/{glwe_ct.rs => glwe.rs} | 91 +++++----- ...{gglwe_atk.rs => glwe_automorphism_key.rs} | 15 +- .../{glwe_pk.rs => glwe_public_key.rs} | 10 +- .../{gglwe_ksk.rs => glwe_switching_key.rs} | 6 +- .../{gglwe_tsk.rs => glwe_tensor_key.rs} | 5 +- ...we_ksk.rs => glwe_to_lwe_switching_key.rs} | 11 +- .../src/encryption/{lwe_ct.rs => lwe.rs} | 0 .../{lwe_ksk.rs => lwe_switching_key.rs} | 6 +- ...we_ksk.rs => lwe_to_glwe_switching_key.rs} | 4 +- poulpy-core/src/encryption/mod.rs | 44 ++--- .../{gglwe_ksk.rs => gglwe.rs} | 43 ++++- poulpy-core/src/external_product/gglwe_atk.rs | 46 ----- .../external_product/{ggsw_ct.rs => ggsw.rs} | 0 .../external_product/{glwe_ct.rs => glwe.rs} | 0 poulpy-core/src/external_product/mod.rs | 13 +- poulpy-core/src/glwe_packing.rs | 22 ++- poulpy-core/src/glwe_trace.rs | 13 +- .../keyswitching/{gglwe_ct.rs => gglwe.rs} | 2 +- .../src/keyswitching/{ggsw_ct.rs => ggsw.rs} | 2 +- .../src/keyswitching/{glwe_ct.rs => glwe.rs} | 0 .../src/keyswitching/{lwe_ct.rs => lwe.rs} | 6 +- poulpy-core/src/keyswitching/mod.rs | 16 +- .../compressed/{gglwe_ct.rs => gglwe.rs} | 0 .../compressed/{ggsw_ct.rs => ggsw.rs} | 0 .../compressed/{glwe_ct.rs => glwe.rs} | 0 ...{gglwe_atk.rs => glwe_automorphism_key.rs} | 77 +++------ .../{gglwe_ksk.rs => glwe_switching_key.rs} | 0 .../{gglwe_tsk.rs => glwe_tensor_key.rs} | 44 ++--- ...we_ksk.rs => glwe_to_lwe_switching_key.rs} | 0 .../layouts/compressed/{lwe_ct.rs => lwe.rs} | 0 .../{lwe_ksk.rs => lwe_switching_key.rs} | 0 ...we_ksk.rs => lwe_to_glwe_switching_key.rs} | 0 poulpy-core/src/layouts/compressed/mod.rs | 40 ++--- .../src/layouts/{gglwe_ct.rs => gglwe.rs} | 0 .../src/layouts/{ggsw_ct.rs => ggsw.rs} | 0 .../src/layouts/{glwe_ct.rs => glwe.rs} | 0 ...{gglwe_atk.rs => glwe_automorphism_key.rs} | 13 +- .../layouts/{glwe_pt.rs => glwe_plaintext.rs} | 0 .../{glwe_pk.rs => glwe_public_key.rs} | 61 ++----- .../layouts/{glwe_sk.rs => glwe_secret.rs} | 0 .../{gglwe_ksk.rs => glwe_switching_key.rs} | 0 .../{gglwe_tsk.rs => glwe_tensor_key.rs} | 0 ...we_ksk.rs => glwe_to_lwe_switching_key.rs} | 0 poulpy-core/src/layouts/{lwe_ct.rs => lwe.rs} | 0 .../layouts/{lwe_pt.rs => lwe_plaintext.rs} | 0 .../src/layouts/{lwe_sk.rs => lwe_secret.rs} | 0 .../{lwe_ksk.rs => lwe_switching_key.rs} | 0 ...we_ksk.rs => lwe_to_glwe_switching_key.rs} | 0 poulpy-core/src/layouts/mod.rs | 60 +++---- .../prepared/{gglwe_ct.rs => gglwe.rs} | 82 +++++---- .../layouts/prepared/{ggsw_ct.rs => ggsw.rs} | 72 ++++---- poulpy-core/src/layouts/prepared/glwe.rs | 161 ++++++++++++++++++ ...{gglwe_atk.rs => glwe_automorphism_key.rs} | 123 ++++++------- .../{glwe_pk.rs => glwe_public_key.rs} | 110 +++++------- .../prepared/{glwe_sk.rs => glwe_secret.rs} | 99 +++++------ .../{gglwe_ksk.rs => glwe_switching_key.rs} | 47 +++-- .../{gglwe_tsk.rs => glwe_tensor_key.rs} | 59 +++---- ...we_ksk.rs => glwe_to_lwe_switching_key.rs} | 43 ++--- .../{lwe_ksk.rs => lwe_switching_key.rs} | 41 ++--- ...we_ksk.rs => lwe_to_glwe_switching_key.rs} | 43 ++--- poulpy-core/src/layouts/prepared/mod.rs | 42 ++--- .../src/noise/{gglwe_ct.rs => gglwe.rs} | 0 poulpy-core/src/noise/{ggsw_ct.rs => ggsw.rs} | 0 poulpy-core/src/noise/{glwe_ct.rs => glwe.rs} | 0 poulpy-core/src/noise/mod.rs | 12 +- poulpy-core/src/scratch.rs | 48 ++++-- poulpy-core/src/tests/serialization.rs | 11 +- .../test_suite/automorphism/gglwe_atk.rs | 32 ++-- .../tests/test_suite/automorphism/ggsw_ct.rs | 28 ++- .../tests/test_suite/automorphism/glwe_ct.rs | 26 ++- .../src/tests/test_suite/conversion.rs | 17 +- .../tests/test_suite/encryption/gglwe_atk.rs | 15 +- .../tests/test_suite/encryption/gglwe_ct.rs | 13 +- .../tests/test_suite/encryption/ggsw_ct.rs | 7 +- .../tests/test_suite/encryption/glwe_ct.rs | 21 +-- .../tests/test_suite/encryption/glwe_tsk.rs | 15 +- .../test_suite/external_product/gglwe_ksk.rs | 11 +- .../test_suite/external_product/ggsw_ct.rs | 10 +- .../test_suite/external_product/glwe_ct.rs | 11 +- .../tests/test_suite/keyswitch/gglwe_ct.rs | 11 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 19 +-- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 12 +- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 12 +- poulpy-core/src/tests/test_suite/packing.rs | 17 +- poulpy-core/src/tests/test_suite/trace.rs | 17 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 4 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 4 +- .../src/tfhe/circuit_bootstrapping/key.rs | 8 +- 106 files changed, 1026 insertions(+), 1108 deletions(-) rename poulpy-core/src/decryption/{glwe_ct.rs => glwe.rs} (100%) rename poulpy-core/src/decryption/{lwe_ct.rs => lwe.rs} (100%) rename poulpy-core/src/encryption/compressed/{gglwe_ct.rs => gglwe.rs} (97%) rename poulpy-core/src/encryption/compressed/{ggsw_ct.rs => ggsw.rs} (96%) rename poulpy-core/src/encryption/compressed/{gglwe_atk.rs => glwe_automorphism_key.rs} (79%) rename poulpy-core/src/encryption/compressed/{gglwe_ksk.rs => glwe_switching_key.rs} (94%) rename poulpy-core/src/encryption/compressed/{gglwe_tsk.rs => glwe_tensor_key.rs} (92%) rename poulpy-core/src/encryption/{gglwe_ct.rs => gglwe.rs} (97%) rename poulpy-core/src/encryption/{ggsw_ct.rs => ggsw.rs} (95%) rename poulpy-core/src/encryption/{glwe_ct.rs => glwe.rs} (88%) rename poulpy-core/src/encryption/{gglwe_atk.rs => glwe_automorphism_key.rs} (90%) rename poulpy-core/src/encryption/{glwe_pk.rs => glwe_public_key.rs} (84%) rename poulpy-core/src/encryption/{gglwe_ksk.rs => glwe_switching_key.rs} (96%) rename poulpy-core/src/encryption/{gglwe_tsk.rs => glwe_tensor_key.rs} (96%) rename poulpy-core/src/encryption/{glwe_to_lwe_ksk.rs => glwe_to_lwe_switching_key.rs} (90%) rename poulpy-core/src/encryption/{lwe_ct.rs => lwe.rs} (100%) rename poulpy-core/src/encryption/{lwe_ksk.rs => lwe_switching_key.rs} (96%) rename poulpy-core/src/encryption/{lwe_to_glwe_ksk.rs => lwe_to_glwe_switching_key.rs} (97%) rename poulpy-core/src/external_product/{gglwe_ksk.rs => gglwe.rs} (74%) delete mode 100644 poulpy-core/src/external_product/gglwe_atk.rs rename poulpy-core/src/external_product/{ggsw_ct.rs => ggsw.rs} (100%) rename poulpy-core/src/external_product/{glwe_ct.rs => glwe.rs} (100%) rename poulpy-core/src/keyswitching/{gglwe_ct.rs => gglwe.rs} (99%) rename poulpy-core/src/keyswitching/{ggsw_ct.rs => ggsw.rs} (99%) rename poulpy-core/src/keyswitching/{glwe_ct.rs => glwe.rs} (100%) rename poulpy-core/src/keyswitching/{lwe_ct.rs => lwe.rs} (95%) rename poulpy-core/src/layouts/compressed/{gglwe_ct.rs => gglwe.rs} (100%) rename poulpy-core/src/layouts/compressed/{ggsw_ct.rs => ggsw.rs} (100%) rename poulpy-core/src/layouts/compressed/{glwe_ct.rs => glwe.rs} (100%) rename poulpy-core/src/layouts/compressed/{gglwe_atk.rs => glwe_automorphism_key.rs} (62%) rename poulpy-core/src/layouts/compressed/{gglwe_ksk.rs => glwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/compressed/{gglwe_tsk.rs => glwe_tensor_key.rs} (81%) rename poulpy-core/src/layouts/compressed/{glwe_to_lwe_ksk.rs => glwe_to_lwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/compressed/{lwe_ct.rs => lwe.rs} (100%) rename poulpy-core/src/layouts/compressed/{lwe_ksk.rs => lwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/compressed/{lwe_to_glwe_ksk.rs => lwe_to_glwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/{gglwe_ct.rs => gglwe.rs} (100%) rename poulpy-core/src/layouts/{ggsw_ct.rs => ggsw.rs} (100%) rename poulpy-core/src/layouts/{glwe_ct.rs => glwe.rs} (100%) rename poulpy-core/src/layouts/{gglwe_atk.rs => glwe_automorphism_key.rs} (95%) rename poulpy-core/src/layouts/{glwe_pt.rs => glwe_plaintext.rs} (100%) rename poulpy-core/src/layouts/{glwe_pk.rs => glwe_public_key.rs} (62%) rename poulpy-core/src/layouts/{glwe_sk.rs => glwe_secret.rs} (100%) rename poulpy-core/src/layouts/{gglwe_ksk.rs => glwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/{gglwe_tsk.rs => glwe_tensor_key.rs} (100%) rename poulpy-core/src/layouts/{glwe_to_lwe_ksk.rs => glwe_to_lwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/{lwe_ct.rs => lwe.rs} (100%) rename poulpy-core/src/layouts/{lwe_pt.rs => lwe_plaintext.rs} (100%) rename poulpy-core/src/layouts/{lwe_sk.rs => lwe_secret.rs} (100%) rename poulpy-core/src/layouts/{lwe_ksk.rs => lwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/{lwe_to_glwe_ksk.rs => lwe_to_glwe_switching_key.rs} (100%) rename poulpy-core/src/layouts/prepared/{gglwe_ct.rs => gglwe.rs} (90%) rename poulpy-core/src/layouts/prepared/{ggsw_ct.rs => ggsw.rs} (93%) create mode 100644 poulpy-core/src/layouts/prepared/glwe.rs rename poulpy-core/src/layouts/prepared/{gglwe_atk.rs => glwe_automorphism_key.rs} (67%) rename poulpy-core/src/layouts/prepared/{glwe_pk.rs => glwe_public_key.rs} (54%) rename poulpy-core/src/layouts/prepared/{glwe_sk.rs => glwe_secret.rs} (88%) rename poulpy-core/src/layouts/prepared/{gglwe_ksk.rs => glwe_switching_key.rs} (94%) rename poulpy-core/src/layouts/prepared/{gglwe_tsk.rs => glwe_tensor_key.rs} (93%) rename poulpy-core/src/layouts/prepared/{glwe_to_lwe_ksk.rs => glwe_to_lwe_switching_key.rs} (94%) rename poulpy-core/src/layouts/prepared/{lwe_ksk.rs => lwe_switching_key.rs} (94%) rename poulpy-core/src/layouts/prepared/{lwe_to_glwe_ksk.rs => lwe_to_glwe_switching_key.rs} (94%) rename poulpy-core/src/noise/{gglwe_ct.rs => gglwe.rs} (100%) rename poulpy-core/src/noise/{ggsw_ct.rs => ggsw.rs} (100%) rename poulpy-core/src/noise/{glwe_ct.rs => glwe.rs} (100%) 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)); } From 681ec7e34910b69003cb46903bcce86074b33e34 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 21 Oct 2025 10:22:50 +0200 Subject: [PATCH 54/60] update trait name --- .../compressed/glwe_automorphism_key.rs | 4 ++-- .../compressed/glwe_switching_key.rs | 4 ++-- .../encryption/compressed/glwe_tensor_key.rs | 4 ++-- .../src/encryption/glwe_automorphism_key.rs | 4 ++-- .../src/encryption/glwe_switching_key.rs | 4 ++-- poulpy-core/src/encryption/glwe_tensor_key.rs | 4 ++-- .../encryption/glwe_to_lwe_switching_key.rs | 4 ++-- .../src/encryption/lwe_switching_key.rs | 4 ++-- .../encryption/lwe_to_glwe_switching_key.rs | 4 ++-- poulpy-core/src/layouts/prepared/gglwe.rs | 16 +++++++-------- poulpy-core/src/layouts/prepared/ggsw.rs | 16 +++++++-------- poulpy-core/src/layouts/prepared/glwe.rs | 14 ++++++------- .../layouts/prepared/glwe_automorphism_key.rs | 20 +++++++++---------- .../src/layouts/prepared/glwe_public_key.rs | 18 ++++++++--------- .../src/layouts/prepared/glwe_secret.rs | 14 ++++++------- .../layouts/prepared/glwe_switching_key.rs | 20 +++++++++---------- .../src/layouts/prepared/glwe_tensor_key.rs | 20 +++++++++---------- .../prepared/glwe_to_lwe_switching_key.rs | 20 +++++++++---------- .../src/layouts/prepared/lwe_switching_key.rs | 20 +++++++++---------- .../prepared/lwe_to_glwe_switching_key.rs | 20 +++++++++---------- .../test_suite/automorphism/gglwe_atk.rs | 12 +++++------ .../tests/test_suite/automorphism/ggsw_ct.rs | 16 +++++++-------- .../tests/test_suite/automorphism/glwe_ct.rs | 12 +++++------ .../src/tests/test_suite/conversion.rs | 14 ++++++------- .../tests/test_suite/encryption/gglwe_atk.rs | 6 +++--- .../tests/test_suite/encryption/gglwe_ct.rs | 12 +++++------ .../tests/test_suite/encryption/ggsw_ct.rs | 6 +++--- .../tests/test_suite/encryption/glwe_ct.rs | 14 ++++++------- .../tests/test_suite/encryption/glwe_tsk.rs | 6 +++--- .../test_suite/external_product/gglwe_ksk.rs | 10 +++++----- .../test_suite/external_product/ggsw_ct.rs | 10 +++++----- .../test_suite/external_product/glwe_ct.rs | 10 +++++----- .../tests/test_suite/keyswitch/gglwe_ct.rs | 10 +++++----- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 16 +++++++-------- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 12 +++++------ .../src/tests/test_suite/keyswitch/lwe_ct.rs | 4 ++-- poulpy-core/src/tests/test_suite/packing.rs | 8 ++++---- poulpy-core/src/tests/test_suite/trace.rs | 8 ++++---- 38 files changed, 210 insertions(+), 210 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index 713dd31..9c9046d 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -8,7 +8,7 @@ use crate::{ GGLWECompressedEncryptSk, ScratchTakeCore, layouts::{ GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, - GLWESecretPreparedApi, GLWESecretToRef, LWEInfos, SetGaloisElement, compressed::GLWEAutomorphismKeyCompressed, + GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement, compressed::GLWEAutomorphismKeyCompressed, }, }; @@ -60,7 +60,7 @@ pub trait AutomorphismKeyCompressedEncryptSk { impl AutomorphismKeyCompressedEncryptSk for Module where - Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk + GLWESecretPreparedApi, + Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs index cdc65b1..c492cfd 100644 --- a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs @@ -10,7 +10,7 @@ use crate::{ GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKeyDegreesMut, LWEInfos, compressed::GLWESwitchingKeyCompressed, - prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, + prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; @@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk { impl GLWESwitchingKeyCompressedEncryptSk for Module where - Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedApi + VecZnxSwitchRing, + Self: ModuleN + GGLWECompressedEncryptSk + GLWESecretPreparedFactory + VecZnxSwitchRing, Scratch: ScratchTakeCore, { fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs index 76050b5..9456e89 100644 --- a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ use crate::{ GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, TensorKeyEncryptSk, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedApi, GLWESecretToRef, LWEInfos, Rank, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, Rank, TensorKeyCompressedAtMut, compressed::GLWETensorKeyCompressed, }, }; @@ -72,7 +72,7 @@ where + SvpPPolBytesOf + VecZnxDftBytesOf + VecZnxBigBytesOf - + GLWESecretPreparedApi, + + GLWESecretPreparedFactory, Scratch: ScratchTakeBasic + ScratchTakeCore, { fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index bcd01c7..68782e3 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedApi, + AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement, }, }; @@ -70,7 +70,7 @@ pub trait AutomorphismKeyEncryptSk { impl AutomorphismKeyEncryptSk for Module where - Self: GGLWEEncryptSk + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedApi, + Self: GGLWEEncryptSk + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/glwe_switching_key.rs b/poulpy-core/src/encryption/glwe_switching_key.rs index cf6cf8e..fc19a71 100644 --- a/poulpy-core/src/encryption/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_switching_key.rs @@ -9,7 +9,7 @@ use crate::{ encryption::gglwe::GGLWEEncryptSk, layouts::{ GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, - prepared::GLWESecretPreparedApi, + prepared::GLWESecretPreparedFactory, }, }; @@ -71,7 +71,7 @@ pub trait GLWESwitchingKeyEncryptSk { impl GLWESwitchingKeyEncryptSk for Module where - Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedApi + VecZnxSwitchRing + SvpPrepare, + Self: ModuleN + GGLWEEncryptSk + GLWESecretPreparedFactory + VecZnxSwitchRing + SvpPrepare, Scratch: ScratchTakeCore, { fn glwe_switching_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize diff --git a/poulpy-core/src/encryption/glwe_tensor_key.rs b/poulpy-core/src/encryption/glwe_tensor_key.rs index 21bf474..758afcc 100644 --- a/poulpy-core/src/encryption/glwe_tensor_key.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::{GLWESecretPrepared, GLWESecretPreparedApi}, + prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; @@ -65,7 +65,7 @@ where + GGLWEEncryptSk + VecZnxDftBytesOf + VecZnxBigBytesOf - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA diff --git a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs index d77eb60..36ecff2 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs @@ -9,7 +9,7 @@ use crate::{ layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretToRef, GLWEToLWESwitchingKey, LWEInfos, LWESecret, LWESecretToRef, Rank, - prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, + prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; @@ -65,7 +65,7 @@ impl GLWEToLWESwitchingKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, diff --git a/poulpy-core/src/encryption/lwe_switching_key.rs b/poulpy-core/src/encryption/lwe_switching_key.rs index e06c910..daee764 100644 --- a/poulpy-core/src/encryption/lwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_switching_key.rs @@ -10,7 +10,7 @@ use crate::{ layouts::{ GGLWEInfos, GGLWEToMut, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyDegreesMut, LWEInfos, LWESecret, LWESecretToRef, LWESwitchingKey, Rank, - prepared::{GLWESecretPrepared, GLWESecretPreparedApi}, + prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; @@ -63,7 +63,7 @@ pub trait LWESwitchingKeyEncrypt { impl LWESwitchingKeyEncrypt for Module where - Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedApi + VecZnxAutomorphismInplace, + Self: ModuleN + GLWESwitchingKeyEncryptSk + GLWESecretPreparedFactory + 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_switching_key.rs b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs index 24153b6..588875d 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.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, GLWESecretPreparedApi, GLWESecretPreparedToRef, LWEInfos, LWESecret, + GGLWE, GGLWEInfos, GGLWEToMut, GLWESecret, GLWESecretPreparedFactory, GLWESecretPreparedToRef, LWEInfos, LWESecret, LWESecretToRef, LWEToGLWESwitchingKey, Rank, }, }; @@ -65,7 +65,7 @@ where Self: ModuleN + GGLWEEncryptSk + VecZnxAutomorphismInplace - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxAutomorphismInplaceTmpBytes, Scratch: ScratchTakeCore, { diff --git a/poulpy-core/src/layouts/prepared/gglwe.rs b/poulpy-core/src/layouts/prepared/gglwe.rs index 27e80ee..792adbf 100644 --- a/poulpy-core/src/layouts/prepared/gglwe.rs +++ b/poulpy-core/src/layouts/prepared/gglwe.rs @@ -57,7 +57,7 @@ impl GGLWEInfos for GGLWEPrepared { } } -pub trait GGLWEPreparedAlloc +pub trait GGLWEPreparedFactory where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepare + VmpPrepareTmpBytes, { @@ -178,7 +178,7 @@ where } } -impl GGLWEPreparedAlloc for Module where +impl GGLWEPreparedFactory for Module where Module: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepare + VmpPrepareTmpBytes { } @@ -187,7 +187,7 @@ impl GGLWEPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.alloc_gglwe_prepared_from_infos(infos) } @@ -202,7 +202,7 @@ impl GGLWEPrepared, B> { dsize: Dsize, ) -> Self where - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } @@ -210,7 +210,7 @@ impl GGLWEPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.bytes_of_gglwe_prepared_from_infos(infos) } @@ -225,7 +225,7 @@ impl GGLWEPrepared, B> { dsize: Dsize, ) -> usize where - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } @@ -235,7 +235,7 @@ impl GGLWEPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef, - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.prepare_gglwe(self, other, scratch); } @@ -244,7 +244,7 @@ impl GGLWEPrepared { impl GGLWEPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: GGLWEPreparedAlloc, + M: GGLWEPreparedFactory, { module.prepare_gglwe_tmp_bytes(self) } diff --git a/poulpy-core/src/layouts/prepared/ggsw.rs b/poulpy-core/src/layouts/prepared/ggsw.rs index c2c62a2..3115980 100644 --- a/poulpy-core/src/layouts/prepared/ggsw.rs +++ b/poulpy-core/src/layouts/prepared/ggsw.rs @@ -49,7 +49,7 @@ impl GGSWInfos for GGSWPrepared { } } -pub trait GGSWPreparedAlloc +pub trait GGSWPreparedFactory where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare, { @@ -162,7 +162,7 @@ where } } -impl GGSWPreparedAlloc for Module where +impl GGSWPreparedFactory for Module where Self: GetDegree + VmpPMatAlloc + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare { } @@ -171,14 +171,14 @@ impl GGSWPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGSWInfos, - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.alloc_ggsw_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank) } @@ -186,14 +186,14 @@ impl GGSWPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGSWInfos, - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.bytes_of_ggsw_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize where - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) } @@ -209,7 +209,7 @@ impl GGSWPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGSWInfos, - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.ggsw_prepare_tmp_bytes(infos) } @@ -219,7 +219,7 @@ impl GGSWPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGSWToRef, - M: GGSWPreparedAlloc, + M: GGSWPreparedFactory, { module.ggsw_prepare(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/glwe.rs b/poulpy-core/src/layouts/prepared/glwe.rs index b09ca72..cf7e564 100644 --- a/poulpy-core/src/layouts/prepared/glwe.rs +++ b/poulpy-core/src/layouts/prepared/glwe.rs @@ -36,7 +36,7 @@ impl GLWEInfos for GLWEPrepared { } } -pub trait GLWEPreparedAlloc +pub trait GLWEPreparedFactory where Self: GetDegree + VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply, { @@ -88,20 +88,20 @@ where } } -impl GLWEPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} +impl GLWEPreparedFactory 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, + M: GLWEPreparedFactory, { module.alloc_glwe_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - M: GLWEPreparedAlloc, + M: GLWEPreparedFactory, { module.alloc_glwe_prepared(base2k, k, rank) } @@ -109,14 +109,14 @@ impl GLWEPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - M: GLWEPreparedAlloc, + M: GLWEPreparedFactory, { module.bytes_of_glwe_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where - M: GLWEPreparedAlloc, + M: GLWEPreparedFactory, { module.bytes_of_glwe_prepared(base2k, k, rank) } @@ -126,7 +126,7 @@ impl GLWEPrepared { pub fn prepare(&mut self, module: &M, other: &O) where O: GLWEToRef, - M: GLWEPreparedAlloc, + M: GLWEPreparedFactory, { module.prepare_glwe(self, other); } diff --git a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs index 5790491..0296e0f 100644 --- a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision, }; @@ -65,9 +65,9 @@ impl GGLWEInfos for GLWEAutomorphismKeyPrepared { } } -pub trait GLWEAutomorphismKeyPreparedApi +pub trait GLWEAutomorphismKeyPreparedFactory where - Self: GGLWEPreparedAlloc, + Self: GGLWEPreparedFactory, { fn alloc_automorphism_key_prepared( &self, @@ -147,20 +147,20 @@ where } } -impl GLWEAutomorphismKeyPreparedApi for Module where Module: GGLWEPreparedAlloc {} +impl GLWEAutomorphismKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} impl GLWEAutomorphismKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: GLWEAutomorphismKeyPreparedApi, + M: GLWEAutomorphismKeyPreparedFactory, { 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, + M: GLWEAutomorphismKeyPreparedFactory, { module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } @@ -168,14 +168,14 @@ impl GLWEAutomorphismKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWEAutomorphismKeyPreparedApi, + M: GLWEAutomorphismKeyPreparedFactory, { 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, + M: GLWEAutomorphismKeyPreparedFactory, { module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } @@ -184,7 +184,7 @@ impl GLWEAutomorphismKeyPrepared, B> { impl GLWEAutomorphismKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: GLWEAutomorphismKeyPreparedApi, + M: GLWEAutomorphismKeyPreparedFactory, { module.prepare_automorphism_key_tmp_bytes(self) } @@ -194,7 +194,7 @@ impl GLWEAutomorphismKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GetGaloisElement, - M: GLWEAutomorphismKeyPreparedApi, + M: GLWEAutomorphismKeyPreparedFactory, { module.prepare_automorphism_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/glwe_public_key.rs b/poulpy-core/src/layouts/prepared/glwe_public_key.rs index 8938c93..b20682d 100644 --- a/poulpy-core/src/layouts/prepared/glwe_public_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_public_key.rs @@ -7,7 +7,7 @@ use crate::{ GetDistribution, GetDistributionMut, dist::Distribution, layouts::{ - Base2K, Degree, GLWEInfos, GLWEPrepared, GLWEPreparedAlloc, GLWEPreparedToMut, GLWEPreparedToRef, GLWEToRef, GetDegree, + Base2K, Degree, GLWEInfos, GLWEPrepared, GLWEPreparedFactory, GLWEPreparedToMut, GLWEPreparedToRef, GLWEToRef, GetDegree, LWEInfos, Rank, TorusPrecision, }, }; @@ -54,9 +54,9 @@ impl GLWEInfos for GLWEPublicKeyPrepared { } } -pub trait GLWEPublicKeyPreparedAlloc +pub trait GLWEPublicKeyPreparedFactory where - Self: GetDegree + GLWEPreparedAlloc, + Self: GetDegree + GLWEPreparedFactory, { fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { @@ -93,20 +93,20 @@ where } } -impl GLWEPublicKeyPreparedAlloc for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} +impl GLWEPublicKeyPreparedFactory for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} impl GLWEPublicKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GLWEInfos, - M: GLWEPublicKeyPreparedAlloc, + M: GLWEPublicKeyPreparedFactory, { module.alloc_glwe_public_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self where - M: GLWEPublicKeyPreparedAlloc, + M: GLWEPublicKeyPreparedFactory, { module.alloc_glwe_public_key_prepared(base2k, k, rank) } @@ -114,14 +114,14 @@ impl GLWEPublicKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - M: GLWEPublicKeyPreparedAlloc, + M: GLWEPublicKeyPreparedFactory, { module.bytes_of_glwe_public_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize where - M: GLWEPublicKeyPreparedAlloc, + M: GLWEPublicKeyPreparedFactory, { module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } @@ -131,7 +131,7 @@ impl GLWEPublicKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O) where O: GLWEToRef + GetDistribution, - M: GLWEPublicKeyPreparedAlloc, + M: GLWEPublicKeyPreparedFactory, { module.prepare_glwe_public_key(self, other); } diff --git a/poulpy-core/src/layouts/prepared/glwe_secret.rs b/poulpy-core/src/layouts/prepared/glwe_secret.rs index 98e8078..412733c 100644 --- a/poulpy-core/src/layouts/prepared/glwe_secret.rs +++ b/poulpy-core/src/layouts/prepared/glwe_secret.rs @@ -49,7 +49,7 @@ impl GLWEInfos for GLWESecretPrepared { } } -pub trait GLWESecretPreparedApi +pub trait GLWESecretPreparedFactory where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare, { @@ -96,20 +96,20 @@ where } } -impl GLWESecretPreparedApi for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare {} +impl GLWESecretPreparedFactory 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, + M: GLWESecretPreparedFactory, { module.alloc_glwe_secret_prepared_from_infos(infos) } pub fn alloc(module: &M, rank: Rank) -> Self where - M: GLWESecretPreparedApi, + M: GLWESecretPreparedFactory, { module.alloc_glwe_secret_prepared(rank) } @@ -117,14 +117,14 @@ impl GLWESecretPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GLWEInfos, - M: GLWESecretPreparedApi, + M: GLWESecretPreparedFactory, { module.bytes_of_glwe_secret_prepared_from_infos(infos) } pub fn bytes_of(module: &M, rank: Rank) -> usize where - M: GLWESecretPreparedApi, + M: GLWESecretPreparedFactory, { module.bytes_of_glwe_secret_prepared(rank) } @@ -143,7 +143,7 @@ impl GLWESecretPrepared { impl GLWESecretPrepared { pub fn prepare(&mut self, module: &M, other: &O) where - M: GLWESecretPreparedApi, + M: GLWESecretPreparedFactory, O: GLWESecretToRef + GetDistribution, { module.prepare_glwe_secret(self, other); diff --git a/poulpy-core/src/layouts/prepared/glwe_switching_key.rs b/poulpy-core/src/layouts/prepared/glwe_switching_key.rs index ce9cdfa..d73d17d 100644 --- a/poulpy-core/src/layouts/prepared/glwe_switching_key.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::{GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef}, + prepared::{GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] @@ -75,9 +75,9 @@ impl GGLWEInfos for GLWESwitchingKeyPrepared { } } -pub trait GLWESwitchingKeyPreparedAlloc +pub trait GLWESwitchingKeyPreparedFactory where - Self: GGLWEPreparedAlloc, + Self: GGLWEPreparedFactory, { fn alloc_glwe_switching_key_prepared( &self, @@ -153,13 +153,13 @@ where } } -impl GLWESwitchingKeyPreparedAlloc for Module where Self: GGLWEPreparedAlloc {} +impl GLWESwitchingKeyPreparedFactory for Module where Self: GGLWEPreparedFactory {} impl GLWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.alloc_glwe_switching_key_prepared_from_infos(infos) } @@ -174,7 +174,7 @@ impl GLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> Self where - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } @@ -182,7 +182,7 @@ impl GLWESwitchingKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.bytes_of_glwe_switching_key_prepared_from_infos(infos) } @@ -197,7 +197,7 @@ impl GLWESwitchingKeyPrepared, B> { dsize: Dsize, ) -> usize where - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } @@ -207,7 +207,7 @@ impl GLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.prepare_glwe_switching(self, other, scratch); } @@ -216,7 +216,7 @@ impl GLWESwitchingKeyPrepared { impl GLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M) -> usize where - M: GLWESwitchingKeyPreparedAlloc, + M: GLWESwitchingKeyPreparedFactory, { module.prepare_glwe_switching_key_tmp_bytes(self) } diff --git a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs index d1287e8..7ac936e 100644 --- a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs @@ -1,7 +1,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, }; @@ -52,9 +52,9 @@ impl GGLWEInfos for TensorKeyPrepared { } } -pub trait TensorKeyPreparedAlloc +pub trait TensorKeyPreparedFactory where - Self: GGLWEPreparedAlloc, + Self: GGLWEPreparedFactory, { fn alloc_tensor_key_prepared( &self, @@ -131,20 +131,20 @@ where } } -impl TensorKeyPreparedAlloc for Module where Module: GGLWEPreparedAlloc {} +impl TensorKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} impl TensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } @@ -152,14 +152,14 @@ impl TensorKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } @@ -191,7 +191,7 @@ impl TensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.prepare_tensor_key_tmp_bytes(infos) } @@ -201,7 +201,7 @@ impl TensorKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: TensorKeyToRef, - M: TensorKeyPreparedAlloc, + M: TensorKeyPreparedFactory, { module.prepare_tensor_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs index 96a9907..865e9cd 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.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::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedFactory}, }; #[derive(PartialEq, Eq)] @@ -51,9 +51,9 @@ impl GGLWEInfos for GLWEToLWESwitchingKeyPrepared { } } -pub trait GLWEToLWESwitchingKeyPreparedAlloc +pub trait GLWEToLWESwitchingKeyPreparedFactory where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GLWESwitchingKeyPreparedFactory, { fn alloc_glwe_to_lwe_switching_key_prepared( &self, @@ -118,20 +118,20 @@ where } } -impl GLWEToLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} +impl GLWEToLWESwitchingKeyPreparedFactory for Module where Self: GLWESwitchingKeyPreparedFactory {} impl GLWEToLWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } @@ -139,14 +139,14 @@ impl GLWEToLWESwitchingKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum) } @@ -156,7 +156,7 @@ impl GLWEToLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos); } @@ -166,7 +166,7 @@ impl GLWEToLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: GLWEToLWESwitchingKeyPreparedAlloc, + M: GLWEToLWESwitchingKeyPreparedFactory, { module.prepare_glwe_to_lwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/lwe_switching_key.rs b/poulpy-core/src/layouts/prepared/lwe_switching_key.rs index 6fc0ee9..327d001 100644 --- a/poulpy-core/src/layouts/prepared/lwe_switching_key.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::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedFactory}, }; #[derive(PartialEq, Eq)] @@ -50,9 +50,9 @@ impl GGLWEInfos for LWESwitchingKeyPrepared { } } -pub trait LWESwitchingKeyPreparedAlloc +pub trait LWESwitchingKeyPreparedFactory where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GLWESwitchingKeyPreparedFactory, { fn alloc_lwe_switching_key_prepared( &self, @@ -126,20 +126,20 @@ where } } -impl LWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} +impl LWESwitchingKeyPreparedFactory for Module where Self: GLWESwitchingKeyPreparedFactory {} impl LWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.alloc_lwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.alloc_lwe_switching_key_prepared(base2k, k, dnum) } @@ -147,14 +147,14 @@ impl LWESwitchingKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.bytes_of_lwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.bytes_of_lwe_switching_key_prepared(base2k, k, dnum) } @@ -164,7 +164,7 @@ impl LWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.prepare_lwe_switching_key_tmp_bytes(infos); } @@ -174,7 +174,7 @@ impl LWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: LWESwitchingKeyPreparedAlloc, + M: LWESwitchingKeyPreparedFactory, { module.prepare_lwe_switching_key(self, other, scratch); } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.rs index bb0952c..30ed131 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_switching_key.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::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc}, + prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedFactory}, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE]. @@ -52,9 +52,9 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyPrepared { } } -pub trait LWEToGLWESwitchingKeyPreparedAlloc +pub trait LWEToGLWESwitchingKeyPreparedFactory where - Self: GLWESwitchingKeyPreparedAlloc, + Self: GLWESwitchingKeyPreparedFactory, { fn alloc_lwe_to_glwe_switching_key_prepared( &self, @@ -125,20 +125,20 @@ where } } -impl LWEToGLWESwitchingKeyPreparedAlloc for Module where Self: GLWESwitchingKeyPreparedAlloc {} +impl LWEToGLWESwitchingKeyPreparedFactory for Module where Self: GLWESwitchingKeyPreparedFactory {} impl LWEToGLWESwitchingKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } @@ -146,14 +146,14 @@ impl LWEToGLWESwitchingKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize where - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum) } @@ -163,7 +163,7 @@ impl LWEToGLWESwitchingKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) where A: GGLWEInfos, - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos); } @@ -173,7 +173,7 @@ impl LWEToGLWESwitchingKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where O: GGLWEToRef + GLWESwitchingKeyDegrees, - M: LWEToGLWESwitchingKeyPreparedAlloc, + M: LWEToGLWESwitchingKeyPreparedFactory, { module.prepare_lwe_to_glwe_switching_key(self, other, scratch); } 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 fabd835..a8d3d53 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -8,8 +8,8 @@ use crate::{ AutomorphismKeyEncryptSk, GLWEAutomorphismKeyAutomorphism, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEAutomorphismKeyPreparedApi, GLWEPlaintext, GLWESecret, - GLWESecretPreparedApi, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext, GLWESecret, + GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -19,12 +19,12 @@ use crate::{ pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where Module: AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism + GaloisElement + VecZnxSubScalarInplace - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -188,12 +188,12 @@ where pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where Module: AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism + GaloisElement + VecZnxSubScalarInplace - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, 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 b8cd8d1..384a86b 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -8,8 +8,8 @@ use crate::{ AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - AutomorphismKey, GGSW, GGSWLayout, GLWEAutomorphismKeyPreparedApi, GLWESecret, GLWESecretPreparedApi, TensorKey, - TensorKeyLayout, TensorKeyPreparedAlloc, + AutomorphismKey, GGSW, GGSWLayout, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory, TensorKey, + TensorKeyLayout, TensorKeyPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, @@ -19,11 +19,11 @@ pub fn test_ggsw_automorphism(module: &Module) where Module: GGSWEncryptSk + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedAlloc + + TensorKeyPreparedFactory + TensorKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -178,11 +178,11 @@ pub fn test_ggsw_automorphism_inplace(module: &Module) where Module: GGSWEncryptSk + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedAlloc + + TensorKeyPreparedFactory + TensorKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, 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 64540cb..df9d36d 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -8,8 +8,8 @@ use crate::{ AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedApi, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -18,12 +18,12 @@ use crate::{ pub fn test_glwe_automorphism(module: &Module) where Module: GLWEEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -136,12 +136,12 @@ where pub fn test_glwe_automorphism_inplace(module: &Module) where Module: GLWEEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 0c55bad..c6e7d00 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -8,9 +8,9 @@ use crate::{ GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore, layouts::{ - Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, GLWEToLWEKeyLayout, - GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPreparedAlloc, LWE, LWELayout, LWEPlaintext, LWESecret, - LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPreparedAlloc, Rank, TorusPrecision, + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKeyLayout, + GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPreparedFactory, LWE, LWELayout, LWEPlaintext, LWESecret, + LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPreparedFactory, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared}, }, }; @@ -20,9 +20,9 @@ where Module: GLWEFromLWE + LWEToGLWESwitchingKeyEncryptSk + GLWEDecrypt - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + LWEEncryptSk - + LWEToGLWESwitchingKeyPreparedAlloc, + + LWEToGLWESwitchingKeyPreparedFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -112,9 +112,9 @@ where + GLWEEncryptSk + LWEDecrypt + GLWEDecrypt - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWEToLWESwitchingKeyEncryptSk - + GLWEToLWESwitchingKeyPreparedAlloc, + + GLWEToLWESwitchingKeyPreparedFactory, 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 11d0c00..3b642c4 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -9,7 +9,7 @@ use crate::{ GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedApi, + AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, prepared::GLWESecretPrepared, }, noise::GGLWENoise, @@ -19,7 +19,7 @@ pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where Module: AutomorphismKeyEncryptSk + GGLWEKeyswitch - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + GLWESwitchingKeyDecompress @@ -91,7 +91,7 @@ pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &M where Module: AutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk + AutomorphismKeyDecompress 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 1516684..9e8aafe 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -9,8 +9,8 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GGLWELayout, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress, - prepared::{GGLWEPreparedAlloc, GLWESecretPrepared}, + GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress, + prepared::{GGLWEPreparedFactory, GLWESecretPrepared}, }, noise::GGLWENoise, }; @@ -18,10 +18,10 @@ use crate::{ pub fn test_gglwe_switching_key_encrypt_sk(module: &Module) where Module: GGLWEEncryptSk - + GGLWEPreparedAlloc + + GGLWEPreparedFactory + GGLWEKeyswitch + GLWEDecrypt - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk + VecZnxFillUniform + GGLWENoise, @@ -83,10 +83,10 @@ where pub fn test_gglwe_switching_key_compressed_encrypt_sk(module: &Module) where Module: GGLWEEncryptSk - + GGLWEPreparedAlloc + + GGLWEPreparedFactory + GGLWEKeyswitch + GLWEDecrypt - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + 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 b315fef..54b5df0 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, GLWESecretPreparedApi, compressed::GGSWCompressed, + GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, compressed::GGSWCompressed, prepared::GLWESecretPrepared, }, }; @@ -17,7 +17,7 @@ pub fn test_ggsw_encrypt_sk(module: &Module) where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, - Module: GGSWEncryptSk + GLWESecretPreparedApi + GGSWNoise, + Module: GGSWEncryptSk + GLWESecretPreparedFactory + GGSWNoise, { let base2k: usize = 12; let k: usize = 54; @@ -74,7 +74,7 @@ pub fn test_ggsw_compressed_encrypt_sk(module: &Module) where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, - Module: GGSWCompressedEncryptSk + GLWESecretPreparedApi + GGSWNoise + GGSWDecompress, + Module: GGSWCompressedEncryptSk + GLWESecretPreparedFactory + 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 135561d..514d28d 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, GLWEPublicKeyPreparedAlloc, GLWESecret, - GLWESecretPreparedApi, LWEInfos, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWEPublicKeyPreparedFactory, GLWESecret, + GLWESecretPreparedFactory, LWEInfos, compressed::GLWECompressed, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared}, }, @@ -18,7 +18,7 @@ use crate::{ pub fn test_glwe_encrypt_sk(module: &Module) where - Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, + Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedFactory + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -83,7 +83,7 @@ where pub fn test_glwe_compressed_encrypt_sk(module: &Module) where - Module: GLWECompressedEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, + Module: GLWECompressedEncryptSk + GLWEDecrypt + GLWESecretPreparedFactory + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -160,7 +160,7 @@ where pub fn test_glwe_encrypt_zero_sk(module: &Module) where - Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedApi + VecZnxFillUniform + GLWESub, + Module: GLWEEncryptSk + GLWEDecrypt + GLWESecretPreparedFactory + VecZnxFillUniform + GLWESub, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -210,10 +210,10 @@ where pub fn test_glwe_encrypt_pk(module: &Module) where Module: GLWEEncryptPk - + GLWEPublicKeyPreparedAlloc + + GLWEPublicKeyPreparedFactory + GLWEPublicKeyGenerate + GLWEDecrypt - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + 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 e56d0fb..9135374 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -13,7 +13,7 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, GLWETensorKeyCompressed, TensorKey, TensorKeyLayout, + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKeyCompressed, TensorKey, TensorKeyLayout, prepared::GLWESecretPrepared, }, }; @@ -21,7 +21,7 @@ use crate::{ pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWEDecrypt + VecZnxDftAlloc + VecZnxBigAlloc @@ -112,7 +112,7 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where Module: TensorKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GGLWETensorKeyCompressedEncryptSk + GLWEDecrypt + VecZnxDftAlloc 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 419da96..07a0926 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,7 +8,7 @@ use crate::{ GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GGSWPreparedAlloc, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, + GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -20,9 +20,9 @@ where Module: GGLWEExternalProduct + GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxRotateInplace - + GGSWPreparedAlloc + + GGSWPreparedFactory + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -169,9 +169,9 @@ where Module: GGLWEExternalProduct + GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxRotateInplace - + GGSWPreparedAlloc + + GGSWPreparedFactory + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, 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 7e7535c..3fe2da4 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, GGSWPreparedAlloc, GLWESecret, GLWESecretPreparedApi, + GGSW, GGSWLayout, GGSWPreparedFactory, GLWESecret, GLWESecretPreparedFactory, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -19,8 +19,8 @@ pub fn test_ggsw_external_product(module: &Module) where Module: GGSWEncryptSk + GGSWExternalProduct - + GLWESecretPreparedApi - + GGSWPreparedAlloc + + GLWESecretPreparedFactory + + GGSWPreparedFactory + VecZnxRotateInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -153,8 +153,8 @@ pub fn test_ggsw_external_product_inplace(module: &Module) where Module: GGSWEncryptSk + GGSWExternalProduct - + GLWESecretPreparedApi - + GGSWPreparedAlloc + + GLWESecretPreparedFactory + + GGSWPreparedFactory + VecZnxRotateInplace + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, 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 d501b9a..0425d35 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,7 +8,7 @@ use crate::{ GGSWEncryptSk, GLWEEncryptSk, GLWEExternalProduct, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedApi, + GGSW, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, prepared::{GGSWPrepared, GLWESecretPrepared}, }, noise::noise_ggsw_product, @@ -18,13 +18,13 @@ use crate::{ pub fn test_glwe_external_product(module: &Module) where Module: GGSWEncryptSk - + GGSWPreparedAlloc + + GGSWPreparedFactory + VecZnxFillUniform + GLWEExternalProduct + GLWEEncryptSk + GLWENoise + VecZnxRotateInplace - + GLWESecretPreparedApi, + + GLWESecretPreparedFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { @@ -148,13 +148,13 @@ where pub fn test_glwe_external_product_inplace(module: &Module) where Module: GGSWEncryptSk - + GGSWPreparedAlloc + + GGSWPreparedFactory + VecZnxFillUniform + GLWEExternalProduct + GLWEEncryptSk + GLWENoise + VecZnxRotateInplace - + GLWESecretPreparedApi, + + GLWESecretPreparedFactory, 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 baa1c34..548d1f0 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -8,7 +8,7 @@ use crate::{ GGLWEKeyswitch, GGLWENoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedAlloc, + GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedFactory, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -18,8 +18,8 @@ pub fn test_gglwe_switching_key_keyswitch(module: &Module) where Module: GLWESwitchingKeyEncryptSk + GGLWEKeyswitch - + GLWESwitchingKeyPreparedAlloc - + GLWESecretPreparedApi + + GLWESwitchingKeyPreparedFactory + + GLWESecretPreparedFactory + GGLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -162,9 +162,9 @@ pub fn test_gglwe_switching_key_keyswitch_inplace(module: &Module: GLWESwitchingKeyEncryptSk + GGLWEKeyswitch - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GGLWENoise - + GLWESwitchingKeyPreparedAlloc, + + GLWESwitchingKeyPreparedFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, { 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 8f5be61..288eb88 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -8,8 +8,8 @@ use crate::{ GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, encryption::SIGMA, layouts::{ - GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPreparedAlloc, + GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPreparedFactory, TensorKey, TensorKeyLayout, TensorKeyPreparedFactory, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, @@ -22,9 +22,9 @@ where + GLWESwitchingKeyEncryptSk + TensorKeyEncryptSk + GGSWKeyswitch - + GLWESecretPreparedApi - + TensorKeyPreparedAlloc - + GLWESwitchingKeyPreparedAlloc + + GLWESecretPreparedFactory + + TensorKeyPreparedFactory + + GLWESwitchingKeyPreparedFactory + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -187,9 +187,9 @@ where + GLWESwitchingKeyEncryptSk + TensorKeyEncryptSk + GGSWKeyswitch - + GLWESecretPreparedApi - + TensorKeyPreparedAlloc - + GLWESwitchingKeyPreparedAlloc + + GLWESecretPreparedFactory + + TensorKeyPreparedFactory + + GLWESwitchingKeyPreparedFactory + 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 8619a7b..90cc543 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, GLWESecretPreparedApi, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedAlloc, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWESwitchingKeyPreparedFactory, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -22,8 +22,8 @@ where + GLWESwitchingKeyEncryptSk + GLWEEncryptSk + GLWEKeyswitch - + GLWESecretPreparedApi - + GLWESwitchingKeyPreparedAlloc + + GLWESecretPreparedFactory + + GLWESwitchingKeyPreparedFactory + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, @@ -143,8 +143,8 @@ where + GLWESwitchingKeyEncryptSk + GLWEEncryptSk + GLWEKeyswitch - + GLWESecretPreparedApi - + GLWESwitchingKeyPreparedAlloc + + GLWESecretPreparedFactory + + GLWESwitchingKeyPreparedFactory + GLWENoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchAvailable + ScratchTakeCore, 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 cbda02f..7617b09 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ LWEDecrypt, LWEEncryptSk, LWEKeySwitch, LWESwitchingKeyEncrypt, ScratchTakeCore, layouts::{ - LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPreparedAlloc, + LWE, LWELayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, LWESwitchingKeyPreparedFactory, prepared::LWESwitchingKeyPrepared, }, }; @@ -15,7 +15,7 @@ use crate::{ pub fn test_lwe_keyswitch(module: &Module) where Module: - LWEKeySwitch + LWESwitchingKeyEncrypt + LWEEncryptSk + LWESwitchingKeyPreparedAlloc + LWEDecrypt, + LWEKeySwitch + LWESwitchingKeyEncrypt + LWEEncryptSk + LWESwitchingKeyPreparedFactory + 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 a9e340e..ec685f4 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -9,8 +9,8 @@ use poulpy_hal::{ use crate::{ AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedApi, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, }; @@ -19,9 +19,9 @@ pub fn test_glwe_packing(module: &Module) where Module: GLWEEncryptSk + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + GLWEPacking - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + GLWESub + GLWEDecrypt + GLWERotate, diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index c15e584..4b83808 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -11,8 +11,8 @@ use crate::{ encryption::SIGMA, glwe_trace::GLWETrace, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedApi, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedApi, LWEInfos, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, + GLWESecretPreparedFactory, LWEInfos, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::var_noise_gglwe_product, @@ -24,9 +24,9 @@ where + GLWEEncryptSk + GLWEDecrypt + AutomorphismKeyEncryptSk - + GLWEAutomorphismKeyPreparedApi + + GLWEAutomorphismKeyPreparedFactory + VecZnxFillUniform - + GLWESecretPreparedApi + + GLWESecretPreparedFactory + VecZnxSubInplace + VecZnxNormalizeInplace, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, From 07e2319826954d33678ffe768fe066fe8298b64b Mon Sep 17 00:00:00 2001 From: Rasoul Akhavan Mahdavi Date: Tue, 21 Oct 2025 03:46:20 -0500 Subject: [PATCH 55/60] Extra bit needed for bootstrapping --- poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 145ce6b..5478c84 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -116,7 +116,7 @@ impl FheUintBlocks { let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); for i in 0..T::WORD_SIZE { - pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(1), 0); + pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(2), 0); self.blocks[i].encrypt_sk(&module, &pt, sk, source_xa, source_xe, scratch_1); } } From fef2a2fc2736f24ab10c53547606a6c0afe4761a Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 21 Oct 2025 10:47:46 +0200 Subject: [PATCH 56/60] fixed scratch API --- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 2 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 2 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 17 ++--- .../src/encryption/compressed/gglwe.rs | 2 +- poulpy-core/src/encryption/compressed/ggsw.rs | 4 +- .../compressed/glwe_automorphism_key.rs | 4 +- .../compressed/glwe_switching_key.rs | 4 +- .../encryption/compressed/glwe_tensor_key.rs | 4 +- poulpy-core/src/encryption/gglwe.rs | 3 +- poulpy-core/src/encryption/ggsw.rs | 2 +- poulpy-core/src/encryption/glwe.rs | 6 +- .../src/encryption/glwe_automorphism_key.rs | 8 +-- .../src/encryption/glwe_switching_key.rs | 4 +- poulpy-core/src/encryption/glwe_tensor_key.rs | 10 +-- .../encryption/glwe_to_lwe_switching_key.rs | 2 +- .../src/encryption/lwe_switching_key.rs | 4 +- .../encryption/lwe_to_glwe_switching_key.rs | 2 +- poulpy-core/src/external_product/glwe.rs | 4 +- poulpy-core/src/glwe_packing.rs | 8 +-- poulpy-core/src/glwe_trace.rs | 15 ++--- poulpy-core/src/keyswitching/glwe.rs | 4 +- poulpy-core/src/keyswitching/lwe.rs | 30 ++++----- .../src/layouts/prepared/glwe_public_key.rs | 3 +- .../src/layouts/prepared/glwe_secret.rs | 5 +- poulpy-core/src/scratch.rs | 66 +++++++------------ .../tests/test_suite/encryption/gglwe_ct.rs | 3 +- poulpy-hal/src/api/scratch.rs | 34 ++++------ .../ciphertexts/block_prepared.rs | 13 ++-- 28 files changed, 112 insertions(+), 153 deletions(-) diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 7ad73e6..87b6791 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -186,7 +186,7 @@ where self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, a, i); } } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, a_size); + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self.n(), 1, a_size); for i in 0..cols { self.vec_znx_normalize(basek_tsk, &mut a_conv, 0, basek_in, a, i, scratch_2); self.vec_znx_dft_apply(1, 0, &mut ci_dft, i, &a_conv, 0); diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 461d07d..fbf5912 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(self, &glwe_layout); + let (mut tmp_glwe, scratch_1) = scratch.take_glwe(&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 90c71a2..c759ee5 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -50,15 +50,12 @@ where assert_eq!(ksk.n(), self.n() as u32); assert!(lwe.n() <= self.n() as u32); - let (mut glwe, scratch_1) = scratch.take_glwe( - self, - &GLWELayout { - n: ksk.n(), - base2k: ksk.base2k(), - k: lwe.k(), - rank: 1u32.into(), - }, - ); + let (mut glwe, scratch_1) = scratch.take_glwe(&GLWELayout { + n: ksk.n(), + base2k: ksk.base2k(), + k: lwe.k(), + rank: 1u32.into(), + }); glwe.data.zero(); let n_lwe: usize = lwe.n().into(); @@ -70,7 +67,7 @@ where glwe.data.at_mut(1, i)[..n_lwe].copy_from_slice(&data_lwe[1..]); } } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self, 1, lwe.size()); + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(self.n(), 1, lwe.size()); a_conv.zero(); for j in 0..lwe.size() { let data_lwe: &[i64] = lwe.data.at(0, j); diff --git a/poulpy-core/src/encryption/compressed/gglwe.rs b/poulpy-core/src/encryption/compressed/gglwe.rs index 1f502c5..1dfbf58 100644 --- a/poulpy-core/src/encryption/compressed/gglwe.rs +++ b/poulpy-core/src/encryption/compressed/gglwe.rs @@ -142,7 +142,7 @@ where let mut source_xa = Source::new(seed); - let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(self, res); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(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.rs b/poulpy-core/src/encryption/compressed/ggsw.rs index e1695b8..14b0de5 100644 --- a/poulpy-core/src/encryption/compressed/ggsw.rs +++ b/poulpy-core/src/encryption/compressed/ggsw.rs @@ -8,7 +8,7 @@ use crate::{ ScratchTakeCore, encryption::{GGSWEncryptSk, GLWEEncryptSkInternal, SIGMA}, layouts::{ - GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos, + GGSWCompressedSeedMut, GGSWInfos, LWEInfos, compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -107,7 +107,7 @@ where println!("res.seed: {:?}", res.seed); - let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(self, &res.glwe_layout()); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res); let mut source = Source::new(seed_xa); diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index 9c9046d..df57f0f 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -98,8 +98,8 @@ where let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { - let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); - for i in 0..res.rank_out().into() { + let (mut sk_out, _) = scratch_1.take_glwe_secret(self.n().into(), sk.rank()); + for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), diff --git a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs index c492cfd..e1c40ea 100644 --- a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs @@ -102,7 +102,7 @@ where self.gglwe_compressed_encrypt_sk_tmp_bytes(res) ); - let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); + let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self.n(), sk_in.rank().into()); for i in 0..sk_in.rank().into() { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), @@ -114,7 +114,7 @@ where let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { - let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); + let (mut tmp, _) = scratch_2.take_scalar_znx(self.n(), 1); for i in 0..sk_out.rank().into() { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); diff --git a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs index 9456e89..b50b47d 100644 --- a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs @@ -99,7 +99,7 @@ where R: GGLWEInfos + TensorKeyCompressedAtMut, S: GLWESecretToRef + GetDistribution, { - let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out()); + let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank()); sk_dft_prep.prepare(self, sk); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); @@ -120,7 +120,7 @@ where } let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1); - let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1)); + let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self.n().into(), Rank(1)); let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1); let mut source_xa: Source = Source::new(seed_xa); diff --git a/poulpy-core/src/encryption/gglwe.rs b/poulpy-core/src/encryption/gglwe.rs index 01f1bc3..ba78cde 100644 --- a/poulpy-core/src/encryption/gglwe.rs +++ b/poulpy-core/src/encryption/gglwe.rs @@ -6,7 +6,6 @@ use poulpy_hal::{ use crate::{ GLWEEncryptSk, ScratchTakeCore, - layouts::GLWEInfos, layouts::{ GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, @@ -143,7 +142,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_plaintext(self, &res.glwe_layout()); + let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(res); // 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.rs b/poulpy-core/src/encryption/ggsw.rs index c3bef71..85b8be5 100644 --- a/poulpy-core/src/encryption/ggsw.rs +++ b/poulpy-core/src/encryption/ggsw.rs @@ -109,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_plaintext(self, res); + let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res); for row_i in 0..res.dnum().into() { tmp_pt.data.zero(); diff --git a/poulpy-core/src/encryption/glwe.rs b/poulpy-core/src/encryption/glwe.rs index d4aca7d..fc59e4b 100644 --- a/poulpy-core/src/encryption/glwe.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -372,7 +372,7 @@ where let (mut u_dft, scratch_1) = scratch.take_svp_ppol(self, 1); { - let (mut u, _) = scratch_1.take_scalar_znx(self, 1); + let (mut u, _) = scratch_1.take_scalar_znx(self.n(), 1); match pk.dist() { Distribution::NONE => panic!( "invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \ @@ -499,11 +499,11 @@ where let size: usize = ct.size(); - let (mut c0, scratch_1) = scratch.take_vec_znx(self, 1, size); + let (mut c0, scratch_1) = scratch.take_vec_znx(self.n(), 1, size); c0.zero(); { - let (mut ci, scratch_2) = scratch_1.take_vec_znx(self, 1, size); + let (mut ci, scratch_2) = scratch_1.take_vec_znx(self.n(), 1, size); // ct[i] = uniform // ct[0] -= c[i] * s[i], diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index 68782e3..cee3163 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.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, GLWESecretPrepared, GLWESecretPreparedFactory, - GLWESecretToRef, LWEInfos, SetGaloisElement, + AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, + GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement, }, }; @@ -115,8 +115,8 @@ where let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { - let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank()); - for i in 0..res.rank().into() { + let (mut sk_out, _) = scratch_1.take_glwe_secret(sk.n(), sk.rank()); + for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), &mut sk_out.data.as_vec_znx_mut(), diff --git a/poulpy-core/src/encryption/glwe_switching_key.rs b/poulpy-core/src/encryption/glwe_switching_key.rs index fc19a71..62d60c0 100644 --- a/poulpy-core/src/encryption/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_switching_key.rs @@ -109,7 +109,7 @@ where self.glwe_switching_key_encrypt_sk_tmp_bytes(res) ); - let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into()); + let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self.n(), sk_in.rank().into()); for i in 0..sk_in.rank().into() { self.vec_znx_switch_ring( &mut sk_in_tmp.as_vec_znx_mut(), @@ -121,7 +121,7 @@ where let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank()); { - let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1); + let (mut tmp, _) = scratch_2.take_scalar_znx(self.n(), 1); for i in 0..sk_out.rank().into() { self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i); self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0); diff --git a/poulpy-core/src/encryption/glwe_tensor_key.rs b/poulpy-core/src/encryption/glwe_tensor_key.rs index 758afcc..e151b8e 100644 --- a/poulpy-core/src/encryption/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/glwe_tensor_key.rs @@ -35,7 +35,7 @@ impl TensorKey { scratch: &mut Scratch, ) where M: TensorKeyEncryptSk, - S: GLWESecretToRef + GetDistribution, + S: GLWESecretToRef + GetDistribution + GLWEInfos, Scratch: ScratchTakeCore, { module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); @@ -56,7 +56,7 @@ pub trait TensorKeyEncryptSk { scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDistribution; + S: GLWESecretToRef + GetDistribution + GLWEInfos; } impl TensorKeyEncryptSk for Module @@ -93,14 +93,14 @@ where scratch: &mut Scratch, ) where R: TensorKeyToMut, - S: GLWESecretToRef + GetDistribution, + S: GLWESecretToRef + GetDistribution + GLWEInfos, { let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); // let n: RingDegree = sk.n(); let rank: Rank = res.rank_out(); - let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank); + let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); sk_prepared.prepare(self, sk); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); @@ -115,7 +115,7 @@ where }); let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1); - let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1)); + let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self.n().into(), Rank(1)); let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1); (0..rank.into()).for_each(|i| { diff --git a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs index 36ecff2..30a46a8 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs @@ -100,7 +100,7 @@ where let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1)); { - let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), sk_lwe_as_glwe_prep.rank()); sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2); diff --git a/poulpy-core/src/encryption/lwe_switching_key.rs b/poulpy-core/src/encryption/lwe_switching_key.rs index daee764..859f0c9 100644 --- a/poulpy-core/src/encryption/lwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_switching_key.rs @@ -111,8 +111,8 @@ where assert!(sk_lwe_out.n().0 <= res.n().0); assert!(res.n() <= self.n() as u32); - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1)); + let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); + let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1)); sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); diff --git a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs index 588875d..524bb1a 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs @@ -99,7 +99,7 @@ where assert!(sk_lwe.n().0 <= self.n() as u32); - let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1)); + let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); diff --git a/poulpy-core/src/external_product/glwe.rs b/poulpy-core/src/external_product/glwe.rs index ab9968c..a1cd5ee 100644 --- a/poulpy-core/src/external_product/glwe.rs +++ b/poulpy-core/src/external_product/glwe.rs @@ -147,7 +147,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); for j in 0..cols { self.vec_znx_normalize( @@ -262,7 +262,7 @@ where } } } else { - let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self, cols, a_size); + let (mut a_conv, scratch_3) = scratch_2.take_vec_znx(self.n(), cols, a_size); for j in 0..cols { self.vec_znx_normalize( diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 1982269..09540b2 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -331,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(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(a); // a = a * X^-t module.glwe_rotate_inplace(-t, a, scratch_1); @@ -371,7 +371,7 @@ fn combine( } } } else if let Some(b) = b { - let (mut tmp_b, scratch_1) = scratch.take_glwe(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(a); module.glwe_rotate(t, &mut tmp_b, b); module.glwe_rsh(1, &mut tmp_b, scratch_1); @@ -415,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(module, a); + let (mut tmp_b, scratch_1) = scratch.take_glwe(a); // a = a * X^-t module.glwe_rotate_inplace(-t, a, scratch_1); @@ -449,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(module, b); + let (mut tmp_b, scratch_1) = scratch.take_glwe(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 421692f..80cf84c 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -140,15 +140,12 @@ where } if res.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe( - self, - &GLWELayout { - n: self.n().into(), - base2k: basek_ksk, - k: res.k(), - rank: res.rank(), - }, - ); + let (mut self_conv, scratch_1) = scratch.take_glwe(&GLWELayout { + n: self.n().into(), + base2k: basek_ksk, + k: res.k(), + rank: res.rank(), + }); for j in 0..(res.rank() + 1).into() { self.vec_znx_normalize( diff --git a/poulpy-core/src/keyswitching/glwe.rs b/poulpy-core/src/keyswitching/glwe.rs index be81ebf..a021777 100644 --- a/poulpy-core/src/keyswitching/glwe.rs +++ b/poulpy-core/src/keyswitching/glwe.rs @@ -278,7 +278,7 @@ where module.vec_znx_dft_apply(1, 0, &mut ai_dft, col_i, a.data(), col_i + 1); }); } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, 1, a_size); + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module.n(), 1, a_size); (0..cols - 1).for_each(|col_i| { module.vec_znx_normalize( base2k_out, @@ -324,7 +324,7 @@ where } } } else { - let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module, cols - 1, a_size); + let (mut a_conv, scratch_2) = scratch_1.take_vec_znx(module.n(), cols - 1, a_size); for j in 0..cols - 1 { module.vec_znx_normalize( base2k_out, diff --git a/poulpy-core/src/keyswitching/lwe.rs b/poulpy-core/src/keyswitching/lwe.rs index 249ccca..bf5abf5 100644 --- a/poulpy-core/src/keyswitching/lwe.rs +++ b/poulpy-core/src/keyswitching/lwe.rs @@ -87,26 +87,20 @@ where let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe( - self, - &GLWELayout { - n: ksk.n(), - base2k: a.base2k(), - k: max_k, - rank: Rank(1), - }, - ); + let (mut glwe_in, scratch_1) = scratch.take_glwe(&GLWELayout { + n: ksk.n(), + base2k: a.base2k(), + k: max_k, + rank: Rank(1), + }); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe( - self, - &GLWELayout { - n: ksk.n(), - base2k: res.base2k(), - k: max_k, - rank: Rank(1), - }, - ); + let (mut glwe_out, scratch_1) = scratch_1.take_glwe(&GLWELayout { + n: ksk.n(), + base2k: res.base2k(), + k: max_k, + rank: Rank(1), + }); let n_lwe: usize = a.n().into(); diff --git a/poulpy-core/src/layouts/prepared/glwe_public_key.rs b/poulpy-core/src/layouts/prepared/glwe_public_key.rs index b20682d..fab30bb 100644 --- a/poulpy-core/src/layouts/prepared/glwe_public_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_public_key.rs @@ -93,7 +93,8 @@ where } } -impl GLWEPublicKeyPreparedFactory for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply {} +impl GLWEPublicKeyPreparedFactory for Module where Self: VecZnxDftAlloc + VecZnxDftBytesOf + VecZnxDftApply +{} impl GLWEPublicKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/layouts/prepared/glwe_secret.rs b/poulpy-core/src/layouts/prepared/glwe_secret.rs index 412733c..0e38221 100644 --- a/poulpy-core/src/layouts/prepared/glwe_secret.rs +++ b/poulpy-core/src/layouts/prepared/glwe_secret.rs @@ -96,7 +96,10 @@ where } } -impl GLWESecretPreparedFactory for Module where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare {} +impl GLWESecretPreparedFactory for Module where + Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc + SvpPrepare +{ +} impl GLWESecretPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 7261410..880af1b 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -19,13 +19,11 @@ pub trait ScratchTakeCore where Self: ScratchTakeBasic + ScratchAvailable, { - fn take_glwe(&mut self, module: &M, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) + fn take_glwe(&mut self, infos: &A) -> (GLWE<&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_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( GLWE { k: infos.k(), @@ -36,28 +34,25 @@ where ) } - fn take_glwe_slice(&mut self, module: &M, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, - M: ModuleN, { 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(module, infos); + let (ct, new_scratch) = scratch.take_glwe(infos); scratch = new_scratch; cts.push(ct); } (cts, scratch) } - fn take_glwe_plaintext(&mut self, module: &M, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self) + fn take_glwe_plaintext(&mut self, infos: &A) -> (GLWEPlaintext<&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, 1, infos.size()); + let (data, scratch) = self.take_vec_znx(infos.n().into(), 1, infos.size()); ( GLWEPlaintext { k: infos.k(), @@ -68,14 +63,12 @@ where ) } - fn take_gglwe(&mut self, module: &M, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, - M: ModuleN, { - assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - module, + infos.n().into(), infos.dnum().0.div_ceil(infos.dsize().0) as usize, infos.rank_in().into(), (infos.rank_out() + 1).into(), @@ -116,14 +109,12 @@ where ) } - fn take_ggsw(&mut self, module: &M, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, - M: ModuleN, { - assert_eq!(module.n() as u32, infos.n()); let (data, scratch) = self.take_mat_znx( - module, + infos.n().into(), infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), @@ -184,13 +175,11 @@ where (cts, scratch) } - fn take_glwe_public_key(&mut self, module: &M, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self) + fn take_glwe_public_key(&mut self, 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_glwe(module, infos); + let (data, scratch) = self.take_glwe(infos); ( GLWEPublicKey { dist: Distribution::NONE, @@ -232,11 +221,8 @@ where ) } - fn take_glwe_secret(&mut self, module: &M, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) - where - M: ModuleN, - { - let (data, scratch) = self.take_scalar_znx(module, rank.into()); + fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) { + let (data, scratch) = self.take_scalar_znx(n.into(), rank.into()); ( GLWESecret { data, @@ -260,13 +246,11 @@ where ) } - fn take_glwe_switching_key(&mut self, module: &M, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, - M: ModuleN, { - assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_gglwe(module, infos); + let (data, scratch) = self.take_gglwe(infos); ( GLWESwitchingKey { key: data, @@ -298,17 +282,15 @@ where ) } - fn take_gglwe_automorphism_key(&mut self, module: &M, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) + fn take_glwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, - M: ModuleN, { - assert_eq!(module.n() as u32, infos.n()); - let (data, scratch) = self.take_gglwe(module, infos); + let (data, scratch) = self.take_gglwe(infos); (AutomorphismKey { key: data, p: 0 }, scratch) } - fn take_gglwe_automorphism_key_prepared( + fn take_glwe_automorphism_key_prepared( &mut self, module: &M, infos: &A, @@ -322,12 +304,10 @@ where (GLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } - fn take_tensor_key(&mut self, module: &M, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) + fn take_glwe_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, - M: ModuleN, { - assert_eq!(module.n() as u32, infos.n()); assert_eq!( infos.rank_in(), infos.rank_out(), @@ -342,19 +322,19 @@ where ksk_infos.rank_in = Rank(1); if pairs != 0 { - let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(&ksk_infos); scratch = s; keys.push(gglwe); } for _ in 1..pairs { - let (gglwe, s) = scratch.take_gglwe(module, &ksk_infos); + let (gglwe, s) = scratch.take_gglwe(&ksk_infos); scratch = s; keys.push(gglwe); } (TensorKey { keys }, scratch) } - fn take_gglwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, M: ModuleN + VmpPMatBytesOf, 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 9e8aafe..2b64f02 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -9,7 +9,8 @@ use crate::{ decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress, + GGLWELayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyCompressed, + GLWESwitchingKeyDecompress, prepared::{GGLWEPreparedFactory, GLWESecretPrepared}, }, noise::GGLWENoise, diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index fb17266..714a58d 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -34,13 +34,11 @@ pub trait ScratchTakeBasic where Self: TakeSlice, { - fn take_scalar_znx(&mut self, module: &M, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) - where - M: ModuleN, + fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { - let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(module.n(), cols)); + let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(n, cols)); ( - ScalarZnx::from_data(take_slice, module.n(), cols), + ScalarZnx::from_data(take_slice, n, cols), rem_slice, ) } @@ -53,13 +51,10 @@ where (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice) } - fn take_vec_znx(&mut self, module: &M, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) - where - M: ModuleN, - { - let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(module.n(), cols, size)); + fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self){ + let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(n, cols, size)); ( - VecZnx::from_data(take_slice, module.n(), cols, size), + VecZnx::from_data(take_slice, n, cols, size), rem_slice, ) } @@ -107,14 +102,11 @@ where (slice, scratch) } - fn take_vec_znx_slice(&mut self, module: &M, len: usize, cols: usize, size: usize) -> (Vec>, &mut Self) - where - M: ModuleN, - { + fn take_vec_znx_slice(&mut self, n: usize, len: usize, cols: usize, size: usize) -> (Vec>, &mut Self){ let mut scratch: &mut Self = self; let mut slice: Vec> = Vec::with_capacity(len); for _ in 0..len { - let (znx, new_scratch) = scratch.take_vec_znx(module, cols, size); + let (znx, new_scratch) = scratch.take_vec_znx(n, cols, size); scratch = new_scratch; slice.push(znx); } @@ -139,20 +131,18 @@ where ) } - fn take_mat_znx( + fn take_mat_znx( &mut self, - module: &M, + n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, ) -> (MatZnx<&mut [u8]>, &mut Self) - where - M: ModuleN, { - let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(module.n(), rows, cols_in, cols_out, size)); + let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(n, rows, cols_in, cols_out, size)); ( - MatZnx::from_data(take_slice, module.n(), rows, cols_in, cols_out, size), + MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), rem_slice, ) } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index c0cd09f..aa70910 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -3,8 +3,7 @@ use std::marker::PhantomData; use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}; #[cfg(test)] use poulpy_core::{ - TakeGGSW, - layouts::{GGSW, prepared::GLWESecretPrepared}, + layouts::{prepared::GLWESecretPrepared, GGSW}, ScratchTakeCore, }; use poulpy_hal::{ api::VmpPMatAlloc, @@ -13,13 +12,12 @@ use poulpy_hal::{ #[cfg(test)] use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, + SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPrepare, }, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, source::Source, }; @@ -137,7 +135,7 @@ impl FheUintBlocksPrep + VecZnxSub + VmpPrepare, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -146,11 +144,11 @@ impl FheUintBlocksPrep FheUintBlocksPrepDebug { + VecZnxIdftApplyTmpA + VecZnxAddScalarInplace + VecZnxSubInplace, - BE: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { for (i, ggsw) in self.blocks.iter().enumerate() { use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut}; From 0926913001b5fae6638cadf2d470b8f68f36b542 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 21 Oct 2025 14:26:53 +0200 Subject: [PATCH 57/60] fix blind rotation --- poulpy-core/src/layouts/lwe_secret.rs | 7 + poulpy-core/src/lib.rs | 3 +- poulpy-core/src/operations/glwe.rs | 2 + poulpy-hal/src/api/scratch.rs | 22 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 14 +- .../ciphertexts/block_prepared.rs | 2 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 14 +- .../src/tfhe/bdd_arithmetic/eval.rs | 10 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 13 +- .../cggi/algorithm.rs} | 372 +++++++----------- .../blind_rotation/algorithms/cggi/key.rs | 79 ++++ .../algorithms/cggi/key_compressed.rs | 84 ++++ .../algorithms/cggi/key_prepared.rs | 69 ++++ .../blind_rotation/algorithms/cggi/mod.rs | 10 + .../src/tfhe/blind_rotation/algorithms/mod.rs | 116 ++++++ .../src/tfhe/blind_rotation/cggi_key.rs | 223 ----------- .../src/tfhe/blind_rotation/encryption/key.rs | 60 +++ .../encryption/key_compressed.rs | 30 ++ .../src/tfhe/blind_rotation/encryption/mod.rs | 5 + .../src/tfhe/blind_rotation/key_prepared.rs | 130 ------ .../tfhe/blind_rotation/{ => layouts}/key.rs | 40 +- .../{ => layouts}/key_compressed.rs | 18 + .../blind_rotation/layouts/key_prepared.rs | 108 +++++ .../src/tfhe/blind_rotation/layouts/mod.rs | 6 + poulpy-schemes/src/tfhe/blind_rotation/lut.rs | 318 +++++++++------ poulpy-schemes/src/tfhe/blind_rotation/mod.rs | 36 +- .../tests/generic_blind_rotation.rs | 127 ++---- .../tfhe/blind_rotation/tests/generic_lut.rs | 49 ++- .../tests/generic_serialization.rs | 6 +- .../tests/implementation/cpu_spqlios/fft64.rs | 37 -- .../tests/implementation/cpu_spqlios/mod.rs | 1 - .../tests/implementation/fft64.rs | 40 ++ .../tests/implementation/mod.rs | 2 +- .../src/tfhe/circuit_bootstrapping/key.rs | 5 +- .../src/tfhe/circuit_bootstrapping/mod.rs | 4 +- .../tfhe/circuit_bootstrapping/tests/mod.rs | 1 - poulpy-schemes/src/tfhe/mod.rs | 4 +- 37 files changed, 1106 insertions(+), 961 deletions(-) rename poulpy-schemes/src/tfhe/blind_rotation/{cggi_algo.rs => algorithms/cggi/algorithm.rs} (55%) create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_compressed.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/mod.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/algorithms/mod.rs delete mode 100644 poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/encryption/key_compressed.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/encryption/mod.rs delete mode 100644 poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs rename poulpy-schemes/src/tfhe/blind_rotation/{ => layouts}/key.rs (87%) rename poulpy-schemes/src/tfhe/blind_rotation/{ => layouts}/key_compressed.rs (89%) create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/layouts/mod.rs delete mode 100644 poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs delete mode 100644 poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs create mode 100644 poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/fft64.rs diff --git a/poulpy-core/src/layouts/lwe_secret.rs b/poulpy-core/src/layouts/lwe_secret.rs index 78ad9c7..00a7849 100644 --- a/poulpy-core/src/layouts/lwe_secret.rs +++ b/poulpy-core/src/layouts/lwe_secret.rs @@ -4,6 +4,7 @@ use poulpy_hal::{ }; use crate::{ + GetDistribution, dist::Distribution, layouts::{Base2K, Degree, LWEInfos, TorusPrecision}, }; @@ -22,6 +23,12 @@ impl LWESecret> { } } +impl GetDistribution for LWESecret { + fn dist(&self) -> &Distribution { + &self.dist + } +} + impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index 78fb717..a5c5152 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -23,9 +23,8 @@ pub use external_product::*; pub use glwe_packing::*; pub use keyswitching::*; pub use noise::*; +pub use scratch::*; pub use encryption::SIGMA; -pub use scratch::*; - pub mod tests; diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index 417e5f2..c6f1818 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -217,6 +217,8 @@ where } } +impl GLWEMulXpMinusOne for Module where Self: ModuleN + VecZnxMulXpMinusOne + VecZnxMulXpMinusOneInplace {} + pub trait GLWEMulXpMinusOne where Self: ModuleN + VecZnxMulXpMinusOne + VecZnxMulXpMinusOneInplace, diff --git a/poulpy-hal/src/api/scratch.rs b/poulpy-hal/src/api/scratch.rs index 714a58d..9e3c484 100644 --- a/poulpy-hal/src/api/scratch.rs +++ b/poulpy-hal/src/api/scratch.rs @@ -34,13 +34,9 @@ pub trait ScratchTakeBasic where Self: TakeSlice, { - fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) - { + fn take_scalar_znx(&mut self, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Self) { let (take_slice, rem_slice) = self.take_slice(ScalarZnx::bytes_of(n, cols)); - ( - ScalarZnx::from_data(take_slice, n, cols), - rem_slice, - ) + (ScalarZnx::from_data(take_slice, n, cols), rem_slice) } fn take_svp_ppol(&mut self, module: &M, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Self) @@ -51,12 +47,9 @@ where (SvpPPol::from_data(take_slice, module.n(), cols), rem_slice) } - fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self){ + fn take_vec_znx(&mut self, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Self) { let (take_slice, rem_slice) = self.take_slice(VecZnx::bytes_of(n, cols, size)); - ( - VecZnx::from_data(take_slice, n, cols, size), - rem_slice, - ) + (VecZnx::from_data(take_slice, n, cols, size), rem_slice) } fn take_vec_znx_big(&mut self, module: &M, cols: usize, size: usize) -> (VecZnxBig<&mut [u8], B>, &mut Self) @@ -102,7 +95,7 @@ where (slice, scratch) } - fn take_vec_znx_slice(&mut self, n: usize, len: usize, cols: usize, size: usize) -> (Vec>, &mut Self){ + fn take_vec_znx_slice(&mut self, len: usize, n: usize, cols: usize, size: usize) -> (Vec>, &mut Self) { let mut scratch: &mut Self = self; let mut slice: Vec> = Vec::with_capacity(len); for _ in 0..len { @@ -133,13 +126,12 @@ where fn take_mat_znx( &mut self, - n: usize, + n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, - ) -> (MatZnx<&mut [u8]>, &mut Self) - { + ) -> (MatZnx<&mut [u8]>, &mut Self) { let (take_slice, rem_slice) = self.take_slice(MatZnx::bytes_of(n, rows, cols_in, cols_out, size)); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 70bd910..3e07242 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -2,17 +2,19 @@ use std::marker::PhantomData; use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; -use poulpy_core::{TakeGLWEPlaintext, layouts::prepared::GLWESecretPrepared}; +#[cfg(test)] +use poulpy_core::ScratchTakeCore; +use poulpy_core::{layouts::prepared::GLWESecretPrepared}; use poulpy_hal::api::VecZnxBigBytesOf; #[cfg(test)] use poulpy_hal::api::{ - ScratchAvailable, TakeVecZnx, VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, + VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, }; #[cfg(test)] use poulpy_hal::source::Source; use poulpy_hal::{ api::{ - TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, + VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, @@ -96,7 +98,7 @@ impl FheUintBlocks { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, + Scratch: ScratchTakeCore, { use poulpy_core::layouts::GLWEPlaintextLayout; @@ -136,7 +138,7 @@ impl FheUintBlocks { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -186,7 +188,7 @@ impl FheUintBlocks { + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, - Scratch: TakeGLWEPlaintext + TakeVecZnxDft + TakeVecZnxBig, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index aa70910..1ed7229 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -144,7 +144,7 @@ impl FheUintBlocksPrep FheUintWord { + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotate, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWESlice, + Scratch: ScratchTakeCore, { // Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; @@ -122,7 +120,7 @@ impl FheUintWord { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -167,7 +165,7 @@ impl FheUintWord { + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 245fe03..953ee7a 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -1,10 +1,8 @@ use itertools::Itertools; use poulpy_core::{ - GLWEExternalProductInplace, GLWEOperations, TakeGLWESlice, layouts::{ - GLWE, GLWEToMut, LWEInfos, - prepared::{GGSWPrepared, GGSWPreparedToRef}, - }, + prepared::{GGSWPrepared, GGSWPreparedToRef}, GLWEToMut, LWEInfos, GLWE + }, GLWEExternalProduct, ScratchTakeCore }; use poulpy_hal::{ api::{VecZnxAddInplace, VecZnxCopy, VecZnxNegateInplace, VecZnxSub}, @@ -49,7 +47,7 @@ impl Circuit where Self: GetBitCircuitInfo, Module: Cmux + VecZnxCopy, - Scratch: TakeGLWESlice, + Scratch: ScratchTakeCore, { fn execute( &self, @@ -169,7 +167,7 @@ pub trait Cmux { impl Cmux for Module where - Module: GLWEExternalProductInplace + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, + Module: GLWEExternalProduct + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, { fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) where diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index fdcf5b3..e2b8453 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -9,16 +9,13 @@ use crate::tfhe::{ }, }; use poulpy_core::{ - TakeGGSW, TakeGLWE, layouts::{ - GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, - prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, - }, + prepared::GLWEToLWESwitchingKeyPrepared, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWESecret + }, ScratchTakeCore, }; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, - TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, @@ -96,7 +93,7 @@ impl BDDKey, Vec, BRA> { + SvpPPolAlloc + VecZnxAutomorphism + VecZnxAutomorphismInplace, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, + Scratch: ScratchTakeCore, { let mut ks: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos()); ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); @@ -217,7 +214,7 @@ where + VecZnxBigNormalize + VecZnxNormalize + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, + Scratch: ScratchTakeCore, CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, { fn prepare( diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/algorithm.rs similarity index 55% rename from poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs rename to poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/algorithm.rs index 8bf0a9e..b9ec277 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/algorithm.rs @@ -1,158 +1,142 @@ use itertools::izip; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpPPolBytesOf, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, - TakeVecZnxSlice, VecZnxAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, - VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, - VecZnxDftSubInplace, VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, - VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, - VecZnxSubInplace, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, + ModuleN, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, + VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, + VecZnxDftZero, VecZnxIdftApply, VecZnxIdftApplyTmpBytes, VecZnxRotate, VmpApplyDftToDft, VmpApplyDftToDftTmpBytes, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, SvpPPol, VecZnx, ZnxView, ZnxZero}, + layouts::{Backend, DataMut, DataRef, Module, Scratch, SvpPPol, VecZnx, ZnxZero}, }; use poulpy_core::{ - Distribution, GLWEOperations, TakeGLWE, + Distribution, GLWEAdd, GLWEExternalProduct, GLWEMulXpMinusOne, GLWENormalize, ScratchTakeCore, layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWEInfos, LWEToRef}, }; use crate::tfhe::blind_rotation::{ - BlincRotationExecute, BlindRotationKeyInfos, BlindRotationKeyPrepared, CGGI, LookUpTable, LookUpTableRotationDirection, + BlindRotationExecute, BlindRotationKeyInfos, BlindRotationKeyPrepared, CGGI, LookupTable, mod_switch_2n, }; -#[allow(clippy::too_many_arguments)] -pub fn cggi_blind_rotate_tmp_bytes( - module: &Module, - block_size: usize, - extension_factor: usize, - glwe_infos: &OUT, - brk_infos: &GGSW, -) -> usize +impl BlindRotationExecute for Module where - OUT: GLWEInfos, - GGSW: GGSWInfos, - Module: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes + Self: VecZnxDftBytesOf + VecZnxBigBytesOf - + VecZnxIdftApplyTmpBytes - + VecZnxBigNormalizeTmpBytes, -{ - let brk_size: usize = brk_infos.size(); - - if block_size > 1 { - let cols: usize = (brk_infos.rank() + 1).into(); - let dnum: usize = brk_infos.dnum().into(); - let acc_dft: usize = module.bytes_of_vec_znx_dft(cols, dnum) * extension_factor; - let acc_big: usize = module.bytes_of_vec_znx_big(1, brk_size); - let vmp_res: usize = module.bytes_of_vec_znx_dft(cols, brk_size) * extension_factor; - let vmp_xai: usize = module.bytes_of_vec_znx_dft(1, brk_size); - let acc_dft_add: usize = vmp_res; - let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) - let acc: usize = if extension_factor > 1 { - VecZnx::bytes_of(module.n(), cols, glwe_infos.size()) * extension_factor - } else { - 0 - }; - - acc + acc_dft - + acc_dft_add - + vmp_res - + vmp_xai - + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes()))) - } else { - GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_tmp_bytes(module, glwe_infos, brk_infos) - } -} - -impl BlincRotationExecute for BlindRotationKeyPrepared -where - Module: VecZnxBigBytesOf - + VecZnxDftBytesOf - + SvpPPolBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxIdftApplyTmpBytes - + VecZnxIdftApply - + VecZnxDftAdd - + VecZnxDftAddInplace - + VecZnxDftApply - + VecZnxDftZero - + SvpApplyDftToDft - + VecZnxDftSubInplace - + VecZnxBigAddSmallInplace + + GLWEExternalProduct + + ModuleN + VecZnxRotate - + VecZnxAddInplace - + VecZnxSubInplace - + VecZnxNormalize - + VecZnxNormalizeInplace + + VecZnxDftApply + + VecZnxDftZero + + VmpApplyDftToDft + + SvpApplyDftToDft + + VecZnxDftAddInplace + + VecZnxDftSubInplace + + VecZnxIdftApply + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + VecZnxCopy - + VecZnxMulXpMinusOneInplace - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + TakeVecZnx + ScratchAvailable, + + GLWEMulXpMinusOne + + GLWEAdd + + GLWENormalize, + Scratch: ScratchTakeCore, { - fn execute( + fn blind_rotation_execute_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + glwe_infos: &G, + brk_infos: &B, + ) -> usize + where + G: GLWEInfos, + B: GGSWInfos, + { + let brk_size: usize = brk_infos.size(); + + if block_size > 1 { + let cols: usize = (brk_infos.rank() + 1).into(); + let dnum: usize = brk_infos.dnum().into(); + let acc_dft: usize = self.bytes_of_vec_znx_dft(cols, dnum) * extension_factor; + let acc_big: usize = self.bytes_of_vec_znx_big(1, brk_size); + let vmp_res: usize = self.bytes_of_vec_znx_dft(cols, brk_size) * extension_factor; + let vmp_xai: usize = self.bytes_of_vec_znx_dft(1, brk_size); + let acc_dft_add: usize = vmp_res; + let vmp: usize = self.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) + let acc: usize = if extension_factor > 1 { + VecZnx::bytes_of(self.n(), cols, glwe_infos.size()) * extension_factor + } else { + 0 + }; + + acc + acc_dft + + acc_dft_add + + vmp_res + + vmp_xai + + (vmp + | (acc_big + + (self + .vec_znx_big_normalize_tmp_bytes() + .max(self.vec_znx_idft_apply_tmp_bytes())))) + } else { + GLWE::bytes_of_from_infos(glwe_infos) + GLWE::external_product_tmp_bytes(self, glwe_infos, glwe_infos, brk_infos) + } + } + + fn blind_rotation_execute( &self, - module: &Module, res: &mut GLWE, - lwe: &LWE, - lut: &LookUpTable, - scratch: &mut Scratch, - ) { - match self.dist { + lwe: &LWE
, + lut: &LookupTable, + brk: &BlindRotationKeyPrepared, + scratch: &mut Scratch, + ) where + DR: DataMut, + DL: DataRef, + DB: DataRef, + { + match brk.dist { Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => { if lut.extension_factor() > 1 { - execute_block_binary_extended(module, res, lwe, lut, self, scratch) - } else if self.block_size() > 1 { - execute_block_binary(module, res, lwe, lut, self, scratch); + execute_block_binary_extended(self, res, lwe, lut, brk, scratch) + } else if brk.block_size() > 1 { + execute_block_binary(self, res, lwe, lut, brk, scratch); } else { - execute_standard(module, res, lwe, lut, self, scratch); + execute_standard(self, res, lwe, lut, brk, scratch); } } - _ => panic!("invalid CGGI distribution"), + _ => panic!("invalid CGGI distribution (have you prepared the key?)"), } } } -fn execute_block_binary_extended( - module: &Module, +fn execute_block_binary_extended( + module: &M, res: &mut GLWE, lwe: &LWE, - lut: &LookUpTable, - brk: &BlindRotationKeyPrepared, - scratch: &mut Scratch, + lut: &LookupTable, + brk: &BlindRotationKeyPrepared, + scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigBytesOf - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpBytes - + VecZnxIdftApply - + VecZnxDftAdd - + VecZnxDftAddInplace - + VecZnxDftApply - + VecZnxDftZero - + SvpApplyDftToDft - + VecZnxDftSubInplace - + VecZnxBigAddSmallInplace + M: VecZnxDftBytesOf + + ModuleN + VecZnxRotate - + VecZnxAddInplace - + VecZnxSubInplace - + VecZnxNormalize - + VecZnxNormalizeInplace + + VecZnxDftApply + + VecZnxDftZero + + VmpApplyDftToDft + + SvpApplyDftToDft + + VecZnxDftAddInplace + + VecZnxDftSubInplace + + VecZnxIdftApply + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + VecZnxCopy - + VecZnxMulXpMinusOneInplace - + VecZnxBigNormalize - + VmpApplyDftToDft, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, + + VecZnxBigBytesOf, + Scratch: ScratchTakeCore, { let n_glwe: usize = brk.n_glwe().into(); let extension_factor: usize = lut.extension_factor(); @@ -161,16 +145,16 @@ fn execute_block_binary_extended( let cols: usize = (res.rank() + 1).into(); let (mut acc, scratch_1) = scratch.take_vec_znx_slice(extension_factor, n_glwe, cols, res.size()); - let (mut acc_dft, scratch_2) = scratch_1.take_vec_znx_dft_slice(extension_factor, n_glwe, cols, dnum); - let (mut vmp_res, scratch_3) = scratch_2.take_vec_znx_dft_slice(extension_factor, n_glwe, cols, brk.size()); - let (mut acc_add_dft, scratch_4) = scratch_3.take_vec_znx_dft_slice(extension_factor, n_glwe, cols, brk.size()); - let (mut vmp_xai, scratch_5) = scratch_4.take_vec_znx_dft(n_glwe, 1, brk.size()); + let (mut acc_dft, scratch_2) = scratch_1.take_vec_znx_dft_slice(module, extension_factor, cols, dnum); + let (mut vmp_res, scratch_3) = scratch_2.take_vec_znx_dft_slice(module, extension_factor, cols, brk.size()); + let (mut acc_add_dft, scratch_4) = scratch_3.take_vec_znx_dft_slice(module, extension_factor, cols, brk.size()); + let (mut vmp_xai, scratch_5) = scratch_4.take_vec_znx_dft(module, 1, brk.size()); (0..extension_factor).for_each(|i| { acc[i].zero(); }); - let x_pow_a: &Vec, B>>; + let x_pow_a: &Vec, BE>>; if let Some(b) = &brk.x_pow_a { x_pow_a = b } else { @@ -268,7 +252,7 @@ fn execute_block_binary_extended( }); { - let (mut acc_add_big, scratch7) = scratch_5.take_vec_znx_big(n_glwe, 1, brk.size()); + let (mut acc_add_big, scratch7) = scratch_5.take_vec_znx_big(module, 1, brk.size()); (0..extension_factor).for_each(|j| { (0..cols).for_each(|i| { @@ -285,41 +269,32 @@ fn execute_block_binary_extended( }); } -fn execute_block_binary( - module: &Module, +fn execute_block_binary( + module: &M, res: &mut GLWE, lwe: &LWE, - lut: &LookUpTable, - brk: &BlindRotationKeyPrepared, - scratch: &mut Scratch, + lut: &LookupTable, + brk: &BlindRotationKeyPrepared, + scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigBytesOf - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpBytes - + VecZnxIdftApply - + VecZnxDftAdd - + VecZnxDftAddInplace - + VecZnxDftApply - + VecZnxDftZero - + SvpApplyDftToDft - + VecZnxDftSubInplace - + VecZnxBigAddSmallInplace + M: VecZnxDftBytesOf + + ModuleN + VecZnxRotate - + VecZnxAddInplace - + VecZnxSubInplace - + VecZnxNormalize - + VecZnxNormalizeInplace + + VecZnxDftApply + + VecZnxDftZero + + VmpApplyDftToDft + + SvpApplyDftToDft + + VecZnxDftAddInplace + + VecZnxDftSubInplace + + VecZnxIdftApply + + VecZnxBigAddSmallInplace + + VecZnxBigNormalize + VecZnxCopy - + VecZnxMulXpMinusOneInplace - + VmpApplyDftToDft - + VecZnxBigNormalize, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, + + VecZnxBigBytesOf, + Scratch: ScratchTakeCore, { let n_glwe: usize = brk.n_glwe().into(); let mut lwe_2n: Vec = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space @@ -350,12 +325,12 @@ fn execute_block_binary( // ACC + [sum DFT(X^ai -1) * (DFT(ACC) x BRKi)] - let (mut acc_dft, scratch_1) = scratch.take_vec_znx_dft(n_glwe, cols, dnum); - let (mut vmp_res, scratch_2) = scratch_1.take_vec_znx_dft(n_glwe, cols, brk.size()); - let (mut acc_add_dft, scratch_3) = scratch_2.take_vec_znx_dft(n_glwe, cols, brk.size()); - let (mut vmp_xai, scratch_4) = scratch_3.take_vec_znx_dft(n_glwe, 1, brk.size()); + let (mut acc_dft, scratch_1) = scratch.take_vec_znx_dft(module, cols, dnum); + let (mut vmp_res, scratch_2) = scratch_1.take_vec_znx_dft(module, cols, brk.size()); + let (mut acc_add_dft, scratch_3) = scratch_2.take_vec_znx_dft(module, cols, brk.size()); + let (mut vmp_xai, scratch_4) = scratch_3.take_vec_znx_dft(module, 1, brk.size()); - let x_pow_a: &Vec, B>>; + let x_pow_a: &Vec, BE>>; if let Some(b) = &brk.x_pow_a { x_pow_a = b } else { @@ -388,7 +363,7 @@ fn execute_block_binary( }); { - let (mut acc_add_big, scratch_5) = scratch_4.take_vec_znx_big(n_glwe, 1, brk.size()); + let (mut acc_add_big, scratch_5) = scratch_4.take_vec_znx_big(module, 1, brk.size()); (0..cols).for_each(|i| { module.vec_znx_idft_apply(&mut acc_add_big, 0, &acc_add_dft, i, scratch_5); @@ -407,44 +382,19 @@ fn execute_block_binary( }); } -fn execute_standard( - module: &Module, +fn execute_standard( + module: &M, res: &mut GLWE, lwe: &LWE, - lut: &LookUpTable, - brk: &BlindRotationKeyPrepared, - scratch: &mut Scratch, + lut: &LookupTable, + brk: &BlindRotationKeyPrepared, + scratch: &mut Scratch, ) where DataRes: DataMut, DataIn: DataRef, DataBrk: DataRef, - Module: VecZnxBigBytesOf - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpBytes - + VecZnxIdftApply - + VecZnxDftAdd - + VecZnxDftAddInplace - + VecZnxDftApply - + VecZnxDftZero - + SvpApplyDftToDft - + VecZnxDftSubInplace - + VecZnxBigAddSmallInplace - + VecZnxRotate - + VecZnxAddInplace - + VecZnxSubInplace - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxCopy - + VecZnxMulXpMinusOneInplace - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes, - Scratch: TakeVecZnxDftSlice + TakeVecZnxDft + TakeVecZnxBig + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx, + M: VecZnxRotate + GLWEExternalProduct + GLWEMulXpMinusOne + GLWEAdd + GLWENormalize, + Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { @@ -498,7 +448,7 @@ fn execute_standard( module.vec_znx_rotate(b, out_mut.data_mut(), 0, &lut.data[0], 0); // ACC + [sum DFT(X^ai -1) * (DFT(ACC) x BRKi)] - let (mut acc_tmp, scratch_1) = scratch.take_glwe_ct(&out_mut); + let (mut acc_tmp, scratch_1) = scratch.take_glwe(&out_mut); // TODO: see if faster by skipping normalization in external product and keeping acc in big coeffs // TODO: first iteration can be optimized to be a gglwe product @@ -507,55 +457,13 @@ fn execute_standard( acc_tmp.external_product(module, &out_mut, ski, scratch_1); // acc_tmp = (sk[i] * acc) * (X^{ai} - 1) - acc_tmp.mul_xp_minus_one_inplace(module, *ai, scratch_1); + module.glwe_mul_xp_minus_one_inplace(*ai, &mut acc_tmp, scratch_1); // acc = acc + (sk[i] * acc) * (X^{ai} - 1) - out_mut.add_inplace(module, &acc_tmp); + module.glwe_add_inplace(&mut out_mut, &acc_tmp); }); // We can normalize only at the end because we add normalized values in [-2^{base2k-1}, 2^{base2k-1}] // on top of each others, thus ~ 2^{63-base2k} additions are supported before overflow. - out_mut.normalize_inplace(module, scratch_1); -} - -pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) { - let base2k: usize = lwe.base2k().into(); - - let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; - - res.copy_from_slice(lwe.data().at(0, 0)); - - match rot_dir { - LookUpTableRotationDirection::Left => { - res.iter_mut().for_each(|x| *x = -*x); - } - LookUpTableRotationDirection::Right => {} - } - - if base2k > log2n { - let diff: usize = base2k - (log2n - 1); // additional -1 because we map to [-N/2, N/2) instead of [0, N) - res.iter_mut().for_each(|x| { - *x = div_round_by_pow2(x, diff); - }) - } else { - let rem: usize = base2k - (log2n % base2k); - let size: usize = log2n.div_ceil(base2k); - (1..size).for_each(|i| { - if i == size - 1 && rem != base2k { - let k_rem: usize = base2k - rem; - izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { - *y = (*y << k_rem) + (x >> rem); - }); - } else { - izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { - *y = (*y << base2k) + x; - }); - } - }) - } -} - -#[inline(always)] -fn div_round_by_pow2(x: &i64, k: usize) -> i64 { - (x + (1 << (k - 1))) >> k + module.glwe_normalize_inplace(&mut out_mut, scratch_1); } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs new file mode 100644 index 0000000..830a829 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs @@ -0,0 +1,79 @@ +use poulpy_hal::{ + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, + source::Source, +}; + +use std::marker::PhantomData; + +use poulpy_core::{ + Distribution, GGSWEncryptSk, GetDistribution, ScratchTakeCore, + layouts::{GGSW, GGSWInfos, GLWEInfos, GLWESecretPreparedToRef, LWEInfos, LWESecret, LWESecretToRef}, +}; + +use crate::tfhe::blind_rotation::{ + BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory, BlindRotationKeyInfos, CGGI, +}; + +impl BlindRotationKeyFactory for BlindRotationKey { + fn blind_rotation_key_alloc(infos: &A) -> BlindRotationKey, CGGI> + where + A: BlindRotationKeyInfos, + { + BlindRotationKey { + keys: (0..infos.n_lwe().as_usize()) + .map(|_| GGSW::alloc_from_infos(infos)) + .collect(), + dist: Distribution::NONE, + _phantom: PhantomData, + } + } +} + +impl BlindRotationKeyEncryptSk for Module +where + Self: GGSWEncryptSk, + Scratch: ScratchTakeCore, +{ + fn blind_rotation_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize { + self.ggsw_encrypt_sk_tmp_bytes(infos) + } + + fn blind_rotation_key_encrypt_sk( + &self, + res: &mut BlindRotationKey, + sk_glwe: &S0, + sk_lwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: GLWESecretPreparedToRef + GLWEInfos, + S1: LWESecretToRef + LWEInfos + GetDistribution, + { + assert_eq!(res.keys.len() as u32, sk_lwe.n()); + assert!(sk_glwe.n() <= self.n() as u32); + assert_eq!(sk_glwe.rank(), res.rank()); + + match sk_lwe.dist() { + Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => {} + _ => { + panic!("invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)") + } + } + + { + let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); + + res.dist = sk_lwe.dist(); + + let mut pt: ScalarZnx> = ScalarZnx::alloc(sk_glwe.n().into(), 1); + let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data().to_ref(); + + for (i, ggsw) in res.keys.iter_mut().enumerate() { + pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i]; + ggsw.encrypt_sk(self, &pt, sk_glwe, source_xa, source_xe, scratch); + } + } + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_compressed.rs new file mode 100644 index 0000000..255f8f2 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_compressed.rs @@ -0,0 +1,84 @@ +use std::marker::PhantomData; + +use poulpy_core::{ + Distribution, GGSWCompressedEncryptSk, GetDistribution, ScratchTakeCore, + layouts::{GGSWCompressed, GGSWInfos, GLWEInfos, GLWESecretPreparedToRef, LWEInfos, LWESecret, LWESecretToRef}, +}; +use poulpy_hal::{ + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, + source::Source, +}; + +use crate::tfhe::blind_rotation::{ + BlindRotationKeyCompressed, BlindRotationKeyCompressedEncryptSk, BlindRotationKeyCompressedFactory, BlindRotationKeyInfos, + CGGI, +}; + +impl BlindRotationKeyCompressedFactory for BlindRotationKeyCompressed { + fn blind_rotation_key_compressed_alloc(infos: &A) -> BlindRotationKeyCompressed, CGGI> + where + A: BlindRotationKeyInfos, + { + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos))); + BlindRotationKeyCompressed { + keys: data, + dist: Distribution::NONE, + _phantom: PhantomData, + } + } +} + +impl BlindRotationKeyCompressedEncryptSk for Module +where + Self: GGSWCompressedEncryptSk, + Scratch: ScratchTakeCore, +{ + fn blind_rotation_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.ggsw_compressed_encrypt_sk_tmp_bytes(infos) + } + + fn blind_rotation_key_compressed_encrypt_sk( + &self, + res: &mut BlindRotationKeyCompressed, + sk_glwe: &S0, + sk_lwe: &S1, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: GLWESecretPreparedToRef + GLWEInfos, + S1: LWESecretToRef + LWEInfos + GetDistribution, + { + assert_eq!(res.keys.len() as u32, sk_lwe.n()); + assert!(sk_glwe.n() <= self.n() as u32); + assert_eq!(sk_glwe.rank(), res.rank()); + + match sk_lwe.dist() { + Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => {} + _ => { + panic!("invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)") + } + } + + { + let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); + + let mut source_xa: Source = Source::new(seed_xa); + + res.dist = sk_lwe.dist(); + + let mut pt: ScalarZnx> = ScalarZnx::alloc(sk_glwe.n().into(), 1); + let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data().to_ref(); + + for (i, ggsw) in res.keys.iter_mut().enumerate() { + pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i]; + ggsw.encrypt_sk(self, &pt, sk_glwe, source_xa.new_seed(), source_xe, scratch); + } + } + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs new file mode 100644 index 0000000..b711f16 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs @@ -0,0 +1,69 @@ +use poulpy_hal::{ + api::{SvpPPolAlloc, SvpPrepare}, + layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch, SvpPPol}, +}; + +use std::marker::PhantomData; + +use poulpy_core::{ + Distribution, + layouts::{GGSWPreparedFactory, LWEInfos, prepared::GGSWPrepared}, +}; + +use crate::tfhe::blind_rotation::{ + BlindRotationKey, BlindRotationKeyInfos, BlindRotationKeyPrepared, BlindRotationKeyPreparedFactory, CGGI, + utils::set_xai_plus_y, +}; + +impl BlindRotationKeyPreparedFactory for Module +where + Self: GGSWPreparedFactory + SvpPPolAlloc + SvpPrepare, +{ + fn blind_rotation_key_prepared_alloc(&self, infos: &A) -> BlindRotationKeyPrepared, CGGI, BE> + where + A: BlindRotationKeyInfos, + { + BlindRotationKeyPrepared { + data: (0..infos.n_lwe().as_usize()) + .map(|_| GGSWPrepared::alloc_from_infos(self, infos)) + .collect(), + dist: Distribution::NONE, + x_pow_a: None, + _phantom: PhantomData, + } + } + + fn blind_rotation_key_prepare( + &self, + res: &mut BlindRotationKeyPrepared, + other: &BlindRotationKey, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR: DataRef, + { + #[cfg(debug_assertions)] + { + assert_eq!(res.data.len(), other.keys.len()); + } + + let n: usize = other.n().as_usize(); + + for (a, b) in res.data.iter_mut().zip(other.keys.iter()) { + a.prepare(self, b, scratch); + } + + res.dist = other.dist; + + if let Distribution::BinaryBlock(_) = other.dist { + let mut x_pow_a: Vec, BE>> = Vec::with_capacity(n << 1); + let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); + (0..n << 1).for_each(|i| { + let mut res: SvpPPol, BE> = self.svp_ppol_alloc(1); + set_xai_plus_y(self, i, 0, &mut res, &mut buf); + x_pow_a.push(res); + }); + res.x_pow_a = Some(x_pow_a); + } + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/mod.rs new file mode 100644 index 0000000..d67ee45 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/mod.rs @@ -0,0 +1,10 @@ +mod algorithm; +mod key; +mod key_compressed; +mod key_prepared; + +use crate::tfhe::blind_rotation::BlindRotationAlgo; + +#[derive(Clone)] +pub struct CGGI {} +impl BlindRotationAlgo for CGGI {} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/mod.rs new file mode 100644 index 0000000..f25fb51 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/mod.rs @@ -0,0 +1,116 @@ +mod cggi; + +pub use cggi::*; + +use itertools::izip; +use poulpy_core::{ + ScratchTakeCore, + layouts::{GGSWInfos, GLWE, GLWEInfos, LWE, LWEInfos}, +}; +use poulpy_hal::layouts::{Backend, DataMut, DataRef, Scratch, ZnxView}; + +use crate::tfhe::blind_rotation::{BlindRotationKeyInfos, BlindRotationKeyPrepared, LookUpTableRotationDirection, LookupTable}; + +pub trait BlindRotationAlgo {} + +pub trait BlindRotationExecute { + fn blind_rotation_execute_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + glwe_infos: &G, + brk_infos: &B, + ) -> usize + where + G: GLWEInfos, + B: GGSWInfos; + + fn blind_rotation_execute( + &self, + res: &mut GLWE, + lwe: &LWE
, + lut: &LookupTable, + brk: &BlindRotationKeyPrepared, + scratch: &mut Scratch, + ) where + DR: DataMut, + DL: DataRef, + DB: DataRef; +} + +impl BlindRotationKeyPrepared +where + Scratch: ScratchTakeCore, +{ + pub fn execute( + &self, + module: &M, + res: &mut GLWE, + lwe: &LWE, + lut: &LookupTable, + scratch: &mut Scratch, + ) where + M: BlindRotationExecute, + { + module.blind_rotation_execute(res, lwe, lut, self, scratch); + } +} + +impl BlindRotationKeyPrepared, BRA, BE> { + pub fn execute_tmp_bytes( + module: &M, + block_size: usize, + extension_factor: usize, + glwe_infos: &A, + brk_infos: &B, + ) -> usize + where + A: GLWEInfos, + B: BlindRotationKeyInfos, + M: BlindRotationExecute, + { + module.blind_rotation_execute_tmp_bytes(block_size, extension_factor, glwe_infos, brk_infos) + } +} + +pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) { + let base2k: usize = lwe.base2k().into(); + + let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; + + res.copy_from_slice(lwe.data().at(0, 0)); + + match rot_dir { + LookUpTableRotationDirection::Left => { + res.iter_mut().for_each(|x| *x = -*x); + } + LookUpTableRotationDirection::Right => {} + } + + if base2k > log2n { + let diff: usize = base2k - (log2n - 1); // additional -1 because we map to [-N/2, N/2) instead of [0, N) + res.iter_mut().for_each(|x| { + *x = div_round_by_pow2(x, diff); + }) + } else { + let rem: usize = base2k - (log2n % base2k); + let size: usize = log2n.div_ceil(base2k); + (1..size).for_each(|i| { + if i == size - 1 && rem != base2k { + let k_rem: usize = base2k - rem; + izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { + *y = (*y << k_rem) + (x >> rem); + }); + } else { + izip!(lwe.data().at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| { + *y = (*y << base2k) + x; + }); + } + }) + } +} + +#[inline(always)] +fn div_round_by_pow2(x: &i64, k: usize) -> i64 { + (x + (1 << (k - 1))) >> k +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs deleted file mode 100644 index 3114ea1..0000000 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ /dev/null @@ -1,223 +0,0 @@ -use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, - source::Source, -}; - -use std::marker::PhantomData; - -use poulpy_core::{ - Distribution, - layouts::{ - GGSW, GGSWInfos, LWESecret, - compressed::GGSWCompressed, - prepared::{GGSWPrepared, GLWESecretPrepared}, - }, -}; - -use crate::tfhe::blind_rotation::{ - BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyCompressed, BlindRotationKeyEncryptSk, BlindRotationKeyInfos, - BlindRotationKeyPrepared, BlindRotationKeyPreparedAlloc, CGGI, -}; - -impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { - fn alloc(infos: &A) -> Self - where - A: BlindRotationKeyInfos, - { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); - for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSW::alloc_from_infos(infos)); - } - - Self { - keys: data, - dist: Distribution::NONE, - _phantom: PhantomData, - } - } -} - -impl BlindRotationKey, CGGI> { - pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize - where - A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - { - GGSW::encrypt_sk_tmp_bytes(module, infos) - } -} - -impl BlindRotationKeyEncryptSk for BlindRotationKey -where - Module: VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, -{ - fn encrypt_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretPrepared, - sk_lwe: &LWESecret, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef, - { - #[cfg(debug_assertions)] - { - use poulpy_core::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(self.keys.len() as u32, sk_lwe.n()); - assert!(sk_glwe.n() <= module.n() as u32); - assert_eq!(sk_glwe.rank(), self.rank()); - match sk_lwe.dist() { - Distribution::BinaryBlock(_) - | Distribution::BinaryFixed(_) - | Distribution::BinaryProb(_) - | Distribution::ZERO => {} - _ => panic!( - "invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)" - ), - } - } - - self.dist = sk_lwe.dist(); - - let mut pt: ScalarZnx> = ScalarZnx::alloc(sk_glwe.n().into(), 1); - let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data().to_ref(); - - self.keys.iter_mut().enumerate().for_each(|(i, ggsw)| { - pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i]; - ggsw.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, scratch); - }); - } -} - -impl BlindRotationKeyPreparedAlloc for BlindRotationKeyPrepared, CGGI, B> -where - Module: VmpPMatAlloc + VmpPrepare, -{ - fn alloc(module: &Module, infos: &A) -> Self - where - A: BlindRotationKeyInfos, - { - let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc_from_infos(module, infos))); - Self { - data, - dist: Distribution::NONE, - x_pow_a: None, - _phantom: PhantomData, - } - } -} - -impl BlindRotationKeyCompressed, CGGI> { - pub fn alloc(infos: &A) -> Self - where - A: BlindRotationKeyInfos, - { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos))); - Self { - keys: data, - dist: Distribution::NONE, - _phantom: PhantomData, - } - } - - pub fn generate_from_sk_tmp_bytes(module: &Module, infos: &A) -> usize - where - A: GGSWInfos, - Module: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf, - { - GGSWCompressed::encrypt_sk_tmp_bytes(module, infos) - } -} - -impl BlindRotationKeyCompressed { - #[allow(clippy::too_many_arguments)] - pub fn encrypt_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretPrepared, - sk_lwe: &LWESecret, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef, - Module: VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - #[cfg(debug_assertions)] - { - use poulpy_core::layouts::{GLWEInfos, LWEInfos}; - - assert_eq!(self.n_lwe(), sk_lwe.n()); - assert!(sk_glwe.n() <= module.n() as u32); - assert_eq!(sk_glwe.rank(), self.rank()); - match sk_lwe.dist() { - Distribution::BinaryBlock(_) - | Distribution::BinaryFixed(_) - | Distribution::BinaryProb(_) - | Distribution::ZERO => {} - _ => panic!( - "invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)" - ), - } - } - - self.dist = sk_lwe.dist(); - - let mut pt: ScalarZnx> = ScalarZnx::alloc(sk_glwe.n().into(), 1); - let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data().to_ref(); - - let mut source_xa: Source = Source::new(seed_xa); - - self.keys.iter_mut().enumerate().for_each(|(i, ggsw)| { - pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i]; - ggsw.encrypt_sk( - module, - &pt, - sk_glwe, - source_xa.new_seed(), - source_xe, - scratch, - ); - }); - } -} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs new file mode 100644 index 0000000..99a6e2e --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs @@ -0,0 +1,60 @@ +use poulpy_hal::{ + layouts::{Backend, DataMut, Scratch}, + source::Source, +}; + +use poulpy_core::{ + GetDistribution, ScratchTakeCore, + layouts::{GGSWInfos, GLWEInfos, GLWESecretPreparedToRef, LWEInfos, LWESecretToRef}, +}; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey}; + +pub trait BlindRotationKeyEncryptSk { + fn blind_rotation_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; + + #[allow(clippy::too_many_arguments)] + fn blind_rotation_key_encrypt_sk( + &self, + res: &mut BlindRotationKey, + sk_glwe: &S0, + sk_lwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: GLWESecretPreparedToRef + GLWEInfos, + S1: LWESecretToRef + LWEInfos + GetDistribution; +} + +impl BlindRotationKey { + pub fn encrypt_sk( + &mut self, + module: &M, + sk_glwe: &S0, + sk_lwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S0: GLWESecretPreparedToRef + GLWEInfos, + S1: LWESecretToRef + LWEInfos + GetDistribution, + Scratch: ScratchTakeCore, + M: BlindRotationKeyEncryptSk, + { + module.blind_rotation_key_encrypt_sk(self, sk_glwe, sk_lwe, source_xa, source_xe, scratch); + } +} + +impl BlindRotationKey, BRA> { + pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize + where + A: GGSWInfos, + M: BlindRotationKeyEncryptSk, + { + module.blind_rotation_key_encrypt_sk_tmp_bytes(infos) + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key_compressed.rs new file mode 100644 index 0000000..4898365 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key_compressed.rs @@ -0,0 +1,30 @@ +use poulpy_core::{ + GetDistribution, + layouts::{GGSWInfos, GLWEInfos, GLWESecretPreparedToRef, LWEInfos, LWESecretToRef}, +}; +use poulpy_hal::{ + layouts::{Backend, DataMut, Scratch}, + source::Source, +}; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyCompressed}; + +pub trait BlindRotationKeyCompressedEncryptSk { + fn blind_rotation_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; + + #[allow(clippy::too_many_arguments)] + fn blind_rotation_key_compressed_encrypt_sk( + &self, + res: &mut BlindRotationKeyCompressed, + sk_glwe: &S0, + sk_lwe: &S1, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: GLWESecretPreparedToRef + GLWEInfos, + S1: LWESecretToRef + LWEInfos + GetDistribution; +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/encryption/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/encryption/mod.rs new file mode 100644 index 0000000..62623fc --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/encryption/mod.rs @@ -0,0 +1,5 @@ +mod key; +mod key_compressed; + +pub use key::*; +pub use key_compressed::*; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs deleted file mode 100644 index 2001083..0000000 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ /dev/null @@ -1,130 +0,0 @@ -use poulpy_hal::{ - api::{SvpPPolAlloc, SvpPrepare, VmpPMatAlloc, VmpPrepare}, - layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, Scratch, SvpPPol}, -}; - -use std::marker::PhantomData; - -use poulpy_core::{ - Distribution, - layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGSWPrepared, Prepare, PrepareAlloc}, - }, -}; - -use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyInfos, utils::set_xai_plus_y}; - -pub trait BlindRotationKeyPreparedAlloc { - fn alloc(module: &Module, infos: &A) -> Self - where - A: BlindRotationKeyInfos; -} - -#[derive(PartialEq, Eq)] -pub struct BlindRotationKeyPrepared { - pub(crate) data: Vec>, - pub(crate) dist: Distribution, - pub(crate) x_pow_a: Option, B>>>, - pub(crate) _phantom: PhantomData, -} - -impl BlindRotationKeyInfos for BlindRotationKeyPrepared { - fn n_glwe(&self) -> Degree { - self.n() - } - - fn n_lwe(&self) -> Degree { - Degree(self.data.len() as u32) - } -} - -impl LWEInfos for BlindRotationKeyPrepared { - fn base2k(&self) -> Base2K { - self.data[0].base2k() - } - - fn k(&self) -> TorusPrecision { - self.data[0].k() - } - - fn n(&self) -> Degree { - self.data[0].n() - } - - fn size(&self) -> usize { - self.data[0].size() - } -} - -impl GLWEInfos for BlindRotationKeyPrepared { - fn rank(&self) -> Rank { - self.data[0].rank() - } -} -impl GGSWInfos for BlindRotationKeyPrepared { - fn dsize(&self) -> poulpy_core::layouts::Dsize { - Dsize(1) - } - - fn dnum(&self) -> Dnum { - self.data[0].dnum() - } -} - -impl BlindRotationKeyPrepared { - pub fn block_size(&self) -> usize { - match self.dist { - Distribution::BinaryBlock(value) => value, - _ => 1, - } - } -} - -impl PrepareAlloc, BRA, B>> - for BlindRotationKey -where - BlindRotationKeyPrepared, BRA, B>: BlindRotationKeyPreparedAlloc, - BlindRotationKeyPrepared, BRA, B>: Prepare>, -{ - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BlindRotationKeyPrepared, BRA, B> { - let mut brk: BlindRotationKeyPrepared, BRA, B> = BlindRotationKeyPrepared::alloc(module, self); - brk.prepare(module, self, scratch); - brk - } -} - -impl Prepare> - for BlindRotationKeyPrepared -where - Module: VmpPMatAlloc + VmpPrepare + SvpPPolAlloc + SvpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &BlindRotationKey, scratch: &mut Scratch) { - #[cfg(debug_assertions)] - { - assert_eq!(self.data.len(), other.keys.len()); - } - - let n: usize = other.n().as_usize(); - - self.data - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(ggsw_prepared, other)| { - ggsw_prepared.prepare(module, other, scratch); - }); - - self.dist = other.dist; - - if let Distribution::BinaryBlock(_) = other.dist { - let mut x_pow_a: Vec, B>> = Vec::with_capacity(n << 1); - let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); - (0..n << 1).for_each(|i| { - let mut res: SvpPPol, B> = module.svp_ppol_alloc(1); - set_xai_plus_y(module, i, 0, &mut res, &mut buf); - x_pow_a.push(res); - }); - self.x_pow_a = Some(x_pow_a); - } - } -} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key.rs similarity index 87% rename from poulpy-schemes/src/tfhe/blind_rotation/key.rs rename to poulpy-schemes/src/tfhe/blind_rotation/layouts/key.rs index d9bb0a9..182c973 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, Scratch, WriterTo}, + layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; @@ -7,10 +7,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, - layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, - prepared::GLWESecretPrepared, - }, + layouts::{Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -81,21 +78,6 @@ pub trait BlindRotationKeyAlloc { A: BlindRotationKeyInfos; } -pub trait BlindRotationKeyEncryptSk { - #[allow(clippy::too_many_arguments)] - fn encrypt_sk( - &mut self, - module: &Module, - sk_glwe: &GLWESecretPrepared, - sk_lwe: &LWESecret, - source_xa: &mut Source, - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - DataSkGLWE: DataRef, - DataSkLWE: DataRef; -} - #[derive(Clone)] pub struct BlindRotationKey { pub(crate) keys: Vec>, @@ -103,6 +85,24 @@ pub struct BlindRotationKey { pub(crate) _phantom: PhantomData, } +pub trait BlindRotationKeyFactory { + fn blind_rotation_key_alloc(infos: &A) -> BlindRotationKey, BRA> + where + A: BlindRotationKeyInfos; +} + +impl BlindRotationKey, BRA> +where + Self: BlindRotationKeyFactory, +{ + pub fn alloc(infos: &A) -> BlindRotationKey, BRA> + where + A: BlindRotationKeyInfos, + { + Self::blind_rotation_key_alloc(infos) + } +} + impl fmt::Debug for BlindRotationKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_compressed.rs similarity index 89% rename from poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs rename to poulpy-schemes/src/tfhe/blind_rotation/layouts/key_compressed.rs index 784d332..26539e1 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_compressed.rs @@ -20,6 +20,24 @@ pub struct BlindRotationKeyCompressed { pub(crate) _phantom: PhantomData, } +pub trait BlindRotationKeyCompressedFactory { + fn blind_rotation_key_compressed_alloc(infos: &A) -> BlindRotationKeyCompressed, BRA> + where + A: BlindRotationKeyInfos; +} + +impl BlindRotationKeyCompressed, BRA> +where + Self: BlindRotationKeyCompressedFactory, +{ + pub fn alloc(infos: &A) -> BlindRotationKeyCompressed, BRA> + where + A: BlindRotationKeyInfos, + { + Self::blind_rotation_key_compressed_alloc(infos) + } +} + impl fmt::Debug for BlindRotationKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") diff --git a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs new file mode 100644 index 0000000..5830c84 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs @@ -0,0 +1,108 @@ +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Scratch, SvpPPol}; + +use std::marker::PhantomData; + +use poulpy_core::{ + Distribution, ScratchTakeCore, + layouts::{Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}, +}; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyInfos}; + +pub trait BlindRotationKeyPreparedFactory { + fn blind_rotation_key_prepared_alloc(&self, infos: &A) -> BlindRotationKeyPrepared, BRA, BE> + where + A: BlindRotationKeyInfos; + + fn blind_rotation_key_prepare( + &self, + res: &mut BlindRotationKeyPrepared, + other: &BlindRotationKey, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR: DataRef, + Scratch: ScratchTakeCore; +} + +impl BlindRotationKeyPrepared, BRA, BE> { + pub fn alloc(module: &M, infos: &A) -> Self + where + A: BlindRotationKeyInfos, + M: BlindRotationKeyPreparedFactory, + { + module.blind_rotation_key_prepared_alloc(infos) + } +} + +impl BlindRotationKeyPrepared +where + Scratch: ScratchTakeCore, +{ + pub fn prepare(&mut self, module: &M, other: &BlindRotationKey, scratch: &mut Scratch) + where + M: BlindRotationKeyPreparedFactory, + { + module.blind_rotation_key_prepare(self, other, scratch); + } +} + +#[derive(PartialEq, Eq)] +pub struct BlindRotationKeyPrepared { + pub(crate) data: Vec>, + pub(crate) dist: Distribution, + pub(crate) x_pow_a: Option, B>>>, + pub(crate) _phantom: PhantomData, +} + +impl BlindRotationKeyInfos for BlindRotationKeyPrepared { + fn n_glwe(&self) -> Degree { + self.n() + } + + fn n_lwe(&self) -> Degree { + Degree(self.data.len() as u32) + } +} + +impl LWEInfos for BlindRotationKeyPrepared { + fn base2k(&self) -> Base2K { + self.data[0].base2k() + } + + fn k(&self) -> TorusPrecision { + self.data[0].k() + } + + fn n(&self) -> Degree { + self.data[0].n() + } + + fn size(&self) -> usize { + self.data[0].size() + } +} + +impl GLWEInfos for BlindRotationKeyPrepared { + fn rank(&self) -> Rank { + self.data[0].rank() + } +} +impl GGSWInfos for BlindRotationKeyPrepared { + fn dsize(&self) -> poulpy_core::layouts::Dsize { + Dsize(1) + } + + fn dnum(&self) -> Dnum { + self.data[0].dnum() + } +} + +impl BlindRotationKeyPrepared { + pub fn block_size(&self) -> usize { + match self.dist { + Distribution::BinaryBlock(value) => value, + _ => 1, + } + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/layouts/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/mod.rs new file mode 100644 index 0000000..f3e285f --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/mod.rs @@ -0,0 +1,6 @@ +mod key; +mod key_compressed; +mod key_prepared; +pub use key::*; +pub use key_compressed::*; +pub use key_prepared::*; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/lut.rs b/poulpy-schemes/src/tfhe/blind_rotation/lut.rs index f8e9006..74c0441 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/lut.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/lut.rs @@ -1,3 +1,4 @@ +use poulpy_core::layouts::{Base2K, Degree, TorusPrecision}; use poulpy_hal::{ api::{ ScratchOwnedAlloc, ScratchOwnedBorrow, TakeSlice, VecZnxCopy, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, @@ -13,32 +14,97 @@ pub enum LookUpTableRotationDirection { Right, } -pub struct LookUpTable { +pub struct LookUpTableLayout { + pub n: Degree, + pub extension_factor: usize, + pub k: TorusPrecision, + pub base2k: Base2K, +} + +pub trait LookupTableInfos { + fn n(&self) -> Degree; + fn extension_factor(&self) -> usize; + fn k(&self) -> TorusPrecision; + fn base2k(&self) -> Base2K; + fn size(&self) -> usize; +} + +impl LookupTableInfos for LookUpTableLayout { + fn base2k(&self) -> Base2K { + self.base2k + } + + fn extension_factor(&self) -> usize { + self.extension_factor + } + + fn k(&self) -> TorusPrecision { + self.k + } + + fn size(&self) -> usize { + self.k().as_usize().div_ceil(self.base2k().as_usize()) + } + + fn n(&self) -> Degree { + self.n + } +} + +pub struct LookupTable { pub(crate) data: Vec>>, pub(crate) rot_dir: LookUpTableRotationDirection, - pub(crate) base2k: usize, - pub(crate) k: usize, + pub(crate) base2k: Base2K, + pub(crate) k: TorusPrecision, pub(crate) drift: usize, } -impl LookUpTable { - pub fn alloc(module: &Module, base2k: usize, k: usize, extension_factor: usize) -> Self { +impl LookupTableInfos for LookupTable { + fn base2k(&self) -> Base2K { + self.base2k + } + + fn extension_factor(&self) -> usize { + self.data.len() + } + + fn k(&self) -> TorusPrecision { + self.k + } + + fn n(&self) -> Degree { + self.data[0].n().into() + } + + fn size(&self) -> usize { + self.data[0].size() + } +} + +pub trait LookupTableFactory { + fn lookup_table_set(&self, res: &mut LookupTable, f: &[i64], k: usize); + fn lookup_table_rotate(&self, k: i64, res: &mut LookupTable); +} + +impl LookupTable { + pub fn alloc(infos: &A) -> Self + where + A: LookupTableInfos, + { #[cfg(debug_assertions)] { assert!( - extension_factor & (extension_factor - 1) == 0, - "extension_factor must be a power of two but is: {extension_factor}" + infos.extension_factor() & (infos.extension_factor() - 1) == 0, + "extension_factor must be a power of two but is: {}", + infos.extension_factor() ); } - let size: usize = k.div_ceil(base2k); - let mut data: Vec>> = Vec::with_capacity(extension_factor); - (0..extension_factor).for_each(|_| { - data.push(VecZnx::alloc(module.n(), 1, size)); - }); Self { - data, - base2k, - k, + data: (0..infos.extension_factor()) + .map(|_| VecZnx::alloc(infos.n().into(), 1, infos.size())) + .collect(), + base2k: infos.base2k(), + k: infos.k(), drift: 0, rot_dir: LookUpTableRotationDirection::Left, } @@ -68,115 +134,18 @@ impl LookUpTable { self.rot_dir = rot_dir } - pub fn set(&mut self, module: &Module, f: &[i64], k: usize) + pub fn set(&mut self, module: &M, f: &[i64], k: usize) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxCopy - + VecZnxRotateInplaceTmpBytes, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Scratch: TakeSlice, + M: LookupTableFactory, { - assert!(f.len() <= module.n()); - - let base2k: usize = self.base2k; - - let mut scratch: ScratchOwned = ScratchOwned::alloc(module.vec_znx_normalize_tmp_bytes() | (self.domain_size() << 3)); - - // Get the number minimum limb to store the message modulus - let limbs: usize = k.div_ceil(base2k); - - #[cfg(debug_assertions)] - { - assert!(f.len() <= module.n()); - assert!( - (max_bit_size(f) + (k % base2k) as u32) < i64::BITS, - "overflow: max(|f|) << (k%base2k) > i64::BITS" - ); - assert!(limbs <= self.data[0].size()); - } - - // Scaling factor - let mut scale = 1; - if !k.is_multiple_of(base2k) { - scale <<= base2k - (k % base2k); - } - - // #elements in lookup table - let f_len: usize = f.len(); - - // If LUT size > TakeScalarZnx - let domain_size: usize = self.domain_size(); - - let size: usize = self.k.div_ceil(self.base2k); - - // Equivalent to AUTO([f(0), -f(n-1), -f(n-2), ..., -f(1)], -1) - let mut lut_full: VecZnx> = VecZnx::alloc(domain_size, 1, size); - - let lut_at: &mut [i64] = lut_full.at_mut(0, limbs - 1); - - let step: usize = domain_size.div_round(f_len); - - f.iter().enumerate().for_each(|(i, fi)| { - let start: usize = i * step; - let end: usize = start + step; - lut_at[start..end].fill(fi * scale); - }); - - let drift: usize = step >> 1; - - // Rotates half the step to the left - - if self.extension_factor() > 1 { - let (tmp, _) = scratch.borrow().take_slice(lut_full.n()); - - for i in 0..self.extension_factor() { - module.vec_znx_switch_ring(&mut self.data[i], 0, &lut_full, 0); - if i < self.extension_factor() { - vec_znx_rotate_inplace::<_, ZnxRef>(-1, &mut lut_full, 0, tmp); - } - } - } else { - module.vec_znx_copy(&mut self.data[0], 0, &lut_full, 0); - } - - for a in self.data.iter_mut() { - module.vec_znx_normalize_inplace(self.base2k, a, 0, scratch.borrow()); - } - - self.rotate(module, -(drift as i64)); - - self.drift = drift + module.lookup_table_set(self, f, k); } - #[allow(dead_code)] - pub(crate) fn rotate(&mut self, module: &Module, k: i64) + pub(crate) fn rotate(&mut self, module: &M, k: i64) where - Module: VecZnxRotateInplace + VecZnxRotateInplaceTmpBytes, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + M: LookupTableFactory, { - let extension_factor: usize = self.extension_factor(); - let two_n: usize = 2 * self.data[0].n(); - let two_n_ext: usize = two_n * extension_factor; - - let mut scratch: ScratchOwned<_> = ScratchOwned::alloc(module.vec_znx_rotate_inplace_tmp_bytes()); - - let k_pos: usize = ((k + two_n_ext as i64) % two_n_ext as i64) as usize; - - let k_hi: usize = k_pos / extension_factor; - let k_lo: usize = k_pos % extension_factor; - - (0..extension_factor - k_lo).for_each(|i| { - module.vec_znx_rotate_inplace(k_hi as i64, &mut self.data[i], 0, scratch.borrow()); - }); - - (extension_factor - k_lo..extension_factor).for_each(|i| { - module.vec_znx_rotate_inplace(k_hi as i64 + 1, &mut self.data[i], 0, scratch.borrow()); - }); - - self.data.rotate_right(k_lo); + module.lookup_table_rotate(k, self); } } @@ -204,3 +173,116 @@ fn max_bit_size(vec: &[i64]) -> u32 { .max() .unwrap_or(0) } + +impl LookupTableFactory for Module +where + Self: VecZnxRotateInplace + + VecZnxNormalizeInplace + + VecZnxNormalizeTmpBytes + + VecZnxSwitchRing + + VecZnxCopy + + VecZnxRotateInplaceTmpBytes + + VecZnxRotateInplace + + VecZnxRotateInplaceTmpBytes, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: TakeSlice, +{ + fn lookup_table_set(&self, res: &mut LookupTable, f: &[i64], k: usize) { + assert!(f.len() <= self.n()); + + let base2k: usize = res.base2k.into(); + + let mut scratch: ScratchOwned = ScratchOwned::alloc( + self.vec_znx_normalize_tmp_bytes() + .max(res.domain_size() << 3), + ); + + // Get the number minimum limb to store the message modulus + let limbs: usize = k.div_ceil(base2k); + + #[cfg(debug_assertions)] + { + assert!(f.len() <= self.n()); + assert!( + (max_bit_size(f) + (k % base2k) as u32) < i64::BITS, + "overflow: max(|f|) << (k%base2k) > i64::BITS" + ); + assert!(limbs <= res.data[0].size()); + } + + // Scaling factor + let mut scale = 1; + if !k.is_multiple_of(base2k) { + scale <<= base2k - (k % base2k); + } + + // #elements in lookup table + let f_len: usize = f.len(); + + // If LUT size > TakeScalarZnx + let domain_size: usize = res.domain_size(); + + let size: usize = res.k.div_ceil(res.base2k) as usize; + + // Equivalent to AUTO([f(0), -f(n-1), -f(n-2), ..., -f(1)], -1) + let mut lut_full: VecZnx> = VecZnx::alloc(domain_size, 1, size); + + let lut_at: &mut [i64] = lut_full.at_mut(0, limbs - 1); + + let step: usize = domain_size.div_round(f_len); + + f.iter().enumerate().for_each(|(i, fi)| { + let start: usize = i * step; + let end: usize = start + step; + lut_at[start..end].fill(fi * scale); + }); + + let drift: usize = step >> 1; + + // Rotates half the step to the left + + if res.extension_factor() > 1 { + let (tmp, _) = scratch.borrow().take_slice(lut_full.n()); + + for i in 0..res.extension_factor() { + self.vec_znx_switch_ring(&mut res.data[i], 0, &lut_full, 0); + if i < res.extension_factor() { + vec_znx_rotate_inplace::<_, ZnxRef>(-1, &mut lut_full, 0, tmp); + } + } + } else { + self.vec_znx_copy(&mut res.data[0], 0, &lut_full, 0); + } + + for a in res.data.iter_mut() { + self.vec_znx_normalize_inplace(res.base2k.into(), a, 0, scratch.borrow()); + } + + res.rotate(self, -(drift as i64)); + + res.drift = drift + } + + fn lookup_table_rotate(&self, k: i64, res: &mut LookupTable) { + let extension_factor: usize = res.extension_factor(); + let two_n: usize = 2 * res.data[0].n(); + let two_n_ext: usize = two_n * extension_factor; + + let mut scratch: ScratchOwned<_> = ScratchOwned::alloc(self.vec_znx_rotate_inplace_tmp_bytes()); + + let k_pos: usize = ((k + two_n_ext as i64) % two_n_ext as i64) as usize; + + let k_hi: usize = k_pos / extension_factor; + let k_lo: usize = k_pos % extension_factor; + + (0..extension_factor - k_lo).for_each(|i| { + self.vec_znx_rotate_inplace(k_hi as i64, &mut res.data[i], 0, scratch.borrow()); + }); + + (extension_factor - k_lo..extension_factor).for_each(|i| { + self.vec_znx_rotate_inplace(k_hi as i64 + 1, &mut res.data[i], 0, scratch.borrow()); + }); + + res.data.rotate_right(k_lo); + } +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs index 8cc262b..93da18b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs @@ -1,35 +1,11 @@ -mod cggi_algo; -mod cggi_key; -mod key; -mod key_compressed; -mod key_prepared; +mod algorithms; +mod encryption; +mod layouts; mod lut; mod utils; -pub use cggi_algo::*; -pub use key::*; -pub use key_compressed::*; -pub use key_prepared::*; +pub use algorithms::*; +pub use encryption::*; +pub use layouts::*; pub use lut::*; - pub mod tests; - -use poulpy_core::layouts::{GLWE, LWE}; -use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; - -pub trait BlindRotationAlgo {} - -#[derive(Clone)] -pub struct CGGI {} -impl BlindRotationAlgo for CGGI {} - -pub trait BlincRotationExecute { - fn execute( - &self, - module: &Module, - res: &mut GLWE, - lwe: &LWE, - lut: &LookUpTable, - scratch: &mut Scratch, - ); -} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 4b8131a..8d0a017 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -1,88 +1,40 @@ use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAdd, VecZnxDftAddInplace, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftSubInplace, VecZnxDftZero, VecZnxFillUniform, VecZnxIdftApply, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpBytes, VecZnxMulXpMinusOneInplace, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, - ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned, ZnxView}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, - TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl, - }, + api::{ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, Scratch, ScratchOwned, ZnxView}, source::Source, }; use crate::tfhe::blind_rotation::{ - BlincRotationExecute, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyLayout, - BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_tmp_bytes, mod_switch_2n, + BlindRotationAlgo, BlindRotationExecute, BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory, + BlindRotationKeyLayout, BlindRotationKeyPrepared, BlindRotationKeyPreparedFactory, LookUpTableLayout, LookupTable, + LookupTableFactory, mod_switch_2n, }; -use poulpy_core::layouts::{ - GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, LWEPlaintext, LWESecret, LWEToRef, - prepared::{GLWESecretPrepared, PrepareAlloc}, +use poulpy_core::{ + GLWEDecrypt, LWEEncryptSk, ScratchTakeCore, + layouts::{ + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, LWE, LWEInfos, LWELayout, LWEPlaintext, + LWESecret, LWEToRef, prepared::GLWESecretPrepared, + }, }; -pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) -where - Module: VecZnxBigBytesOf - + VecZnxDftBytesOf - + SvpPPolBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpBytes - + VecZnxIdftApply - + VecZnxDftAdd - + VecZnxDftAddInplace - + VecZnxDftApply - + VecZnxDftZero - + SvpApplyDftToDft - + VecZnxDftSubInplace - + VecZnxBigAddSmallInplace - + VecZnxRotate - + VecZnxAddInplace - + VecZnxSubInplace - + VecZnxNormalize - + VecZnxNormalizeInplace - + VecZnxCopy - + VecZnxMulXpMinusOneInplace - + SvpPrepare - + SvpPPolAlloc - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxAddScalarInplace - + VecZnxRotateInplace - + VecZnxSwitchRing - + VecZnxSub - + VmpPMatAlloc - + VmpPrepare - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + ZnFillUniform - + ZnAddNormal - + VecZnxRotateInplaceTmpBytes - + ZnNormalizeInplace, - B: Backend - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, +pub fn test_blind_rotation( + module: &M, + n_lwe: usize, + block_size: usize, + extension_factor: usize, +) where + M: BlindRotationKeyEncryptSk + + BlindRotationKeyPreparedFactory + + BlindRotationExecute + + GLWESecretPreparedFactory + + BlindRotationExecute + + LWEEncryptSk + + LookupTableFactory + + GLWEDecrypt, + BlindRotationKey, BRA>: BlindRotationKeyFactory, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let n_glwe: usize = module.n(); let base2k: usize = 19; @@ -123,18 +75,17 @@ where base2k: base2k.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::generate_from_sk_tmp_bytes( - module, &brk_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::::alloc(BlindRotationKey::encrypt_sk_tmp_bytes(module, &brk_infos)); let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_dft: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_dft: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(module, &glwe_infos); + sk_glwe_dft.prepare(module, &sk_glwe); let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(cggi_blind_rotate_tmp_bytes( + let mut scratch_br: ScratchOwned = ScratchOwned::::alloc(BlindRotationKeyPrepared::execute_tmp_bytes( module, block_size, extension_factor, @@ -142,7 +93,7 @@ where &brk_infos, )); - let mut brk: BlindRotationKey, CGGI> = BlindRotationKey::, CGGI>::alloc(&brk_infos); + let mut brk: BlindRotationKey, BRA> = BlindRotationKey::, BRA>::alloc(&brk_infos); brk.encrypt_sk( module, @@ -171,12 +122,20 @@ where .enumerate() .for_each(|(i, x)| *x = f(i as i64)); - let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); + let lut_infos = LookUpTableLayout { + n: module.n().into(), + extension_factor, + k: k_lut.into(), + base2k: base2k.into(), + }; + + let mut lut: LookupTable = LookupTable::alloc(&lut_infos); lut.set(module, &f_vec, log_message_modulus + 1); let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_infos); - let brk_prepared: BlindRotationKeyPrepared, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); + let mut brk_prepared: BlindRotationKeyPrepared, BRA, BE> = BlindRotationKeyPrepared::alloc(module, &brk); + brk_prepared.prepare(module, &brk, scratch_br.borrow()); brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_lut.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_lut.rs index bb6492c..80d7663 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_lut.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_lut.rs @@ -1,25 +1,12 @@ use std::vec; -use poulpy_hal::{ - api::{ - VecZnxCopy, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, - VecZnxSwitchRing, - }, - layouts::{Backend, Module}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeSliceImpl}, -}; +use poulpy_hal::api::ModuleN; -use crate::tfhe::blind_rotation::{DivRound, LookUpTable}; +use crate::tfhe::blind_rotation::{DivRound, LookUpTableLayout, LookupTable, LookupTableFactory}; -pub fn test_lut_standard(module: &Module) +pub fn test_lut_standard(module: &M) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxCopy - + VecZnxRotateInplaceTmpBytes, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeSliceImpl, + M: LookupTableFactory + ModuleN, { let base2k: usize = 20; let k_lut: usize = 40; @@ -33,7 +20,14 @@ where .enumerate() .for_each(|(i, x)| *x = (i as i64) - 8); - let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); + let lut_infos: LookUpTableLayout = LookUpTableLayout { + n: module.n().into(), + extension_factor, + k: k_lut.into(), + base2k: base2k.into(), + }; + + let mut lut: LookupTable = LookupTable::alloc(&lut_infos); lut.set(module, &f, log_scale); let half_step: i64 = lut.domain_size().div_round(message_modulus << 1) as i64; @@ -51,15 +45,9 @@ where }); } -pub fn test_lut_extended(module: &Module) +pub fn test_lut_extended(module: &M) where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxCopy - + VecZnxRotateInplaceTmpBytes, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeSliceImpl, + M: LookupTableFactory + ModuleN, { let base2k: usize = 20; let k_lut: usize = 40; @@ -73,7 +61,14 @@ where .enumerate() .for_each(|(i, x)| *x = (i as i64) - 8); - let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); + let lut_infos: LookUpTableLayout = LookUpTableLayout { + n: module.n().into(), + extension_factor, + k: k_lut.into(), + base2k: base2k.into(), + }; + + let mut lut: LookupTable = LookupTable::alloc(&lut_infos); lut.set(module, &f, log_scale); let half_step: i64 = lut.domain_size().div_round(message_modulus << 1) as i64; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs index f25a236..341dd49 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_serialization.rs @@ -1,8 +1,6 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; -use crate::tfhe::blind_rotation::{ - BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyCompressed, BlindRotationKeyLayout, CGGI, -}; +use crate::tfhe::blind_rotation::{BlindRotationKey, BlindRotationKeyCompressed, BlindRotationKeyLayout, CGGI}; #[test] fn test_cggi_blind_rotation_key_serialization() { @@ -14,7 +12,6 @@ fn test_cggi_blind_rotation_key_serialization() { dnum: 2_usize.into(), rank: 2_usize.into(), }; - let original: BlindRotationKey, CGGI> = BlindRotationKey::alloc(&layout); test_reader_writer_interface(original); } @@ -29,7 +26,6 @@ fn test_cggi_blind_rotation_key_compressed_serialization() { dnum: 2_usize.into(), rank: 2_usize.into(), }; - let original: BlindRotationKeyCompressed, CGGI> = BlindRotationKeyCompressed::alloc(&layout); test_reader_writer_interface(original); } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs deleted file mode 100644 index ecb2421..0000000 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/fft64.rs +++ /dev/null @@ -1,37 +0,0 @@ -use poulpy_backend::cpu_spqlios::FFT64Spqlios; -use poulpy_hal::{api::ModuleNew, layouts::Module}; - -use crate::tfhe::blind_rotation::tests::{ - generic_blind_rotation::test_blind_rotation, - generic_lut::{test_lut_extended, test_lut_standard}, -}; - -#[test] -fn lut_standard() { - let module: Module = Module::::new(32); - test_lut_standard(&module); -} - -#[test] -fn lut_extended() { - let module: Module = Module::::new(32); - test_lut_extended(&module); -} - -#[test] -fn standard() { - let module: Module = Module::::new(512); - test_blind_rotation(&module, 224, 1, 1); -} - -#[test] -fn block_binary() { - let module: Module = Module::::new(512); - test_blind_rotation(&module, 224, 7, 1); -} - -#[test] -fn block_binary_extended() { - let module: Module = Module::::new(512); - test_blind_rotation(&module, 224, 7, 2); -} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs deleted file mode 100644 index aebaafb..0000000 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/cpu_spqlios/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod fft64; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/fft64.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/fft64.rs new file mode 100644 index 0000000..5a471e2 --- /dev/null +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/fft64.rs @@ -0,0 +1,40 @@ +use poulpy_backend::cpu_fft64_ref::FFT64Ref; +use poulpy_hal::{api::ModuleNew, layouts::Module}; + +use crate::tfhe::blind_rotation::{ + CGGI, + tests::{ + generic_blind_rotation::test_blind_rotation, + generic_lut::{test_lut_extended, test_lut_standard}, + }, +}; + +#[test] +fn lut_standard() { + let module: Module = Module::::new(32); + test_lut_standard(&module); +} + +#[test] +fn lut_extended() { + let module: Module = Module::::new(32); + test_lut_extended(&module); +} + +#[test] +fn standard() { + let module: Module = Module::::new(512); + test_blind_rotation::(&module, 224, 1, 1); +} + +#[test] +fn block_binary() { + let module: Module = Module::::new(512); + test_blind_rotation::(&module, 224, 7, 1); +} + +#[test] +fn block_binary_extended() { + let module: Module = Module::::new(512); + test_blind_rotation::(&module, 224, 7, 2); +} diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs index f2bc1d4..aebaafb 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/implementation/mod.rs @@ -1 +1 @@ -mod cpu_spqlios; +mod fft64; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index a471ffc..81f99e6 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,14 +1,13 @@ use poulpy_core::layouts::{ AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, TensorKeyLayout, - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, }; use std::collections::HashMap; use poulpy_hal::{ api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, - TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, + ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index 5835765..ed3b6a1 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -1,6 +1,8 @@ mod circuit; mod key; -pub mod tests; + +//[cfg(tests)] +//pub mod tests; pub use circuit::*; pub use key::*; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs index f9bc7d9..22f8f4f 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs @@ -1,4 +1,3 @@ pub mod circuit_bootstrapping; -#[cfg(test)] mod implementation; diff --git a/poulpy-schemes/src/tfhe/mod.rs b/poulpy-schemes/src/tfhe/mod.rs index 85c84d4..cc2dbe9 100644 --- a/poulpy-schemes/src/tfhe/mod.rs +++ b/poulpy-schemes/src/tfhe/mod.rs @@ -1,3 +1,3 @@ -pub mod bdd_arithmetic; +// pub mod bdd_arithmetic; pub mod blind_rotation; -pub mod circuit_bootstrapping; +//pub mod circuit_bootstrapping; From 706ecf3d07a7dac91eb30fe79aeae4398f8ca1ac Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 22 Oct 2025 10:00:32 +0200 Subject: [PATCH 58/60] compiling CBT but failing tests --- poulpy-core/benches/keyswitch_glwe_fft64.rs | 6 +- poulpy-core/src/automorphism/gglwe_atk.rs | 19 +- poulpy-core/src/automorphism/ggsw_ct.rs | 14 +- poulpy-core/src/automorphism/glwe_ct.rs | 2 +- poulpy-core/src/conversion/gglwe_to_ggsw.rs | 14 +- .../compressed/glwe_automorphism_key.rs | 20 +- .../encryption/compressed/glwe_tensor_key.rs | 32 +- .../src/encryption/glwe_automorphism_key.rs | 42 +-- poulpy-core/src/encryption/glwe_tensor_key.rs | 32 +- poulpy-core/src/external_product/gglwe.rs | 6 +- poulpy-core/src/glwe_trace.rs | 25 +- poulpy-core/src/keyswitching/gglwe.rs | 6 +- poulpy-core/src/keyswitching/ggsw.rs | 10 +- .../compressed/glwe_automorphism_key.rs | 6 +- .../src/layouts/compressed/glwe_tensor_key.rs | 34 +- .../src/layouts/glwe_automorphism_key.rs | 44 +-- poulpy-core/src/layouts/glwe_tensor_key.rs | 54 +-- poulpy-core/src/layouts/lwe.rs | 3 + .../src/layouts/prepared/glwe_tensor_key.rs | 72 ++-- poulpy-core/src/lib.rs | 1 + poulpy-core/src/scratch.rs | 20 +- poulpy-core/src/tests/serialization.rs | 8 +- .../test_suite/automorphism/gglwe_atk.rs | 42 +-- .../tests/test_suite/automorphism/ggsw_ct.rs | 52 +-- .../tests/test_suite/automorphism/glwe_ct.rs | 22 +- .../tests/test_suite/encryption/gglwe_atk.rs | 23 +- .../tests/test_suite/encryption/glwe_tsk.rs | 23 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 30 +- poulpy-core/src/tests/test_suite/packing.rs | 14 +- poulpy-core/src/tests/test_suite/trace.rs | 14 +- poulpy-hal/src/layouts/module.rs | 17 +- .../benches/circuit_bootstrapping.rs | 216 +++--------- .../examples/circuit_bootstrapping.rs | 36 +- .../blind_rotation/algorithms/cggi/key.rs | 2 +- .../algorithms/cggi/key_prepared.rs | 2 +- .../src/tfhe/blind_rotation/encryption/key.rs | 6 +- .../blind_rotation/layouts/key_prepared.rs | 16 +- .../tests/generic_blind_rotation.rs | 11 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 321 +++++++++--------- .../src/tfhe/circuit_bootstrapping/key.rs | 234 ++++++------- .../circuit_bootstrapping/key_compressed.rs | 13 + .../circuit_bootstrapping/key_prepared.rs | 136 ++++++++ .../src/tfhe/circuit_bootstrapping/mod.rs | 36 +- .../tests/circuit_bootstrapping.rs | 243 ++++--------- .../tfhe/circuit_bootstrapping/tests/fft64.rs | 21 ++ .../tests/implementation/cpu_spqlios/fft64.rs | 21 -- .../tests/implementation/cpu_spqlios/mod.rs | 1 - .../tests/implementation/mod.rs | 1 - .../tfhe/circuit_bootstrapping/tests/mod.rs | 2 +- poulpy-schemes/src/tfhe/mod.rs | 2 +- 50 files changed, 967 insertions(+), 1060 deletions(-) create mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs create mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs create mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs delete mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs delete mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs delete mode 100644 poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 2833bee..e0ca001 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,5 +1,5 @@ use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + Base2K, Degree, Dnum, Dsize, GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWELayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPrepared, Rank, TorusPrecision, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }; @@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); - let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let gglwe_atk_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n, base2k, k: k_gglwe, @@ -62,7 +62,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ksk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&gglwe_atk_layout); let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 6918ee1..a291f96 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,11 +7,12 @@ use crate::{ ScratchTakeCore, automorphism::glwe_ct::GLWEAutomorphism, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, GetGaloisElement, SetGaloisElement, + GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWE, GLWEAutomorphismKey, GetGaloisElement, + SetGaloisElement, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn automorphism_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, @@ -19,11 +20,11 @@ impl AutomorphismKey> { K: GGLWEInfos, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) + module.glwe_automorphism_key_automorphism_tmp_bytes(res_infos, a_infos, key_infos) } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn automorphism(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where A: GGLWEToRef + GetGaloisElement + GGLWEInfos, @@ -31,7 +32,7 @@ impl AutomorphismKey { Scratch: ScratchTakeCore, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism(self, a, key, scratch); + module.glwe_automorphism_key_automorphism(self, a, key, scratch); } pub fn automorphism_inplace(&mut self, module: &M, key: &K, scratch: &mut Scratch) @@ -40,7 +41,7 @@ impl AutomorphismKey { Scratch: ScratchTakeCore, M: GLWEAutomorphismKeyAutomorphism, { - module.automorphism_key_automorphism_inplace(self, key, scratch); + module.glwe_automorphism_key_automorphism_inplace(self, key, scratch); } } @@ -53,7 +54,7 @@ pub trait GLWEAutomorphismKeyAutomorphism where Self: GaloisElement + GLWEAutomorphism + VecZnxAutomorphism, { - fn automorphism_key_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + fn glwe_automorphism_key_automorphism_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos, @@ -62,7 +63,7 @@ where self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } - fn automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) + fn glwe_automorphism_key_automorphism(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch) where R: GGLWEToMut + SetGaloisElement + GGLWEInfos, A: GGLWEToRef + GetGaloisElement + GGLWEInfos, @@ -118,7 +119,7 @@ where res.set_p((p * key.p()) % (self.cyclotomic_order() as i64)); } - fn automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) + fn glwe_automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) where R: GGLWEToMut + SetGaloisElement + GetGaloisElement + GGLWEInfos, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index cf77758..fb54f6d 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ automorphism::glwe_ct::GLWEAutomorphism, layouts::{ GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetGaloisElement, - prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + prepared::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef}, }, }; @@ -36,7 +36,7 @@ impl GGSW { where A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, { @@ -46,7 +46,7 @@ impl GGSW { pub fn automorphism_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWAutomorphism, { @@ -79,12 +79,12 @@ where R: GGSWToMut, A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGSW<&[u8]> = &a.to_ref(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); assert_eq!(res.dsize(), a.dsize()); assert!(res.dnum() <= a.dnum()); @@ -104,11 +104,11 @@ where where R: GGSWToMut, K: GetGaloisElement + GGLWEPreparedToRef + GGLWEInfos, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); // Keyswitch the j-th row of the col 0 for row in 0..res.dnum().as_usize() { diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 2c05af5..7161239 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -54,7 +54,7 @@ impl GLWE { module.glwe_automorphism_sub(self, a, key, scratch); } - pub fn glwe_automorphism_sub_negate(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) + pub fn automorphism_sub_negate(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch) where M: GLWEAutomorphism, A: GLWEToRef, diff --git a/poulpy-core/src/conversion/gglwe_to_ggsw.rs b/poulpy-core/src/conversion/gglwe_to_ggsw.rs index 87b6791..b33759e 100644 --- a/poulpy-core/src/conversion/gglwe_to_ggsw.rs +++ b/poulpy-core/src/conversion/gglwe_to_ggsw.rs @@ -11,7 +11,7 @@ use crate::{ GLWECopy, ScratchTakeCore, layouts::{ GGLWE, GGLWEInfos, GGLWEToRef, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, - prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + prepared::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef}, }, }; @@ -31,7 +31,7 @@ impl GGSW { where M: GGSWFromGGLWE, G: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { module.ggsw_from_gglwe(self, gglwe, tsk, scratch); @@ -54,12 +54,12 @@ where where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let a: &GGLWE<&[u8]> = &a.to_ref(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); assert_eq!(res.rank(), a.rank_out()); assert_eq!(res.dnum(), a.dnum()); @@ -85,7 +85,7 @@ pub trait GGSWFromGGLWE { where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore; } @@ -158,11 +158,11 @@ where fn ggsw_expand_row(&self, res: &mut R, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); - let tsk: &TensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); + let tsk: &GLWETensorKeyPrepared<&[u8], BE> = &tsk.to_ref(); let basek_in: usize = res.base2k().into(); let basek_tsk: usize = tsk.base2k().into(); diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index df57f0f..fb1d567 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -16,9 +16,9 @@ impl GLWEAutomorphismKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyCompressedEncryptSk, + M: GLWEAutomorphismKeyCompressedEncryptSk, { - module.automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -34,18 +34,18 @@ impl GLWEAutomorphismKeyCompressed { scratch: &mut Scratch, ) where S: GLWESecretToRef + GLWEInfos, - M: AutomorphismKeyCompressedEncryptSk, + M: GLWEAutomorphismKeyCompressedEncryptSk, { - module.automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); + module.glwe_automorphism_key_compressed_encrypt_sk(self, p, sk, seed_xa, source_xe, scratch); } } -pub trait AutomorphismKeyCompressedEncryptSk { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyCompressedEncryptSk { + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_compressed_encrypt_sk( + fn glwe_automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, @@ -58,12 +58,12 @@ pub trait AutomorphismKeyCompressedEncryptSk { S: GLWESecretToRef + GLWEInfos; } -impl AutomorphismKeyCompressedEncryptSk for Module +impl GLWEAutomorphismKeyCompressedEncryptSk for Module where Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -73,7 +73,7 @@ where + GLWESecretPrepared::bytes_of_from_infos(self, infos) } - fn automorphism_key_compressed_encrypt_sk( + fn glwe_automorphism_key_compressed_encrypt_sk( &self, res: &mut R, p: i64, diff --git a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs index b50b47d..14c9217 100644 --- a/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_tensor_key.rs @@ -8,10 +8,10 @@ use poulpy_hal::{ }; use crate::{ - GGLWECompressedEncryptSk, GetDistribution, ScratchTakeCore, TensorKeyEncryptSk, + GGLWECompressedEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, Rank, - TensorKeyCompressedAtMut, compressed::GLWETensorKeyCompressed, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, + GLWETensorKeyCompressedAtMut, LWEInfos, Rank, compressed::GLWETensorKeyCompressed, }, }; @@ -19,9 +19,9 @@ impl GLWETensorKeyCompressed> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWETensorKeyCompressedEncryptSk, + M: GLWETensorKeyCompressedEncryptSk, { - module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } } @@ -35,18 +35,18 @@ impl GLWETensorKeyCompressed { scratch: &mut Scratch, ) where S: GLWESecretToRef + GetDistribution, - M: GGLWETensorKeyCompressedEncryptSk, + M: GLWETensorKeyCompressedEncryptSk, { - module.tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); + module.glwe_tensor_key_compressed_encrypt_sk(self, sk, seed_xa, source_xe, scratch); } } -pub trait GGLWETensorKeyCompressedEncryptSk { - fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWETensorKeyCompressedEncryptSk { + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_compressed_encrypt_sk( + fn glwe_tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -55,15 +55,15 @@ pub trait GGLWETensorKeyCompressedEncryptSk { scratch: &mut Scratch, ) where D: DataMut, - R: TensorKeyCompressedAtMut + GGLWEInfos, + R: GLWETensorKeyCompressedAtMut + GGLWEInfos, S: GLWESecretToRef + GetDistribution; } -impl GGLWETensorKeyCompressedEncryptSk for Module +impl GLWETensorKeyCompressedEncryptSk for Module where Self: ModuleN + GGLWECompressedEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + VecZnxDftApply + SvpApplyDftToDft + VecZnxIdftApplyTmpA @@ -75,7 +75,7 @@ where + GLWESecretPreparedFactory, Scratch: ScratchTakeBasic + ScratchTakeCore, { - fn tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -87,7 +87,7 @@ where + self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } - fn tensor_key_compressed_encrypt_sk( + fn glwe_tensor_key_compressed_encrypt_sk( &self, res: &mut R, sk: &S, @@ -96,7 +96,7 @@ where scratch: &mut Scratch, ) where D: DataMut, - R: GGLWEInfos + TensorKeyCompressedAtMut, + R: GGLWEInfos + GLWETensorKeyCompressedAtMut, S: GLWESecretToRef + GetDistribution, { let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank()); diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index cee3163..429a6d7 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -7,30 +7,30 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, + GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEAutomorphismKey, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyEncryptSk, + M: GLWEAutomorphismKeyEncryptSk, { - module.automorphism_key_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_sk_tmp_bytes(infos) } pub fn encrypt_pk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptPk, + M: GLWEAutomorphismKeyEncryptPk, { - module.automorphism_key_encrypt_pk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_pk_tmp_bytes(infos) } } -impl AutomorphismKey +impl GLWEAutomorphismKey where Self: GGLWEToRef, { @@ -44,18 +44,18 @@ where scratch: &mut Scratch, ) where S: GLWESecretToRef, - M: AutomorphismKeyEncryptSk, + M: GLWEAutomorphismKeyEncryptSk, { - module.automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); + module.glwe_automorphism_key_encrypt_sk(self, p, sk, source_xa, source_xe, scratch); } } -pub trait AutomorphismKeyEncryptSk { - fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptSk { + fn glwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_encrypt_sk( + fn glwe_automorphism_key_encrypt_sk( &self, res: &mut R, p: i64, @@ -68,12 +68,12 @@ pub trait AutomorphismKeyEncryptSk { S: GLWESecretToRef; } -impl AutomorphismKeyEncryptSk for Module +impl GLWEAutomorphismKeyEncryptSk for Module where Self: GGLWEEncryptSk + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedFactory, Scratch: ScratchTakeCore, { - fn automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_automorphism_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -88,7 +88,7 @@ where .max(GLWESecret::bytes_of_from_infos(infos)) } - fn automorphism_key_encrypt_sk( + fn glwe_automorphism_key_encrypt_sk( &self, res: &mut R, p: i64, @@ -106,10 +106,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= self.automorphism_key_encrypt_sk_tmp_bytes(res), + scratch.available() >= self.glwe_automorphism_key_encrypt_sk_tmp_bytes(res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_tmp_bytes: {:?}", scratch.available(), - self.automorphism_key_encrypt_sk_tmp_bytes(res) + self.glwe_automorphism_key_encrypt_sk_tmp_bytes(res) ); let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); @@ -141,18 +141,18 @@ where } } -pub trait GGLWEAutomorphismKeyEncryptPk { - fn automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptPk { + fn glwe_automorphism_key_encrypt_pk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; } -impl GGLWEAutomorphismKeyEncryptPk for Module +impl GLWEAutomorphismKeyEncryptPk for Module where Self:, Scratch: ScratchTakeCore, { - fn automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize + fn glwe_automorphism_key_encrypt_pk_tmp_bytes(&self, _infos: &A) -> usize where A: GGLWEInfos, { diff --git a/poulpy-core/src/encryption/glwe_tensor_key.rs b/poulpy-core/src/encryption/glwe_tensor_key.rs index e151b8e..b7afae5 100644 --- a/poulpy-core/src/encryption/glwe_tensor_key.rs +++ b/poulpy-core/src/encryption/glwe_tensor_key.rs @@ -10,22 +10,22 @@ use poulpy_hal::{ use crate::{ GGLWEEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ - GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, TensorKeyToMut, + GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWETensorKey, GLWETensorKeyToMut, LWEInfos, Rank, prepared::{GLWESecretPrepared, GLWESecretPreparedFactory}, }, }; -impl TensorKey> { +impl GLWETensorKey> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyEncryptSk, + M: GLWETensorKeyEncryptSk, { - module.tensor_key_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_encrypt_sk_tmp_bytes(infos) } } -impl TensorKey { +impl GLWETensorKey { pub fn encrypt_sk( &mut self, module: &M, @@ -34,20 +34,20 @@ impl TensorKey { source_xe: &mut Source, scratch: &mut Scratch, ) where - M: TensorKeyEncryptSk, + M: GLWETensorKeyEncryptSk, S: GLWESecretToRef + GetDistribution + GLWEInfos, Scratch: ScratchTakeCore, { - module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); + module.glwe_tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch); } } -pub trait TensorKeyEncryptSk { - fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize +pub trait GLWETensorKeyEncryptSk { + fn glwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_encrypt_sk( + fn glwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -55,11 +55,11 @@ pub trait TensorKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyToMut, + R: GLWETensorKeyToMut, S: GLWESecretToRef + GetDistribution + GLWEInfos; } -impl TensorKeyEncryptSk for Module +impl GLWETensorKeyEncryptSk for Module where Self: ModuleN + GGLWEEncryptSk @@ -72,7 +72,7 @@ where + VecZnxBigNormalize, Scratch: ScratchTakeCore, { - fn tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize + fn glwe_tensor_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -84,7 +84,7 @@ where + GGLWE::encrypt_sk_tmp_bytes(self, infos) } - fn tensor_key_encrypt_sk( + fn glwe_tensor_key_encrypt_sk( &self, res: &mut R, sk: &S, @@ -92,10 +92,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: TensorKeyToMut, + R: GLWETensorKeyToMut, S: GLWESecretToRef + GetDistribution + GLWEInfos, { - let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWETensorKey<&mut [u8]> = &mut res.to_mut(); // let n: RingDegree = sk.n(); let rank: Rank = res.rank_out(); diff --git a/poulpy-core/src/external_product/gglwe.rs b/poulpy-core/src/external_product/gglwe.rs index a9537c1..437cf39 100644 --- a/poulpy-core/src/external_product/gglwe.rs +++ b/poulpy-core/src/external_product/gglwe.rs @@ -3,12 +3,12 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::{ GLWEExternalProduct, ScratchTakeCore, layouts::{ - AutomorphismKey, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GGSWPrepared, GLWEInfos, GLWESwitchingKey, + GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GGSWInfos, GGSWPrepared, GLWEAutomorphismKey, GLWEInfos, GLWESwitchingKey, prepared::GGSWPreparedToRef, }, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn external_product_tmp_bytes( &self, module: &M, @@ -26,7 +26,7 @@ impl AutomorphismKey> { } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn external_product(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where M: GGLWEExternalProduct, diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 80cf84c..bfcb6a3 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use poulpy_hal::{ api::ModuleLogN, - layouts::{Backend, DataMut, GaloisElement, Module, Scratch, VecZnx}, + layouts::{Backend, DataMut, GaloisElement, Module, Scratch, VecZnx, galois_element}, }; use crate::{ @@ -70,20 +70,25 @@ impl GLWETrace for Module where { } +#[inline(always)] +pub fn trace_galois_elements(log_n: usize, cyclotomic_order: i64) -> Vec { + (0..log_n) + .map(|i| { + if i == 0 { + -1 + } else { + galois_element(1 << (i - 1), cyclotomic_order) + } + }) + .collect() +} + pub trait GLWETrace where Self: ModuleLogN + GaloisElement + GLWEAutomorphism + GLWEShift + GLWECopy, { fn glwe_trace_galois_elements(&self) -> Vec { - (0..self.log_n()) - .map(|i| { - if i == 0 { - -1 - } else { - self.galois_element(1 << (i - 1)) - } - }) - .collect() + trace_galois_elements(self.log_n(), self.cyclotomic_order()) } fn glwe_trace_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize diff --git a/poulpy-core/src/keyswitching/gglwe.rs b/poulpy-core/src/keyswitching/gglwe.rs index 1f9232f..d837002 100644 --- a/poulpy-core/src/keyswitching/gglwe.rs +++ b/poulpy-core/src/keyswitching/gglwe.rs @@ -3,10 +3,10 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ ScratchTakeCore, keyswitching::GLWEKeyswitch, - layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, + layouts::{GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWEAutomorphismKey, GLWESwitchingKey}, }; -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn keyswitch_tmp_bytes(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, @@ -18,7 +18,7 @@ impl AutomorphismKey> { } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn keyswitch(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch) where A: GGLWEToRef + GGLWEToRef, diff --git a/poulpy-core/src/keyswitching/ggsw.rs b/poulpy-core/src/keyswitching/ggsw.rs index 334572d..231b071 100644 --- a/poulpy-core/src/keyswitching/ggsw.rs +++ b/poulpy-core/src/keyswitching/ggsw.rs @@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, VecZnx}; use crate::{ GGSWExpandRows, ScratchTakeCore, keyswitching::GLWEKeyswitch, - layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::TensorKeyPreparedToRef}, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, prepared::GLWETensorKeyPreparedToRef}, }; impl GGSW> { @@ -30,7 +30,7 @@ impl GGSW { where A: GGSWToRef, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { @@ -40,7 +40,7 @@ impl GGSW { pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { @@ -91,7 +91,7 @@ where R: GGSWToMut, A: GGSWToRef, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); @@ -113,7 +113,7 @@ where where R: GGSWToMut, K: GGLWEPreparedToRef, - T: TensorKeyPreparedToRef, + T: GLWETensorKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); diff --git a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs index 6760d98..3e0a4b5 100644 --- a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs @@ -4,8 +4,8 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, - GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWECompressedToRef, + GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEAutomorphismKey, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -154,7 +154,7 @@ where impl AutomorphismKeyDecompress for Module where Self: GLWEDecompress {} -impl AutomorphismKey +impl GLWEAutomorphismKey where Self: SetGaloisElement, { diff --git a/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs index e8752d3..6939ff2 100644 --- a/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_tensor_key.rs @@ -5,7 +5,7 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, - GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, + GLWEInfos, GLWETensorKey, GLWETensorKeyToMut, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -149,11 +149,11 @@ impl WriterTo for GLWETensorKeyCompressed { } } -pub trait TensorKeyCompressedAtRef { +pub trait GLWETensorKeyCompressedAtRef { fn at(&self, i: usize, j: usize) -> &GGLWECompressed; } -impl TensorKeyCompressedAtRef for GLWETensorKeyCompressed { +impl GLWETensorKeyCompressedAtRef for GLWETensorKeyCompressed { fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed { if i > j { std::mem::swap(&mut i, &mut j); @@ -163,11 +163,11 @@ impl TensorKeyCompressedAtRef for GLWETensorKeyCompressed { } } -pub trait TensorKeyCompressedAtMut { +pub trait GLWETensorKeyCompressedAtMut { fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed; } -impl TensorKeyCompressedAtMut for GLWETensorKeyCompressed { +impl GLWETensorKeyCompressedAtMut 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); @@ -177,16 +177,16 @@ impl TensorKeyCompressedAtMut for GLWETensorKeyCompressed { } } -pub trait TensorKeyDecompress +pub trait GLWETensorKeyDecompress where Self: GGLWEDecompress, { fn decompress_tensor_key(&self, res: &mut R, other: &O) where - R: TensorKeyToMut, - O: TensorKeyCompressedToRef, + R: GLWETensorKeyToMut, + O: GLWETensorKeyCompressedToRef, { - let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWETensorKey<&mut [u8]> = &mut res.to_mut(); let other: &GLWETensorKeyCompressed<&[u8]> = &other.to_ref(); assert_eq!( @@ -203,23 +203,23 @@ where } } -impl TensorKeyDecompress for Module where Self: GGLWEDecompress {} +impl GLWETensorKeyDecompress for Module where Self: GGLWEDecompress {} -impl TensorKey { +impl GLWETensorKey { pub fn decompress(&mut self, module: &M, other: &O) where - O: TensorKeyCompressedToRef, - M: TensorKeyDecompress, + O: GLWETensorKeyCompressedToRef, + M: GLWETensorKeyDecompress, { module.decompress_tensor_key(self, other); } } -pub trait TensorKeyCompressedToMut { +pub trait GLWETensorKeyCompressedToMut { fn to_mut(&mut self) -> GLWETensorKeyCompressed<&mut [u8]>; } -impl TensorKeyCompressedToMut for GLWETensorKeyCompressed +impl GLWETensorKeyCompressedToMut for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToMut, { @@ -230,11 +230,11 @@ where } } -pub trait TensorKeyCompressedToRef { +pub trait GLWETensorKeyCompressedToRef { fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]>; } -impl TensorKeyCompressedToRef for GLWETensorKeyCompressed +impl GLWETensorKeyCompressedToRef for GLWETensorKeyCompressed where GGLWECompressed: GGLWECompressedToRef, { diff --git a/poulpy-core/src/layouts/glwe_automorphism_key.rs b/poulpy-core/src/layouts/glwe_automorphism_key.rs index c15c2db..a378e6d 100644 --- a/poulpy-core/src/layouts/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/glwe_automorphism_key.rs @@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct AutomorphismKeyLayout { +pub struct GLWEAutomorphismKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -21,7 +21,7 @@ pub struct AutomorphismKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct AutomorphismKey { +pub struct GLWEAutomorphismKey { pub(crate) key: GGLWE, pub(crate) p: i64, } @@ -34,25 +34,25 @@ pub trait SetGaloisElement { fn set_p(&mut self, p: i64); } -impl SetGaloisElement for AutomorphismKey { +impl SetGaloisElement for GLWEAutomorphismKey { fn set_p(&mut self, p: i64) { self.p = p } } -impl GetGaloisElement for AutomorphismKey { +impl GetGaloisElement for GLWEAutomorphismKey { fn p(&self) -> i64 { self.p } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for AutomorphismKey { +impl LWEInfos for GLWEAutomorphismKey { fn n(&self) -> Degree { self.key.n() } @@ -70,13 +70,13 @@ impl LWEInfos for AutomorphismKey { } } -impl GLWEInfos for AutomorphismKey { +impl GLWEInfos for GLWEAutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for AutomorphismKey { +impl GGLWEInfos for GLWEAutomorphismKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -94,7 +94,7 @@ impl GGLWEInfos for AutomorphismKey { } } -impl LWEInfos for AutomorphismKeyLayout { +impl LWEInfos for GLWEAutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -108,13 +108,13 @@ impl LWEInfos for AutomorphismKeyLayout { } } -impl GLWEInfos for AutomorphismKeyLayout { +impl GLWEInfos for GLWEAutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } -impl GGLWEInfos for AutomorphismKeyLayout { +impl GGLWEInfos for GLWEAutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -132,25 +132,25 @@ impl GGLWEInfos for AutomorphismKeyLayout { } } -impl fmt::Debug for AutomorphismKey { +impl fmt::Debug for GLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for AutomorphismKey { +impl FillUniform for GLWEAutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for AutomorphismKey { +impl fmt::Display for GLWEAutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl AutomorphismKey> { +impl GLWEAutomorphismKey> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -166,7 +166,7 @@ impl AutomorphismKey> { } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - AutomorphismKey { + GLWEAutomorphismKey { key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize), p: 0, } @@ -196,38 +196,38 @@ impl AutomorphismKey> { } } -impl GGLWEToMut for AutomorphismKey { +impl GGLWEToMut for GLWEAutomorphismKey { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { self.key.to_mut() } } -impl GGLWEToRef for AutomorphismKey { +impl GGLWEToRef for GLWEAutomorphismKey { fn to_ref(&self) -> GGLWE<&[u8]> { self.key.to_ref() } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } -impl AutomorphismKey { +impl GLWEAutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for AutomorphismKey { +impl ReaderFrom for GLWEAutomorphismKey { 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 AutomorphismKey { +impl WriterTo for GLWEAutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) diff --git a/poulpy-core/src/layouts/glwe_tensor_key.rs b/poulpy-core/src/layouts/glwe_tensor_key.rs index 7f1bb4c..bc0100f 100644 --- a/poulpy-core/src/layouts/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/glwe_tensor_key.rs @@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct TensorKeyLayout { +pub struct GLWETensorKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -21,11 +21,11 @@ pub struct TensorKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct TensorKey { +pub struct GLWETensorKey { pub(crate) keys: Vec>, } -impl LWEInfos for TensorKey { +impl LWEInfos for GLWETensorKey { fn n(&self) -> Degree { self.keys[0].n() } @@ -43,13 +43,13 @@ impl LWEInfos for TensorKey { } } -impl GLWEInfos for TensorKey { +impl GLWEInfos for GLWETensorKey { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl GGLWEInfos for TensorKey { +impl GGLWEInfos for GLWETensorKey { fn rank_in(&self) -> Rank { self.rank_out() } @@ -67,7 +67,7 @@ impl GGLWEInfos for TensorKey { } } -impl LWEInfos for TensorKeyLayout { +impl LWEInfos for GLWETensorKeyLayout { fn n(&self) -> Degree { self.n } @@ -81,13 +81,13 @@ impl LWEInfos for TensorKeyLayout { } } -impl GLWEInfos for TensorKeyLayout { +impl GLWEInfos for GLWETensorKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for TensorKeyLayout { +impl GGLWEInfos for GLWETensorKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -105,13 +105,13 @@ impl GGLWEInfos for TensorKeyLayout { } } -impl fmt::Debug for TensorKey { +impl fmt::Debug for GLWETensorKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for TensorKey { +impl FillUniform for GLWETensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() @@ -119,7 +119,7 @@ impl FillUniform for TensorKey { } } -impl fmt::Display for TensorKey { +impl fmt::Display for GLWETensorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -129,7 +129,7 @@ impl fmt::Display for TensorKey { } } -impl TensorKey> { +impl GLWETensorKey> { pub fn alloc_from_infos(infos: &A) -> Self where A: GGLWEInfos, @@ -151,7 +151,7 @@ impl TensorKey> { pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - TensorKey { + GLWETensorKey { keys: (0..pairs) .map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize)) .collect(), @@ -183,7 +183,7 @@ impl TensorKey> { } } -impl TensorKey { +impl GLWETensorKey { // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE { if i > j { @@ -194,7 +194,7 @@ impl TensorKey { } } -impl TensorKey { +impl GLWETensorKey { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE { if i > j { @@ -205,7 +205,7 @@ impl TensorKey { } } -impl ReaderFrom for TensorKey { +impl ReaderFrom for GLWETensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -221,7 +221,7 @@ impl ReaderFrom for TensorKey { } } -impl WriterTo for TensorKey { +impl WriterTo for GLWETensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -231,31 +231,31 @@ impl WriterTo for TensorKey { } } -pub trait TensorKeyToRef { - fn to_ref(&self) -> TensorKey<&[u8]>; +pub trait GLWETensorKeyToRef { + fn to_ref(&self) -> GLWETensorKey<&[u8]>; } -impl TensorKeyToRef for TensorKey +impl GLWETensorKeyToRef for GLWETensorKey where GGLWE: GGLWEToRef, { - fn to_ref(&self) -> TensorKey<&[u8]> { - TensorKey { + fn to_ref(&self) -> GLWETensorKey<&[u8]> { + GLWETensorKey { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } } -pub trait TensorKeyToMut { - fn to_mut(&mut self) -> TensorKey<&mut [u8]>; +pub trait GLWETensorKeyToMut { + fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]>; } -impl TensorKeyToMut for TensorKey +impl GLWETensorKeyToMut for GLWETensorKey where GGLWE: GGLWEToMut, { - fn to_mut(&mut self) -> TensorKey<&mut [u8]> { - TensorKey { + fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]> { + GLWETensorKey { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } diff --git a/poulpy-core/src/layouts/lwe.rs b/poulpy-core/src/layouts/lwe.rs index 0900d78..6f8cdce 100644 --- a/poulpy-core/src/layouts/lwe.rs +++ b/poulpy-core/src/layouts/lwe.rs @@ -10,6 +10,9 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; pub trait LWEInfos { fn n(&self) -> Degree; + fn log_n(&self) -> usize { + (u64::BITS - (self.n().as_usize() as u64 - 1).leading_zeros()) as usize + } fn k(&self) -> TorusPrecision; fn max_k(&self) -> TorusPrecision { TorusPrecision(self.k().0 * self.size() as u32) diff --git a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs index 7ac936e..bd63c75 100644 --- a/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_tensor_key.rs @@ -2,15 +2,15 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef, - GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + GLWEInfos, GLWETensorKey, GLWETensorKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct TensorKeyPrepared { +pub struct GLWETensorKeyPrepared { pub(crate) keys: Vec>, } -impl LWEInfos for TensorKeyPrepared { +impl LWEInfos for GLWETensorKeyPrepared { fn n(&self) -> Degree { self.keys[0].n() } @@ -28,13 +28,13 @@ impl LWEInfos for TensorKeyPrepared { } } -impl GLWEInfos for TensorKeyPrepared { +impl GLWEInfos for GLWETensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for TensorKeyPrepared { +impl GGLWEInfos for GLWETensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } @@ -52,7 +52,7 @@ impl GGLWEInfos for TensorKeyPrepared { } } -pub trait TensorKeyPreparedFactory +pub trait GLWETensorKeyPreparedFactory where Self: GGLWEPreparedFactory, { @@ -63,16 +63,16 @@ where dnum: Dnum, dsize: Dsize, rank: Rank, - ) -> TensorKeyPrepared, B> { + ) -> GLWETensorKeyPrepared, B> { let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); - TensorKeyPrepared { + GLWETensorKeyPrepared { keys: (0..pairs) .map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> + fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> GLWETensorKeyPrepared, B> where A: GGLWEInfos, { @@ -117,11 +117,11 @@ where fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where - R: TensorKeyPreparedToMut, - O: TensorKeyToRef, + R: GLWETensorKeyPreparedToMut, + O: GLWETensorKeyToRef, { - let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut(); - let other: TensorKey<&[u8]> = other.to_ref(); + let mut res: GLWETensorKeyPrepared<&mut [u8], B> = res.to_mut(); + let other: GLWETensorKey<&[u8]> = other.to_ref(); assert_eq!(res.keys.len(), other.keys.len()); @@ -131,20 +131,20 @@ where } } -impl TensorKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} +impl GLWETensorKeyPreparedFactory for Module where Module: GGLWEPreparedFactory {} -impl TensorKeyPrepared, B> { +impl GLWETensorKeyPrepared, B> { pub fn alloc_from_infos(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } @@ -152,20 +152,20 @@ impl TensorKeyPrepared, B> { pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared_from_infos(infos) } pub fn bytes_of(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { // Returns a mutable reference to GGLWE_{s}(s[i] * s[j]) pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared { if i > j { @@ -176,7 +176,7 @@ impl TensorKeyPrepared { } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared { if i > j { @@ -187,51 +187,51 @@ impl TensorKeyPrepared { } } -impl TensorKeyPrepared, B> { +impl GLWETensorKeyPrepared, B> { pub fn prepare_tmp_bytes(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory, + M: GLWETensorKeyPreparedFactory, { module.prepare_tensor_key_tmp_bytes(infos) } } -impl TensorKeyPrepared { +impl GLWETensorKeyPrepared { pub fn prepare(&mut self, module: &M, other: &O, scratch: &mut Scratch) where - O: TensorKeyToRef, - M: TensorKeyPreparedFactory, + O: GLWETensorKeyToRef, + M: GLWETensorKeyPreparedFactory, { module.prepare_tensor_key(self, other, scratch); } } -pub trait TensorKeyPreparedToMut { - fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +pub trait GLWETensorKeyPreparedToMut { + fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B>; } -impl TensorKeyPreparedToMut for TensorKeyPrepared +impl GLWETensorKeyPreparedToMut for GLWETensorKeyPrepared where GGLWEPrepared: GGLWEPreparedToMut, { - fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { - TensorKeyPrepared { + fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B> { + GLWETensorKeyPrepared { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } -pub trait TensorKeyPreparedToRef { - fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +pub trait GLWETensorKeyPreparedToRef { + fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B>; } -impl TensorKeyPreparedToRef for TensorKeyPrepared +impl GLWETensorKeyPreparedToRef for GLWETensorKeyPrepared where GGLWEPrepared: GGLWEPreparedToRef, { - fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { - TensorKeyPrepared { + fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B> { + GLWETensorKeyPrepared { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } diff --git a/poulpy-core/src/lib.rs b/poulpy-core/src/lib.rs index a5c5152..ccad084 100644 --- a/poulpy-core/src/lib.rs +++ b/poulpy-core/src/lib.rs @@ -21,6 +21,7 @@ pub use dist::*; pub use encryption::*; pub use external_product::*; pub use glwe_packing::*; +pub use glwe_trace::*; pub use keyswitching::*; pub use noise::*; pub use scratch::*; diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 880af1b..2220dc4 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPrepared, - GLWEPublicKey, GLWESecret, GLWESwitchingKey, Rank, TensorKey, + Degree, GGLWE, GGLWEInfos, GGLWELayout, GGSW, GGSWInfos, GLWE, GLWEAutomorphismKey, GLWEInfos, GLWEPlaintext, + GLWEPrepared, GLWEPublicKey, GLWESecret, GLWESwitchingKey, GLWETensorKey, Rank, prepared::{ GGLWEPrepared, GGSWPrepared, GLWEAutomorphismKeyPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, - GLWESwitchingKeyPrepared, TensorKeyPrepared, + GLWESwitchingKeyPrepared, GLWETensorKeyPrepared, }, }, }; @@ -282,12 +282,12 @@ where ) } - fn take_glwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) + fn take_glwe_automorphism_key(&mut self, infos: &A) -> (GLWEAutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe(infos); - (AutomorphismKey { key: data, p: 0 }, scratch) + (GLWEAutomorphismKey { key: data, p: 0 }, scratch) } fn take_glwe_automorphism_key_prepared( @@ -304,14 +304,14 @@ where (GLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } - fn take_glwe_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) + fn take_glwe_tensor_key(&mut self, infos: &A) -> (GLWETensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKey" + "rank_in != rank_out is not supported for GLWETensorKey" ); let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; @@ -331,10 +331,10 @@ where scratch = s; keys.push(gglwe); } - (TensorKey { keys }, scratch) + (GLWETensorKey { keys }, scratch) } - fn take_glwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_tensor_key_prepared(&mut self, module: &M, infos: &A) -> (GLWETensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, M: ModuleN + VmpPMatBytesOf, @@ -364,7 +364,7 @@ where scratch = s; keys.push(gglwe); } - (TensorKeyPrepared { keys }, scratch) + (GLWETensorKeyPrepared { keys }, scratch) } } diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 502a59b..c67d87d 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,8 +1,8 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, - LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWEAutomorphismKey, GLWESwitchingKey, GLWETensorKey, GLWEToLWESwitchingKey, + LWE, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, compressed::{ GGLWECompressed, GGSWCompressed, GLWEAutomorphismKeyCompressed, GLWECompressed, GLWESwitchingKeyCompressed, GLWETensorKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, @@ -69,7 +69,7 @@ fn test_glwe_switching_key_compressed_serialization() { #[test] fn test_automorphism_key_serialization() { - let original: AutomorphismKey> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -82,7 +82,7 @@ fn test_automorphism_key_compressed_serialization() { #[test] fn test_tensor_key_serialization() { - let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWETensorKey> = GLWETensorKey::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 a8d3d53..3f2b94a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEAutomorphismKeyAutomorphism, GLWEDecrypt, ScratchTakeCore, + GLWEAutomorphismKeyAutomorphism, GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GGLWEInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -18,7 +18,7 @@ use crate::{ #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism @@ -46,7 +46,7 @@ where let dnum_out: usize = k_out / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_in_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -55,7 +55,7 @@ where rank: rank.into(), }; - let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_out_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -64,7 +64,7 @@ where rank: rank.into(), }; - let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_apply_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -73,18 +73,18 @@ where rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); + let mut auto_key_in: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) - | AutomorphismKey::automorphism_tmp_bytes( + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos) + | GLWEAutomorphismKey::automorphism_tmp_bytes( module, &auto_key_out_infos, &auto_key_in_infos, @@ -187,7 +187,7 @@ where #[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism_inplace(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEAutomorphismKeyAutomorphism + VecZnxAutomorphism @@ -213,7 +213,7 @@ where let dnum_in: usize = k_in / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -222,7 +222,7 @@ where rank: rank.into(), }; - let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { + let auto_key_apply_layout: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -231,17 +231,17 @@ where rank: rank.into(), }; - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) - | AutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply) + | GLWEAutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); 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 384a86b..c3aa3c3 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk, + GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, GGSW, GGSWLayout, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory, TensorKey, - TensorKeyLayout, TensorKeyPreparedFactory, - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, + GGSW, GGSWLayout, GLWEAutomorphismKey, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory, + GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory, + prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, GLWETensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -18,11 +18,11 @@ use crate::{ pub fn test_ggsw_automorphism(module: &Module) where Module: GGSWEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedFactory - + TensorKeyEncryptSk + + GLWETensorKeyPreparedFactory + + GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, @@ -64,7 +64,7 @@ where rank: rank.into(), }; - let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { + let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -73,7 +73,7 @@ where rank: rank.into(), }; - let auto_key_layout: TensorKeyLayout = TensorKeyLayout { + let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -84,8 +84,8 @@ where let mut ct_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_layout); let mut ct_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -94,8 +94,8 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct_in) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) | GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); @@ -138,8 +138,8 @@ where GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = + GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -177,11 +177,11 @@ where pub fn test_ggsw_automorphism_inplace(module: &Module) where Module: GGSWEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GGSWAutomorphism - + TensorKeyPreparedFactory - + TensorKeyEncryptSk + + GLWETensorKeyPreparedFactory + + GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + VecZnxAutomorphismInplace + GGSWNoise, @@ -211,7 +211,7 @@ where rank: rank.into(), }; - let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { + let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -220,7 +220,7 @@ where rank: rank.into(), }; - let auto_key_layout: TensorKeyLayout = TensorKeyLayout { + let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -230,8 +230,8 @@ where }; let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -240,8 +240,8 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ct) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) - | TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key) | GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key), ); @@ -284,8 +284,8 @@ where GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = + GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); 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 df9d36d..58f737a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, + GLWEAutomorphism, GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::log2_std_noise_gglwe_product, @@ -22,7 +22,7 @@ where + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, @@ -55,7 +55,7 @@ where rank: rank.into(), }; - let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let autokey_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -64,7 +64,7 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct_in: GLWE> = GLWE::alloc_from_infos(&ct_in_infos); let mut ct_out: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); @@ -76,7 +76,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct_out) | GLWE::encrypt_sk_tmp_bytes(module, &ct_in) | GLWE::automorphism_tmp_bytes(module, &ct_out, &ct_in, &autokey), @@ -140,7 +140,7 @@ where + VecZnxFillUniform + GLWEDecrypt + GLWEAutomorphism - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWENoise + VecZnxAutomorphismInplace, @@ -165,7 +165,7 @@ where rank: rank.into(), }; - let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let autokey_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -174,7 +174,7 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); @@ -185,7 +185,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) + GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey) | GLWE::decrypt_tmp_bytes(module, &ct) | GLWE::encrypt_sk_tmp_bytes(module, &ct) | GLWE::automorphism_tmp_bytes(module, &ct, &ct, &autokey), 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 3b642c4..ad456c9 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -5,19 +5,20 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyCompressedEncryptSk, AutomorphismKeyEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, + GGLWEKeyswitch, GLWEAutomorphismKeyCompressedEncryptSk, GLWEAutomorphismKeyEncryptSk, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, - GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, prepared::GLWESecretPrepared, + AutomorphismKeyDecompress, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, + GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, + prepared::GLWESecretPrepared, }, noise::GGLWENoise, }; pub fn test_gglwe_automorphism_key_encrypt_sk(module: &Module) where - Module: AutomorphismKeyEncryptSk + Module: GLWEAutomorphismKeyEncryptSk + GGLWEKeyswitch + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk @@ -37,7 +38,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let atk_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -46,13 +47,15 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey> = GLWEAutomorphismKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWEAutomorphismKey::encrypt_sk_tmp_bytes( + module, &atk_infos, + )); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -89,7 +92,7 @@ where pub fn test_gglwe_automorphism_key_compressed_encrypt_sk(module: &Module) where - Module: AutomorphismKeyCompressedEncryptSk + Module: GLWEAutomorphismKeyCompressedEncryptSk + GGLWEKeyswitch + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk @@ -109,7 +112,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let atk_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -150,7 +153,7 @@ where let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key 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 9135374..940f917 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -9,18 +9,18 @@ use poulpy_hal::{ }; use crate::{ - GGLWETensorKeyCompressedEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + GLWETensorKeyCompressedEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, decryption::GLWEDecrypt, encryption::SIGMA, layouts::{ - Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKeyCompressed, TensorKey, TensorKeyLayout, + Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKey, GLWETensorKeyCompressed, GLWETensorKeyLayout, prepared::GLWESecretPrepared, }, }; pub fn test_gglwe_tensor_key_encrypt_sk(module: &Module) where - Module: TensorKeyEncryptSk + Module: GLWETensorKeyEncryptSk + GLWESecretPreparedFactory + GLWEDecrypt + VecZnxDftAlloc @@ -40,7 +40,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos = TensorKeyLayout { + let tensor_key_infos = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -49,13 +49,16 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::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 source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWETensorKey::encrypt_sk_tmp_bytes( + module, + &tensor_key_infos, + )); let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -111,9 +114,9 @@ where pub fn test_gglwe_tensor_key_compressed_encrypt_sk(module: &Module) where - Module: TensorKeyEncryptSk + Module: GLWETensorKeyEncryptSk + GLWESecretPreparedFactory - + GGLWETensorKeyCompressedEncryptSk + + GLWETensorKeyCompressedEncryptSk + GLWEDecrypt + VecZnxDftAlloc + VecZnxBigAlloc @@ -133,7 +136,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos: TensorKeyLayout = TensorKeyLayout { + let tensor_key_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -162,7 +165,7 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); 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 288eb88..b582d89 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ }; use crate::{ - GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore, TensorKeyEncryptSk, + GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, - GLWESwitchingKeyPreparedFactory, TensorKey, TensorKeyLayout, TensorKeyPreparedFactory, - prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared}, + GLWESwitchingKeyPreparedFactory, GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, GLWETensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -20,10 +20,10 @@ pub fn test_ggsw_keyswitch(module: &Module) where Module: GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + GGSWKeyswitch + GLWESecretPreparedFactory - + TensorKeyPreparedFactory + + GLWETensorKeyPreparedFactory + GLWESwitchingKeyPreparedFactory + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -61,7 +61,7 @@ where rank: rank.into(), }; - let tsk_infos: TensorKeyLayout = TensorKeyLayout { + let tsk_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -82,7 +82,7 @@ where let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -93,7 +93,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, @@ -148,7 +148,7 @@ where GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk); tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch( @@ -185,10 +185,10 @@ pub fn test_ggsw_keyswitch_inplace(module: &Module) where Module: GGSWEncryptSk + GLWESwitchingKeyEncryptSk - + TensorKeyEncryptSk + + GLWETensorKeyEncryptSk + GGSWKeyswitch + GLWESecretPreparedFactory - + TensorKeyPreparedFactory + + GLWETensorKeyPreparedFactory + GLWESwitchingKeyPreparedFactory + GGSWNoise, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, @@ -216,7 +216,7 @@ where rank: rank.into(), }; - let tsk_infos: TensorKeyLayout = TensorKeyLayout { + let tsk_infos: GLWETensorKeyLayout = GLWETensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -236,7 +236,7 @@ where }; let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -247,7 +247,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos) | GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos) - | TensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) + | GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos) | GGSW::keyswitch_tmp_bytes( module, &ggsw_out_infos, @@ -302,7 +302,7 @@ where GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk); tsk_prepared.prepare(module, &tsk, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index ec685f4..029e059 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -7,10 +7,10 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, + GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, }; @@ -18,7 +18,7 @@ use crate::{ pub fn test_glwe_packing(module: &Module) where Module: GLWEEncryptSk - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + GLWEPacking + GLWESecretPreparedFactory @@ -49,7 +49,7 @@ where rank: rank.into(), }; - let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -60,7 +60,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWEPacker::tmp_bytes(module, &glwe_out_infos, &key_infos), ); @@ -81,7 +81,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap, BE>> = HashMap::new(); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); + let mut tmp: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index 4b83808..ed2ed79 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -7,12 +7,12 @@ use poulpy_hal::{ }; use crate::{ - AutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, + GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore, encryption::SIGMA, glwe_trace::GLWETrace, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, GLWESecret, - GLWESecretPreparedFactory, LWEInfos, + GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext, + GLWESecret, GLWESecretPreparedFactory, LWEInfos, prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, }, noise::var_noise_gglwe_product, @@ -23,7 +23,7 @@ where Module: GLWETrace + GLWEEncryptSk + GLWEDecrypt - + AutomorphismKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + GLWEAutomorphismKeyPreparedFactory + VecZnxFillUniform + GLWESecretPreparedFactory @@ -49,7 +49,7 @@ where rank: rank.into(), }; - let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { + let key_infos: GLWEAutomorphismKeyLayout = GLWEAutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_autokey.into(), @@ -69,7 +69,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_out_infos) - | AutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) + | GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &key_infos) | GLWE::trace_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &key_infos), ); @@ -98,7 +98,7 @@ where 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); + let mut tmp: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, diff --git a/poulpy-hal/src/layouts/module.rs b/poulpy-hal/src/layouts/module.rs index bc074dd..0556a6f 100644 --- a/poulpy-hal/src/layouts/module.rs +++ b/poulpy-hal/src/layouts/module.rs @@ -97,18 +97,23 @@ impl ModuleLogN for Module where Self: ModuleN {} impl CyclotomicOrder for Module where Self: ModuleN {} +#[inline(always)] +pub fn galois_element(generator: i64, cyclotomic_order: i64) -> i64 { + if generator == 0 { + return 1; + } + + let g_exp: u64 = mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (cyclotomic_order - 1) as u64; + g_exp as i64 * generator.signum() +} + pub trait GaloisElement where Self: CyclotomicOrder, { // Returns GALOISGENERATOR^|generator| * sign(generator) fn galois_element(&self, generator: i64) -> i64 { - if generator == 0 { - return 1; - } - - let g_exp: u64 = mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (self.cyclotomic_order() - 1) as u64; - g_exp as i64 * generator.signum() + galois_element(generator, self.cyclotomic_order()) } // Returns gen^-1 diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index a1f39c6..0d90062 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -2,107 +2,46 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; -use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSW, GGSWLayout, GLWESecret, LWE, LWELayout, LWESecret, TensorKeyLayout, - prepared::PrepareAlloc, +use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + layouts::{ + Dsize, GGSW, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWESecret, GLWESecretPreparedFactory, + GLWETensorKeyLayout, LWE, LWELayout, LWESecret, + }, }; use poulpy_hal::{ - api::{ - ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, - SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, - VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, - VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, - VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, - VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, - VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, - TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, + api::{ModuleN, ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, Module, Scratch, ScratchOwned}, source::Source, }; use poulpy_schemes::tfhe::{ blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, - BlindRotationKeyInfos, BlindRotationKeyLayout, BlindRotationKeyPrepared, CGGI, + BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyInfos, BlindRotationKeyLayout, CGGI, }, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; -pub fn benc_circuit_bootstrapping(c: &mut Criterion, label: &str) +pub fn benc_circuit_bootstrapping(c: &mut Criterion, label: &str) where - Module: ModuleNew - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Module: ModuleNew + + ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let group_name: String = format!("circuit_bootstrapping::{label}"); @@ -118,81 +57,33 @@ where cbt_infos: CircuitBootstrappingKeyLayout, } - fn runner(params: &Params) -> impl FnMut() + fn runner(params: &Params) -> impl FnMut() where - Module: ModuleNew - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Module: ModuleNew + + ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, /* TODO find a way to remove this bound or move it to CBT KEY */ + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { // Scratch space (4MB) - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); let n_glwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_glwe(); let n_lwe: poulpy_core::layouts::Degree = params.cbt_infos.layout_brk.n_lwe(); let rank: poulpy_core::layouts::Rank = params.cbt_infos.layout_brk.rank; - let module: Module = Module::::new(n_glwe.as_u32() as u64); + let module: Module = Module::::new(n_glwe.as_u32() as u64); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -208,19 +99,20 @@ where let ct_lwe: LWE> = LWE::alloc_from_infos(¶ms.lwe_infos); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(¶ms.cbt_infos); + cbt_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - ¶ms.cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); let mut res: GGSW> = GGSW::alloc_from_infos(¶ms.ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); - + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(&module, ¶ms.cbt_infos); + cbt_prepared.prepare(&module, &cbt_key, scratch.borrow()); move || { cbt_prepared.execute_to_constant( &module, @@ -261,7 +153,7 @@ where dnum: 3_u32.into(), rank: 2_u32.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -269,7 +161,7 @@ where dsize: Dsize(1), rank: 2_u32.into(), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -280,7 +172,7 @@ where }, }] { let id: BenchmarkId = BenchmarkId::from_parameter(params.name.clone()); - let mut runner = runner::(¶ms); + let mut runner = runner::(¶ms); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); } diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 3383114..a7c56bd 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,9 +1,9 @@ use poulpy_core::{ - GLWEOperations, + GLWENormalize, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWEInfos, LWELayout, - LWEPlaintext, LWESecret, TensorKeyLayout, - prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWLayout, GLWE, GLWEAutomorphismKeyLayout, GLWELayout, GLWEPlaintext, GLWESecret, GLWETensorKeyLayout, LWE, + LWEInfos, LWELayout, LWEPlaintext, LWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; use std::time::Instant; @@ -22,10 +22,7 @@ use poulpy_hal::{ use poulpy_schemes::tfhe::{ blind_rotation::{BlindRotationKeyLayout, CGGI}, - circuit_bootstrapping::{ - CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, - }, + circuit_bootstrapping::{CircuitBootstrappingKey, CircuitBootstrappingKeyLayout, CircuitBootstrappingKeyPrepared}, }; fn main() { @@ -89,7 +86,7 @@ fn main() { dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_trace.into(), @@ -97,7 +94,7 @@ fn main() { dsize: 1_u32.into(), rank: rank.into(), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -145,7 +142,8 @@ fn main() { // sk_glwe.fill_zero(); // GLWE secret prepared (opaque backend dependant write only struct) - let sk_glwe_prepared: GLWESecretPrepared, BackendImpl> = sk_glwe.prepare_alloc(&module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BackendImpl> = GLWESecretPrepared::alloc(&module, rank.into()); + sk_glwe_prepared.prepare(&module, &sk_glwe); // Plaintext value to circuit bootstrap let data: i64 = 1 % (1 << k_lwe_pt); @@ -175,23 +173,26 @@ fn main() { let now: Instant = Instant::now(); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey, CGGI> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, CGGI> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + + cbt_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); + println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) - let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = - cbt_key.prepare_alloc(&module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(&module, &cbt_infos); + cbt_prepared.prepare(&module, &cbt_key, scratch.borrow()); // Apply circuit bootstrapping: LWE(data * 2^{- (k_lwe_pt + 2)}) -> GGSW(data) let now: Instant = Instant::now(); @@ -234,7 +235,7 @@ fn main() { .for_each(|(x, y)| *y = (x % (1 << (k_glwe_pt - 1))) as i64 - (1 << (k_glwe_pt - 2))); pt_glwe.encode_vec_i64(&data_vec, (k_lwe_pt + 2).into()); - pt_glwe.normalize_inplace(&module, scratch.borrow()); + module.glwe_normalize_inplace(&mut pt_glwe, scratch.borrow()); println!("{}", pt_glwe); @@ -249,7 +250,8 @@ fn main() { ); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BackendImpl> = GGSWPrepared::alloc_from_infos(&module, &res); + res_prepared.prepare(&module, &res, scratch.borrow()); // Apply GLWE x GGSW ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs index 830a829..ee3f454 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs @@ -29,7 +29,7 @@ impl BlindRotationKeyFactory for BlindRotationKey { } } -impl BlindRotationKeyEncryptSk for Module +impl BlindRotationKeyEncryptSk for Module where Self: GGSWEncryptSk, Scratch: ScratchTakeCore, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs index b711f16..fe3e795 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs @@ -15,7 +15,7 @@ use crate::tfhe::blind_rotation::{ utils::set_xai_plus_y, }; -impl BlindRotationKeyPreparedFactory for Module +impl BlindRotationKeyPreparedFactory for Module where Self: GGSWPreparedFactory + SvpPPolAlloc + SvpPrepare, { diff --git a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs index 99a6e2e..b250e07 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs @@ -10,7 +10,7 @@ use poulpy_core::{ use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey}; -pub trait BlindRotationKeyEncryptSk { +pub trait BlindRotationKeyEncryptSk { fn blind_rotation_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize where A: GGSWInfos; @@ -43,7 +43,7 @@ impl BlindRotationKey { S0: GLWESecretPreparedToRef + GLWEInfos, S1: LWESecretToRef + LWEInfos + GetDistribution, Scratch: ScratchTakeCore, - M: BlindRotationKeyEncryptSk, + M: BlindRotationKeyEncryptSk, { module.blind_rotation_key_encrypt_sk(self, sk_glwe, sk_lwe, source_xa, source_xe, scratch); } @@ -53,7 +53,7 @@ impl BlindRotationKey, BRA> { pub fn encrypt_sk_tmp_bytes(module: &M, infos: &A) -> usize where A: GGSWInfos, - M: BlindRotationKeyEncryptSk, + M: BlindRotationKeyEncryptSk, { module.blind_rotation_key_encrypt_sk_tmp_bytes(infos) } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs index 5830c84..c18e71c 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs @@ -3,13 +3,13 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Scratch, SvpPPol}; use std::marker::PhantomData; use poulpy_core::{ - Distribution, ScratchTakeCore, + Distribution, layouts::{Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyInfos}; -pub trait BlindRotationKeyPreparedFactory { +pub trait BlindRotationKeyPreparedFactory { fn blind_rotation_key_prepared_alloc(&self, infos: &A) -> BlindRotationKeyPrepared, BRA, BE> where A: BlindRotationKeyInfos; @@ -21,27 +21,23 @@ pub trait BlindRotationKeyPreparedFactory { scratch: &mut Scratch, ) where DM: DataMut, - DR: DataRef, - Scratch: ScratchTakeCore; + DR: DataRef; } impl BlindRotationKeyPrepared, BRA, BE> { pub fn alloc(module: &M, infos: &A) -> Self where A: BlindRotationKeyInfos, - M: BlindRotationKeyPreparedFactory, + M: BlindRotationKeyPreparedFactory, { module.blind_rotation_key_prepared_alloc(infos) } } -impl BlindRotationKeyPrepared -where - Scratch: ScratchTakeCore, -{ +impl BlindRotationKeyPrepared { pub fn prepare(&mut self, module: &M, other: &BlindRotationKey, scratch: &mut Scratch) where - M: BlindRotationKeyPreparedFactory, + M: BlindRotationKeyPreparedFactory, { module.blind_rotation_key_prepare(self, other, scratch); } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 8d0a017..1651987 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -24,14 +24,13 @@ pub fn test_blind_rotation( block_size: usize, extension_factor: usize, ) where - M: BlindRotationKeyEncryptSk - + BlindRotationKeyPreparedFactory + M: BlindRotationKeyEncryptSk + + BlindRotationKeyPreparedFactory + BlindRotationExecute - + GLWESecretPreparedFactory - + BlindRotationExecute - + LWEEncryptSk + LookupTableFactory - + GLWEDecrypt, + + GLWESecretPreparedFactory + + GLWEDecrypt + + LWEEncryptSk, BlindRotationKey, BRA>: BlindRotationKeyFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index a8b7212..9838fe0 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -1,185 +1,182 @@ use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchAvailable, TakeMatZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, TakeVecZnxDftSlice, TakeVecZnxSlice, - VecZnxAddInplace, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, - VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, - VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, - VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ToOwnedDeep}, - oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, + api::{ModuleLogN, ModuleN, ScratchOwnedAlloc, ScratchOwnedBorrow}, + layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ToOwnedDeep}, }; use poulpy_core::{ - GLWEOperations, TakeGGLWE, TakeGLWE, - layouts::{Dsize, GGLWELayout, GGSWInfos, GLWEInfos, LWEInfos}, + GGSWFromGGLWE, GLWEPacking, GLWETrace, ScratchTakeCore, + layouts::{Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef}, }; -use poulpy_core::glwe_packing; use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKeyPrepared, LookUpTable, LookUpTableRotationDirection, + BlindRotationAlgo, BlindRotationExecute, LookUpTableLayout, LookUpTableRotationDirection, LookupTable, LookupTableFactory, }, - circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute}, + circuit_bootstrapping::CircuitBootstrappingKeyPrepared, }; -impl CirtuitBootstrappingExecute for CircuitBootstrappingKeyPrepared -where - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + VecZnxNormalize, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeVecZnx - + TakeVecZnxDftSlice - + TakeVecZnxBig - + TakeVecZnxDft - + TakeMatZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeSlice, - BlindRotationKeyPrepared: BlincRotationExecute, -{ - fn execute_to_constant( +pub trait CirtuitBootstrappingExecute { + fn circuit_bootstrapping_execute_to_constant( &self, - module: &Module, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch, - ) { + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; + + #[allow(clippy::too_many_arguments)] + fn circuit_bootstrapping_execute_to_exponent( + &self, + log_gap_out: usize, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; +} + +impl CircuitBootstrappingKeyPrepared { + pub fn execute_to_constant( + &self, + module: &M, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + M: CirtuitBootstrappingExecute, + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + { + module.circuit_bootstrapping_execute_to_constant(res, lwe, self, log_domain, extension_factor, scratch); + } + + pub fn execute_to_exponent( + &self, + module: &M, + log_gap_out: usize, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + M: CirtuitBootstrappingExecute, + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + { + module.circuit_bootstrapping_execute_to_exponent( + log_gap_out, + res, + lwe, + self, + log_domain, + extension_factor, + scratch, + ); + } +} + +impl CirtuitBootstrappingExecute for Module +where + Self: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, +{ + fn circuit_bootstrapping_execute_to_constant( + &self, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef, + { circuit_bootstrap_core( false, - module, + self, 0, res, lwe, log_domain, extension_factor, - self, + key, scratch, ); } - fn execute_to_exponent( + fn circuit_bootstrapping_execute_to_exponent( &self, - module: &Module, log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch, - ) { + scratch: &mut Scratch, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef, + { circuit_bootstrap_core( true, - module, + self, log_gap_out, res, lwe, log_domain, extension_factor, - self, + key, scratch, ); } } #[allow(clippy::too_many_arguments)] -pub fn circuit_bootstrap_core( +pub fn circuit_bootstrap_core( to_exponent: bool, - module: &Module, + module: &M, log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, + res: &mut R, + lwe: &L, log_domain: usize, extension_factor: usize, - key: &CircuitBootstrappingKeyPrepared, - scratch: &mut Scratch, + key: &CircuitBootstrappingKeyPrepared, + scratch: &mut Scratch, ) where - DRes: DataMut, - DLwe: DataRef, - DBrk: DataRef, - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxRotate - + VecZnxNormalize, - B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, - Scratch: TakeVecZnxDftSlice - + TakeVecZnxBig - + TakeVecZnxDft - + TakeVecZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeMatZnx - + TakeSlice, - BlindRotationKeyPrepared: BlincRotationExecute, + R: GGSWToMut, + L: LWEToRef, + D: DataRef, + M: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - use poulpy_core::layouts::LWEInfos; + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let lwe: &LWE<&[u8]> = &lwe.to_ref(); - assert_eq!(res.n(), key.brk.n()); - assert_eq!(lwe.base2k(), key.brk.base2k()); - assert_eq!(res.base2k(), key.brk.base2k()); - } + assert_eq!(res.n(), key.brk.n()); + assert_eq!(lwe.base2k(), key.brk.base2k()); + assert_eq!(res.base2k(), key.brk.base2k()); let n: usize = res.n().into(); let base2k: usize = res.base2k().into(); @@ -203,8 +200,15 @@ pub fn circuit_bootstrap_core( }); } + let lut_infos: LookUpTableLayout = LookUpTableLayout { + n: module.n().into(), + extension_factor, + k: (base2k * dnum).into(), + base2k: base2k.into(), + }; + // Lut precision, basically must be able to hold the decomposition power basis of the GGSW - let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, base2k * dnum, extension_factor); + let mut lut: LookupTable = LookupTable::alloc(&lut_infos); lut.set(module, &f, base2k * dnum); if to_exponent { @@ -212,7 +216,7 @@ pub fn circuit_bootstrap_core( } // TODO: separate GGSW k from output of blind rotation k - let (mut res_glwe, scratch_1) = scratch.take_glwe_ct(res); + let (mut res_glwe, scratch_1) = scratch.take_glwe(res); let gglwe_infos: GGLWELayout = GGLWELayout { n: n.into(), @@ -252,7 +256,7 @@ pub fn circuit_bootstrap_core( } if i < dnum { - res_glwe.rotate_inplace(module, -(gap as i64), scratch_2); + module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2); } }); @@ -261,46 +265,24 @@ pub fn circuit_bootstrap_core( } #[allow(clippy::too_many_arguments)] -fn post_process( - module: &Module, - res: &mut GLWE, - a: &GLWE, +fn post_process( + module: &M, + res: &mut R, + a: &A, log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, B>>, - scratch: &mut Scratch, + auto_keys: &HashMap, BE>>, + scratch: &mut Scratch, ) where - DataRes: DataMut, - DataA: DataRef, - Module: VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotate - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + R: GLWEToMut, + A: GLWEToRef, + M: ModuleLogN + GLWETrace + GLWEPacking, + Scratch: ScratchTakeCore, { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let a: &GLWE<&[u8]> = &a.to_ref(); + let log_n: usize = module.log_n(); let mut cts: HashMap>> = HashMap::new(); @@ -326,7 +308,7 @@ fn post_process( for i in 0..steps { if i != 0 { - res.rotate_inplace(module, -(1 << log_gap_in), scratch); + module.glwe_rotate_inplace(-(1 << log_gap_in), res, scratch); } cts_vec.push(res.to_owned_deep()); } @@ -335,7 +317,8 @@ fn post_process( cts.insert(i * (1 << log_gap_out), ct); } - glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); + module.glwe_pack(&mut cts, log_gap_out, auto_keys, scratch); + let packed: &mut GLWE> = cts.remove(&0).unwrap(); res.trace( module, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 81f99e6..172d430 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,41 +1,38 @@ -use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, - TensorKeyLayout, - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared}, +use poulpy_core::{ + GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, + layouts::{ + GGLWEInfos, GGSWInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecretPreparedFactory, + GLWESecretToRef, GLWETensorKey, GLWETensorKeyLayout, LWEInfos, LWESecretToRef, prepared::GLWESecretPrepared, + }, + trace_galois_elements, }; use std::collections::HashMap; use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphism, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - VecZnxSwitchRing, VmpPMatAlloc, VmpPrepare, - }, - layouts::{Backend, Data, DataRef, Module, Scratch}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::tfhe::blind_rotation::{ - BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, BlindRotationKeyInfos, - BlindRotationKeyLayout, BlindRotationKeyPrepared, + BlindRotationAlgo, BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory, BlindRotationKeyInfos, + BlindRotationKeyLayout, }; pub trait CircuitBootstrappingKeyInfos { fn brk_infos(&self) -> BlindRotationKeyLayout; - fn atk_infos(&self) -> AutomorphismKeyLayout; - fn tsk_infos(&self) -> TensorKeyLayout; + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout; + fn tsk_infos(&self) -> GLWETensorKeyLayout; } #[derive(Debug, Clone, Copy)] pub struct CircuitBootstrappingKeyLayout { pub layout_brk: BlindRotationKeyLayout, - pub layout_atk: AutomorphismKeyLayout, - pub layout_tsk: TensorKeyLayout, + pub layout_atk: GLWEAutomorphismKeyLayout, + pub layout_tsk: GLWETensorKeyLayout, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { - fn atk_infos(&self) -> AutomorphismKeyLayout { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { self.layout_atk } @@ -43,96 +40,114 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { self.layout_brk } - fn tsk_infos(&self) -> TensorKeyLayout { + fn tsk_infos(&self) -> GLWETensorKeyLayout { self.layout_tsk } } -pub trait CircuitBootstrappingKeyEncryptSk { +pub trait CircuitBootstrappingKeyEncryptSk { #[allow(clippy::too_many_arguments)] - fn encrypt_sk( - module: &Module, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - cbt_infos: &INFOS, + fn circuit_bootstrapping_key_encrypt_sk( + &self, + res: &mut CircuitBootstrappingKey, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, - ) -> Self + scratch: &mut Scratch, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution; +} + +impl CircuitBootstrappingKey, BRA> { + pub fn alloc_from_infos(infos: &A) -> Self where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef; + A: CircuitBootstrappingKeyInfos, + BlindRotationKey, BRA>: BlindRotationKeyFactory, + { + let atk_infos: &GLWEAutomorphismKeyLayout = &infos.atk_infos(); + let brk_infos: &BlindRotationKeyLayout = &infos.brk_infos(); + let trk_infos: &GLWETensorKeyLayout = &infos.tsk_infos(); + let gal_els: Vec = trace_galois_elements(atk_infos.log_n(), 2 * atk_infos.n().as_usize() as i64); + + Self { + brk: , BRA> as BlindRotationKeyFactory>::blind_rotation_key_alloc(brk_infos), + atk: gal_els + .iter() + .map(|&gal_el| { + let key = GLWEAutomorphismKey::alloc_from_infos(atk_infos); + (gal_el, key) + }) + .collect(), + tsk: GLWETensorKey::alloc_from_infos(trk_infos), + } + } } pub struct CircuitBootstrappingKey { pub(crate) brk: BlindRotationKey, - pub(crate) tsk: TensorKey>, - pub(crate) atk: HashMap>>, + pub(crate) tsk: GLWETensorKey>, + pub(crate) atk: HashMap>>, } -impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> -where - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, - Module: SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAutomorphism, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, -{ - fn encrypt_sk( - module: &Module, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - cbt_infos: &INFOS, +impl CircuitBootstrappingKey { + pub fn encrypt_sk( + &mut self, + module: &M, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch, - ) -> Self - where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef, + scratch: &mut Scratch, + ) where + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, + M: CircuitBootstrappingKeyEncryptSk, { - assert_eq!(sk_lwe.n(), cbt_infos.brk_infos().n_lwe()); - assert_eq!(sk_glwe.n(), cbt_infos.brk_infos().n_glwe()); - assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); - assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); + module.circuit_bootstrapping_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } +} - let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); - let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); +impl CircuitBootstrappingKeyEncryptSk for Module +where + Self: GLWETensorKeyEncryptSk + + BlindRotationKeyEncryptSk + + GLWEAutomorphismKeyEncryptSk + + GLWESecretPreparedFactory, + Scratch: ScratchTakeCore, +{ + fn circuit_bootstrapping_key_encrypt_sk( + &self, + res: &mut CircuitBootstrappingKey, + sk_lwe: &S0, + sk_glwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, + { + let brk_infos: &BlindRotationKeyLayout = &res.brk_infos(); + let atk_infos: &GLWEAutomorphismKeyLayout = &res.atk_infos(); + let tsk_infos: &GLWETensorKeyLayout = &res.tsk_infos(); - let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWE::trace_galois_elements(module); - gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); - key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); - auto_keys.insert(*gal_el, key); - }); + assert_eq!(sk_lwe.n(), brk_infos.n_lwe()); + assert_eq!(sk_glwe.n(), brk_infos.n_glwe()); + assert_eq!(sk_glwe.n(), atk_infos.n()); + assert_eq!(sk_glwe.n(), tsk_infos.n()); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch); + for (p, atk) in res.atk.iter_mut() { + atk.encrypt_sk(self, *p, sk_glwe, source_xa, source_xe, scratch); + } - let mut brk: BlindRotationKey, BRA> = BlindRotationKey::, BRA>::alloc(&brk_infos); - brk.encrypt_sk( - module, + let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + + res.brk.encrypt_sk( + self, &sk_glwe_prepared, sk_lwe, source_xa, @@ -140,27 +155,15 @@ where scratch, ); - let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&trk_infos); - tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); - - Self { - brk, - atk: auto_keys, - tsk, - } + res.tsk + .encrypt_sk(self, sk_glwe, source_xa, source_xe, scratch); } } -pub struct CircuitBootstrappingKeyPrepared { - pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: TensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, -} - -impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { - fn atk_infos(&self) -> AutomorphismKeyLayout { +impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKey { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { let (_, atk) = self.atk.iter().next().expect("atk is empty"); - AutomorphismKeyLayout { + GLWEAutomorphismKeyLayout { n: atk.n(), base2k: atk.base2k(), k: atk.k(), @@ -181,8 +184,8 @@ impl CircuitBootstrappingKeyInfo } } - fn tsk_infos(&self) -> TensorKeyLayout { - TensorKeyLayout { + fn tsk_infos(&self) -> GLWETensorKeyLayout { + GLWETensorKeyLayout { n: self.tsk.n(), base2k: self.tsk.base2k(), k: self.tsk.k(), @@ -192,22 +195,3 @@ impl CircuitBootstrappingKeyInfo } } } - -impl PrepareAlloc, BRA, B>> - for CircuitBootstrappingKey -where - Module: VmpPMatAlloc + VmpPrepare, - BlindRotationKey: PrepareAlloc, BRA, B>>, - TensorKey: 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(); - for (key, value) in &self.atk { - atk.insert(*key, value.prepare_alloc(module, scratch)); - } - CircuitBootstrappingKeyPrepared { brk, tsk, atk } - } -} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs new file mode 100644 index 0000000..223c193 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_compressed.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +use poulpy_core::layouts::{GLWEAutomorphismKeyCompressed, GLWETensorKeyCompressed}; +use poulpy_hal::layouts::Data; + +use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyCompressed}; + +#[allow(dead_code)] +pub struct CircuitBootstrappingKey { + pub(crate) brk: BlindRotationKeyCompressed, + pub(crate) tsk: GLWETensorKeyCompressed>, + pub(crate) atk: HashMap>>, +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs new file mode 100644 index 0000000..db39a45 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs @@ -0,0 +1,136 @@ +use poulpy_core::{ + layouts::{ + GGLWEInfos, GGSWInfos, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWETensorKeyLayout, + GLWETensorKeyPreparedFactory, LWEInfos, + prepared::{GLWEAutomorphismKeyPrepared, GLWETensorKeyPrepared}, + }, + trace_galois_elements, +}; +use std::collections::HashMap; + +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; + +use crate::tfhe::{ + blind_rotation::{ + BlindRotationAlgo, BlindRotationKeyInfos, BlindRotationKeyLayout, BlindRotationKeyPrepared, + BlindRotationKeyPreparedFactory, + }, + circuit_bootstrapping::{CircuitBootstrappingKey, CircuitBootstrappingKeyInfos}, +}; + +impl CircuitBootstrappingKeyPrepared, BRA, BE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> CircuitBootstrappingKeyPrepared, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + M: CircuitBootstrappingKeyPreparedFactory, + { + module.circuit_bootstrapping_key_prepared_alloc_from_infos(infos) + } +} + +impl CircuitBootstrappingKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &CircuitBootstrappingKey, scratch: &mut Scratch) + where + DR: DataRef, + M: CircuitBootstrappingKeyPreparedFactory, + { + module.circuit_bootstrapping_key_prepare(self, other, scratch); + } +} + +impl CircuitBootstrappingKeyPreparedFactory for Module where + Self: Sized + + BlindRotationKeyPreparedFactory + + GLWETensorKeyPreparedFactory + + GLWEAutomorphismKeyPreparedFactory +{ +} + +pub trait CircuitBootstrappingKeyPreparedFactory +where + Self: Sized + + BlindRotationKeyPreparedFactory + + GLWETensorKeyPreparedFactory + + GLWEAutomorphismKeyPreparedFactory, +{ + fn circuit_bootstrapping_key_prepared_alloc_from_infos( + &self, + infos: &A, + ) -> CircuitBootstrappingKeyPrepared, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + { + let atk_infos: &GLWEAutomorphismKeyLayout = &infos.atk_infos(); + let gal_els: Vec = trace_galois_elements(atk_infos.log_n(), 2 * atk_infos.n().as_usize() as i64); + + CircuitBootstrappingKeyPrepared { + brk: BlindRotationKeyPrepared::alloc(self, &infos.brk_infos()), + tsk: GLWETensorKeyPrepared::alloc_from_infos(self, &infos.tsk_infos()), + atk: gal_els + .iter() + .map(|&gal_el| { + let key = GLWEAutomorphismKeyPrepared::alloc_from_infos(self, atk_infos); + (gal_el, key) + }) + .collect(), + } + } + fn circuit_bootstrapping_key_prepare( + &self, + res: &mut CircuitBootstrappingKeyPrepared, + other: &CircuitBootstrappingKey, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR: DataRef, + { + res.brk.prepare(self, &other.brk, scratch); + res.tsk.prepare(self, &other.tsk, scratch); + + for (k, a) in res.atk.iter_mut() { + a.prepare(self, other.atk.get(k).unwrap(), scratch); + } + } +} + +pub struct CircuitBootstrappingKeyPrepared { + pub(crate) brk: BlindRotationKeyPrepared, + pub(crate) tsk: GLWETensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, +} + +impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { + fn atk_infos(&self) -> GLWEAutomorphismKeyLayout { + let (_, atk) = self.atk.iter().next().expect("atk is empty"); + GLWEAutomorphismKeyLayout { + n: atk.n(), + base2k: atk.base2k(), + k: atk.k(), + dnum: atk.dnum(), + dsize: atk.dsize(), + rank: atk.rank(), + } + } + + fn brk_infos(&self) -> BlindRotationKeyLayout { + BlindRotationKeyLayout { + n_glwe: self.brk.n_glwe(), + n_lwe: self.brk.n_lwe(), + base2k: self.brk.base2k(), + k: self.brk.k(), + dnum: self.brk.dnum(), + rank: self.brk.rank(), + } + } + + fn tsk_infos(&self) -> GLWETensorKeyLayout { + GLWETensorKeyLayout { + n: self.tsk.n(), + base2k: self.tsk.base2k(), + k: self.tsk.k(), + dnum: self.tsk.dnum(), + dsize: self.tsk.dsize(), + rank: self.tsk.rank(), + } + } +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index ed3b6a1..e857cd0 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -1,36 +1,12 @@ mod circuit; mod key; +mod key_compressed; +mod key_prepared; -//[cfg(tests)] -//pub mod tests; +#[cfg(test)] +pub mod tests; pub use circuit::*; pub use key::*; - -use poulpy_core::layouts::{GGSW, LWE}; - -use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; - -pub trait CirtuitBootstrappingExecute { - fn execute_to_constant( - &self, - module: &Module, - res: &mut GGSW, - lwe: &LWE, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch, - ); - - #[allow(clippy::too_many_arguments)] - fn execute_to_exponent( - &self, - module: &Module, - log_gap_out: usize, - res: &mut GGSW, - lwe: &LWE, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch, - ); -} +// pub use key_compressed::*; +pub use key_prepared::*; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index cfe238f..77469d1 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -1,108 +1,49 @@ use std::time::Instant; use poulpy_hal::{ - api::{ - ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, - SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, - VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, - ZnNormalizeInplace, - }, - layouts::{Backend, Module, ScalarZnx, ScratchOwned, ZnxView, ZnxViewMut}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSliceImpl, - TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, + api::{ModuleN, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace}, + layouts::{Backend, ScalarZnx, Scratch, ScratchOwned, ZnxView, ZnxViewMut}, source::Source, }; use crate::tfhe::{ - blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, - BlindRotationKeyLayout, BlindRotationKeyPrepared, - }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, BlindRotationKeyLayout}, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; -use poulpy_core::layouts::{AutomorphismKeyLayout, Dsize, GGSWLayout, LWELayout, TensorKeyLayout, prepared::PrepareAlloc}; +use poulpy_core::{ + GGSWNoise, GLWEDecrypt, GLWEEncryptSk, GLWEExternalProduct, LWEEncryptSk, ScratchTakeCore, + layouts::{ + Dsize, GGSWLayout, GGSWPreparedFactory, GLWEAutomorphismKeyLayout, GLWESecretPreparedFactory, GLWETensorKeyLayout, + LWELayout, + }, +}; use poulpy_core::layouts::{ GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, prepared::{GGSWPrepared, GLWESecretPrepared}, }; -pub fn test_circuit_bootstrapping_to_exponent(module: &Module) +pub fn test_circuit_bootstrapping_to_exponent(module: &M) where - Module: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + M: ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let n_glwe: usize = module.n(); let base2k: usize = 17; @@ -141,7 +82,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -149,7 +90,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -168,7 +109,7 @@ where rank: rank.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -180,7 +121,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1; @@ -193,22 +135,27 @@ where ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + println!("CBT-ALLOC: {} ms", now.elapsed().as_millis()); + + let now: Instant = Instant::now(); + cbt_key.encrypt_sk( module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); - println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); + println!("CBT-ENCRYPT: {} ms", now.elapsed().as_millis()); let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(module, &cbt_infos); + cbt_prepared.prepare(module, &cbt_key, scratch.borrow()); let now: Instant = Instant::now(); cbt_prepared.execute_to_exponent( @@ -247,7 +194,8 @@ where scratch.borrow(), ); - let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &res); + res_prepared.prepare(module, &res, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); @@ -260,71 +208,23 @@ where assert_eq!(pt_res.data.at(0, 0), pt_want); } -pub fn test_circuit_bootstrapping_to_constant(module: &Module) +pub fn test_circuit_bootstrapping_to_constant(module: &M) where - Module: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc - + VmpPrepare - + SvpPrepare - + SvpPPolAlloc - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - B: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl - + TakeSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, B>>, - BlindRotationKeyPrepared, BRA, B>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + M: ModuleN + + GLWESecretPreparedFactory + + GLWEExternalProduct + + GLWEDecrypt + + LWEEncryptSk + + CircuitBootstrappingKeyEncryptSk + + CircuitBootstrappingKeyPreparedFactory + + CirtuitBootstrappingExecute + + GGSWPreparedFactory + + GGSWNoise + + GLWEEncryptSk + + VecZnxRotateInplace, + BlindRotationKey, BRA>: BlindRotationKeyFactory, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, + Scratch: ScratchTakeCore, { let n_glwe: usize = module.n(); let base2k: usize = 14; @@ -363,7 +263,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -371,7 +271,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -390,7 +290,7 @@ where rank: rank.into(), }; - let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 23); let mut source_xs: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([1u8; 32]); @@ -402,7 +302,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1; @@ -415,20 +316,25 @@ where ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now(); - let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::alloc_from_infos(&cbt_infos); + println!("CBT-ALLOC: {} ms", now.elapsed().as_millis()); + + let now: Instant = Instant::now(); + cbt_key.encrypt_sk( module, &sk_lwe, &sk_glwe, - &cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); - println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); + println!("CBT-ENCRYPT: {} ms", now.elapsed().as_millis()); let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(module, &cbt_infos); + cbt_prepared.prepare(module, &cbt_key, scratch.borrow()); let now: Instant = Instant::now(); cbt_prepared.execute_to_constant( @@ -460,7 +366,8 @@ where scratch.borrow(), ); - let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared, BE> = GGSWPrepared::alloc_from_infos(module, &res); + res_prepared.prepare(module, &res, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs new file mode 100644 index 0000000..a1fdab2 --- /dev/null +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/fft64.rs @@ -0,0 +1,21 @@ +use poulpy_backend::cpu_fft64_ref::FFT64Ref; +use poulpy_hal::{api::ModuleNew, layouts::Module}; + +use crate::tfhe::{ + blind_rotation::CGGI, + circuit_bootstrapping::tests::circuit_bootstrapping::{ + test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent, + }, +}; + +#[test] +fn test_to_constant_cggi() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_constant::(&module); +} + +#[test] +fn test_to_exponent_cggi() { + let module: Module = Module::::new(256); + test_circuit_bootstrapping_to_exponent::(&module); +} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs deleted file mode 100644 index 3661f81..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs +++ /dev/null @@ -1,21 +0,0 @@ -use poulpy_backend::cpu_spqlios::FFT64Spqlios; -use poulpy_hal::{api::ModuleNew, layouts::Module}; - -use crate::tfhe::{ - blind_rotation::CGGI, - circuit_bootstrapping::tests::circuit_bootstrapping::{ - test_circuit_bootstrapping_to_constant, test_circuit_bootstrapping_to_exponent, - }, -}; - -#[test] -fn test_to_constant() { - let module: Module = Module::::new(256); - test_circuit_bootstrapping_to_constant::(&module); -} - -#[test] -fn test_to_exponent() { - let module: Module = Module::::new(256); - test_circuit_bootstrapping_to_exponent::(&module); -} diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs deleted file mode 100644 index aebaafb..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod fft64; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs deleted file mode 100644 index f2bc1d4..0000000 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod cpu_spqlios; diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs index 22f8f4f..893a6be 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs @@ -1,3 +1,3 @@ pub mod circuit_bootstrapping; -mod implementation; +mod fft64; diff --git a/poulpy-schemes/src/tfhe/mod.rs b/poulpy-schemes/src/tfhe/mod.rs index cc2dbe9..b91a937 100644 --- a/poulpy-schemes/src/tfhe/mod.rs +++ b/poulpy-schemes/src/tfhe/mod.rs @@ -1,3 +1,3 @@ // pub mod bdd_arithmetic; pub mod blind_rotation; -//pub mod circuit_bootstrapping; +pub mod circuit_bootstrapping; From 56a88892a5ee983613baf2ead045ac1e78574a3f Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 22 Oct 2025 10:37:45 +0200 Subject: [PATCH 59/60] fixed cbt tests --- .../compressed/glwe_automorphism_key.rs | 1 + .../compressed/glwe_switching_key.rs | 2 ++ poulpy-core/src/encryption/glwe.rs | 22 +++++++------- .../src/encryption/glwe_automorphism_key.rs | 2 ++ .../src/encryption/glwe_switching_key.rs | 2 ++ .../encryption/glwe_to_lwe_switching_key.rs | 1 + .../src/encryption/lwe_switching_key.rs | 23 +++++++------- .../encryption/lwe_to_glwe_switching_key.rs | 2 ++ poulpy-core/src/glwe_trace.rs | 2 ++ .../src/tfhe/circuit_bootstrapping/circuit.rs | 30 ++++++++++++++++--- .../src/tfhe/circuit_bootstrapping/key.rs | 7 +++-- .../tests/circuit_bootstrapping.rs | 2 +- 12 files changed, 69 insertions(+), 27 deletions(-) diff --git a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs index fb1d567..0c15e96 100644 --- a/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs @@ -99,6 +99,7 @@ where let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank()); { let (mut sk_out, _) = scratch_1.take_glwe_secret(self.n().into(), sk.rank()); + sk_out.dist = sk.dist; for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), diff --git a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs index e1c40ea..b801927 100644 --- a/poulpy-core/src/encryption/compressed/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/compressed/glwe_switching_key.rs @@ -121,6 +121,8 @@ where } } + sk_out_tmp.dist = sk_out.dist; + self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2); *res.input_degree() = sk_in.n(); diff --git a/poulpy-core/src/encryption/glwe.rs b/poulpy-core/src/encryption/glwe.rs index fc59e4b..b032b38 100644 --- a/poulpy-core/src/encryption/glwe.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -485,18 +485,20 @@ where let ct: &mut VecZnx<&mut [u8]> = &mut res.to_mut(); let sk: GLWESecretPrepared<&[u8], BE> = sk.to_ref(); - #[cfg(debug_assertions)] - { - if compressed { - assert_eq!( - ct.cols(), - 1, - "invalid glwe: compressed tag=true but #cols={} != 1", - ct.cols() - ) - } + if compressed { + assert_eq!( + ct.cols(), + 1, + "invalid glwe: compressed tag=true but #cols={} != 1", + ct.cols() + ) } + assert!( + sk.dist != Distribution::NONE, + "glwe secret distribution is NONE (have you prepared the key?)" + ); + let size: usize = ct.size(); let (mut c0, scratch_1) = scratch.take_vec_znx(self.n(), 1, size); diff --git a/poulpy-core/src/encryption/glwe_automorphism_key.rs b/poulpy-core/src/encryption/glwe_automorphism_key.rs index 429a6d7..4feaeb6 100644 --- a/poulpy-core/src/encryption/glwe_automorphism_key.rs +++ b/poulpy-core/src/encryption/glwe_automorphism_key.rs @@ -116,6 +116,8 @@ where { let (mut sk_out, _) = scratch_1.take_glwe_secret(sk.n(), sk.rank()); + sk_out.dist = sk.dist; + for i in 0..sk.rank().into() { self.vec_znx_automorphism( self.galois_element_inv(p), diff --git a/poulpy-core/src/encryption/glwe_switching_key.rs b/poulpy-core/src/encryption/glwe_switching_key.rs index 62d60c0..e1467f3 100644 --- a/poulpy-core/src/encryption/glwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_switching_key.rs @@ -128,6 +128,8 @@ where } } + sk_out_tmp.dist = sk_out.dist; + self.gglwe_encrypt_sk( res, &sk_in_tmp, diff --git a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs index 30a46a8..71877a4 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_switching_key.rs @@ -101,6 +101,7 @@ where { let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), sk_lwe_as_glwe_prep.rank()); + sk_lwe_as_glwe.dist = sk_lwe.dist; sk_lwe_as_glwe.data.zero(); sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2); diff --git a/poulpy-core/src/encryption/lwe_switching_key.rs b/poulpy-core/src/encryption/lwe_switching_key.rs index 859f0c9..431545b 100644 --- a/poulpy-core/src/encryption/lwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_switching_key.rs @@ -111,21 +111,24 @@ where assert!(sk_lwe_out.n().0 <= res.n().0); assert!(res.n() <= self.n() as u32); - let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); - let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1)); + let (mut sk_glwe_in, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); + let (mut sk_glwe_out, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1)); - sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); - sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); - self.vec_znx_automorphism_inplace(-1, &mut sk_out_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_glwe_in.dist = sk_lwe_in.dist; + sk_glwe_out.dist = sk_lwe_out.dist; - sk_in_glwe.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); - sk_in_glwe.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); - self.vec_znx_automorphism_inplace(-1, &mut sk_in_glwe.data.as_vec_znx_mut(), 0, scratch_2); + sk_glwe_out.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0)); + sk_glwe_out.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_glwe_out.data.as_vec_znx_mut(), 0, scratch_2); + + sk_glwe_in.data.at_mut(0, 0)[..sk_lwe_in.n().into()].copy_from_slice(sk_lwe_in.data.at(0, 0)); + sk_glwe_in.data.at_mut(0, 0)[sk_lwe_in.n().into()..].fill(0); + self.vec_znx_automorphism_inplace(-1, &mut sk_glwe_in.data.as_vec_znx_mut(), 0, scratch_2); self.glwe_switching_key_encrypt_sk( res, - &sk_in_glwe, - &sk_out_glwe, + &sk_glwe_in, + &sk_glwe_out, source_xa, source_xe, scratch_2, diff --git a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs index 524bb1a..af31420 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_switching_key.rs @@ -100,6 +100,8 @@ where assert!(sk_lwe.n().0 <= self.n() as u32); let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1)); + sk_lwe_as_glwe.dist = sk_lwe.dist; + sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0)); sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0); self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1); diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index bfcb6a3..c2ba15c 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -192,6 +192,8 @@ where ); } } else { + // println!("res: {}", res); + for i in start..end { self.glwe_rsh(1, res, scratch); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 9838fe0..5b3ea4d 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -6,8 +6,10 @@ use poulpy_hal::{ }; use poulpy_core::{ - GGSWFromGGLWE, GLWEPacking, GLWETrace, ScratchTakeCore, - layouts::{Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef}, + GGSWFromGGLWE, GLWEDecrypt, GLWEPacking, GLWETrace, ScratchTakeCore, + layouts::{ + Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef, + }, }; use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; @@ -94,7 +96,14 @@ impl CircuitBootstrappingKeyPre impl CirtuitBootstrappingExecute for Module where - Self: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + Self: ModuleN + + LookupTableFactory + + BlindRotationExecute + + GLWETrace + + GLWEPacking + + GGSWFromGGLWE + + GLWESecretPreparedFactory + + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { @@ -167,7 +176,14 @@ pub fn circuit_bootstrap_core( R: GGSWToMut, L: LWEToRef, D: DataRef, - M: ModuleN + LookupTableFactory + BlindRotationExecute + GLWETrace + GLWEPacking + GGSWFromGGLWE, + M: ModuleN + + LookupTableFactory + + BlindRotationExecute + + GLWETrace + + GLWEPacking + + GGSWFromGGLWE + + GLWESecretPreparedFactory + + GLWEDecrypt, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { @@ -255,6 +271,12 @@ pub fn circuit_bootstrap_core( tmp_glwe.trace(module, 0, module.log_n(), &res_glwe, &key.atk, scratch_2); } + // let sk_glwe: &poulpy_core::layouts::GLWESecret<&[u8]> = &sk_glwe.to_ref(); + // let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_glwe.rank()); + // let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&res_glwe); + // res_glwe.decrypt(module, &mut pt, &sk_glwe_prepared, scratch_2); + // println!("pt[{i}]: {}", pt); + if i < dnum { module.glwe_rotate_inplace(-(gap as i64), &mut res_glwe, scratch_2); } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 172d430..c6b8adc 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,5 +1,5 @@ use poulpy_core::{ - GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, + Distribution, GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, GetDistribution, ScratchTakeCore, layouts::{ GGLWEInfos, GGSWInfos, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecretPreparedFactory, GLWESecretToRef, GLWETensorKey, GLWETensorKeyLayout, LWEInfos, LWESecretToRef, prepared::GLWESecretPrepared, @@ -140,11 +140,14 @@ where assert_eq!(sk_glwe.n(), atk_infos.n()); assert_eq!(sk_glwe.n(), tsk_infos.n()); + assert!(sk_glwe.dist() != &Distribution::NONE); + for (p, atk) in res.atk.iter_mut() { atk.encrypt_sk(self, *p, sk_glwe, source_xa, source_xe, scratch); } - let sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + let mut sk_glwe_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); + sk_glwe_prepared.prepare(self, sk_glwe); res.brk.encrypt_sk( self, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 77469d1..59223ea 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -229,7 +229,7 @@ where let n_glwe: usize = module.n(); let base2k: usize = 14; let extension_factor: usize = 1; - let rank: usize = 2; + let rank: usize = 1; let n_lwe: usize = 77; let k_lwe_pt: usize = 1; From cedf7b9c593f64c5598f863d41bf71c504213a8c Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 22 Oct 2025 16:43:46 +0200 Subject: [PATCH 60/60] clippy --- poulpy-core/src/automorphism/gglwe_atk.rs | 4 +- poulpy-core/src/encryption/ggsw.rs | 2 +- poulpy-core/src/encryption/glwe.rs | 1 + poulpy-core/src/layouts/compressed/glwe.rs | 4 +- .../layouts/compressed/glwe_switching_key.rs | 4 +- poulpy-core/src/layouts/glwe_switching_key.rs | 4 +- poulpy-core/src/layouts/mod.rs | 2 +- .../layouts/prepared/glwe_automorphism_key.rs | 28 +- .../src/layouts/prepared/glwe_secret.rs | 5 +- .../prepared/glwe_to_lwe_switching_key.rs | 4 +- .../tests/test_suite/encryption/gglwe_atk.rs | 4 +- .../src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs | 97 ++--- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 83 +--- .../bdd_arithmetic/ciphertexts/block_debug.rs | 147 +++++++ .../ciphertexts/block_prepared.rs | 283 ++++--------- .../tfhe/bdd_arithmetic/ciphertexts/mod.rs | 5 + .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 95 +---- .../src/tfhe/bdd_arithmetic/eval.rs | 58 ++- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 396 ++++++++++-------- .../src/tfhe/bdd_arithmetic/parameters.rs | 8 +- .../src/tfhe/bdd_arithmetic/test.rs | 130 ++---- .../algorithms/cggi/key_prepared.rs | 9 +- .../blind_rotation/layouts/key_prepared.rs | 16 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 35 +- .../circuit_bootstrapping/key_prepared.rs | 10 + poulpy-schemes/src/tfhe/mod.rs | 2 +- 26 files changed, 713 insertions(+), 723 deletions(-) create mode 100644 poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index a291f96..ffc35cd 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -116,7 +116,7 @@ where } } - res.set_p((p * key.p()) % (self.cyclotomic_order() as i64)); + res.set_p((p * key.p()) % self.cyclotomic_order()); } fn glwe_automorphism_key_automorphism_inplace(&self, res: &mut R, key: &K, scratch: &mut Scratch) @@ -160,6 +160,6 @@ where } } - res.set_p((res.p() * key.p()) % (self.cyclotomic_order() as i64)); + res.set_p((res.p() * key.p()) % self.cyclotomic_order()); } } diff --git a/poulpy-core/src/encryption/ggsw.rs b/poulpy-core/src/encryption/ggsw.rs index 85b8be5..86b810c 100644 --- a/poulpy-core/src/encryption/ggsw.rs +++ b/poulpy-core/src/encryption/ggsw.rs @@ -107,7 +107,7 @@ where let base2k: usize = res.base2k().into(); let rank: usize = res.rank().into(); let dsize: usize = res.dsize().into(); - let cols: usize = (rank + 1).into(); + let cols: usize = rank + 1; let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res); diff --git a/poulpy-core/src/encryption/glwe.rs b/poulpy-core/src/encryption/glwe.rs index b032b38..c81833e 100644 --- a/poulpy-core/src/encryption/glwe.rs +++ b/poulpy-core/src/encryption/glwe.rs @@ -427,6 +427,7 @@ where } pub(crate) trait GLWEEncryptSkInternal { + #[allow(clippy::too_many_arguments)] fn glwe_encrypt_sk_internal( &self, base2k: usize, diff --git a/poulpy-core/src/layouts/compressed/glwe.rs b/poulpy-core/src/layouts/compressed/glwe.rs index fa61b81..eda1e8f 100644 --- a/poulpy-core/src/layouts/compressed/glwe.rs +++ b/poulpy-core/src/layouts/compressed/glwe.rs @@ -192,7 +192,7 @@ pub trait GLWECompressedToRef { impl GLWECompressedToRef for GLWECompressed { fn to_ref(&self) -> GLWECompressed<&[u8]> { GLWECompressed { - seed: self.seed.clone(), + seed: self.seed, base2k: self.base2k, k: self.k, rank: self.rank, @@ -208,7 +208,7 @@ pub trait GLWECompressedToMut { impl GLWECompressedToMut for GLWECompressed { fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> { GLWECompressed { - seed: self.seed.clone(), + seed: self.seed, base2k: self.base2k, k: self.k, rank: self.rank, diff --git a/poulpy-core/src/layouts/compressed/glwe_switching_key.rs b/poulpy-core/src/layouts/compressed/glwe_switching_key.rs index 82fb797..6da1d32 100644 --- a/poulpy-core/src/layouts/compressed/glwe_switching_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_switching_key.rs @@ -146,8 +146,8 @@ where { impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.input_degree = Degree(reader.read_u32::()? as u32); - self.output_degree = Degree(reader.read_u32::()? as u32); + self.input_degree = Degree(reader.read_u32::()?); + self.output_degree = Degree(reader.read_u32::()?); self.key.read_from(reader) } } diff --git a/poulpy-core/src/layouts/glwe_switching_key.rs b/poulpy-core/src/layouts/glwe_switching_key.rs index 1e67222..3f94c06 100644 --- a/poulpy-core/src/layouts/glwe_switching_key.rs +++ b/poulpy-core/src/layouts/glwe_switching_key.rs @@ -240,8 +240,8 @@ impl GLWESwitchingKey { impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { - self.input_degree = Degree(reader.read_u32::()? as u32); - self.output_degree = Degree(reader.read_u32::()? as u32); + self.input_degree = Degree(reader.read_u32::()?); + self.output_degree = Degree(reader.read_u32::()?); self.key.read_from(reader) } } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 6447fc5..1168ac8 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -43,7 +43,7 @@ pub trait GetDegree { impl GetDegree for Module { fn ring_degree(&self) -> Degree { - Self::n(&self).into() + Self::n(self).into() } } diff --git a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs index 0296e0f..adf54eb 100644 --- a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs @@ -69,7 +69,7 @@ pub trait GLWEAutomorphismKeyPreparedFactory where Self: GGLWEPreparedFactory, { - fn alloc_automorphism_key_prepared( + fn alloc_glwe_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -83,7 +83,7 @@ where } } - fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> GLWEAutomorphismKeyPrepared, B> + fn alloc_glwe_automorphism_key_prepared_from_infos(&self, infos: &A) -> GLWEAutomorphismKeyPrepared, B> where A: GGLWEInfos, { @@ -92,7 +92,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - self.alloc_automorphism_key_prepared( + self.alloc_glwe_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), @@ -101,7 +101,7 @@ where ) } - fn bytes_of_automorphism_key_prepared( + fn bytes_of_glwe_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -112,7 +112,7 @@ where self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize) } - fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize + fn bytes_of_glwe_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -121,7 +121,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - self.bytes_of_automorphism_key_prepared( + self.bytes_of_glwe_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(), @@ -130,14 +130,14 @@ where ) } - fn prepare_automorphism_key_tmp_bytes(&self, infos: &A) -> usize + fn prepare_glwe_automorphism_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { self.prepare_gglwe_tmp_bytes(infos) } - fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_glwe_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut + SetGaloisElement, O: GGLWEToRef + GetGaloisElement, @@ -155,14 +155,14 @@ impl GLWEAutomorphismKeyPrepared, B> { A: GGLWEInfos, M: GLWEAutomorphismKeyPreparedFactory, { - module.alloc_automorphism_key_prepared_from_infos(infos) + module.alloc_glwe_automorphism_key_prepared_from_infos(infos) } pub fn alloc(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where M: GLWEAutomorphismKeyPreparedFactory, { - module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) + module.alloc_glwe_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } pub fn bytes_of_from_infos(module: &M, infos: &A) -> usize @@ -170,14 +170,14 @@ impl GLWEAutomorphismKeyPrepared, B> { A: GGLWEInfos, M: GLWEAutomorphismKeyPreparedFactory, { - module.bytes_of_automorphism_key_prepared_from_infos(infos) + module.bytes_of_glwe_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: GLWEAutomorphismKeyPreparedFactory, { - module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) + module.bytes_of_glwe_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } } @@ -186,7 +186,7 @@ impl GLWEAutomorphismKeyPrepared, B> { where M: GLWEAutomorphismKeyPreparedFactory, { - module.prepare_automorphism_key_tmp_bytes(self) + module.prepare_glwe_automorphism_key_tmp_bytes(self) } } @@ -196,7 +196,7 @@ impl GLWEAutomorphismKeyPrepared { O: GGLWEToRef + GetGaloisElement, M: GLWEAutomorphismKeyPreparedFactory, { - module.prepare_automorphism_key(self, other, scratch); + module.prepare_glwe_automorphism_key(self, other, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_secret.rs b/poulpy-core/src/layouts/prepared/glwe_secret.rs index 0e38221..8f4917d 100644 --- a/poulpy-core/src/layouts/prepared/glwe_secret.rs +++ b/poulpy-core/src/layouts/prepared/glwe_secret.rs @@ -166,7 +166,10 @@ impl GLWESecretPreparedToRef for GLWESecretPrepared { +pub trait GLWESecretPreparedToMut +where + Self: GLWESecretPreparedToRef, +{ fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B>; } diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs index 865e9cd..6edac5e 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_switching_key.rs @@ -102,11 +102,11 @@ 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) + fn prepare_glwe_to_lwe_switching_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.prepare_glwe_switching_key_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos) } fn prepare_glwe_to_lwe_switching_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) 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 ad456c9..9363539 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -81,7 +81,7 @@ where i, ); }); - let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank()); sk_out_prepared.prepare(module, &sk_out); atk.key @@ -150,7 +150,7 @@ where i, ); }); - let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); + let mut sk_out_prepared: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc(module, sk_out.rank()); sk_out_prepared.prepare(module, &sk_out); let mut atk: GLWEAutomorphismKey> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs index ba0d38f..db4a1de 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs @@ -3,72 +3,44 @@ use poulpy_core::layouts::prepared::GGSWPreparedToRef; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; use crate::tfhe::bdd_arithmetic::{ - BitCircuitInfo, Circuit, CircuitExecute, FheUintBlocks, FheUintBlocksPrep, UnsignedInteger, circuits, + ExecuteBDDCircuit, FheUintBlocks, FheUintBlocksPrepared, GetBitCircuitInfo, UnsignedInteger, circuits, }; -/// Operations Z x Z -> Z -pub(crate) struct Circuits2WTo1W(pub &'static Circuit); +impl ExecuteBDDCircuit2WTo1W for Module where Self: Sized + ExecuteBDDCircuit {} -pub trait EvalBDD2WTo1W { - fn eval_bdd_2w_to_1w( - &self, - module: &Module, - out: &mut FheUintBlocks, - a: &FheUintBlocksPrep, - b: &FheUintBlocksPrep, - scratch: &mut Scratch, - ) where - R: DataMut, - A: DataRef, - B: DataRef; -} - -impl EvalBDD2WTo1W - for Circuits2WTo1W +pub trait ExecuteBDDCircuit2WTo1W where - Circuit: CircuitExecute, + Self: Sized + ExecuteBDDCircuit, { - fn eval_bdd_2w_to_1w( + /// Operations Z x Z -> Z + fn execute_bdd_circuit_2w_to_1w( &self, - module: &Module, out: &mut FheUintBlocks, - a: &FheUintBlocksPrep, - b: &FheUintBlocksPrep, + circuit: &C, + a: &FheUintBlocksPrepared, + b: &FheUintBlocksPrepared, scratch: &mut Scratch, ) where + C: GetBitCircuitInfo, R: DataMut, A: DataRef, B: DataRef, - { - eval_bdd_2w_to_1w(module, self.0, out, a, b, scratch); - } -} - -pub fn eval_bdd_2w_to_1w, BE: Backend>( - module: &Module, - circuit: &C, - out: &mut FheUintBlocks, - a: &FheUintBlocksPrep, - b: &FheUintBlocksPrep, - scratch: &mut Scratch, -) { - #[cfg(debug_assertions)] { assert_eq!(out.blocks.len(), T::WORD_SIZE); assert_eq!(b.blocks.len(), T::WORD_SIZE); assert_eq!(b.blocks.len(), T::WORD_SIZE); + + // Collects inputs into a single array + let inputs: Vec<&dyn GGSWPreparedToRef> = a + .blocks + .iter() + .map(|x| x as &dyn GGSWPreparedToRef) + .chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef)) + .collect_vec(); + + // Evaluates out[i] = circuit[i](a, b) + self.execute_bdd_circuit(&mut out.blocks, &inputs, circuit, scratch); } - - // Collects inputs into a single array - let inputs: Vec<&dyn GGSWPreparedToRef> = a - .blocks - .iter() - .map(|x| x as &dyn GGSWPreparedToRef) - .chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef)) - .collect_vec(); - - // Evaluates out[i] = circuit[i](a, b) - circuit.execute(module, &mut out.blocks, &inputs, scratch); } #[macro_export] @@ -76,13 +48,14 @@ macro_rules! define_bdd_2w_to_1w_trait { ($(#[$meta:meta])* $vis:vis $trait_name:ident, $method_name:ident) => { $(#[$meta])* $vis trait $trait_name { - fn $method_name( + fn $method_name( &mut self, - module: &Module, - a: &FheUintBlocksPrep, - b: &FheUintBlocksPrep, + module: &M, + a: &FheUintBlocksPrepared, + b: &FheUintBlocksPrepared, scratch: &mut Scratch, ) where + M: ExecuteBDDCircuit2WTo1W, A: DataRef, B: DataRef; } @@ -92,23 +65,19 @@ macro_rules! define_bdd_2w_to_1w_trait { #[macro_export] macro_rules! impl_bdd_2w_to_1w_trait { ($trait_name:ident, $method_name:ident, $ty:ty, $n:literal, $circuit_ty:ty, $output_circuits:path) => { - impl $trait_name<$ty, BE> for FheUintBlocks - where - Circuits2WTo1W<$circuit_ty, $n>: EvalBDD2WTo1W, - { - fn $method_name( + impl $trait_name<$ty, BE> for FheUintBlocks { + fn $method_name( &mut self, - module: &Module, - a: &FheUintBlocksPrep, - b: &FheUintBlocksPrep, + module: &M, + a: &FheUintBlocksPrepared, + b: &FheUintBlocksPrepared, scratch: &mut Scratch, ) where + M: ExecuteBDDCircuit2WTo1W<$ty, BE>, A: DataRef, B: DataRef, { - const OP: Circuits2WTo1W<$circuit_ty, $n> = Circuits2WTo1W::<$circuit_ty, $n>(&$output_circuits); - - OP.eval_bdd_2w_to_1w(module, self, a, b, scratch); + module.execute_bdd_circuit_2w_to_1w(self, &$output_circuits, a, b, scratch) } } }; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 00fe458..82d985e 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs @@ -1,26 +1,17 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; +use poulpy_core::{ + GLWEDecrypt, GLWENoise, + layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision}, +}; #[cfg(test)] +use poulpy_core::GLWEEncryptSk; use poulpy_core::ScratchTakeCore; -use poulpy_core::{layouts::prepared::GLWESecretPrepared}; -use poulpy_hal::api::VecZnxBigBytesOf; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; #[cfg(test)] -use poulpy_hal::api::{ - VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, -}; #[cfg(test)] use poulpy_hal::source::Source; -use poulpy_hal::{ - api::{ - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, - VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, - }, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, -}; - -use poulpy_hal::api::{SvpApplyDftToDftInplace, VecZnxNormalizeInplace, VecZnxSubInplace}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; @@ -79,25 +70,13 @@ impl FheUintBlocks { &mut self, module: &Module, value: T, - sk: &GLWESecretPrepared, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - S: DataRef, - Module: VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, + S: GLWESecretPreparedToRef + GLWEInfos, + Module: GLWEEncryptSk, Scratch: ScratchTakeCore, { use poulpy_core::layouts::GLWEPlaintextLayout; @@ -115,29 +94,20 @@ impl FheUintBlocks { k: 1_usize.into(), }; - let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); + let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); for i in 0..T::WORD_SIZE { pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(2), 0); - self.blocks[i].encrypt_sk(&module, &pt, sk, source_xa, source_xe, scratch_1); + self.blocks[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); } } } impl FheUintBlocks { - pub fn decrypt( - &self, - module: &Module, - sk: &GLWESecretPrepared, - scratch: &mut Scratch, - ) -> T + pub fn decrypt(&self, module: &Module, sk: &S, scratch: &mut Scratch) -> T where - Module: VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, + Module: GLWEDecrypt, + S: GLWESecretPreparedToRef + GLWEInfos, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -153,7 +123,7 @@ impl FheUintBlocks { k: self.k(), }; - let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); + let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); let mut bits: Vec = vec![0u8; T::WORD_SIZE]; @@ -169,25 +139,10 @@ impl FheUintBlocks { T::from_bits(&bits) } - pub fn noise( - &self, - module: &Module, - sk: &GLWESecretPrepared, - want: T, - scratch: &mut Scratch, - ) -> Vec + pub fn noise(&self, module: &Module, sk: &S, want: T, scratch: &mut Scratch) -> Vec where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxNormalizeInplace, + Module: GLWENoise, + S: GLWESecretPreparedToRef + GLWEInfos, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -203,7 +158,7 @@ impl FheUintBlocks { k: 1_usize.into(), }; - let (mut pt_want, scratch_1) = scratch.take_glwe_pt(&pt_infos); + let (mut pt_want, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); let mut noise: Vec = vec![0f64; T::WORD_SIZE]; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs new file mode 100644 index 0000000..f291c62 --- /dev/null +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_debug.rs @@ -0,0 +1,147 @@ +use std::marker::PhantomData; + +use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUintBlockDebugPrepare, ToBits}; +use crate::tfhe::{ + bdd_arithmetic::{FheUintBlocks, UnsignedInteger}, + blind_rotation::BlindRotationAlgo, + circuit_bootstrapping::CirtuitBootstrappingExecute, +}; +use poulpy_core::GGSWNoise; +#[cfg(test)] +use poulpy_core::layouts::{Base2K, Dnum, Dsize, Rank, TorusPrecision}; +use poulpy_core::layouts::{GGSW, GLWESecretPreparedToRef}; +use poulpy_core::{ + LWEFromGLWE, ScratchTakeCore, + layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWE, LWEInfos}, +}; +#[cfg(test)] +use poulpy_hal::api::ModuleN; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; + +pub(crate) struct FheUintBlocksPreparedDebug { + pub(crate) blocks: Vec>, + pub(crate) _base: u8, + pub(crate) _phantom: PhantomData, +} + +#[cfg(test)] +impl FheUintBlocksPreparedDebug, T> { + #[allow(dead_code)] + pub(crate) fn alloc(module: &M, infos: &A) -> Self + where + M: ModuleN, + A: GGSWInfos, + { + Self::alloc_with( + module, + infos.base2k(), + infos.k(), + infos.dnum(), + infos.dsize(), + infos.rank(), + ) + } + + #[allow(dead_code)] + pub(crate) fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + where + M: ModuleN, + { + Self { + blocks: (0..T::WORD_SIZE) + .map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize)) + .collect(), + _base: 1, + _phantom: PhantomData, + } + } +} + +impl LWEInfos for FheUintBlocksPreparedDebug { + fn base2k(&self) -> poulpy_core::layouts::Base2K { + self.blocks[0].base2k() + } + + fn k(&self) -> poulpy_core::layouts::TorusPrecision { + self.blocks[0].k() + } + + fn n(&self) -> poulpy_core::layouts::Degree { + self.blocks[0].n() + } +} + +impl GLWEInfos for FheUintBlocksPreparedDebug { + fn rank(&self) -> poulpy_core::layouts::Rank { + self.blocks[0].rank() + } +} + +impl GGSWInfos for FheUintBlocksPreparedDebug { + fn dsize(&self) -> poulpy_core::layouts::Dsize { + self.blocks[0].dsize() + } + + fn dnum(&self) -> poulpy_core::layouts::Dnum { + self.blocks[0].dnum() + } +} + +impl FheUintBlocksPreparedDebug { + pub(crate) fn noise(&self, module: &M, sk: &S, want: T) + where + S: GLWESecretPreparedToRef, + M: GGSWNoise, + { + for (i, ggsw) in self.blocks.iter().enumerate() { + use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut}; + let mut pt_want = ScalarZnx::alloc(self.n().into(), 1); + pt_want.at_mut(0, 0)[0] = want.bit(i) as i64; + ggsw.print_noise(module, sk, &pt_want); + } + } +} + +impl FheUintBlockDebugPrepare for Module +where + Self: LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, + Scratch: ScratchTakeCore, +{ + fn fhe_uint_block_debug_prepare( + &self, + res: &mut FheUintBlocksPreparedDebug, + bits: &FheUintBlocks, + key: &BDDKeyPrepared, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR0: DataRef, + DR1: DataRef, + { + assert_eq!(res.blocks.len(), bits.blocks.len()); + + let mut lwe: LWE> = LWE::alloc_from_infos(&bits.blocks[0]); //TODO: add TakeLWE + for (dst, src) in res.blocks.iter_mut().zip(bits.blocks.iter()) { + lwe.from_glwe(self, src, &key.ks, scratch); + key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch); + } + } +} + +impl FheUintBlocksPreparedDebug { + pub fn prepare( + &mut self, + module: &M, + other: &FheUintBlocks, + key: &BDDKeyPrepared, + scratch: &mut Scratch, + ) where + BRA: BlindRotationAlgo, + O: DataRef, + K: DataRef, + M: FheUintBlockDebugPrepare, + Scratch: ScratchTakeCore, + { + module.fhe_uint_block_debug_prepare(self, other, key, scratch); + } +} diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 1ed7229..3753f99 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -1,89 +1,60 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}; -#[cfg(test)] -use poulpy_core::{ - layouts::{prepared::GLWESecretPrepared, GGSW}, ScratchTakeCore, -}; -use poulpy_hal::{ - api::VmpPMatAlloc, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, +use poulpy_core::layouts::{ + Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared, }; #[cfg(test)] +use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef}; +use poulpy_hal::layouts::{Backend, Data, DataRef, Module}; +#[cfg(test)] use poulpy_hal::{ - api::{ - SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, - VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, - VecZnxSubInplace, VmpPrepare, - }, + api::ModuleN, + layouts::{DataMut, Scratch}, source::Source, }; -use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, UnsignedInteger}; - #[cfg(test)] -pub(crate) struct FheUintBlocksPrepDebug { - pub(crate) blocks: Vec>, - pub(crate) _base: u8, - pub(crate) _phantom: PhantomData, -} - -#[cfg(test)] -impl FheUintBlocksPrepDebug, T> { - #[allow(dead_code)] - pub(crate) fn alloc(module: &Module, infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - module, - infos.base2k(), - infos.k(), - infos.dnum(), - infos.dsize(), - infos.rank(), - ) - } - - #[allow(dead_code)] - pub(crate) fn alloc_with( - module: &Module, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - dsize: Dsize, - rank: Rank, - ) -> Self { - Self { - blocks: (0..T::WORD_SIZE) - .map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize)) - .collect(), - _base: 1, - _phantom: PhantomData, - } - } -} +use crate::tfhe::bdd_arithmetic::ToBits; +use crate::tfhe::bdd_arithmetic::UnsignedInteger; /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. -pub struct FheUintBlocksPrep { +pub struct FheUintBlocksPrepared { pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } -impl FheUintBlocksPrep, BE, T> -where - Module: VmpPMatAlloc, +impl FheUintBlocksPreparedFactory for Module where + Self: Sized + GGSWPreparedFactory { - #[allow(dead_code)] - pub(crate) fn alloc(module: &Module, infos: &A) -> Self +} + +pub trait FheUintBlocksPreparedFactory +where + Self: Sized + GGSWPreparedFactory, +{ + fn alloc_fhe_uint_blocks_prepared( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + dsize: Dsize, + rank: Rank, + ) -> FheUintBlocksPrepared, T, BE> { + FheUintBlocksPrepared { + blocks: (0..T::WORD_SIZE) + .map(|_| GGSWPrepared::alloc(self, base2k, k, dnum, dsize, rank)) + .collect(), + _base: 1, + _phantom: PhantomData, + } + } + + fn alloc_fhe_uint_blocks_prepared_from_infos(&self, infos: &A) -> FheUintBlocksPrepared, T, BE> where A: GGSWInfos, { - Self::alloc_with( - module, + self.alloc_fhe_uint_blocks_prepared( infos.base2k(), infos.k(), infos.dnum(), @@ -91,129 +62,90 @@ where infos.rank(), ) } +} + +impl FheUintBlocksPrepared, T, BE> { + #[allow(dead_code)] + pub(crate) fn alloc(module: &M, infos: &A) -> Self + where + A: GGSWInfos, + M: FheUintBlocksPreparedFactory, + { + module.alloc_fhe_uint_blocks_prepared_from_infos(infos) + } #[allow(dead_code)] - pub(crate) fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self + pub(crate) fn alloc_with(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - Module: VmpPMatAlloc, + M: FheUintBlocksPreparedFactory, { - Self { - blocks: (0..T::WORD_SIZE) - .map(|_| GGSWPrepared::alloc(module, base2k, k, dnum, dsize, rank)) - .collect(), - _base: 1, - _phantom: PhantomData, - } + module.alloc_fhe_uint_blocks_prepared(base2k, k, dnum, dsize, rank) } } -impl FheUintBlocksPrep { - #[allow(dead_code)] - #[cfg(test)] - pub(crate) fn encrypt_sk( - &mut self, - module: &Module, +#[cfg(test)] +impl FheUintBlocksPreparedEncryptSk for Module where + Self: Sized + ModuleN + GGSWEncryptSk + GGSWPreparedFactory +{ +} + +#[cfg(test)] +pub trait FheUintBlocksPreparedEncryptSk +where + Self: Sized + ModuleN + GGSWEncryptSk + GGSWPreparedFactory, +{ + fn fhe_uint_blocks_prepared_encrypt_sk( + &self, + res: &mut FheUintBlocksPrepared, value: T, - sk: &GLWESecretPrepared, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - S: DataRef, - Module: VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VmpPrepare, + DM: DataMut, + S: GLWESecretPreparedToRef + GLWEInfos, Scratch: ScratchTakeCore, { - #[cfg(debug_assertions)] - { - assert!(module.n().is_multiple_of(T::WORD_SIZE)); - assert_eq!(self.n(), module.n() as u32); - assert_eq!(sk.n(), module.n() as u32); - } + use poulpy_hal::api::ScratchTakeBasic; - let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(self); - let (mut pt, scratch_2) = scratch_1.take_scalar_znx(module.n(), 1); + assert!(self.n().is_multiple_of(T::WORD_SIZE)); + assert_eq!(res.n(), self.n() as u32); + assert_eq!(sk.n(), self.n() as u32); + + let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); + let (mut pt, scratch_2) = scratch_1.take_scalar_znx(self.n(), 1); for i in 0..T::WORD_SIZE { - use poulpy_hal::layouts::ZnxViewMut; pt.at_mut(0, 0)[0] = value.bit(i) as i64; - tmp_ggsw.encrypt_sk(&module, &pt, sk, source_xa, source_xe, scratch_2); - self.blocks[i].prepare(module, &tmp_ggsw, scratch_2); + tmp_ggsw.encrypt_sk(self, &pt, sk, source_xa, source_xe, scratch_2); + res.blocks[i].prepare(self, &tmp_ggsw, scratch_2); } } - - /// Prepares [FheUintBits] to [FheUintBitsPrep]. - pub fn prepare(&mut self, module: &Module, bits: &FheUintBlocks, key: &KEY, scratch: &mut Scratch) - where - BIT: DataRef, - KEY: FheUintPrepare, FheUintBlocks>, - { - key.prepare(module, self, bits, scratch); - } } #[cfg(test)] -impl FheUintBlocksPrepDebug { - pub(crate) fn prepare( +impl FheUintBlocksPrepared { + pub(crate) fn encrypt_sk( &mut self, - module: &Module, - bits: &FheUintBlocks, - key: &KEY, + module: &M, + value: T, + sk: &S, + source_xa: &mut Source, + source_xe: &mut Source, scratch: &mut Scratch, ) where - BIT: DataRef, - KEY: FheUintPrepare, FheUintBlocks>, + S: GLWESecretPreparedToRef + GLWEInfos, + M: FheUintBlocksPreparedEncryptSk, + Scratch: ScratchTakeCore, { - key.prepare(module, self, bits, scratch); + module.fhe_uint_blocks_prepared_encrypt_sk(self, value, sk, source_xa, source_xe, scratch); } } -#[cfg(test)] -impl FheUintBlocksPrepDebug { - #[allow(dead_code)] - pub(crate) fn noise(&self, module: &Module, sk: &GLWESecretPrepared, want: T) - where - Module: VecZnxDftBytesOf - + VecZnxBigBytesOf - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalizeTmpBytes - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxSubInplace, - { - for (i, ggsw) in self.blocks.iter().enumerate() { - use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut}; - let mut pt_want = ScalarZnx::alloc(self.n().into(), 1); - pt_want.at_mut(0, 0)[0] = want.bit(i) as i64; - ggsw.print_noise(module, sk, &pt_want); - } - } -} - -impl LWEInfos for FheUintBlocksPrep { +impl LWEInfos for FheUintBlocksPrepared { fn base2k(&self) -> poulpy_core::layouts::Base2K { self.blocks[0].base2k() } @@ -227,46 +159,13 @@ impl LWEInfos for FheUintBlocksPrep< } } -impl GLWEInfos for FheUintBlocksPrep { +impl GLWEInfos for FheUintBlocksPrepared { fn rank(&self) -> poulpy_core::layouts::Rank { self.blocks[0].rank() } } -impl GGSWInfos for FheUintBlocksPrep { - fn dsize(&self) -> poulpy_core::layouts::Dsize { - self.blocks[0].dsize() - } - - fn dnum(&self) -> poulpy_core::layouts::Dnum { - self.blocks[0].dnum() - } -} - -#[cfg(test)] -impl LWEInfos for FheUintBlocksPrepDebug { - fn base2k(&self) -> poulpy_core::layouts::Base2K { - self.blocks[0].base2k() - } - - fn k(&self) -> poulpy_core::layouts::TorusPrecision { - self.blocks[0].k() - } - - fn n(&self) -> poulpy_core::layouts::Degree { - self.blocks[0].n() - } -} - -#[cfg(test)] -impl GLWEInfos for FheUintBlocksPrepDebug { - fn rank(&self) -> poulpy_core::layouts::Rank { - self.blocks[0].rank() - } -} - -#[cfg(test)] -impl GGSWInfos for FheUintBlocksPrepDebug { +impl GGSWInfos for FheUintBlocksPrepared { fn dsize(&self) -> poulpy_core::layouts::Dsize { self.blocks[0].dsize() } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs index 8b51045..b8054d7 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/mod.rs @@ -2,6 +2,11 @@ mod block; mod block_prepared; mod word; +#[cfg(test)] +mod block_debug; +#[cfg(test)] +pub(crate) use block_debug::*; + pub use block::*; pub use block_prepared::*; pub use word::*; diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index 9025b2e..1d3e218 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -1,20 +1,14 @@ use itertools::Itertools; use poulpy_core::{ + GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore, layouts::{ - prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, GLWE - }, ScratchTakeCore, + GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision, + prepared::GLWEAutomorphismKeyPrepared, + }, }; use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, - VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, - VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, - VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, - VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, - VmpApplyDftToDftTmpBytes, - }, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, + api::ModuleN, + layouts::{Backend, Data, DataMut, DataRef, Scratch}, source::Source, }; use std::{collections::HashMap, marker::PhantomData}; @@ -26,40 +20,15 @@ pub struct FheUintWord(pub(crate) GLWE, pub(crat impl FheUintWord { #[allow(dead_code)] - fn post_process( + fn post_process( &mut self, - module: &Module, + module: &M, mut tmp_res: Vec>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, - Module: VecZnxSub - + VecZnxCopy - + VecZnxNegateInplace - + VecZnxDftBytesOf - + VecZnxAddInplace - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxRotateInplace - + VecZnxNormalizeInplace - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxIdftApplyTmpA - + VecZnxSubInplace - + VecZnxBigNormalizeTmpBytes - + VecZnxBigAddSmallInplace - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotate, + M: GLWEPacking + GLWECopy, Scratch: ScratchTakeCore, { // Repacks the GLWE ciphertexts bits @@ -69,10 +38,11 @@ impl FheUintWord { for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); } - glwe_packing(module, &mut cts, log_gap, auto_keys, scratch); + + module.glwe_pack(&mut cts, log_gap, auto_keys, scratch); // And copies the repacked ciphertext on the receiver. - self.0.copy(module, cts.remove(&0).unwrap()) + module.glwe_copy(&mut self.0, cts.remove(&0).unwrap()); } } @@ -97,29 +67,17 @@ impl GLWEInfos for FheUintWord { } impl FheUintWord { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, - module: &Module, + module: &M, data: T, - sk: &GLWESecretPrepared, + sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, + S: GLWESecretPreparedToRef + GLWEInfos, + M: ModuleN + GLWEEncryptSk, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -143,7 +101,7 @@ impl FheUintWord { k: 1_usize.into(), }; - let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); + let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); pt.encode_vec_i64(&data_bits, TorusPrecision(1)); self.0 @@ -152,19 +110,10 @@ impl FheUintWord { } impl FheUintWord { - pub fn decrypt( - &self, - module: &Module, - sk: &GLWESecretPrepared, - scratch: &mut Scratch, - ) -> T + pub fn decrypt(&self, module: &M, sk: &S, scratch: &mut Scratch) -> T where - Module: VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxBigAddInplace - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize, + S: GLWESecretPreparedToRef + GLWEInfos, + M: GLWEDecrypt, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] @@ -182,7 +131,7 @@ impl FheUintWord { k: 1_usize.into(), }; - let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); + let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); self.0.decrypt(module, &mut pt, sk, scratch_1); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 953ee7a..2d5d086 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -1,13 +1,12 @@ use itertools::Itertools; use poulpy_core::{ + GLWEAdd, GLWECopy, GLWEExternalProduct, GLWESub, ScratchTakeCore, layouts::{ - prepared::{GGSWPrepared, GGSWPreparedToRef}, GLWEToMut, LWEInfos, GLWE - }, GLWEExternalProduct, ScratchTakeCore -}; -use poulpy_hal::{ - api::{VecZnxAddInplace, VecZnxCopy, VecZnxNegateInplace, VecZnxSub}, - layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, + GLWE, LWEInfos, + prepared::{GGSWPrepared, GGSWPreparedToRef}, + }, }; +use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}; use crate::tfhe::bdd_arithmetic::UnsignedInteger; @@ -29,45 +28,43 @@ pub(crate) struct BitCircuit { pub struct Circuit(pub [C; N]); -pub trait CircuitExecute -where - Self: GetBitCircuitInfo, -{ - fn execute( +pub trait ExecuteBDDCircuit { + fn execute_bdd_circuit( &self, - module: &Module, out: &mut [GLWE], inputs: &[&dyn GGSWPreparedToRef], + circuit: &C, scratch: &mut Scratch, ) where + C: GetBitCircuitInfo, O: DataMut; } -impl CircuitExecute for Circuit +impl ExecuteBDDCircuit for Module where - Self: GetBitCircuitInfo, - Module: Cmux + VecZnxCopy, + Self: Cmux + GLWECopy, Scratch: ScratchTakeCore, { - fn execute( + fn execute_bdd_circuit( &self, - module: &Module, out: &mut [GLWE], inputs: &[&dyn GGSWPreparedToRef], + circuit: &C, scratch: &mut Scratch, ) where + C: GetBitCircuitInfo, O: DataMut, { #[cfg(debug_assertions)] { - assert_eq!(inputs.len(), self.input_size()); - assert!(out.len() >= self.output_size()); + assert_eq!(inputs.len(), circuit.input_size()); + assert!(out.len() >= circuit.output_size()); } - for (i, out_i) in out.iter_mut().enumerate().take(self.output_size()) { - let (nodes, levels, max_inter_state) = self.get_circuit(i); + for (i, out_i) in out.iter_mut().enumerate().take(circuit.output_size()) { + let (nodes, levels, max_inter_state) = circuit.get_circuit(i); - let (mut level, scratch_1) = scratch.take_glwe_ct_slice(max_inter_state * 2, out_i); + let (mut level, scratch_1) = scratch.take_glwe_slice(max_inter_state * 2, out_i); level.iter_mut().for_each(|ct| ct.data_mut().zero()); @@ -87,9 +84,9 @@ where for (j, node) in nodes_lvl.iter().enumerate() { if node.low_index == node.high_index { - next_level[j].copy(module, prev_level[node.low_index]); + self.glwe_copy(next_level[j], prev_level[node.low_index]); } else { - module.cmux( + self.cmux( next_level[j], prev_level[node.high_index], prev_level[node.low_index], @@ -105,7 +102,7 @@ where // handle last output // there's always only 1 node at last level let node: &Node = nodes.last().unwrap(); - module.cmux( + self.cmux( out_i, prev_level[node.high_index], prev_level[node.low_index], @@ -114,7 +111,7 @@ where ); } - for out_i in out.iter_mut().skip(self.output_size()) { + for out_i in out.iter_mut().skip(circuit.output_size()) { out_i.data_mut().zero(); } } @@ -167,7 +164,8 @@ pub trait Cmux { impl Cmux for Module where - Module: GLWEExternalProduct + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, + Module: GLWEExternalProduct + GLWESub + GLWEAdd, + Scratch: ScratchTakeCore, { fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) where @@ -177,8 +175,8 @@ where S: DataRef, { // let mut out: GLWECiphertext<&mut [u8]> = out.to_mut(); - out.sub(self, t, f); - out.external_product_inplace(self, s, scratch); - out.to_mut().add_inplace(self, f); + self.glwe_sub(out, t, f); + self.glwe_external_product_inplace(out, s, scratch); + self.glwe_add_inplace(out, f); } } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index e2b8453..d1c20c6 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -1,26 +1,21 @@ #[cfg(test)] -use crate::tfhe::bdd_arithmetic::FheUintBlocksPrepDebug; +use crate::tfhe::bdd_arithmetic::FheUintBlocksPreparedDebug; use crate::tfhe::{ - bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrep, UnsignedInteger}, - blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk}, + bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrepared, UnsignedInteger}, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory}, circuit_bootstrapping::{ CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, + CircuitBootstrappingKeyPrepared, CircuitBootstrappingKeyPreparedFactory, CirtuitBootstrappingExecute, }, }; use poulpy_core::{ + GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore, layouts::{ - prepared::GLWEToLWESwitchingKeyPrepared, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWESecret - }, ScratchTakeCore, + GGSWInfos, GGSWPreparedFactory, GLWEInfos, GLWESecretToRef, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, + GLWEToLWESwitchingKeyPreparedFactory, LWE, LWEInfos, LWESecretToRef, prepared::GLWEToLWESwitchingKeyPrepared, + }, }; use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, - VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, - VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPrepare, - }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, source::Source, }; @@ -46,193 +41,256 @@ impl BDDKeyInfos for BDDKeyLayout { } } -pub struct BDDKey +pub struct BDDKey where - CBT: Data, - LWE: Data, + D: Data, BRA: BlindRotationAlgo, { - cbt: CircuitBootstrappingKey, - ks: GLWEToLWESwitchingKey, + cbt: CircuitBootstrappingKey, + ks: GLWEToLWESwitchingKey, } -impl BDDKey, Vec, BRA> { - pub fn encrypt_sk( - module: &Module, - sk_lwe: &LWESecret, - sk_glwe: &GLWESecret, - infos: &A, +impl BDDKey, BRA> +where + BlindRotationKey, BRA>: BlindRotationKeyFactory, +{ + pub fn alloc_from_infos(infos: &A) -> Self + where + A: BDDKeyInfos, + { + Self { + cbt: CircuitBootstrappingKey::alloc_from_infos(&infos.cbt_infos()), + ks: GLWEToLWESwitchingKey::alloc_from_infos(&infos.ks_infos()), + } + } +} + +pub trait BDDKeyEncryptSk { + fn bdd_key_encrypt_sk( + &self, + res: &mut BDDKey, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, - ) -> Self - where - A: BDDKeyInfos, - DLwe: DataRef, - DGlwe: DataRef, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, - Module: SvpApplyDftToDft - + VecZnxIdftApplyTmpA - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc - + VecZnxAutomorphism - + VecZnxAutomorphismInplace, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GetDistribution + GLWEInfos; +} + +impl BDDKeyEncryptSk for Module +where + Self: CircuitBootstrappingKeyEncryptSk + GLWEToLWESwitchingKeyEncryptSk, + Scratch: ScratchTakeCore, +{ + fn bdd_key_encrypt_sk( + &self, + res: &mut BDDKey, + sk_lwe: &S0, + sk_glwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GetDistribution + GLWEInfos, + { + res.ks + .encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + res.cbt + .encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } +} + +impl BDDKey { + pub fn encrypt_sk( + &mut self, + module: &M, + sk_lwe: &S0, + sk_glwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GetDistribution + GLWEInfos, + M: BDDKeyEncryptSk, Scratch: ScratchTakeCore, { - let mut ks: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos()); - ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); - - Self { - cbt: CircuitBootstrappingKey::encrypt_sk( - module, - sk_lwe, - sk_glwe, - &infos.cbt_infos(), - source_xa, - source_xe, - scratch, - ), - ks, - } + module.bdd_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); } } -pub struct BDDKeyPrepared +pub struct BDDKeyPrepared where - CBT: Data, - LWE: Data, + D: Data, BRA: BlindRotationAlgo, BE: Backend, { - cbt: CircuitBootstrappingKeyPrepared, - ks: GLWEToLWESwitchingKeyPrepared, + pub(crate) cbt: CircuitBootstrappingKeyPrepared, + pub(crate) ks: GLWEToLWESwitchingKeyPrepared, } -impl PrepareAlloc> - for BDDKey +pub trait BDDKeyPreparedFactory where - CircuitBootstrappingKey: PrepareAlloc>, - GLWEToLWESwitchingKey: PrepareAlloc>, + Self: Sized + CircuitBootstrappingKeyPreparedFactory + GLWEToLWESwitchingKeyPreparedFactory, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BDDKeyPrepared { + fn alloc_bdd_key_from_infos(&self, infos: &A) -> BDDKeyPrepared, BRA, BE> + where + A: BDDKeyInfos, + { BDDKeyPrepared { - cbt: self.cbt.prepare_alloc(module, scratch), - ks: self.ks.prepare_alloc(module, scratch), + cbt: CircuitBootstrappingKeyPrepared::alloc_from_infos(self, &infos.cbt_infos()), + ks: GLWEToLWESwitchingKeyPrepared::alloc_from_infos(self, &infos.ks_infos()), + } + } + + fn prepare_bdd_key_tmp_bytes(&self, infos: &A) -> usize + where + A: BDDKeyInfos, + { + self.circuit_bootstrapping_key_prepare_tmp_bytes(&infos.cbt_infos()) + .max(self.prepare_glwe_to_lwe_switching_key_tmp_bytes(&infos.ks_infos())) + } + + fn prepare_bdd_key(&self, res: &mut BDDKeyPrepared, other: &BDDKey, scratch: &mut Scratch) + where + DM: DataMut, + DR: DataRef, + Scratch: ScratchTakeCore, + { + res.cbt.prepare(self, &other.cbt, scratch); + res.ks.prepare(self, &other.ks, scratch); + } +} +impl BDDKeyPreparedFactory for Module where + Self: Sized + CircuitBootstrappingKeyPreparedFactory + GLWEToLWESwitchingKeyPreparedFactory +{ +} + +impl BDDKeyPrepared, BRA, BE> { + pub fn alloc_from_infos(module: &M, infos: &A) -> Self + where + M: BDDKeyPreparedFactory, + A: BDDKeyInfos, + { + module.alloc_bdd_key_from_infos(infos) + } +} + +impl BDDKeyPrepared { + pub fn prepare(&mut self, module: &M, other: &BDDKey, scratch: &mut Scratch) + where + DR: DataRef, + M: BDDKeyPreparedFactory, + Scratch: ScratchTakeCore, + { + module.prepare_bdd_key(self, other, scratch); + } +} + +pub trait FheUintBlocksPrepare { + fn fhe_uint_blocks_prepare_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + res_infos: &R, + infos: &A, + ) -> usize + where + R: GGSWInfos, + A: BDDKeyInfos; + fn fhe_uint_blocks_prepare( + &self, + res: &mut FheUintBlocksPrepared, + bits: &FheUintBlocks, + key: &BDDKeyPrepared, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR0: DataRef, + DR1: DataRef; +} + +impl FheUintBlocksPrepare for Module +where + Self: LWEFromGLWE + CirtuitBootstrappingExecute + GGSWPreparedFactory, + Scratch: ScratchTakeCore, +{ + fn fhe_uint_blocks_prepare_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + res_infos: &R, + bdd_infos: &A, + ) -> usize + where + R: GGSWInfos, + A: BDDKeyInfos, + { + self.circuit_bootstrapping_execute_tmp_bytes( + block_size, + extension_factor, + res_infos, + &bdd_infos.cbt_infos(), + ) + } + + fn fhe_uint_blocks_prepare( + &self, + res: &mut FheUintBlocksPrepared, + bits: &FheUintBlocks, + key: &BDDKeyPrepared, + scratch: &mut Scratch, + ) where + DM: DataMut, + DR0: DataRef, + DR1: DataRef, + { + assert_eq!(res.blocks.len(), bits.blocks.len()); + + let mut lwe: LWE> = LWE::alloc_from_infos(&bits.blocks[0]); //TODO: add TakeLWE + let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res); + for (dst, src) in res.blocks.iter_mut().zip(bits.blocks.iter()) { + lwe.from_glwe(self, src, &key.ks, scratch_1); + key.cbt + .execute_to_constant(self, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); + dst.prepare(self, &tmp_ggsw, scratch_1); } } } -pub trait FheUintPrepare { - fn prepare(&self, module: &Module, out: &mut OUT, bits: &IN, scratch: &mut Scratch); -} - -impl FheUintPrepare, FheUintBlocks> - for BDDKeyPrepared -where - T: UnsignedInteger, - CBT: DataRef, - OUT: DataMut, - IN: DataRef, - LWE: DataRef, - BRA: BlindRotationAlgo, - BE: Backend, - Module: VmpPrepare - + VecZnxRotate - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchAvailable + TakeVecZnxDft + TakeGLWE + TakeVecZnx + TakeGGSW, - CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, -{ - fn prepare( - &self, - module: &Module, - out: &mut FheUintBlocksPrep, - bits: &FheUintBlocks, +impl FheUintBlocksPrepared { + pub fn prepare( + &mut self, + module: &M, + other: &FheUintBlocks, + key: &BDDKeyPrepared, scratch: &mut Scratch, - ) { - #[cfg(debug_assertions)] - { - assert_eq!(out.blocks.len(), bits.blocks.len()); - } - let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE - let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out); - for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { - lwe.from_glwe(module, src, &self.ks, scratch_1); - self.cbt - .execute_to_constant(module, &mut tmp_ggsw, &lwe, 1, 1, scratch_1); - dst.prepare(module, &tmp_ggsw, scratch_1); - } + ) where + BRA: BlindRotationAlgo, + O: DataRef, + K: DataRef, + M: FheUintBlocksPrepare, + Scratch: ScratchTakeCore, + { + module.fhe_uint_blocks_prepare(self, other, key, scratch); } } #[cfg(test)] -impl FheUintPrepare, FheUintBlocks> - for BDDKeyPrepared -where - T: UnsignedInteger, - CBT: DataRef, - OUT: DataMut, - IN: DataRef, - LWE: DataRef, - BRA: BlindRotationAlgo, - BE: Backend, - Module: VmpPrepare - + VecZnxRotate - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxDftApply - + VecZnxIdftApplyConsume - + VecZnxBigAddSmallInplace - + VecZnxBigNormalize - + VecZnxNormalize - + VecZnxNormalizeTmpBytes, - Scratch: ScratchTakeCore, - CircuitBootstrappingKeyPrepared: CirtuitBootstrappingExecute, -{ - fn prepare( +pub(crate) trait FheUintBlockDebugPrepare { + fn fhe_uint_block_debug_prepare( &self, - module: &Module, - out: &mut FheUintBlocksPrepDebug, - bits: &FheUintBlocks, + res: &mut FheUintBlocksPreparedDebug, + bits: &FheUintBlocks, + key: &BDDKeyPrepared, scratch: &mut Scratch, - ) { - #[cfg(debug_assertions)] - { - assert_eq!(out.blocks.len(), bits.blocks.len()); - } - let mut lwe: LWE> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE - for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { - lwe.from_glwe(module, src, &self.ks, scratch); - self.cbt - .execute_to_constant(module, dst, &lwe, 1, 1, scratch); - } - } + ) where + DM: DataMut, + DR0: DataRef, + DR1: DataRef; } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 90175fb..f807414 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,7 +1,7 @@ #[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, TensorKeyLayout, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSWLayout, GLWEAutomorphismKeyLayout, GLWELayout, GLWETensorKeyLayout, GLWEToLWEKeyLayout, + Rank, TorusPrecision, }; #[cfg(test)] @@ -53,7 +53,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), rank: Rank(TEST_RANK), }, - layout_atk: AutomorphismKeyLayout { + layout_atk: GLWEAutomorphismKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), @@ -61,7 +61,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), dsize: Dsize(1), }, - layout_tsk: TensorKeyLayout { + layout_tsk: GLWETensorKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 1c2e69d..19155ea 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs @@ -2,43 +2,24 @@ use std::time::Instant; use poulpy_backend::FFT64Ref; use poulpy_core::{ - TakeGGSW, TakeGLWEPlaintext, - layouts::{ - GGSWLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, - prepared::{GLWESecretPrepared, PrepareAlloc}, - }, + GGSWNoise, GLWEDecrypt, GLWENoise, ScratchTakeCore, + layouts::{GGSWLayout, GLWELayout, GLWESecret, GLWESecretPreparedFactory, LWEInfos, LWESecret, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ - api::{ - ModuleNew, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, - SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, - VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, - VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAutomorphismInplace, VecZnxBigBytesOf, - VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace, - VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, - VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, - VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, - VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, - ZnNormalizeInplace, - }, + api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow}, layouts::{Backend, Module, Scratch, ScratchOwned}, - oep::{ - ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, - TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl, - }, source::Source, }; use rand::RngCore; use crate::tfhe::{ bdd_arithmetic::{ - Add, BDDKey, BDDKeyLayout, BDDKeyPrepared, FheUintBlocks, FheUintBlocksPrep, FheUintBlocksPrepDebug, Sub, - TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE, - }, - blind_rotation::{ - BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk, - BlindRotationKeyPrepared, CGGI, + Add, BDDKey, BDDKeyEncryptSk, BDDKeyLayout, BDDKeyPrepared, BDDKeyPreparedFactory, ExecuteBDDCircuit2WTo1W, + FheUintBlockDebugPrepare, FheUintBlocks, FheUintBlocksPrepare, FheUintBlocksPrepared, FheUintBlocksPreparedDebug, + FheUintBlocksPreparedEncryptSk, FheUintBlocksPreparedFactory, Sub, TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, + TEST_GLWE_INFOS, TEST_N_LWE, }, + blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyFactory, CGGI}, }; #[test] @@ -48,64 +29,21 @@ fn test_bdd_2w_to_1w_fft64_ref() { fn test_bdd_2w_to_1w() where - Module: ModuleNew + SvpPPolAlloc + SvpPrepare + VmpPMatAlloc, + Module: ModuleNew + + GLWESecretPreparedFactory + + GLWEDecrypt + + GLWENoise + + FheUintBlocksPreparedFactory + + FheUintBlocksPreparedEncryptSk + + FheUintBlockDebugPrepare + + BDDKeyEncryptSk + + BDDKeyPreparedFactory + + GGSWNoise + + FheUintBlocksPrepare + + ExecuteBDDCircuit2WTo1W, + BlindRotationKey, BRA>: BlindRotationKeyFactory, ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, - Module: VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + VmpPrepare, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx + TakeSlice, - Module: VecZnxCopy + VecZnxNegateInplace + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft + VmpApplyDftToDftAdd, - Module: VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, - Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, - Module: VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA - + SvpApplyDftToDft - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + SvpPPolBytesOf - + VecZnxRotateInplace - + VecZnxBigAutomorphismInplace - + VecZnxRshInplace - + VecZnxDftCopy - + VecZnxAutomorphismInplace - + VecZnxBigSubSmallNegateInplace - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace, - BE: Backend - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + TakeVecZnxDftImpl - + ScratchAvailableImpl - + TakeVecZnxImpl - + TakeScalarZnxImpl - + TakeSvpPPolImpl - + TakeVecZnxBigImpl - + TakeVecZnxDftSliceImpl - + TakeMatZnxImpl - + TakeVecZnxSliceImpl, - BlindRotationKey, BRA>: PrepareAlloc, BRA, BE>>, - BlindRotationKeyPrepared, BRA, BE>: BlincRotationExecute, - BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, + Scratch: ScratchTakeCore, { let glwe_infos: GLWELayout = TEST_GLWE_INFOS; let ggsw_infos: GGSWLayout = TEST_GGSW_INFOS; @@ -122,7 +60,8 @@ where let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prep: GLWESecretPrepared, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow()); + let mut sk_glwe_prep: GLWESecretPrepared, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos); + sk_glwe_prep.prepare(&module, &sk_glwe); let a: u32 = source.next_u32(); let b: u32 = source.next_u32(); @@ -130,12 +69,15 @@ where println!("a: {a}"); println!("b: {b}"); - let mut a_enc_prep: FheUintBlocksPrep, BE, u32> = FheUintBlocksPrep::, BE, u32>::alloc(&module, &ggsw_infos); - let mut b_enc_prep: FheUintBlocksPrep, BE, u32> = FheUintBlocksPrep::, BE, u32>::alloc(&module, &ggsw_infos); + let mut a_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); + let mut b_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); let mut c_enc: FheUintBlocks, u32> = FheUintBlocks::, u32>::alloc(&module, &glwe_infos); - let mut c_enc_prep_debug: FheUintBlocksPrepDebug, u32> = - FheUintBlocksPrepDebug::, u32>::alloc(&module, &ggsw_infos); - let mut c_enc_prep: FheUintBlocksPrep, BE, u32> = FheUintBlocksPrep::, BE, u32>::alloc(&module, &ggsw_infos); + let mut c_enc_prep_debug: FheUintBlocksPreparedDebug, u32> = + FheUintBlocksPreparedDebug::, u32>::alloc(&module, &ggsw_infos); + let mut c_enc_prep: FheUintBlocksPrepared, u32, BE> = + FheUintBlocksPrepared::, u32, BE>::alloc(&module, &ggsw_infos); a_enc_prep.encrypt_sk( &module, @@ -178,17 +120,19 @@ where let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT; + let mut bdd_key: BDDKey, BRA> = BDDKey::alloc_from_infos(&bdd_key_infos); + let now: Instant = Instant::now(); - let bdd_key: BDDKey, Vec, BRA> = BDDKey::encrypt_sk( + bdd_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - &bdd_key_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); - let bdd_key_prepared: BDDKeyPrepared, Vec, BRA, BE> = bdd_key.prepare_alloc(&module, scratch.borrow()); + let mut bdd_key_prepared: BDDKeyPrepared, BRA, BE> = BDDKeyPrepared::alloc_from_infos(&module, &bdd_key_infos); + bdd_key_prepared.prepare(&module, &bdd_key, scratch.borrow()); println!("BDD-KGEN: {} ms", now.elapsed().as_millis()); let now: Instant = Instant::now(); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs index fe3e795..bc1ab98 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs @@ -33,7 +33,14 @@ where } } - fn blind_rotation_key_prepare( + fn blind_rotation_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: BlindRotationKeyInfos, + { + self.ggsw_prepare_tmp_bytes(infos) + } + + fn prepare_blind_rotation_key( &self, res: &mut BlindRotationKeyPrepared, other: &BlindRotationKey, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs index c18e71c..cf533c1 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs @@ -14,7 +14,11 @@ pub trait BlindRotationKeyPreparedFactory { where A: BlindRotationKeyInfos; - fn blind_rotation_key_prepare( + fn blind_rotation_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: BlindRotationKeyInfos; + + fn prepare_blind_rotation_key( &self, res: &mut BlindRotationKeyPrepared, other: &BlindRotationKey, @@ -32,6 +36,14 @@ impl BlindRotationKeyPrepared, BRA, { module.blind_rotation_key_prepared_alloc(infos) } + + pub fn prepare_tmp_bytes(module: &M, infos: &A) -> usize + where + A: BlindRotationKeyInfos, + M: BlindRotationKeyPreparedFactory, + { + module.blind_rotation_key_prepare_tmp_bytes(infos) + } } impl BlindRotationKeyPrepared { @@ -39,7 +51,7 @@ impl BlindRotationKeyPrepared, { - module.blind_rotation_key_prepare(self, other, scratch); + module.prepare_blind_rotation_key(self, other, scratch); } } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 5b3ea4d..a627e0c 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -18,10 +18,21 @@ use crate::tfhe::{ blind_rotation::{ BlindRotationAlgo, BlindRotationExecute, LookUpTableLayout, LookUpTableRotationDirection, LookupTable, LookupTableFactory, }, - circuit_bootstrapping::CircuitBootstrappingKeyPrepared, + circuit_bootstrapping::{CircuitBootstrappingKeyInfos, CircuitBootstrappingKeyPrepared}, }; pub trait CirtuitBootstrappingExecute { + fn circuit_bootstrapping_execute_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + res_infos: &R, + cbt_infos: &A, + ) -> usize + where + R: GGSWInfos, + A: CircuitBootstrappingKeyInfos; + fn circuit_bootstrapping_execute_to_constant( &self, res: &mut R, @@ -68,6 +79,7 @@ impl CircuitBootstrappingKeyPre module.circuit_bootstrapping_execute_to_constant(res, lwe, self, log_domain, extension_factor, scratch); } + #[allow(clippy::too_many_arguments)] pub fn execute_to_exponent( &self, module: &M, @@ -107,6 +119,27 @@ where ScratchOwned: ScratchOwnedAlloc + ScratchOwnedBorrow, Scratch: ScratchTakeCore, { + fn circuit_bootstrapping_execute_tmp_bytes( + &self, + block_size: usize, + extension_factor: usize, + res_infos: &R, + cbt_infos: &A, + ) -> usize + where + R: GGSWInfos, + A: CircuitBootstrappingKeyInfos, + { + self.blind_rotation_execute_tmp_bytes( + block_size, + extension_factor, + res_infos, + &cbt_infos.brk_infos(), + ) + .max(self.glwe_trace_tmp_bytes(res_infos, res_infos, &cbt_infos.atk_infos())) + .max(self.ggsw_from_gglwe_tmp_bytes(res_infos, &cbt_infos.tsk_infos())) + } + fn circuit_bootstrapping_execute_to_constant( &self, res: &mut R, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs index db39a45..6adca70 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs @@ -75,6 +75,16 @@ where .collect(), } } + + fn circuit_bootstrapping_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: CircuitBootstrappingKeyInfos, + { + self.blind_rotation_key_prepare_tmp_bytes(&infos.brk_infos()) + .max(self.prepare_tensor_key_tmp_bytes(&infos.tsk_infos())) + .max(self.prepare_glwe_automorphism_key_tmp_bytes(&infos.atk_infos())) + } + fn circuit_bootstrapping_key_prepare( &self, res: &mut CircuitBootstrappingKeyPrepared, diff --git a/poulpy-schemes/src/tfhe/mod.rs b/poulpy-schemes/src/tfhe/mod.rs index b91a937..85c84d4 100644 --- a/poulpy-schemes/src/tfhe/mod.rs +++ b/poulpy-schemes/src/tfhe/mod.rs @@ -1,3 +1,3 @@ -// pub mod bdd_arithmetic; +pub mod bdd_arithmetic; pub mod blind_rotation; pub mod circuit_bootstrapping;