From 10817a85297a26a42f2a8d1e651dd4682e740906 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Wed, 15 Oct 2025 17:15:51 +0200 Subject: [PATCH] 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(),