arnaucube
arnaucube/poulpyPublic Self-hosted repository
compiling CBT but failing tests
Pro7ech jeanphilippe.bossuat@gmail.com
Wed, 22 Oct 2025 10:00:32 +0200
commit

706ecf3d07a7dac91eb30fe79aeae4398f8ca1ac

parent

0926913001b5fae6638cadf2d470b8f68f36b542

50 files changed, 963 insertions(+), 1056 deletions(-)

jump to
M poulpy-core/benches/keyswitch_glwe_fft64.rs → 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 @@ let dsize: Dsize = p.dsize;

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 @@ k: k_glwe_out,

rank, }; - let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ksk: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&gglwe_atk_layout); let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout); let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout);
M poulpy-core/src/automorphism/gglwe_atk.rs → 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<Vec<u8>> { +impl GLWEAutomorphismKey<Vec<u8>> { pub fn automorphism_tmp_bytes<R, A, K, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos,

@@ -19,11 +20,11 @@ A: GGLWEInfos,

K: GGLWEInfos, M: GLWEAutomorphismKeyAutomorphism<BE>, { - 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<DataSelf: DataMut> AutomorphismKey<DataSelf> { +impl<DataSelf: DataMut> GLWEAutomorphismKey<DataSelf> { pub fn automorphism<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) where A: GGLWEToRef + GetGaloisElement + GGLWEInfos,

@@ -31,7 +32,7 @@ K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,

Scratch<BE>: ScratchTakeCore<BE>, M: GLWEAutomorphismKeyAutomorphism<BE>, { - module.automorphism_key_automorphism(self, a, key, scratch); + module.glwe_automorphism_key_automorphism(self, a, key, scratch); } pub fn automorphism_inplace<K, M, BE: Backend>(&mut self, module: &M, key: &K, scratch: &mut Scratch<BE>)

@@ -40,7 +41,7 @@ K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,

Scratch<BE>: ScratchTakeCore<BE>, M: GLWEAutomorphismKeyAutomorphism<BE>, { - module.automorphism_key_automorphism_inplace(self, key, scratch); + module.glwe_automorphism_key_automorphism_inplace(self, key, scratch); } }

@@ -53,7 +54,7 @@ pub trait GLWEAutomorphismKeyAutomorphism<BE: Backend>

where Self: GaloisElement + GLWEAutomorphism<BE> + VecZnxAutomorphism, { - fn automorphism_key_automorphism_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize + fn glwe_automorphism_key_automorphism_tmp_bytes<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos, A: GGLWEInfos,

@@ -62,7 +63,7 @@ {

self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) } - fn automorphism_key_automorphism<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>) + fn glwe_automorphism_key_automorphism<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>) where R: GGLWEToMut + SetGaloisElement + GGLWEInfos, A: GGLWEToRef + GetGaloisElement + GGLWEInfos,

@@ -118,7 +119,7 @@

res.set_p((p * key.p()) % (self.cyclotomic_order() as i64)); } - fn automorphism_key_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) + fn glwe_automorphism_key_automorphism_inplace<R, K>(&self, res: &mut R, key: &K, scratch: &mut Scratch<BE>) where R: GGLWEToMut + SetGaloisElement + GetGaloisElement + GGLWEInfos, K: GGLWEPreparedToRef<BE> + GetGaloisElement + GGLWEInfos,
M poulpy-core/src/automorphism/ggsw_ct.rs → poulpy-core/src/automorphism/ggsw_ct.rs

@@ -8,7 +8,7 @@ GGSWExpandRows, ScratchTakeCore,

automorphism::glwe_ct::GLWEAutomorphism, layouts::{ GGLWEInfos, GGLWEPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GetGaloisElement, - prepared::{TensorKeyPrepared, TensorKeyPreparedToRef}, + prepared::{GLWETensorKeyPrepared, GLWETensorKeyPreparedToRef}, }, };

@@ -36,7 +36,7 @@ pub fn automorphism<A, K, T, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)

where A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, M: GGSWAutomorphism<BE>, {

@@ -46,7 +46,7 @@

pub fn automorphism_inplace<K, T, M, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>) where K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, M: GGSWAutomorphism<BE>, {

@@ -79,12 +79,12 @@ where

R: GGSWToMut, A: GGSWToRef, K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { 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 @@ fn ggsw_automorphism_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>)

where R: GGSWToMut, K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { 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() {
M poulpy-core/src/automorphism/glwe_ct.rs → poulpy-core/src/automorphism/glwe_ct.rs

@@ -54,7 +54,7 @@ {

module.glwe_automorphism_sub(self, a, key, scratch); } - pub fn glwe_automorphism_sub_negate<M, A, K, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) + pub fn automorphism_sub_negate<M, A, K, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>) where M: GLWEAutomorphism<BE>, A: GLWEToRef,
M poulpy-core/src/conversion/gglwe_to_ggsw.rs → 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 @@ pub fn from_gglwe<G, M, T, BE: Backend>(&mut self, module: &M, gglwe: &G, tsk: &T, scratch: &mut Scratch<BE>)

where M: GGSWFromGGLWE<BE>, G: GGLWEToRef, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { module.ggsw_from_gglwe(self, gglwe, tsk, scratch);

@@ -54,12 +54,12 @@ fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)

where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { 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 @@ fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)

where R: GGSWToMut, A: GGLWEToRef, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>; }

@@ -158,11 +158,11 @@

fn ggsw_expand_row<R, T>(&self, res: &mut R, tsk: &T, scratch: &mut Scratch<BE>) where R: GGSWToMut, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { 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();
M poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs → poulpy-core/src/encryption/compressed/glwe_automorphism_key.rs

@@ -16,9 +16,9 @@ impl GLWEAutomorphismKeyCompressed<Vec<u8>> {

pub fn encrypt_sk_tmp_bytes<M, BE: Backend, A>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyCompressedEncryptSk<BE>, + M: GLWEAutomorphismKeyCompressedEncryptSk<BE>, { - module.automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes(infos) } }

@@ -34,18 +34,18 @@ source_xe: &mut Source,

scratch: &mut Scratch<BE>, ) where S: GLWESecretToRef + GLWEInfos, - M: AutomorphismKeyCompressedEncryptSk<BE>, + M: GLWEAutomorphismKeyCompressedEncryptSk<BE>, { - 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<BE: Backend> { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyCompressedEncryptSk<BE: Backend> { + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_compressed_encrypt_sk<R, S>( + fn glwe_automorphism_key_compressed_encrypt_sk<R, S>( &self, res: &mut R, p: i64,

@@ -58,12 +58,12 @@ R: GGLWECompressedToMut + GGLWECompressedSeedMut + SetGaloisElement + GGLWEInfos,

S: GLWESecretToRef + GLWEInfos; } -impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE> +impl<BE: Backend> GLWEAutomorphismKeyCompressedEncryptSk<BE> for Module<BE> where Self: ModuleN + GaloisElement + VecZnxAutomorphism + GGLWECompressedEncryptSk<BE> + GLWESecretPreparedFactory<BE>, Scratch<BE>: ScratchTakeCore<BE>, { - fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + fn glwe_automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -73,7 +73,7 @@ .max(GLWESecret::bytes_of_from_infos(infos))

+ GLWESecretPrepared::bytes_of_from_infos(self, infos) } - fn automorphism_key_compressed_encrypt_sk<R, S>( + fn glwe_automorphism_key_compressed_encrypt_sk<R, S>( &self, res: &mut R, p: i64,
M poulpy-core/src/encryption/compressed/glwe_tensor_key.rs → poulpy-core/src/encryption/compressed/glwe_tensor_key.rs

@@ -8,10 +8,10 @@ source::Source,

}; 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<Vec<u8>> {

pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWETensorKeyCompressedEncryptSk<BE>, + M: GLWETensorKeyCompressedEncryptSk<BE>, { - module.tensor_key_compressed_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_compressed_encrypt_sk_tmp_bytes(infos) } }

@@ -35,18 +35,18 @@ source_xe: &mut Source,

scratch: &mut Scratch<BE>, ) where S: GLWESecretToRef + GetDistribution, - M: GGLWETensorKeyCompressedEncryptSk<BE>, + M: GLWETensorKeyCompressedEncryptSk<BE>, { - 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<BE: Backend> { - fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize +pub trait GLWETensorKeyCompressedEncryptSk<BE: Backend> { + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_compressed_encrypt_sk<R, S, D>( + fn glwe_tensor_key_compressed_encrypt_sk<R, S, D>( &self, res: &mut R, sk: &S,

@@ -55,15 +55,15 @@ source_xe: &mut Source,

scratch: &mut Scratch<BE>, ) where D: DataMut, - R: TensorKeyCompressedAtMut<D> + GGLWEInfos, + R: GLWETensorKeyCompressedAtMut<D> + GGLWEInfos, S: GLWESecretToRef + GetDistribution; } -impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE> +impl<BE: Backend> GLWETensorKeyCompressedEncryptSk<BE> for Module<BE> where Self: ModuleN + GGLWECompressedEncryptSk<BE> - + TensorKeyEncryptSk<BE> + + GLWETensorKeyEncryptSk<BE> + VecZnxDftApply<BE> + SvpApplyDftToDft<BE> + VecZnxIdftApplyTmpA<BE>

@@ -75,7 +75,7 @@ + VecZnxBigBytesOf

+ GLWESecretPreparedFactory<BE>, Scratch<BE>: ScratchTakeBasic + ScratchTakeCore<BE>, { - fn tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + fn glwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -87,7 +87,7 @@ + GLWESecret::bytes_of(self.n().into(), Rank(1))

+ self.gglwe_compressed_encrypt_sk_tmp_bytes(infos) } - fn tensor_key_compressed_encrypt_sk<R, S, D>( + fn glwe_tensor_key_compressed_encrypt_sk<R, S, D>( &self, res: &mut R, sk: &S,

@@ -96,7 +96,7 @@ source_xe: &mut Source,

scratch: &mut Scratch<BE>, ) where D: DataMut, - R: GGLWEInfos + TensorKeyCompressedAtMut<D>, + R: GGLWEInfos + GLWETensorKeyCompressedAtMut<D>, S: GLWESecretToRef + GetDistribution, { let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank());
M poulpy-core/src/encryption/glwe_automorphism_key.rs → poulpy-core/src/encryption/glwe_automorphism_key.rs

@@ -7,30 +7,30 @@

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<Vec<u8>> { +impl GLWEAutomorphismKey<Vec<u8>> { pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: AutomorphismKeyEncryptSk<BE>, + M: GLWEAutomorphismKeyEncryptSk<BE>, { - module.automorphism_key_encrypt_sk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_sk_tmp_bytes(infos) } pub fn encrypt_pk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: GGLWEAutomorphismKeyEncryptPk<BE>, + M: GLWEAutomorphismKeyEncryptPk<BE>, { - module.automorphism_key_encrypt_pk_tmp_bytes(infos) + module.glwe_automorphism_key_encrypt_pk_tmp_bytes(infos) } } -impl<DM: DataMut> AutomorphismKey<DM> +impl<DM: DataMut> GLWEAutomorphismKey<DM> where Self: GGLWEToRef, {

@@ -44,18 +44,18 @@ source_xe: &mut Source,

scratch: &mut Scratch<BE>, ) where S: GLWESecretToRef, - M: AutomorphismKeyEncryptSk<BE>, + M: GLWEAutomorphismKeyEncryptSk<BE>, { - 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<BE: Backend> { - fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptSk<BE: Backend> { + fn glwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos; - fn automorphism_key_encrypt_sk<R, S>( + fn glwe_automorphism_key_encrypt_sk<R, S>( &self, res: &mut R, p: i64,

@@ -68,12 +68,12 @@ R: GGLWEToMut + SetGaloisElement + GGLWEInfos,

S: GLWESecretToRef; } -impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE> +impl<BE: Backend> GLWEAutomorphismKeyEncryptSk<BE> for Module<BE> where Self: GGLWEEncryptSk<BE> + VecZnxAutomorphism + GaloisElement + SvpPPolBytesOf + GLWESecretPreparedFactory<BE>, Scratch<BE>: ScratchTakeCore<BE>, { - fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + fn glwe_automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -88,7 +88,7 @@ .gglwe_encrypt_sk_tmp_bytes(infos)

.max(GLWESecret::bytes_of_from_infos(infos)) } - fn automorphism_key_encrypt_sk<R, S>( + fn glwe_automorphism_key_encrypt_sk<R, S>( &self, res: &mut R, p: i64,

@@ -106,10 +106,10 @@ 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() >= 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 @@ res.set_p(p);

} } -pub trait GGLWEAutomorphismKeyEncryptPk<BE: Backend> { - fn automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize +pub trait GLWEAutomorphismKeyEncryptPk<BE: Backend> { + fn glwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos; } -impl<BE: Backend> GGLWEAutomorphismKeyEncryptPk<BE> for Module<BE> +impl<BE: Backend> GLWEAutomorphismKeyEncryptPk<BE> for Module<BE> where Self:, Scratch<BE>: ScratchTakeCore<BE>, { - fn automorphism_key_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize + fn glwe_automorphism_key_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize where A: GGLWEInfos, {
M poulpy-core/src/encryption/glwe_tensor_key.rs → poulpy-core/src/encryption/glwe_tensor_key.rs

@@ -10,22 +10,22 @@

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<Vec<u8>> { +impl GLWETensorKey<Vec<u8>> { pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyEncryptSk<BE>, + M: GLWETensorKeyEncryptSk<BE>, { - module.tensor_key_encrypt_sk_tmp_bytes(infos) + module.glwe_tensor_key_encrypt_sk_tmp_bytes(infos) } } -impl<DataSelf: DataMut> TensorKey<DataSelf> { +impl<DataSelf: DataMut> GLWETensorKey<DataSelf> { pub fn encrypt_sk<M, S, BE: Backend>( &mut self, module: &M,

@@ -34,20 +34,20 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<BE>, ) where - M: TensorKeyEncryptSk<BE>, + M: GLWETensorKeyEncryptSk<BE>, S: GLWESecretToRef + GetDistribution + GLWEInfos, Scratch<BE>: ScratchTakeCore<BE>, { - 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<BE: Backend> { - fn tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize +pub trait GLWETensorKeyEncryptSk<BE: Backend> { + fn glwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos; - fn tensor_key_encrypt_sk<R, S>( + fn glwe_tensor_key_encrypt_sk<R, S>( &self, res: &mut R, sk: &S,

@@ -55,11 +55,11 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<BE>, ) where - R: TensorKeyToMut, + R: GLWETensorKeyToMut, S: GLWESecretToRef + GetDistribution + GLWEInfos; } -impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE> +impl<BE: Backend> GLWETensorKeyEncryptSk<BE> for Module<BE> where Self: ModuleN + GGLWEEncryptSk<BE>

@@ -72,7 +72,7 @@ + VecZnxIdftApplyTmpA<BE>

+ VecZnxBigNormalize<BE>, Scratch<BE>: ScratchTakeCore<BE>, { - fn tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize + fn glwe_tensor_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -84,7 +84,7 @@ + GLWESecret::bytes_of(self.n().into(), Rank(1))

+ GGLWE::encrypt_sk_tmp_bytes(self, infos) } - fn tensor_key_encrypt_sk<R, S>( + fn glwe_tensor_key_encrypt_sk<R, S>( &self, res: &mut R, sk: &S,

@@ -92,10 +92,10 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<BE>, ) 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();
M poulpy-core/src/external_product/gglwe.rs → poulpy-core/src/external_product/gglwe.rs

@@ -3,12 +3,12 @@

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<Vec<u8>> { +impl GLWEAutomorphismKey<Vec<u8>> { pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>( &self, module: &M,

@@ -26,7 +26,7 @@ module.gglwe_external_product_tmp_bytes(res_infos, a_infos, b_infos)

} } -impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { +impl<DataSelf: DataMut> GLWEAutomorphismKey<DataSelf> { pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) where M: GGLWEExternalProduct<BE>,
M poulpy-core/src/glwe_trace.rs → 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 @@ Self: ModuleLogN + GaloisElement + GLWEAutomorphism<BE> + GLWEShift<BE> + GLWECopy

{ } +#[inline(always)] +pub fn trace_galois_elements(log_n: usize, cyclotomic_order: i64) -> Vec<i64> { + (0..log_n) + .map(|i| { + if i == 0 { + -1 + } else { + galois_element(1 << (i - 1), cyclotomic_order) + } + }) + .collect() +} + pub trait GLWETrace<BE: Backend> where Self: ModuleLogN + GaloisElement + GLWEAutomorphism<BE> + GLWEShift<BE> + GLWECopy, { fn glwe_trace_galois_elements(&self) -> Vec<i64> { - (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<R, A, K>(&self, res_infos: &R, a_infos: &A, key_infos: &K) -> usize
M poulpy-core/src/keyswitching/gglwe.rs → poulpy-core/src/keyswitching/gglwe.rs

@@ -3,10 +3,10 @@

use crate::{ ScratchTakeCore, keyswitching::GLWEKeyswitch, - layouts::{AutomorphismKey, GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWESwitchingKey}, + layouts::{GGLWE, GGLWEInfos, GGLWEPreparedToRef, GGLWEToMut, GGLWEToRef, GLWEAutomorphismKey, GLWESwitchingKey}, }; -impl AutomorphismKey<Vec<u8>> { +impl GLWEAutomorphismKey<Vec<u8>> { pub fn keyswitch_tmp_bytes<R, A, K, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, key_infos: &K) -> usize where R: GGLWEInfos,

@@ -18,7 +18,7 @@ module.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos)

} } -impl<DataSelf: DataMut> AutomorphismKey<DataSelf> { +impl<DataSelf: DataMut> GLWEAutomorphismKey<DataSelf> { pub fn keyswitch<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>) where A: GGLWEToRef + GGLWEToRef,
M poulpy-core/src/keyswitching/ggsw.rs → poulpy-core/src/keyswitching/ggsw.rs

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

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<Vec<u8>> {

@@ -30,7 +30,7 @@ pub fn keyswitch<M, A, K, T, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)

where A: GGSWToRef, K: GGLWEPreparedToRef<BE>, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, M: GGSWKeyswitch<BE>, {

@@ -40,7 +40,7 @@

pub fn keyswitch_inplace<M, K, T, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>) where K: GGLWEPreparedToRef<BE>, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, M: GGSWKeyswitch<BE>, {

@@ -91,7 +91,7 @@ where

R: GGSWToMut, A: GGSWToRef, K: GGLWEPreparedToRef<BE>, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();

@@ -113,7 +113,7 @@ fn ggsw_keyswitch_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>)

where R: GGSWToMut, K: GGLWEPreparedToRef<BE>, - T: TensorKeyPreparedToRef<BE>, + T: GLWETensorKeyPreparedToRef<BE>, Scratch<BE>: ScratchTakeCore<BE>, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
M poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs → poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs

@@ -4,8 +4,8 @@ source::Source,

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

impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWEDecompress {} -impl<D: DataMut> AutomorphismKey<D> +impl<D: DataMut> GLWEAutomorphismKey<D> where Self: SetGaloisElement, {
M poulpy-core/src/layouts/compressed/glwe_tensor_key.rs → poulpy-core/src/layouts/compressed/glwe_tensor_key.rs

@@ -5,7 +5,7 @@ };

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 @@ Ok(())

} } -pub trait TensorKeyCompressedAtRef<D: DataRef> { +pub trait GLWETensorKeyCompressedAtRef<D: DataRef> { fn at(&self, i: usize, j: usize) -> &GGLWECompressed<D>; } -impl<D: DataRef> TensorKeyCompressedAtRef<D> for GLWETensorKeyCompressed<D> { +impl<D: DataRef> GLWETensorKeyCompressedAtRef<D> for GLWETensorKeyCompressed<D> { fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed<D> { if i > j { std::mem::swap(&mut i, &mut j);

@@ -163,11 +163,11 @@ &self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -pub trait TensorKeyCompressedAtMut<D: DataMut> { +pub trait GLWETensorKeyCompressedAtMut<D: DataMut> { fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed<D>; } -impl<D: DataMut> TensorKeyCompressedAtMut<D> for GLWETensorKeyCompressed<D> { +impl<D: DataMut> GLWETensorKeyCompressedAtMut<D> for GLWETensorKeyCompressed<D> { fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed<D> { if i > j { std::mem::swap(&mut i, &mut j);

@@ -177,16 +177,16 @@ &mut self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -pub trait TensorKeyDecompress +pub trait GLWETensorKeyDecompress where Self: GGLWEDecompress, { fn decompress_tensor_key<R, O>(&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 @@ }

} } -impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GGLWEDecompress {} +impl<B: Backend> GLWETensorKeyDecompress for Module<B> where Self: GGLWEDecompress {} -impl<D: DataMut> TensorKey<D> { +impl<D: DataMut> GLWETensorKey<D> { pub fn decompress<O, M>(&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<D: DataMut> TensorKeyCompressedToMut for GLWETensorKeyCompressed<D> +impl<D: DataMut> GLWETensorKeyCompressedToMut for GLWETensorKeyCompressed<D> where GGLWECompressed<D>: GGLWECompressedToMut, {

@@ -230,11 +230,11 @@ }

} } -pub trait TensorKeyCompressedToRef { +pub trait GLWETensorKeyCompressedToRef { fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]>; } -impl<D: DataRef> TensorKeyCompressedToRef for GLWETensorKeyCompressed<D> +impl<D: DataRef> GLWETensorKeyCompressedToRef for GLWETensorKeyCompressed<D> where GGLWECompressed<D>: GGLWECompressedToRef, {
M poulpy-core/src/layouts/glwe_automorphism_key.rs → poulpy-core/src/layouts/glwe_automorphism_key.rs

@@ -11,7 +11,7 @@

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 dsize: Dsize,

} #[derive(PartialEq, Eq, Clone)] -pub struct AutomorphismKey<D: Data> { +pub struct GLWEAutomorphismKey<D: Data> { pub(crate) key: GGLWE<D>, pub(crate) p: i64, }

@@ -34,25 +34,25 @@ pub trait SetGaloisElement {

fn set_p(&mut self, p: i64); } -impl<D: DataMut> SetGaloisElement for AutomorphismKey<D> { +impl<D: DataMut> SetGaloisElement for GLWEAutomorphismKey<D> { fn set_p(&mut self, p: i64) { self.p = p } } -impl<D: DataRef> GetGaloisElement for AutomorphismKey<D> { +impl<D: DataRef> GetGaloisElement for GLWEAutomorphismKey<D> { fn p(&self) -> i64 { self.p } } -impl<D: Data> AutomorphismKey<D> { +impl<D: Data> GLWEAutomorphismKey<D> { pub fn p(&self) -> i64 { self.p } } -impl<D: Data> LWEInfos for AutomorphismKey<D> { +impl<D: Data> LWEInfos for GLWEAutomorphismKey<D> { fn n(&self) -> Degree { self.key.n() }

@@ -70,13 +70,13 @@ self.key.size()

} } -impl<D: Data> GLWEInfos for AutomorphismKey<D> { +impl<D: Data> GLWEInfos for GLWEAutomorphismKey<D> { fn rank(&self) -> Rank { self.rank_out() } } -impl<D: Data> GGLWEInfos for AutomorphismKey<D> { +impl<D: Data> GGLWEInfos for GLWEAutomorphismKey<D> { fn rank_in(&self) -> Rank { self.key.rank_in() }

@@ -94,7 +94,7 @@ self.key.dnum()

} } -impl LWEInfos for AutomorphismKeyLayout { +impl LWEInfos for GLWEAutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k }

@@ -108,13 +108,13 @@ self.n

} } -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 @@ self.dnum

} } -impl<D: DataRef> fmt::Debug for AutomorphismKey<D> { +impl<D: DataRef> fmt::Debug for GLWEAutomorphismKey<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataMut> FillUniform for AutomorphismKey<D> { +impl<D: DataMut> FillUniform for GLWEAutomorphismKey<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl<D: DataRef> fmt::Display for AutomorphismKey<D> { +impl<D: DataRef> fmt::Display for GLWEAutomorphismKey<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl AutomorphismKey<Vec<u8>> { +impl GLWEAutomorphismKey<Vec<u8>> { pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos,

@@ -166,7 +166,7 @@ )

} 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 @@ GGLWE::bytes_of(n, base2k, k, rank, rank, dnum, dsize)

} } -impl<D: DataMut> GGLWEToMut for AutomorphismKey<D> { +impl<D: DataMut> GGLWEToMut for GLWEAutomorphismKey<D> { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { self.key.to_mut() } } -impl<D: DataMut> GGLWEToRef for AutomorphismKey<D> { +impl<D: DataMut> GGLWEToRef for GLWEAutomorphismKey<D> { fn to_ref(&self) -> GGLWE<&[u8]> { self.key.to_ref() } } -impl<D: DataRef> AutomorphismKey<D> { +impl<D: DataRef> GLWEAutomorphismKey<D> { pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } -impl<D: DataMut> AutomorphismKey<D> { +impl<D: DataMut> GLWEAutomorphismKey<D> { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } } -impl<D: DataMut> ReaderFrom for AutomorphismKey<D> { +impl<D: DataMut> ReaderFrom for GLWEAutomorphismKey<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::<LittleEndian>()? as i64; self.key.read_from(reader) } } -impl<D: DataRef> WriterTo for AutomorphismKey<D> { +impl<D: DataRef> WriterTo for GLWEAutomorphismKey<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::<LittleEndian>(self.p as u64)?; self.key.write_to(writer)
M poulpy-core/src/layouts/glwe_tensor_key.rs → poulpy-core/src/layouts/glwe_tensor_key.rs

@@ -11,7 +11,7 @@

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 dsize: Dsize,

} #[derive(PartialEq, Eq, Clone)] -pub struct TensorKey<D: Data> { +pub struct GLWETensorKey<D: Data> { pub(crate) keys: Vec<GGLWE<D>>, } -impl<D: Data> LWEInfos for TensorKey<D> { +impl<D: Data> LWEInfos for GLWETensorKey<D> { fn n(&self) -> Degree { self.keys[0].n() }

@@ -43,13 +43,13 @@ self.keys[0].size()

} } -impl<D: Data> GLWEInfos for TensorKey<D> { +impl<D: Data> GLWEInfos for GLWETensorKey<D> { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl<D: Data> GGLWEInfos for TensorKey<D> { +impl<D: Data> GGLWEInfos for GLWETensorKey<D> { fn rank_in(&self) -> Rank { self.rank_out() }

@@ -67,7 +67,7 @@ self.keys[0].dnum()

} } -impl LWEInfos for TensorKeyLayout { +impl LWEInfos for GLWETensorKeyLayout { fn n(&self) -> Degree { self.n }

@@ -81,13 +81,13 @@ self.k

} } -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 @@ self.dnum

} } -impl<D: DataRef> fmt::Debug for TensorKey<D> { +impl<D: DataRef> fmt::Debug for GLWETensorKey<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataMut> FillUniform for TensorKey<D> { +impl<D: DataMut> FillUniform for GLWETensorKey<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut()

@@ -119,7 +119,7 @@ .for_each(|key: &mut GGLWE<D>| key.fill_uniform(log_bound, source))

} } -impl<D: DataRef> fmt::Display for TensorKey<D> { +impl<D: DataRef> fmt::Display for GLWETensorKey<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() {

@@ -129,7 +129,7 @@ Ok(())

} } -impl TensorKey<Vec<u8>> { +impl GLWETensorKey<Vec<u8>> { pub fn alloc_from_infos<A>(infos: &A) -> Self where A: GGLWEInfos,

@@ -151,7 +151,7 @@ }

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 @@ pairs * GGLWE::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)

} } -impl<D: DataMut> TensorKey<D> { +impl<D: DataMut> GLWETensorKey<D> { // 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<D> { if i > j {

@@ -194,7 +194,7 @@ &mut self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -impl<D: DataRef> TensorKey<D> { +impl<D: DataRef> GLWETensorKey<D> { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE<D> { if i > j {

@@ -205,7 +205,7 @@ &self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -impl<D: DataMut> ReaderFrom for TensorKey<D> { +impl<D: DataMut> ReaderFrom for GLWETensorKey<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::<LittleEndian>()? as usize; if self.keys.len() != len {

@@ -221,7 +221,7 @@ Ok(())

} } -impl<D: DataRef> WriterTo for TensorKey<D> { +impl<D: DataRef> WriterTo for GLWETensorKey<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::<LittleEndian>(self.keys.len() as u64)?; for key in &self.keys {

@@ -231,31 +231,31 @@ Ok(())

} } -pub trait TensorKeyToRef { - fn to_ref(&self) -> TensorKey<&[u8]>; +pub trait GLWETensorKeyToRef { + fn to_ref(&self) -> GLWETensorKey<&[u8]>; } -impl<D: DataRef> TensorKeyToRef for TensorKey<D> +impl<D: DataRef> GLWETensorKeyToRef for GLWETensorKey<D> where GGLWE<D>: 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<D: DataMut> TensorKeyToMut for TensorKey<D> +impl<D: DataMut> GLWETensorKeyToMut for GLWETensorKey<D> where GGLWE<D>: 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(), } }
M poulpy-core/src/layouts/lwe.rs → 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)
M poulpy-core/src/layouts/prepared/glwe_tensor_key.rs → 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<D: Data, B: Backend> { +pub struct GLWETensorKeyPrepared<D: Data, B: Backend> { pub(crate) keys: Vec<GGLWEPrepared<D, B>>, } -impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> { +impl<D: Data, B: Backend> LWEInfos for GLWETensorKeyPrepared<D, B> { fn n(&self) -> Degree { self.keys[0].n() }

@@ -28,13 +28,13 @@ self.keys[0].size()

} } -impl<D: Data, B: Backend> GLWEInfos for TensorKeyPrepared<D, B> { +impl<D: Data, B: Backend> GLWEInfos for GLWETensorKeyPrepared<D, B> { fn rank(&self) -> Rank { self.rank_out() } } -impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> { +impl<D: Data, B: Backend> GGLWEInfos for GLWETensorKeyPrepared<D, B> { fn rank_in(&self) -> Rank { self.rank_out() }

@@ -52,7 +52,7 @@ self.keys[0].dnum()

} } -pub trait TensorKeyPreparedFactory<B: Backend> +pub trait GLWETensorKeyPreparedFactory<B: Backend> where Self: GGLWEPreparedFactory<B>, {

@@ -63,16 +63,16 @@ k: TorusPrecision,

dnum: Dnum, dsize: Dsize, rank: Rank, - ) -> TensorKeyPrepared<Vec<u8>, B> { + ) -> GLWETensorKeyPrepared<Vec<u8>, 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<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B> + fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> GLWETensorKeyPrepared<Vec<u8>, B> where A: GGLWEInfos, {

@@ -117,11 +117,11 @@ }

fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where - R: TensorKeyPreparedToMut<B>, - O: TensorKeyToRef, + R: GLWETensorKeyPreparedToMut<B>, + 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 @@ }

} } -impl<B: Backend> TensorKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {} +impl<B: Backend> GLWETensorKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {} -impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> { +impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> { pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self where A: GGLWEInfos, - M: TensorKeyPreparedFactory<B>, + M: GLWETensorKeyPreparedFactory<B>, { module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self where - M: TensorKeyPreparedFactory<B>, + M: GLWETensorKeyPreparedFactory<B>, { module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) }

@@ -152,20 +152,20 @@

pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory<B>, + M: GLWETensorKeyPreparedFactory<B>, { module.bytes_of_tensor_key_prepared_from_infos(infos) } pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where - M: TensorKeyPreparedFactory<B>, + M: GLWETensorKeyPreparedFactory<B>, { module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } } -impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> { +impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> { // 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<D, B> { if i > j {

@@ -176,7 +176,7 @@ &mut self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> { +impl<D: DataRef, B: Backend> GLWETensorKeyPrepared<D, B> { // Returns a reference to GGLWE_{s}(s[i] * s[j]) pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared<D, B> { if i > j {

@@ -187,51 +187,51 @@ &self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> { +impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> { pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize where A: GGLWEInfos, - M: TensorKeyPreparedFactory<B>, + M: GLWETensorKeyPreparedFactory<B>, { module.prepare_tensor_key_tmp_bytes(infos) } } -impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> { +impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> { pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>) where - O: TensorKeyToRef, - M: TensorKeyPreparedFactory<B>, + O: GLWETensorKeyToRef, + M: GLWETensorKeyPreparedFactory<B>, { module.prepare_tensor_key(self, other, scratch); } } -pub trait TensorKeyPreparedToMut<B: Backend> { - fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +pub trait GLWETensorKeyPreparedToMut<B: Backend> { + fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B>; } -impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B> +impl<D: DataMut, B: Backend> GLWETensorKeyPreparedToMut<B> for GLWETensorKeyPrepared<D, B> where GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>, { - 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<B: Backend> { - fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +pub trait GLWETensorKeyPreparedToRef<B: Backend> { + fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B>; } -impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B> +impl<D: DataRef, B: Backend> GLWETensorKeyPreparedToRef<B> for GLWETensorKeyPrepared<D, B> where GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>, { - 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(), } }
M poulpy-core/src/lib.rs → 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::*;
M poulpy-core/src/scratch.rs → poulpy-core/src/scratch.rs

@@ -6,11 +6,11 @@

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

) } - fn take_glwe_automorphism_key<A>(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) + fn take_glwe_automorphism_key<A>(&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<A, M>(

@@ -304,14 +304,14 @@ let (data, scratch) = self.take_gglwe_prepared(module, infos);

(GLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) } - fn take_glwe_tensor_key<A, M>(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) + fn take_glwe_tensor_key<A, M>(&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<GGLWE<&mut [u8]>> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;

@@ -331,10 +331,10 @@ let (gglwe, s) = scratch.take_gglwe(&ksk_infos);

scratch = s; keys.push(gglwe); } - (TensorKey { keys }, scratch) + (GLWETensorKey { keys }, scratch) } - fn take_glwe_tensor_key_prepared<A, M>(&mut self, module: &M, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_glwe_tensor_key_prepared<A, M>(&mut self, module: &M, infos: &A) -> (GLWETensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, M: ModuleN + VmpPMatBytesOf,

@@ -364,7 +364,7 @@ let (gglwe, s) = scratch.take_gglwe_prepared(module, &ksk_infos);

scratch = s; keys.push(gglwe); } - (TensorKeyPrepared { keys }, scratch) + (GLWETensorKeyPrepared { keys }, scratch) } }
M poulpy-core/src/tests/serialization.rs → 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 @@ }

#[test] fn test_automorphism_key_serialization() { - let original: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); }

@@ -82,7 +82,7 @@ }

#[test] fn test_tensor_key_serialization() { - let original: TensorKey<Vec<u8>> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); }
M poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs → poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs

@@ -5,11 +5,11 @@ source::Source,

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

#[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism<BE: Backend>(module: &Module<BE>) where - Module<BE>: AutomorphismKeyEncryptSk<BE> + Module<BE>: GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GLWEAutomorphismKeyAutomorphism<BE> + VecZnxAutomorphism

@@ -46,7 +46,7 @@ let dnum_in: usize = k_in / (base2k * di);

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

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

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

rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); + let mut auto_key_in: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: GLWEAutomorphismKey<Vec<u8>> = 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<BE> = 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 @@

#[allow(clippy::too_many_arguments)] pub fn test_gglwe_automorphism_key_automorphism_inplace<BE: Backend>(module: &Module<BE>) where - Module<BE>: AutomorphismKeyEncryptSk<BE> + Module<BE>: GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GLWEAutomorphismKeyAutomorphism<BE> + VecZnxAutomorphism

@@ -213,7 +213,7 @@

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

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

rank: rank.into(), }; - let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); + let mut auto_key: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: GLWEAutomorphismKey<Vec<u8>> = 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<BE> = 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<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);
M poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs → poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs

@@ -5,12 +5,12 @@ source::Source,

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

pub fn test_ggsw_automorphism<BE: Backend>(module: &Module<BE>) where Module<BE>: GGSWEncryptSk<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GGSWAutomorphism<BE> - + TensorKeyPreparedFactory<BE> - + TensorKeyEncryptSk<BE> + + GLWETensorKeyPreparedFactory<BE> + + GLWETensorKeyEncryptSk<BE> + GLWESecretPreparedFactory<BE> + VecZnxAutomorphismInplace<BE> + GGSWNoise<BE>,

@@ -64,7 +64,7 @@ dsize: dsize_in.into(),

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

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

let mut ct_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_layout); let mut ct_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]);

@@ -94,8 +94,8 @@ let mut source_xa: Source = Source::new([0u8; 32]);

let mut scratch: ScratchOwned<BE> = 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 @@ let mut auto_key_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =

GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, BE> = + GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism(

@@ -177,11 +177,11 @@ #[allow(clippy::too_many_arguments)]

pub fn test_ggsw_automorphism_inplace<BE: Backend>(module: &Module<BE>) where Module<BE>: GGSWEncryptSk<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GGSWAutomorphism<BE> - + TensorKeyPreparedFactory<BE> - + TensorKeyEncryptSk<BE> + + GLWETensorKeyPreparedFactory<BE> + + GLWETensorKeyEncryptSk<BE> + GLWESecretPreparedFactory<BE> + VecZnxAutomorphismInplace<BE> + GGSWNoise<BE>,

@@ -211,7 +211,7 @@ dsize: dsize_in.into(),

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

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

}; let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout); - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_layout); - let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]);

@@ -240,8 +240,8 @@ let mut source_xa: Source = Source::new([0u8; 32]);

let mut scratch: ScratchOwned<BE> = 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 @@ let mut auto_key_prepared: GLWEAutomorphismKeyPrepared<Vec<u8>, BE> =

GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = - TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout); + let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, 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());
M poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs → poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs

@@ -5,11 +5,11 @@ source::Source,

}; 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 @@ + GLWESecretPreparedFactory<BE>

+ VecZnxFillUniform + GLWEDecrypt<BE> + GLWEAutomorphism<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GLWENoise<BE> + VecZnxAutomorphismInplace<BE>,

@@ -55,7 +55,7 @@ k: k_out.into(),

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

dsize: di.into(), }; - let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_in_infos); let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos);

@@ -76,7 +76,7 @@

module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned<BE> = 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 @@ + GLWESecretPreparedFactory<BE>

+ VecZnxFillUniform + GLWEDecrypt<BE> + GLWEAutomorphism<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GLWENoise<BE> + VecZnxAutomorphismInplace<BE>,

@@ -165,7 +165,7 @@ k: k_out.into(),

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

dsize: di.into(), }; - let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut autokey: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&autokey_infos); let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos);

@@ -185,7 +185,7 @@

module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned<BE> = 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),
M poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs → poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs

@@ -5,19 +5,20 @@ source::Source,

}; 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<BE: Backend>(module: &Module<BE>) where - Module<BE>: AutomorphismKeyEncryptSk<BE> + Module<BE>: GLWEAutomorphismKeyEncryptSk<BE> + GGLWEKeyswitch<BE> + GLWESecretPreparedFactory<BE> + GLWESwitchingKeyEncryptSk<BE>

@@ -37,7 +38,7 @@ for di in 1..dsize + 1 {

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

rank: rank.into(), }; - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey<Vec<u8>> = 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<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(module, &atk_infos)); + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWEAutomorphismKey::encrypt_sk_tmp_bytes( + module, &atk_infos, + )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs);

@@ -89,7 +92,7 @@ }

pub fn test_gglwe_automorphism_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) where - Module<BE>: AutomorphismKeyCompressedEncryptSk<BE> + Module<BE>: GLWEAutomorphismKeyCompressedEncryptSk<BE> + GGLWEKeyswitch<BE> + GLWESecretPreparedFactory<BE> + GLWESwitchingKeyEncryptSk<BE>

@@ -109,7 +112,7 @@ for di in 1..dsize + 1 {

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

let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into()); sk_out_prepared.prepare(module, &sk_out); - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); + let mut atk: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key
M poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs → poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs

@@ -9,18 +9,18 @@ source::Source,

}; 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<BE: Backend>(module: &Module<BE>) where - Module<BE>: TensorKeyEncryptSk<BE> + Module<BE>: GLWETensorKeyEncryptSk<BE> + GLWESecretPreparedFactory<BE> + GLWEDecrypt<BE> + VecZnxDftAlloc<BE>

@@ -40,7 +40,7 @@ for rank in 1_usize..3 {

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 @@ dsize: Dsize(1),

rank: rank.into(), }; - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey<Vec<u8>> = 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<BE> = ScratchOwned::alloc(TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key_infos)); + let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GLWETensorKey::encrypt_sk_tmp_bytes( + module, + &tensor_key_infos, + )); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs);

@@ -111,9 +114,9 @@ }

pub fn test_gglwe_tensor_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>) where - Module<BE>: TensorKeyEncryptSk<BE> + Module<BE>: GLWETensorKeyEncryptSk<BE> + GLWESecretPreparedFactory<BE> - + GGLWETensorKeyCompressedEncryptSk<BE> + + GLWETensorKeyCompressedEncryptSk<BE> + GLWEDecrypt<BE> + VecZnxDftAlloc<BE> + VecZnxBigAlloc<BE>

@@ -133,7 +136,7 @@ for rank in 1_usize..3 {

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 @@ 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<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); + let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
M poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs

@@ -5,12 +5,12 @@ source::Source,

}; 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<BE: Backend>(module: &Module<BE>)

where Module<BE>: GGSWEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE> - + TensorKeyEncryptSk<BE> + + GLWETensorKeyEncryptSk<BE> + GGSWKeyswitch<BE> + GLWESecretPreparedFactory<BE> - + TensorKeyPreparedFactory<BE> + + GLWETensorKeyPreparedFactory<BE> + GLWESwitchingKeyPreparedFactory<BE> + GGSWNoise<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,

@@ -61,7 +61,7 @@ dsize: dsize_in.into(),

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

let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_infos); let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -93,7 +93,7 @@

let mut scratch: ScratchOwned<BE> = 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 @@ let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =

GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, 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<BE: Backend>(module: &Module<BE>)

where Module<BE>: GGSWEncryptSk<BE> + GLWESwitchingKeyEncryptSk<BE> - + TensorKeyEncryptSk<BE> + + GLWETensorKeyEncryptSk<BE> + GGSWKeyswitch<BE> + GLWESecretPreparedFactory<BE> - + TensorKeyPreparedFactory<BE> + + GLWETensorKeyPreparedFactory<BE> + GLWESwitchingKeyPreparedFactory<BE> + GGSWNoise<BE>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,

@@ -216,7 +216,7 @@ dsize: dsize_in.into(),

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

}; let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); - let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tsk_infos); + let mut tsk: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tsk_infos); let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -247,7 +247,7 @@

let mut scratch: ScratchOwned<BE> = 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 @@ let mut ksk_prepared: GLWESwitchingKeyPrepared<Vec<u8>, BE> =

GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk); ksk_prepared.prepare(module, &ksk, scratch.borrow()); - let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> = TensorKeyPrepared::alloc_from_infos(module, &tsk); + let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, 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());
M poulpy-core/src/tests/test_suite/packing.rs → poulpy-core/src/tests/test_suite/packing.rs

@@ -7,10 +7,10 @@ source::Source,

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

pub fn test_glwe_packing<BE: Backend>(module: &Module<BE>) where Module<BE>: GLWEEncryptSk<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + GLWEPacking<BE> + GLWESecretPreparedFactory<BE>

@@ -49,7 +49,7 @@ k: k_ct.into(),

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

let mut scratch: ScratchOwned<BE> = 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 @@

let gal_els: Vec<i64> = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new(); - let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos); + let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module,
M poulpy-core/src/tests/test_suite/trace.rs → poulpy-core/src/tests/test_suite/trace.rs

@@ -7,12 +7,12 @@ source::Source,

}; 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<BE>: GLWETrace<BE> + GLWEEncryptSk<BE> + GLWEDecrypt<BE> - + AutomorphismKeyEncryptSk<BE> + + GLWEAutomorphismKeyEncryptSk<BE> + GLWEAutomorphismKeyPreparedFactory<BE> + VecZnxFillUniform + GLWESecretPreparedFactory<BE>

@@ -49,7 +49,7 @@ k: k.into(),

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

let mut scratch: ScratchOwned<BE> = 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 @@ );

let mut auto_keys: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>> = HashMap::new(); let gal_els: Vec<i64> = GLWE::trace_galois_elements(module); - let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos); + let mut tmp: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module,
M poulpy-hal/src/layouts/module.rs → poulpy-hal/src/layouts/module.rs

@@ -97,18 +97,23 @@ impl<BE: Backend> ModuleLogN for Module<BE> where Self: ModuleN {}

impl<BE: Backend> CyclotomicOrder for Module<BE> 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
M poulpy-schemes/benches/circuit_bootstrapping.rs → 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<B: Backend, BRA: BlindRotationAlgo>(c: &mut Criterion, label: &str) +pub fn benc_circuit_bootstrapping<BE: Backend, BRA: BlindRotationAlgo>(c: &mut Criterion, label: &str) where - Module<B>: ModuleNew<B> - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace<B> - + VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA<B> - + SvpApplyDftToDft<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc<B> - + VmpPrepare<B> - + SvpPrepare<B> - + SvpPPolAlloc<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + SvpPPolBytesOf - + VecZnxRotateInplace<B> - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace<B>, - ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + TakeVecZnxDftImpl<B> - + ScratchAvailableImpl<B> - + TakeVecZnxImpl<B> - + TakeScalarZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B> - + TakeVecZnxDftSliceImpl<B> - + TakeMatZnxImpl<B> - + TakeVecZnxSliceImpl<B> - + TakeSliceImpl<B>, - BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, - BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>, - BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>, + Module<BE>: ModuleNew<BE> + + ModuleN + + GLWESecretPreparedFactory<BE> + + GLWEExternalProduct<BE> + + GLWEDecrypt<BE> + + LWEEncryptSk<BE> + + CircuitBootstrappingKeyEncryptSk<BRA, BE> + + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + + CirtuitBootstrappingExecute<BRA, BE> + + GGSWPreparedFactory<BE> + + GGSWNoise<BE> + + GLWEEncryptSk<BE> + + VecZnxRotateInplace<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { let group_name: String = format!("circuit_bootstrapping::{label}");

@@ -118,81 +57,33 @@ ggsw_infos: GGSWLayout,

cbt_infos: CircuitBootstrappingKeyLayout, } - fn runner<B: Backend, BRA: BlindRotationAlgo>(params: &Params) -> impl FnMut() + fn runner<BE: Backend, BRA: BlindRotationAlgo>(params: &Params) -> impl FnMut() where - Module<B>: ModuleNew<B> - + VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace<B> - + VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA<B> - + SvpApplyDftToDft<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc<B> - + VmpPrepare<B> - + SvpPrepare<B> - + SvpPPolAlloc<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + SvpPPolBytesOf - + VecZnxRotateInplace<B> - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace<B>, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + TakeVecZnxDftImpl<B> - + ScratchAvailableImpl<B> - + TakeVecZnxImpl<B> - + TakeScalarZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B> - + TakeVecZnxDftSliceImpl<B> - + TakeMatZnxImpl<B> - + TakeVecZnxSliceImpl<B> - + TakeSliceImpl<B>, - BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, - BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>, - BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>, + Module<BE>: ModuleNew<BE> + + ModuleN + + GLWESecretPreparedFactory<BE> + + GLWEExternalProduct<BE> + + GLWEDecrypt<BE> + + LWEEncryptSk<BE> + + CircuitBootstrappingKeyEncryptSk<BRA, BE> + + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + + CirtuitBootstrappingExecute<BRA, BE> + + GGSWPreparedFactory<BE> + + GGSWNoise<BE> + + GLWEEncryptSk<BE> + + VecZnxRotateInplace<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, /* TODO find a way to remove this bound or move it to CBT KEY */ + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { // Scratch space (4MB) - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 22); + let mut scratch: ScratchOwned<BE> = 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<B> = Module::<B>::new(n_glwe.as_u32() as u64); + let module: Module<BE> = Module::<BE>::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 @@

let ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&params.lwe_infos); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::alloc_from_infos(&params.cbt_infos); + cbt_key.encrypt_sk( &module, &sk_lwe, &sk_glwe, - &params.cbt_infos, &mut source_xa, &mut source_xe, scratch.borrow(), ); let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&params.ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); - + let mut cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, BE> = + CircuitBootstrappingKeyPrepared::alloc_from_infos(&module, &params.cbt_infos); + cbt_prepared.prepare(&module, &cbt_key, scratch.borrow()); move || { cbt_prepared.execute_to_constant( &module,

@@ -261,7 +153,7 @@ k: 52_u32.into(),

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 @@ dnum: 3_u32.into(),

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

}, }] { let id: BenchmarkId = BenchmarkId::from_parameter(params.name.clone()); - let mut runner = runner::<B, BRA>(&params); + let mut runner = runner::<BE, BRA>(&params); group.bench_with_input(id, &(), |b, _| b.iter(&mut runner)); }
M poulpy-schemes/examples/circuit_bootstrapping.rs → 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_schemes::tfhe::{ blind_rotation::{BlindRotationKeyLayout, CGGI}, - circuit_bootstrapping::{ - CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout, - CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute, - }, + circuit_bootstrapping::{CircuitBootstrappingKey, CircuitBootstrappingKeyLayout, CircuitBootstrappingKeyPrepared}, }; fn main() {

@@ -89,7 +86,7 @@ k: k_brk.into(),

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

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 @@ sk_glwe.fill_ternary_prob(0.5, &mut source_xs);

// sk_glwe.fill_zero(); // GLWE secret prepared (opaque backend dependant write only struct) - let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BackendImpl> = sk_glwe.prepare_alloc(&module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, 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 @@

let now: Instant = Instant::now(); // Circuit bootstrapping evaluation key - let cbt_key: CircuitBootstrappingKey<Vec<u8>, CGGI> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey<Vec<u8>, 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<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) - let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> = - cbt_key.prepare_alloc(&module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, 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 @@ .enumerate()

.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 @@ scratch.borrow(),

); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWPrepared<Vec<u8>, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let mut res_prepared: GGSWPrepared<Vec<u8>, 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());
M poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs → poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key.rs

@@ -29,7 +29,7 @@ }

} } -impl<BE: Backend> BlindRotationKeyEncryptSk<BE, CGGI> for Module<BE> +impl<BE: Backend> BlindRotationKeyEncryptSk<CGGI, BE> for Module<BE> where Self: GGSWEncryptSk<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs → poulpy-schemes/src/tfhe/blind_rotation/algorithms/cggi/key_prepared.rs

@@ -15,7 +15,7 @@ BlindRotationKey, BlindRotationKeyInfos, BlindRotationKeyPrepared, BlindRotationKeyPreparedFactory, CGGI,

utils::set_xai_plus_y, }; -impl<BE: Backend> BlindRotationKeyPreparedFactory<BE, CGGI> for Module<BE> +impl<BE: Backend> BlindRotationKeyPreparedFactory<CGGI, BE> for Module<BE> where Self: GGSWPreparedFactory<BE> + SvpPPolAlloc<BE> + SvpPrepare<BE>, {
M poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs → poulpy-schemes/src/tfhe/blind_rotation/encryption/key.rs

@@ -10,7 +10,7 @@ };

use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKey}; -pub trait BlindRotationKeyEncryptSk<B: Backend, BRA: BlindRotationAlgo> { +pub trait BlindRotationKeyEncryptSk<BRA: BlindRotationAlgo, B: Backend> { fn blind_rotation_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGSWInfos;

@@ -43,7 +43,7 @@ ) where

S0: GLWESecretPreparedToRef<BE> + GLWEInfos, S1: LWESecretToRef + LWEInfos + GetDistribution, Scratch<BE>: ScratchTakeCore<BE>, - M: BlindRotationKeyEncryptSk<BE, BRA>, + M: BlindRotationKeyEncryptSk<BRA, BE>, { module.blind_rotation_key_encrypt_sk(self, sk_glwe, sk_lwe, source_xa, source_xe, scratch); }

@@ -53,7 +53,7 @@ impl<BRA: BlindRotationAlgo> BlindRotationKey<Vec<u8>, BRA> {

pub fn encrypt_sk_tmp_bytes<A, M, BE: Backend>(module: &M, infos: &A) -> usize where A: GGSWInfos, - M: BlindRotationKeyEncryptSk<BE, BRA>, + M: BlindRotationKeyEncryptSk<BRA, BE>, { module.blind_rotation_key_encrypt_sk_tmp_bytes(infos) }
M poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs → poulpy-schemes/src/tfhe/blind_rotation/layouts/key_prepared.rs

@@ -3,13 +3,13 @@

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<BE: Backend, BRA: BlindRotationAlgo> { +pub trait BlindRotationKeyPreparedFactory<BRA: BlindRotationAlgo, BE: Backend> { fn blind_rotation_key_prepared_alloc<A>(&self, infos: &A) -> BlindRotationKeyPrepared<Vec<u8>, BRA, BE> where A: BlindRotationKeyInfos;

@@ -21,27 +21,23 @@ other: &BlindRotationKey<DR, BRA>,

scratch: &mut Scratch<BE>, ) where DM: DataMut, - DR: DataRef, - Scratch<BE>: ScratchTakeCore<BE>; + DR: DataRef; } impl<BE: Backend, BRA: BlindRotationAlgo> BlindRotationKeyPrepared<Vec<u8>, BRA, BE> { pub fn alloc<A, M>(module: &M, infos: &A) -> Self where A: BlindRotationKeyInfos, - M: BlindRotationKeyPreparedFactory<BE, BRA>, + M: BlindRotationKeyPreparedFactory<BRA, BE>, { module.blind_rotation_key_prepared_alloc(infos) } } -impl<D: DataMut, BRA: BlindRotationAlgo, BE: Backend> BlindRotationKeyPrepared<D, BRA, BE> -where - Scratch<BE>: ScratchTakeCore<BE>, -{ +impl<D: DataMut, BRA: BlindRotationAlgo, BE: Backend> BlindRotationKeyPrepared<D, BRA, BE> { pub fn prepare<DR: DataRef, M>(&mut self, module: &M, other: &BlindRotationKey<DR, BRA>, scratch: &mut Scratch<BE>) where - M: BlindRotationKeyPreparedFactory<BE, BRA>, + M: BlindRotationKeyPreparedFactory<BRA, BE>, { module.blind_rotation_key_prepare(self, other, scratch); }
M poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs → poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs

@@ -24,14 +24,13 @@ n_lwe: usize,

block_size: usize, extension_factor: usize, ) where - M: BlindRotationKeyEncryptSk<BE, BRA> - + BlindRotationKeyPreparedFactory<BE, BRA> + M: BlindRotationKeyEncryptSk<BRA, BE> + + BlindRotationKeyPreparedFactory<BRA, BE> + BlindRotationExecute<BRA, BE> + + LookupTableFactory + GLWESecretPreparedFactory<BE> - + BlindRotationExecute<BRA, BE> - + LWEEncryptSk<BE> - + LookupTableFactory - + GLWEDecrypt<BE>, + + GLWEDecrypt<BE> + + LWEEncryptSk<BE>, BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, Scratch<BE>: ScratchTakeCore<BE>,
M poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs → 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<D: DataRef, BRA: BlindRotationAlgo, B> CirtuitBootstrappingExecute<B> for CircuitBootstrappingKeyPrepared<D, BRA, B> +pub trait CirtuitBootstrappingExecute<BRA: BlindRotationAlgo, BE: Backend> { + fn circuit_bootstrapping_execute_to_constant<R, L, D>( + &self, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch<BE>, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; + + #[allow(clippy::too_many_arguments)] + fn circuit_bootstrapping_execute_to_exponent<R, L, D>( + &self, + log_gap_out: usize, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch<BE>, + ) where + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + D: DataRef; +} + +impl<D: DataRef, BRA: BlindRotationAlgo, BE: Backend> CircuitBootstrappingKeyPrepared<D, BRA, BE> { + pub fn execute_to_constant<M, L, R>( + &self, + module: &M, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch<BE>, + ) where + M: CirtuitBootstrappingExecute<BRA, BE>, + 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<R, L, M>( + &self, + module: &M, + log_gap_out: usize, + res: &mut R, + lwe: &L, + log_domain: usize, + extension_factor: usize, + scratch: &mut Scratch<BE>, + ) where + M: CirtuitBootstrappingExecute<BRA, BE>, + R: GGSWToMut + GGSWInfos, + L: LWEToRef + LWEInfos, + { + module.circuit_bootstrapping_execute_to_exponent( + log_gap_out, + res, + lwe, + self, + log_domain, + extension_factor, + scratch, + ); + } +} + +impl<BRA: BlindRotationAlgo, BE: Backend> CirtuitBootstrappingExecute<BRA, BE> for Module<BE> where - Module<B>: VecZnxRotateInplace<B> - + VecZnxNormalizeInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxIdftApplyTmpA<B> - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotate - + VecZnxNormalize<B>, - B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, - Scratch<B>: TakeVecZnx - + TakeVecZnxDftSlice<B> - + TakeVecZnxBig<B> - + TakeVecZnxDft<B> - + TakeMatZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeSlice, - BlindRotationKeyPrepared<D, BRA, B>: BlincRotationExecute<B>, + Self: ModuleN + LookupTableFactory + BlindRotationExecute<BRA, BE> + GLWETrace<BE> + GLWEPacking<BE> + GGSWFromGGLWE<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { - fn execute_to_constant<DM: DataMut, DR: DataRef>( + fn circuit_bootstrapping_execute_to_constant<R, L, D>( &self, - module: &Module<B>, - res: &mut GGSW<DM>, - lwe: &LWE<DR>, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch<B>, - ) { + scratch: &mut Scratch<BE>, + ) 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<DM: DataMut, DR: DataRef>( + fn circuit_bootstrapping_execute_to_exponent<R, L, D>( &self, - module: &Module<B>, log_gap_out: usize, - res: &mut GGSW<DM>, - lwe: &LWE<DR>, + res: &mut R, + lwe: &L, + key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>, log_domain: usize, extension_factor: usize, - scratch: &mut Scratch<B>, - ) { + scratch: &mut Scratch<BE>, + ) 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<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>( +pub fn circuit_bootstrap_core<R, L, D, M, BRA: BlindRotationAlgo, BE: Backend>( to_exponent: bool, - module: &Module<B>, + module: &M, log_gap_out: usize, - res: &mut GGSW<DRes>, - lwe: &LWE<DLwe>, + res: &mut R, + lwe: &L, log_domain: usize, extension_factor: usize, - key: &CircuitBootstrappingKeyPrepared<DBrk, BRA, B>, - scratch: &mut Scratch<B>, + key: &CircuitBootstrappingKeyPrepared<D, BRA, BE>, + scratch: &mut Scratch<BE>, ) where - DRes: DataMut, - DLwe: DataRef, - DBrk: DataRef, - Module<B>: VecZnxRotateInplace<B> - + VecZnxNormalizeInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxIdftApplyTmpA<B> - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxRotate - + VecZnxNormalize<B>, - B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, - Scratch<B>: TakeVecZnxDftSlice<B> - + TakeVecZnxBig<B> - + TakeVecZnxDft<B> - + TakeVecZnx - + ScratchAvailable - + TakeVecZnxSlice - + TakeMatZnx - + TakeSlice, - BlindRotationKeyPrepared<DBrk, BRA, B>: BlincRotationExecute<B>, + R: GGSWToMut, + L: LWEToRef, + D: DataRef, + M: ModuleN + LookupTableFactory + BlindRotationExecute<BRA, BE> + GLWETrace<BE> + GLWEPacking<BE> + GGSWFromGGLWE<BE>, + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { - #[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();

@@ -202,9 +199,16 @@ f[j * alpha + i] = j as i64 * (1 << (base2k * (dnum - 1 - i)));

}); }); } + + 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 @@ lut.set_rotation_direction(LookUpTableRotationDirection::Right);

} // 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 @@ tmp_glwe.trace(module, 0, module.log_n(), &res_glwe, &key.atk, scratch_2);

} 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 @@ res.from_gglwe(module, &tmp_gglwe, &key.tsk, scratch_2);

} #[allow(clippy::too_many_arguments)] -fn post_process<DataRes, DataA, B: Backend>( - module: &Module<B>, - res: &mut GLWE<DataRes>, - a: &GLWE<DataA>, +fn post_process<R, A, M, BE: Backend>( + module: &M, + res: &mut R, + a: &A, log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, B>>, - scratch: &mut Scratch<B>, + auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, BE>>, + scratch: &mut Scratch<BE>, ) where - DataRes: DataMut, - DataA: DataRef, - Module<B>: VecZnxRotateInplace<B> - + VecZnxNormalizeInplace<B> - + VecZnxNormalizeTmpBytes - + VecZnxSwitchRing - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxIdftApplyTmpA<B> - + VecZnxSub - + VecZnxAddInplace - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxSubInplace - + VecZnxDftBytesOf - + VmpApplyDftToDftTmpBytes - + VecZnxBigNormalizeTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + VecZnxDftApply<B> - + VecZnxIdftApplyConsume<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigNormalize<B> - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxRotate - + VecZnxNormalize<B>, - Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx, + R: GLWEToMut, + A: GLWEToRef, + M: ModuleLogN + GLWETrace<BE> + GLWEPacking<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { + 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<usize, &mut GLWE<Vec<u8>>> = HashMap::new();

@@ -326,7 +308,7 @@ let mut cts_vec: Vec<GLWE<Vec<u8>>> = Vec::new();

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 @@ for (i, ct) in cts_vec.iter_mut().enumerate().take(steps) {

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<Vec<u8>> = cts.remove(&0).unwrap(); res.trace( module,
M poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs → 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 @@ fn brk_infos(&self) -> BlindRotationKeyLayout {

self.layout_brk } - fn tsk_infos(&self) -> TensorKeyLayout { + fn tsk_infos(&self) -> GLWETensorKeyLayout { self.layout_tsk } } -pub trait CircuitBootstrappingKeyEncryptSk<B: Backend> { +pub trait CircuitBootstrappingKeyEncryptSk<BRA: BlindRotationAlgo, BE: Backend> { #[allow(clippy::too_many_arguments)] - fn encrypt_sk<DLwe, DGlwe, INFOS>( - module: &Module<B>, - sk_lwe: &LWESecret<DLwe>, - sk_glwe: &GLWESecret<DGlwe>, - cbt_infos: &INFOS, + fn circuit_bootstrapping_key_encrypt_sk<D, S0, S1>( + &self, + res: &mut CircuitBootstrappingKey<D, BRA>, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch<B>, - ) -> Self + scratch: &mut Scratch<BE>, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution; +} + +impl<BRA: BlindRotationAlgo> CircuitBootstrappingKey<Vec<u8>, BRA> { + pub fn alloc_from_infos<A>(infos: &A) -> Self where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef; + A: CircuitBootstrappingKeyInfos, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, + { + 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<i64> = trace_galois_elements(atk_infos.log_n(), 2 * atk_infos.n().as_usize() as i64); + + Self { + brk: <BlindRotationKey<Vec<u8>, BRA> as BlindRotationKeyFactory<BRA>>::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<D: Data, BRA: BlindRotationAlgo> { pub(crate) brk: BlindRotationKey<D, BRA>, - pub(crate) tsk: TensorKey<Vec<u8>>, - pub(crate) atk: HashMap<i64, AutomorphismKey<Vec<u8>>>, + pub(crate) tsk: GLWETensorKey<Vec<u8>>, + pub(crate) atk: HashMap<i64, GLWEAutomorphismKey<Vec<u8>>>, +} + +impl<D: DataMut, BRA: BlindRotationAlgo> CircuitBootstrappingKey<D, BRA> { + pub fn encrypt_sk<M, S0, S1, BE: Backend>( + &mut self, + module: &M, + sk_lwe: &S0, + sk_glwe: &S1, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch<BE>, + ) where + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, + M: CircuitBootstrappingKeyEncryptSk<BRA, BE>, + { + module.circuit_bootstrapping_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch); + } } -impl<BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyEncryptSk<B> for CircuitBootstrappingKey<Vec<u8>, BRA> +impl<BRA: BlindRotationAlgo, BE: Backend> CircuitBootstrappingKeyEncryptSk<BRA, BE> for Module<BE> where - BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>, - Module<B>: SvpApplyDftToDft<B> - + VecZnxIdftApplyTmpA<B> - + VecZnxAddScalarInplace - + VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace<B> - + VecZnxAddNormal - + VecZnxNormalize<B> - + VecZnxSub - + SvpPrepare<B> - + VecZnxSwitchRing - + SvpPPolBytesOf - + SvpPPolAlloc<B> - + VecZnxAutomorphism, - Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol<B> + TakeVecZnxBig<B>, + Self: GLWETensorKeyEncryptSk<BE> + + BlindRotationKeyEncryptSk<BRA, BE> + + GLWEAutomorphismKeyEncryptSk<BE> + + GLWESecretPreparedFactory<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { - fn encrypt_sk<DLwe, DGlwe, INFOS>( - module: &Module<B>, - sk_lwe: &LWESecret<DLwe>, - sk_glwe: &GLWESecret<DGlwe>, - cbt_infos: &INFOS, + fn circuit_bootstrapping_key_encrypt_sk<D, S0, S1>( + &self, + res: &mut CircuitBootstrappingKey<D, BRA>, + sk_lwe: &S0, + sk_glwe: &S1, source_xa: &mut Source, source_xe: &mut Source, - scratch: &mut Scratch<B>, - ) -> Self - where - INFOS: CircuitBootstrappingKeyInfos, - DLwe: DataRef, - DGlwe: DataRef, + scratch: &mut Scratch<BE>, + ) where + D: DataMut, + S0: LWESecretToRef + GetDistribution + LWEInfos, + S1: GLWESecretToRef + GLWEInfos + GetDistribution, { - 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()); + let brk_infos: &BlindRotationKeyLayout = &res.brk_infos(); + let atk_infos: &GLWEAutomorphismKeyLayout = &res.atk_infos(); + let tsk_infos: &GLWETensorKeyLayout = &res.tsk_infos(); - let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); - let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); + 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 mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>>> = HashMap::new(); - let gal_els: Vec<i64> = GLWE::trace_galois_elements(module); - gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey<Vec<u8>> = 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); - }); + 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<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch); + let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(self, brk_infos.rank()); - let mut brk: BlindRotationKey<Vec<u8>, BRA> = BlindRotationKey::<Vec<u8>, BRA>::alloc(&brk_infos); - brk.encrypt_sk( - module, + res.brk.encrypt_sk( + self, &sk_glwe_prepared, sk_lwe, source_xa,

@@ -140,27 +155,15 @@ source_xe,

scratch, ); - let mut tsk: TensorKey<Vec<u8>> = 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<D: Data, BRA: BlindRotationAlgo, B: Backend> { - pub(crate) brk: BlindRotationKeyPrepared<D, BRA, B>, - pub(crate) tsk: TensorKeyPrepared<Vec<u8>, B>, - pub(crate) atk: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, B>>, -} - -impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared<D, BRA, B> { - fn atk_infos(&self) -> AutomorphismKeyLayout { +impl<D: DataRef, BRA: BlindRotationAlgo> CircuitBootstrappingKeyInfos for CircuitBootstrappingKey<D, BRA> { + 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 @@ rank: self.brk.rank(),

} } - 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 @@ rank: self.tsk.rank(),

} } } - -impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> PrepareAlloc<B, CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B>> - for CircuitBootstrappingKey<D, BRA> -where - Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>, - BlindRotationKey<D, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, - TensorKey<D>: PrepareAlloc<B, TensorKeyPrepared<Vec<u8>, B>>, - AutomorphismKey<D>: PrepareAlloc<B, GLWEAutomorphismKeyPrepared<Vec<u8>, B>>, -{ - fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> { - let brk: BlindRotationKeyPrepared<Vec<u8>, BRA, B> = self.brk.prepare_alloc(module, scratch); - let tsk: TensorKeyPrepared<Vec<u8>, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); - for (key, value) in &self.atk { - atk.insert(*key, value.prepare_alloc(module, scratch)); - } - CircuitBootstrappingKeyPrepared { brk, tsk, atk } - } -}
A 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<D: Data, BRA: BlindRotationAlgo> { + pub(crate) brk: BlindRotationKeyCompressed<D, BRA>, + pub(crate) tsk: GLWETensorKeyCompressed<Vec<u8>>, + pub(crate) atk: HashMap<i64, GLWEAutomorphismKeyCompressed<Vec<u8>>>, +}
A 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<BRA: BlindRotationAlgo, BE: Backend> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, BE> { + pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + M: CircuitBootstrappingKeyPreparedFactory<BRA, BE>, + { + module.circuit_bootstrapping_key_prepared_alloc_from_infos(infos) + } +} + +impl<D: DataMut, BRA: BlindRotationAlgo, BE: Backend> CircuitBootstrappingKeyPrepared<D, BRA, BE> { + pub fn prepare<DR, M>(&mut self, module: &M, other: &CircuitBootstrappingKey<DR, BRA>, scratch: &mut Scratch<BE>) + where + DR: DataRef, + M: CircuitBootstrappingKeyPreparedFactory<BRA, BE>, + { + module.circuit_bootstrapping_key_prepare(self, other, scratch); + } +} + +impl<BE: Backend, BRA: BlindRotationAlgo> CircuitBootstrappingKeyPreparedFactory<BRA, BE> for Module<BE> where + Self: Sized + + BlindRotationKeyPreparedFactory<BRA, BE> + + GLWETensorKeyPreparedFactory<BE> + + GLWEAutomorphismKeyPreparedFactory<BE> +{ +} + +pub trait CircuitBootstrappingKeyPreparedFactory<BRA: BlindRotationAlgo, BE: Backend> +where + Self: Sized + + BlindRotationKeyPreparedFactory<BRA, BE> + + GLWETensorKeyPreparedFactory<BE> + + GLWEAutomorphismKeyPreparedFactory<BE>, +{ + fn circuit_bootstrapping_key_prepared_alloc_from_infos<A>( + &self, + infos: &A, + ) -> CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, BE> + where + A: CircuitBootstrappingKeyInfos, + { + let atk_infos: &GLWEAutomorphismKeyLayout = &infos.atk_infos(); + let gal_els: Vec<i64> = 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<DM, DR>( + &self, + res: &mut CircuitBootstrappingKeyPrepared<DM, BRA, BE>, + other: &CircuitBootstrappingKey<DR, BRA>, + scratch: &mut Scratch<BE>, + ) 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<D: Data, BRA: BlindRotationAlgo, B: Backend> { + pub(crate) brk: BlindRotationKeyPrepared<D, BRA, B>, + pub(crate) tsk: GLWETensorKeyPrepared<Vec<u8>, B>, + pub(crate) atk: HashMap<i64, GLWEAutomorphismKeyPrepared<Vec<u8>, B>>, +} + +impl<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared<D, BRA, B> { + 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(), + } + } +}
M poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs → 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<B: Backend> { - fn execute_to_constant<DM: DataMut, DR: DataRef>( - &self, - module: &Module<B>, - res: &mut GGSW<DM>, - lwe: &LWE<DR>, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch<B>, - ); - - #[allow(clippy::too_many_arguments)] - fn execute_to_exponent<DM: DataMut, DR: DataRef>( - &self, - module: &Module<B>, - log_gap_out: usize, - res: &mut GGSW<DM>, - lwe: &LWE<DR>, - log_domain: usize, - extension_factor: usize, - scratch: &mut Scratch<B>, - ); -} +// pub use key_compressed::*; +pub use key_prepared::*;
M poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs → 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<B, BRA: BlindRotationAlgo>(module: &Module<B>) +pub fn test_circuit_bootstrapping_to_exponent<BE: Backend, M, BRA: BlindRotationAlgo>(module: &M) where - Module<B>: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace<B> - + VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA<B> - + SvpApplyDftToDft<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc<B> - + VmpPrepare<B> - + SvpPrepare<B> - + SvpPPolAlloc<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + SvpPPolBytesOf - + VecZnxRotateInplace<B> - + VecZnxBigAutomorphismInplace<B> - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace<B>, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + TakeVecZnxDftImpl<B> - + ScratchAvailableImpl<B> - + TakeVecZnxImpl<B> - + TakeScalarZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B> - + TakeVecZnxDftSliceImpl<B> - + TakeMatZnxImpl<B> - + TakeVecZnxSliceImpl<B> - + TakeSliceImpl<B>, - BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, - BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>, - BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>, + M: ModuleN + + GLWESecretPreparedFactory<BE> + + GLWEExternalProduct<BE> + + GLWEDecrypt<BE> + + LWEEncryptSk<BE> + + CircuitBootstrappingKeyEncryptSk<BRA, BE> + + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + + CirtuitBootstrappingExecute<BRA, BE> + + GGSWPreparedFactory<BE> + + GGSWNoise<BE> + + GLWEEncryptSk<BE> + + VecZnxRotateInplace<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { let n_glwe: usize = module.n(); let base2k: usize = 17;

@@ -141,7 +82,7 @@ k: k_brk.into(),

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

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 @@ dsize: Dsize(1),

rank: rank.into(), }; - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned<BE> = 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 @@

let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1;

@@ -193,22 +135,27 @@ let mut ct_lwe: LWE<Vec<u8>> = 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(); - let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey<Vec<u8>, 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<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; - let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, 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 @@ &mut source_xe,

scratch.borrow(), ); - let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared<Vec<u8>, 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 @@ pt_want[data as usize * (1 << log_gap_out)] = pt_glwe.data.at(0, 0)[0];

assert_eq!(pt_res.data.at(0, 0), pt_want); } -pub fn test_circuit_bootstrapping_to_constant<B, BRA: BlindRotationAlgo>(module: &Module<B>) +pub fn test_circuit_bootstrapping_to_constant<BE: Backend, M, BRA: BlindRotationAlgo>(module: &M) where - Module<B>: VecZnxFillUniform - + VecZnxAddNormal - + VecZnxNormalizeInplace<B> - + VecZnxDftBytesOf - + VecZnxBigNormalize<B> - + VecZnxDftApply<B> - + SvpApplyDftToDftInplace<B> - + VecZnxIdftApplyConsume<B> - + VecZnxNormalizeTmpBytes - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalize<B> - + VecZnxSub - + VecZnxAddScalarInplace - + VecZnxAutomorphism - + VecZnxSwitchRing - + VecZnxBigBytesOf - + VecZnxIdftApplyTmpA<B> - + SvpApplyDftToDft<B> - + VecZnxBigAddInplace<B> - + VecZnxBigAddSmallInplace<B> - + VecZnxBigAlloc<B> - + VecZnxDftAlloc<B> - + VecZnxBigNormalizeTmpBytes - + VmpPMatAlloc<B> - + VmpPrepare<B> - + SvpPrepare<B> - + SvpPPolAlloc<B> - + VmpApplyDftToDftTmpBytes - + VmpApplyDftToDft<B> - + VmpApplyDftToDftAdd<B> - + SvpPPolBytesOf - + VecZnxRotateInplace<B> - + VecZnxBigAutomorphismInplace<B> - + VecZnxRotateInplaceTmpBytes - + VecZnxRshInplace<B> - + VecZnxDftCopy<B> - + VecZnxNegateInplace - + VecZnxCopy - + VecZnxAutomorphismInplace<B> - + VecZnxBigSubSmallNegateInplace<B> - + VecZnxBigBytesOf - + VecZnxDftAddInplace<B> - + VecZnxRotate - + ZnFillUniform - + ZnAddNormal - + ZnNormalizeInplace<B>, - B: Backend - + ScratchOwnedAllocImpl<B> - + ScratchOwnedBorrowImpl<B> - + TakeVecZnxDftImpl<B> - + ScratchAvailableImpl<B> - + TakeVecZnxImpl<B> - + TakeScalarZnxImpl<B> - + TakeSvpPPolImpl<B> - + TakeVecZnxBigImpl<B> - + TakeVecZnxDftSliceImpl<B> - + TakeMatZnxImpl<B> - + TakeVecZnxSliceImpl<B> - + TakeSliceImpl<B>, - BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<B, BlindRotationKeyPrepared<Vec<u8>, BRA, B>>, - BlindRotationKeyPrepared<Vec<u8>, BRA, B>: BlincRotationExecute<B>, - BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<B>, + M: ModuleN + + GLWESecretPreparedFactory<BE> + + GLWEExternalProduct<BE> + + GLWEDecrypt<BE> + + LWEEncryptSk<BE> + + CircuitBootstrappingKeyEncryptSk<BRA, BE> + + CircuitBootstrappingKeyPreparedFactory<BRA, BE> + + CirtuitBootstrappingExecute<BRA, BE> + + GGSWPreparedFactory<BE> + + GGSWNoise<BE> + + GLWEEncryptSk<BE> + + VecZnxRotateInplace<BE>, + BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyFactory<BRA>, // TODO find a way to remove this bound or move it to CBT KEY + ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>, + Scratch<BE>: ScratchTakeCore<BE>, { let n_glwe: usize = module.n(); let base2k: usize = 14;

@@ -363,7 +263,7 @@ k: k_brk.into(),

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

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 @@ dsize: Dsize(1),

rank: rank.into(), }; - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(1 << 23); + let mut scratch: ScratchOwned<BE> = 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 @@

let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); + let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into()); + sk_glwe_prepared.prepare(module, &sk_glwe); let data: i64 = 1;

@@ -415,20 +316,25 @@ let mut ct_lwe: LWE<Vec<u8>> = 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(); - let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk( + let mut cbt_key: CircuitBootstrappingKey<Vec<u8>, 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<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); - let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); + let mut cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, 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 @@ &mut source_xe,

scratch.borrow(), ); - let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow()); + let mut res_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &res); + res_prepared.prepare(module, &res, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
A 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<FFT64Ref> = Module::<FFT64Ref>::new(256); + test_circuit_bootstrapping_to_constant::<FFT64Ref, _, CGGI>(&module); +} + +#[test] +fn test_to_exponent_cggi() { + let module: Module<FFT64Ref> = Module::<FFT64Ref>::new(256); + test_circuit_bootstrapping_to_exponent::<FFT64Ref, _, CGGI>(&module); +}
D poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/implementation/cpu_spqlios/fft64.rs

@@ -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<FFT64Spqlios> = Module::<FFT64Spqlios>::new(256); - test_circuit_bootstrapping_to_constant::<FFT64Spqlios, CGGI>(&module); -} - -#[test] -fn test_to_exponent() { - let module: Module<FFT64Spqlios> = Module::<FFT64Spqlios>::new(256); - test_circuit_bootstrapping_to_exponent::<FFT64Spqlios, CGGI>(&module); -}
M poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/mod.rs

@@ -1,3 +1,3 @@

pub mod circuit_bootstrapping; -mod implementation; +mod fft64;
M poulpy-schemes/src/tfhe/mod.rs → 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;