arnaucube
arnaucube/poulpyPublic Self-hosted repository
wip
Pro7ech jeanphilippe.bossuat@gmail.com
Wed, 15 Oct 2025 17:15:51 +0200
commit

10817a85297a26a42f2a8d1e651dd4682e740906

parent

15ee02e21ddaaad90b0ec488f5ad2ac3bc41c264

M poulpy-core/benches/external_product_glwe_fft64.rs → 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 @@ let dsize: Dsize = Dsize(1);

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 @@ let dsize: Dsize = Dsize(1);

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,
M poulpy-core/src/keyswitching/gglwe_ct.rs → 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, +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::{ - AutomorphismKey, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, - prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, -}; +impl AutomorphismKey<Vec<u8>> { + pub fn keyswitch_inplace_tmp_bytes<R, A, K, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch<BE>, + { + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } +} -impl AutomorphismKey<Vec<u8>> { - pub fn keyswitch_tmp_bytes<B: Backend, OUT, IN, KEY>( - module: &Module<B>, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize +impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { + pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) + where + A: AutomorphismKeyToRef, + B: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, + { + module.gglwe_keyswitch(&mut self.key.key, &a.to_ref().key.key, b, scratch); + } + + pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module<B>: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + A: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, { - GLWESwitchingKey::keyswitch_tmp_bytes(module, out_infos, in_infos, key_infos) + module.gglwe_keyswitch_inplace(&mut self.key.key, a, scratch); } +} - pub fn keyswitch_inplace_tmp_bytes<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_infos: &KEY) -> usize +impl GLWESwitchingKey<Vec<u8>> { + pub fn keyswitch_inplace_tmp_bytes<R, A, K, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: GGLWEInfos, - KEY: GGLWEInfos, - Module<B>: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch<BE>, { - GLWESwitchingKey::keyswitch_inplace_tmp_bytes(module, out_infos, key_infos) + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } -impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { - pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( - &mut self, - module: &Module<B>, - lhs: &AutomorphismKey<DataLhs>, - rhs: &GLWESwitchingKeyPrepared<DataRhs, B>, - scratch: &mut Scratch<B>, - ) where - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalize<B> - + VecZnxNormalizeTmpBytes, - Scratch<B>: ScratchAvailable, +impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> { + pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) + where + A: GLWESwitchingKeyToRef, + B: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, + { + module.gglwe_keyswitch(&mut self.key, &a.to_ref().key, b, scratch); + } + + pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) + where + A: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, { - self.key.keyswitch(module, &lhs.key, rhs, scratch); + module.gglwe_keyswitch_inplace(&mut self.key, a, scratch); } +} - pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( - &mut self, - module: &Module<B>, - rhs: &AutomorphismKeyPrepared<DataRhs, B>, - scratch: &mut Scratch<B>, - ) where - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalize<B> - + VecZnxNormalizeTmpBytes, - Scratch<B>: ScratchAvailable, +impl GGLWE<Vec<u8>> { + pub fn keyswitch_inplace_tmp_bytes<R, A, K, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, + M: GGLWEKeySwitch<BE>, { - self.key.keyswitch_inplace(module, &rhs.key, scratch); + module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } } -impl GLWESwitchingKey<Vec<u8>> { - pub fn keyswitch_tmp_bytes<B: Backend, OUT, IN, KEY>( - module: &Module<B>, - out_infos: &OUT, - in_infos: &IN, - key_apply: &KEY, - ) -> usize +impl<DataSelf: DataMut> GGLWE<DataSelf> { + pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) where - OUT: GGLWEInfos, - IN: GGLWEInfos, - KEY: GGLWEInfos, - Module<B>: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, { - GLWE::keyswitch_tmp_bytes(module, out_infos, in_infos, key_apply) + module.gglwe_keyswitch(self, a, b, scratch); } - pub fn keyswitch_inplace_tmp_bytes<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_apply: &KEY) -> usize + pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) where - OUT: GGLWEInfos + GLWEInfos, - KEY: GGLWEInfos + GLWEInfos, - Module<B>: VecZnxDftBytesOf + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, + A: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + M: GGLWEKeySwitch<BE>, { - GLWE::keyswitch_inplace_tmp_bytes(module, out_infos, key_apply) + module.gglwe_keyswitch_inplace(self, a, scratch); } } -impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> { - pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( - &mut self, - module: &Module<B>, - lhs: &GLWESwitchingKey<DataLhs>, - rhs: &GLWESwitchingKeyPrepared<DataRhs, B>, - scratch: &mut Scratch<B>, - ) where - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalize<B> - + VecZnxNormalizeTmpBytes, - Scratch<B>: ScratchAvailable, +impl<BE: Backend> GGLWEKeySwitch<BE> for Module<BE> where Self: GLWEKeySwitch<BE> {} + +pub trait GGLWEKeySwitch<BE: Backend> +where + Self: GLWEKeySwitch<BE>, +{ + fn gglwe_keyswitch_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + where + R: GGLWEInfos, + A: GGLWEInfos, + K: GGLWEInfos, { - #[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() - ) - } + self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) + } - (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); - }); - }); + fn gglwe_keyswitch<R, A, B>(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch<BE>) + where + R: GGLWEToMut, + A: GGLWEToRef, + B: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, + { + 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(); - (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(); - }); - }); + 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); + } + } } - pub fn keyswitch_inplace<DataRhs: DataRef, B: Backend>( - &mut self, - module: &Module<B>, - rhs: &GLWESwitchingKeyPrepared<DataRhs, B>, - scratch: &mut Scratch<B>, - ) where - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalize<B> - + VecZnxNormalizeTmpBytes, - Scratch<B>: ScratchAvailable, + fn gglwe_keyswitch_inplace<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<BE>) + where + R: GGLWEToMut, + A: GLWESwitchingKeyPreparedToRef<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { - #[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<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {}
M poulpy-core/src/keyswitching/ggsw_ct.rs → 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 @@ }

pub trait GGSWKeySwitch<BE: Backend> where - Self: GLWEKeySwitching<BE> + GGSWExpandRows<BE>, + Self: GLWEKeySwitch<BE> + GGSWExpandRows<BE>, { fn ggsw_keyswitch_tmp_bytes<R, A, K, T>(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize where
M poulpy-core/src/keyswitching/glwe_ct.rs → poulpy-core/src/keyswitching/glwe_ct.rs

@@ -21,7 +21,7 @@ where

R: GLWEInfos, A: GLWEInfos, B: GGLWEInfos, - M: GLWEKeySwitching<BE>, + M: GLWEKeySwitch<BE>, { module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, b_infos) }

@@ -32,7 +32,7 @@ pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)

where A: GLWEToRef, B: GLWESwitchingKeyPreparedToRef<BE>, - M: GLWEKeySwitching<BE>, + M: GLWEKeySwitch<BE>, Scratch<BE>: ScratchTakeCore<BE>, { module.glwe_keyswitch(self, a, b, scratch);

@@ -41,14 +41,14 @@

pub fn keyswitch_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>) where A: GLWESwitchingKeyPreparedToRef<BE>, - M: GLWEKeySwitching<BE>, + M: GLWEKeySwitch<BE>, Scratch<BE>: ScratchTakeCore<BE>, { module.glwe_keyswitch_inplace(self, a, scratch); } } -impl<BE: Backend> GLWEKeySwitching<BE> for Module<BE> where +impl<BE: Backend> GLWEKeySwitch<BE> for Module<BE> where Self: Sized + ModuleN + VecZnxDftBytesOf

@@ -69,7 +69,7 @@ + VecZnxNormalizeTmpBytes

{ } -pub trait GLWEKeySwitching<BE: Backend> +pub trait GLWEKeySwitch<BE: Backend> where Self: Sized + ModuleN
M poulpy-core/src/keyswitching/lwe_ct.rs → poulpy-core/src/keyswitching/lwe_ct.rs

@@ -7,85 +7,58 @@ },

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<Vec<u8>> { - pub fn keyswitch_tmp_bytes<B: Backend, OUT, IN, KEY>( - module: &Module<B>, - out_infos: &OUT, - in_infos: &IN, - key_infos: &KEY, - ) -> usize +pub trait LWEKeySwitch<BE: Backend> +where + Self: GLWEKeySwitch<BE> + GLWEAlloc, +{ + fn keyswitch_tmp_bytes<B: Backend, R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where - OUT: LWEInfos, - IN: LWEInfos, - KEY: GGLWEInfos, - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + 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<DLwe: DataMut> LWE<DLwe> { - pub fn keyswitch<A, DKs, B: Backend>( + fn keyswitch<A, DKs, B: Backend>( &mut self, module: &Module<B>, - a: &LWE<A>, - ksk: &LWESwitchingKeyPrepared<DKs, B>, + a: &A, + ksk: &K, scratch: &mut Scratch<B>, ) where - A: DataRef, + A: LWEToRef, DKs: DataRef, - Module<B>: VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxNormalize<B> - + VecZnxNormalizeTmpBytes - + VecZnxCopy, Scratch<B>: 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 @@ glwe_out.keyswitch(module, &glwe_in, &ksk.0, scratch_1);

self.sample_extract(&glwe_out); } } + +impl LWE<Vec<u8>> {} + +impl<DLwe: DataMut> LWE<DLwe> { + +}
M poulpy-core/src/layouts/gglwe_ct.rs → poulpy-core/src/layouts/gglwe_ct.rs

@@ -18,8 +18,8 @@ fn dnum(&self) -> Dnum;

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 @@ fn set_dsize(&mut self, dsize: usize);

} #[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 dnum: Dnum,

pub dsize: Dsize, } -impl LWEInfos for GGLWECiphertextLayout { +impl LWEInfos for GGLWELayout { fn base2k(&self) -> Base2K { self.base2k }

@@ -60,13 +60,13 @@ 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 }
M poulpy-core/src/layouts/ggsw_ct.rs → poulpy-core/src/layouts/ggsw_ct.rs

@@ -14,8 +14,8 @@ Self: GLWEInfos,

{ 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 @@ }

} #[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 dnum: Dnum,

pub dsize: Dsize, } -impl LWEInfos for GGSWCiphertextLayout { +impl LWEInfos for GGSWLayout { fn base2k(&self) -> Base2K { self.base2k }

@@ -49,13 +49,13 @@ fn n(&self) -> Degree {

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<D: DataRef> fmt::Display for GGSW<D> {

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,
M poulpy-core/src/layouts/glwe_to_lwe_ksk.rs → poulpy-core/src/layouts/glwe_to_lwe_ksk.rs

@@ -57,7 +57,7 @@ self.dnum

} } -/// 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<D: Data>(pub(crate) GLWESwitchingKey<D>);
M poulpy-core/src/layouts/lwe_ct.rs → poulpy-core/src/layouts/lwe_ct.rs

@@ -18,8 +18,8 @@ fn base2k(&self) -> Base2K;

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 @@ fn set_base2k(&mut self, base2k: Base2K);

} #[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<D: DataRef> fmt::Display for LWE<D> {

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 @@ module.bytes_of_lwe(n, base2k, k)

} } -pub trait LWECiphertextToRef { +pub trait LWEToRef { fn to_ref(&self) -> LWE<&[u8]>; } -impl<D: DataRef> LWECiphertextToRef for LWE<D> { +impl<D: DataRef> LWEToRef for LWE<D> { fn to_ref(&self) -> LWE<&[u8]> { LWE { k: self.k,
M poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs → poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs

@@ -8,7 +8,7 @@ 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<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
M poulpy-core/src/scratch.rs → poulpy-core/src/scratch.rs

@@ -326,7 +326,7 @@ 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::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 @@ 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::GGLWECiphertextLayout = infos.gglwe_layout(); + let mut ksk_infos: crate::layouts::GGLWELayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 {
M poulpy-schemes/benches/circuit_bootstrapping.rs → poulpy-schemes/benches/circuit_bootstrapping.rs

@@ -3,7 +3,7 @@

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 @@ name: String,

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 @@ for params in [Params {

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(),
M poulpy-schemes/examples/circuit_bootstrapping.rs → 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 @@ rank: rank.into(),

}, }; - 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 @@ dsize: 1_u32.into(),

rank: rank.into(), }; - let lwe_infos = LWECiphertextLayout { + let lwe_infos = LWELayout { n: n_lwe.into(), k: k_lwe_ct.into(), base2k: base2k.into(),
M poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs → 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 @@ rank: Rank(TEST_RANK),

}; #[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),
M poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs → 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 @@ BlindRotationKeyPrepared<Vec<u8>, BRA, BE>: BlincRotationExecute<BE>,

BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>, { 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();
M poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs → poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs

@@ -13,7 +13,7 @@ };

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::{
M poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs → poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs

@@ -23,7 +23,7 @@ BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_tmp_bytes, mod_switch_2n,

}; 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 @@ 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(),
M poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs

@@ -16,7 +16,7 @@ };

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 @@

// 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(),
M poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs

@@ -32,7 +32,7 @@ },

}; 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 @@

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 @@ rank: rank.into(),

}, }; - 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 @@

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 @@ rank: rank.into(),

}, }; - let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { + let ggsw_infos: GGSWLayout = GGSWLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_ggsw_res.into(),