From 201a1f64eb3d0891138248c0eed61e070354e573 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Thu, 30 Oct 2025 10:42:28 +0100 Subject: [PATCH] Replace hasmap of glweautomorphismkeys by helper trait, enabling not having to pass, for example, but full CBT key for ops that do not require it --- poulpy-core/src/glwe_packer.rs | 39 +++++---- poulpy-core/src/glwe_packing.rs | 29 +++---- poulpy-core/src/glwe_trace.rs | 80 +++++++++---------- .../compressed/glwe_automorphism_key.rs | 6 +- .../src/layouts/glwe_automorphism_key.rs | 10 ++- .../layouts/prepared/glwe_automorphism_key.rs | 22 ++++- .../tests/test_suite/encryption/gglwe_atk.rs | 4 +- .../bdd_arithmetic/ciphertexts/fhe_uint.rs | 24 +++--- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 13 +++ .../src/tfhe/circuit_bootstrapping/circuit.rs | 11 ++- .../circuit_bootstrapping/key_prepared.rs | 22 +++-- 11 files changed, 148 insertions(+), 112 deletions(-) diff --git a/poulpy-core/src/glwe_packer.rs b/poulpy-core/src/glwe_packer.rs index da8c93e..6ebbdea 100644 --- a/poulpy-core/src/glwe_packer.rs +++ b/poulpy-core/src/glwe_packer.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use poulpy_hal::{ api::ModuleLogN, layouts::{Backend, GaloisElement, Module, Scratch}, @@ -8,7 +6,10 @@ use poulpy_hal::{ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, glwe_trace::GLWETrace, - layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos}, + layouts::{ + GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement, + LWEInfos, + }, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -110,12 +111,13 @@ impl GLWEPacker { /// * `res`: space to append fully packed ciphertext. Only when the number /// of packed ciphertexts reaches N/2^log_batch is a result written. /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. - /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. + /// * `auto_keys`: an implementation of [GLWEAutomorphismKeyHelper], containing [GLWEAutomorphismKeyPrepared] with index of [Self::galois_elements]. /// * `scratch`: scratch space of size at least [Self::tmp_bytes]. - pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &HashMap, scratch: &mut Scratch) + pub fn add(&mut self, module: &M, a: Option<&A>, auto_keys: &H, scratch: &mut Scratch) where A: GLWEToRef + GLWEInfos, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, M: GLWEPackerOps, Scratch: ScratchTakeCore, { @@ -187,28 +189,23 @@ where + GLWEAdd + GLWENormalize, { - fn packer_add( - &self, - packer: &mut GLWEPacker, - a: Option<&A>, - i: usize, - auto_keys: &HashMap, - scratch: &mut Scratch, - ) where + fn packer_add(&self, packer: &mut GLWEPacker, a: Option<&A>, i: usize, auto_keys: &H, scratch: &mut Scratch) + where A: GLWEToRef + GLWEInfos, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, { pack_core(self, a, &mut packer.accumulators, i, auto_keys, scratch) } } -fn pack_core( +fn pack_core( module: &M, a: Option<&A>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap, + auto_keys: &H, scratch: &mut Scratch, ) where A: GLWEToRef + GLWEInfos, @@ -229,6 +226,7 @@ fn pack_core( + GLWEAdd + GLWENormalize, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, { let log_n: usize = module.log_n(); @@ -280,12 +278,12 @@ fn pack_core( } } -fn combine( +fn combine( module: &M, acc: &mut Accumulator, b: Option<&B>, i: usize, - auto_keys: &HashMap, + auto_keys: &H, scratch: &mut Scratch, ) where B: GLWEToRef + GLWEInfos, @@ -307,6 +305,7 @@ fn combine( + GLWEAdd + GLWENormalize, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, { let log_n: usize = acc.data.n().log2(); @@ -348,7 +347,7 @@ fn combine( module.glwe_normalize_inplace(&mut tmp_b, scratch_1); // tmp_b = phi(a * X^-t - b) - if let Some(auto_key) = auto_keys.get(&gal_el) { + if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) { module.glwe_automorphism_inplace(&mut tmp_b, auto_key, scratch_1); } else { panic!("auto_key[{gal_el}] not found"); @@ -365,7 +364,7 @@ fn combine( } else { module.glwe_rsh(1, a, scratch); // a = a + phi(a) - if let Some(auto_key) = auto_keys.get(&gal_el) { + if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) { module.glwe_automorphism_add_inplace(a, auto_key, scratch); } else { panic!("auto_key[{gal_el}] not found"); @@ -377,7 +376,7 @@ fn combine( module.glwe_rsh(1, &mut tmp_b, scratch_1); // a = (b* X^t - phi(b* X^t)) - if let Some(auto_key) = auto_keys.get(&gal_el) { + if let Some(auto_key) = auto_keys.get_automorphism_key(gal_el) { module.glwe_automorphism_sub_negate(a, &tmp_b, auto_key, scratch_1); } else { panic!("auto_key[{gal_el}] not found"); diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 6debd0d..e24ddb0 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -7,20 +7,16 @@ use poulpy_hal::{ use crate::{ GLWEAdd, GLWEAutomorphism, GLWECopy, GLWENormalize, GLWERotate, GLWEShift, GLWESub, ScratchTakeCore, - layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement}, + layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEToMut, GLWEToRef, GetGaloisElement}, }; pub trait GLWEPacking { /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] - fn glwe_pack( - &self, - cts: &mut HashMap, - log_gap_out: usize, - keys: &HashMap, - scratch: &mut Scratch, - ) where + fn glwe_pack(&self, cts: &mut HashMap, log_gap_out: usize, keys: &H, scratch: &mut Scratch) + where R: GLWEToMut + GLWEToRef + GLWEInfos, - K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos; + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper; } impl GLWEPacking for Module @@ -38,15 +34,11 @@ where { /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)] /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] - fn glwe_pack( - &self, - cts: &mut HashMap, - log_gap_out: usize, - keys: &HashMap, - scratch: &mut Scratch, - ) where + fn glwe_pack(&self, cts: &mut HashMap, log_gap_out: usize, keys: &H, scratch: &mut Scratch) + where R: GLWEToMut + GLWEToRef + GLWEInfos, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, { #[cfg(debug_assertions)] { @@ -59,9 +51,10 @@ where let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); let key: &K = if i == 0 { - keys.get(&-1).unwrap() + keys.get_automorphism_key(-1).unwrap() } else { - keys.get(&self.galois_element(1 << (i - 1))).unwrap() + keys.get_automorphism_key(self.galois_element(1 << (i - 1))) + .unwrap() }; for j in 0..t { diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 0ba7b81..1240295 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use poulpy_hal::{ api::{ModuleLogN, VecZnxNormalize, VecZnxNormalizeTmpBytes}, layouts::{Backend, CyclotomicOrder, DataMut, GaloisElement, Module, Scratch, VecZnx, galois_element}, @@ -8,7 +6,8 @@ use poulpy_hal::{ use crate::{ GLWEAutomorphism, GLWECopy, GLWEShift, ScratchTakeCore, layouts::{ - Base2K, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, + GGLWEInfos, GGLWELayout, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWELayout, GLWEToMut, + GLWEToRef, GetGaloisElement, LWEInfos, }, }; @@ -32,32 +31,34 @@ impl GLWE> { } impl GLWE { - pub fn trace( + pub fn trace( &mut self, module: &M, start: usize, end: usize, a: &A, - keys: &HashMap, + keys: &H, scratch: &mut Scratch, ) where A: GLWEToRef, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, M: GLWETrace, { module.glwe_trace(self, start, end, a, keys, scratch); } - pub fn trace_inplace( + pub fn trace_inplace( &mut self, module: &M, start: usize, end: usize, - keys: &HashMap, + keys: &H, scratch: &mut Scratch, ) where K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, Scratch: ScratchTakeCore, M: GLWETrace, { @@ -113,52 +114,48 @@ where trace } - fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &HashMap, scratch: &mut Scratch) + fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, A: GLWEToRef, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, { self.glwe_copy(res, a); self.glwe_trace_inplace(res, start, end, keys, scratch); } - fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) + fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper, { let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); - let basek_ksk: Base2K = keys.get(keys.keys().next().unwrap()).unwrap().base2k(); + let ksk_infos: &GGLWELayout = &keys.automorphism_key_infos(); - #[cfg(debug_assertions)] - { - assert_eq!(res.n(), self.n() as u32); - assert!(start < end); - assert!(end <= self.log_n()); - for key in keys.values() { - assert_eq!(key.n(), self.n() as u32); - assert_eq!(key.base2k(), basek_ksk); - assert_eq!(key.rank_in(), res.rank()); - assert_eq!(key.rank_out(), res.rank()); - } - } + assert_eq!(res.n(), self.n() as u32); + assert_eq!(ksk_infos.n(), self.n() as u32); + assert!(start < end); + assert!(end <= self.log_n()); + assert_eq!(ksk_infos.rank_in(), res.rank()); + assert_eq!(ksk_infos.rank_out(), res.rank()); - if res.base2k() != basek_ksk { + if res.base2k() != ksk_infos.base2k() { let (mut self_conv, scratch_1) = scratch.take_glwe(&GLWELayout { n: self.n().into(), - base2k: basek_ksk, + base2k: ksk_infos.base2k(), k: res.k(), rank: res.rank(), }); for j in 0..(res.rank() + 1).into() { self.vec_znx_normalize( - basek_ksk.into(), + ksk_infos.base2k().into(), &mut self_conv.data, j, - basek_ksk.into(), + res.base2k().into(), res.data(), j, scratch_1, @@ -174,7 +171,7 @@ where self.galois_element(1 << (i - 1)) }; - if let Some(key) = keys.get(&p) { + if let Some(key) = keys.get_automorphism_key(p) { self.glwe_automorphism_add_inplace(&mut self_conv, key, scratch_1); } else { panic!("keys[{p}] is empty") @@ -186,7 +183,7 @@ where res.base2k().into(), res.data_mut(), j, - basek_ksk.into(), + ksk_infos.base2k().into(), &self_conv.data, j, scratch_1, @@ -204,7 +201,7 @@ where self.galois_element(1 << (i - 1)) }; - if let Some(key) = keys.get(&p) { + if let Some(key) = keys.get_automorphism_key(p) { self.glwe_automorphism_add_inplace(res, key, scratch); } else { panic!("keys[{p}] is empty") @@ -223,21 +220,16 @@ pub trait GLWETrace { A: GLWEInfos, K: GGLWEInfos; - fn glwe_trace( - &self, - res: &mut R, - start: usize, - end: usize, - a: &A, - keys: &HashMap, - scratch: &mut Scratch, - ) where - R: GLWEToMut, - A: GLWEToRef, - K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos; - - fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &HashMap, scratch: &mut Scratch) + fn glwe_trace(&self, res: &mut R, start: usize, end: usize, a: &A, keys: &H, scratch: &mut Scratch) where R: GLWEToMut, - K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos; + A: GLWEToRef, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper; + + fn glwe_trace_inplace(&self, res: &mut R, start: usize, end: usize, keys: &H, scratch: &mut Scratch) + where + R: GLWEToMut, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, + H: GLWEAutomorphismKeyHelper; } diff --git a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs index 3e0a4b5..7d64f9a 100644 --- a/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/compressed/glwe_automorphism_key.rs @@ -138,7 +138,7 @@ impl WriterTo for GLWEAutomorphismKeyCompressed { } } -pub trait AutomorphismKeyDecompress +pub trait GLWEAutomorphismKeyDecompress where Self: GGLWEDecompress, { @@ -152,7 +152,7 @@ where } } -impl AutomorphismKeyDecompress for Module where Self: GLWEDecompress {} +impl GLWEAutomorphismKeyDecompress for Module where Self: GLWEDecompress {} impl GLWEAutomorphismKey where @@ -161,7 +161,7 @@ where pub fn decompress(&mut self, module: &M, other: &O) where O: GGLWECompressedToRef + GetGaloisElement, - M: AutomorphismKeyDecompress, + M: GLWEAutomorphismKeyDecompress, { module.decompress_automorphism_key(self, other); } diff --git a/poulpy-core/src/layouts/glwe_automorphism_key.rs b/poulpy-core/src/layouts/glwe_automorphism_key.rs index b04ca4d..ce9e527 100644 --- a/poulpy-core/src/layouts/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/glwe_automorphism_key.rs @@ -1,15 +1,21 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWELayout, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, + TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; +pub trait GLWEAutomorphismKeyHelper { + fn get_automorphism_key(&self, k: i64) -> Option<&K>; + fn automorphism_key_infos(&self) -> GGLWELayout; +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GLWEAutomorphismKeyLayout { pub n: Degree, diff --git a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs index adf54eb..7cc5b6e 100644 --- a/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs +++ b/poulpy-core/src/layouts/prepared/glwe_automorphism_key.rs @@ -1,10 +1,28 @@ +use std::collections::HashMap; + use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef, - GGLWEToRef, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWELayout, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, + GGLWEPreparedToRef, GGLWEToRef, GLWEAutomorphismKeyHelper, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, + TorusPrecision, }; +impl GLWEAutomorphismKeyHelper for HashMap +where + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, +{ + fn get_automorphism_key(&self, k: i64) -> Option<&K> { + self.get(&k) + } + + fn automorphism_key_infos(&self) -> GGLWELayout { + self.get(self.keys().next().unwrap()) + .unwrap() + .gglwe_layout() + } +} + #[derive(PartialEq, Eq)] pub struct GLWEAutomorphismKeyPrepared { pub(crate) key: GGLWEPrepared, diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index 9363539..33d3bd7 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -9,7 +9,7 @@ use crate::{ GLWESwitchingKeyEncryptSk, ScratchTakeCore, encryption::SIGMA, layouts::{ - AutomorphismKeyDecompress, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, + GLWEAutomorphismKey, GLWEAutomorphismKeyDecompress, GLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKeyDecompress, compressed::GLWEAutomorphismKeyCompressed, prepared::GLWESecretPrepared, }, @@ -97,7 +97,7 @@ where + GLWESecretPreparedFactory + GLWESwitchingKeyEncryptSk + GLWESwitchingKeyCompressedEncryptSk - + AutomorphismKeyDecompress + + GLWEAutomorphismKeyDecompress + VecZnxAutomorphism + VecZnxFillUniform + GGLWENoise, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs index f4cdd83..aa6ce9f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/fhe_uint.rs @@ -1,8 +1,8 @@ use poulpy_core::{ GLWEAdd, GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, GLWESub, GLWETrace, LWEFromGLWE, ScratchTakeCore, layouts::{ - Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToMut, - GLWEToRef, LWEInfos, LWEToMut, Rank, TorusPrecision, + Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEAutomorphismKeyHelper, GLWEInfos, GLWEPlaintextLayout, + GLWESecretPreparedToRef, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, LWEToMut, Rank, TorusPrecision, }, }; use poulpy_hal::{ @@ -171,20 +171,20 @@ impl FheUint { } #[allow(clippy::too_many_arguments)] - pub fn splice_u16( + pub fn splice_u16( &mut self, module: &M, dst: usize, src: usize, a: &A, b: &B, - keys: &BDDKeyPrepared, + keys: &H, scratch: &mut Scratch, ) where - D0: DataRef, A: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos, - BRA: BlindRotationAlgo, + H: GLWEAutomorphismKeyHelper, + K: GGLWEPreparedToRef + GGLWEInfos + GetGaloisElement, M: ModuleLogN + GLWERotate + GLWETrace + GLWESub + GLWEAdd + GLWECopy, Scratch: ScratchTakeBDD, { @@ -206,20 +206,20 @@ impl FheUint { #[allow(clippy::too_many_arguments)] // Store on the receiver a where the byte_a-th byte of a has been replaced by byte_src2 of src2. - pub fn splice_u8( + pub fn splice_u8( &mut self, module: &M, dst: usize, src: usize, a: &A, b: &B, - keys: &BDDKeyPrepared, + keys: &H, scratch: &mut Scratch, ) where - D0: DataRef, A: GLWEToRef + GLWEInfos, B: GLWEToRef + GLWEInfos, - BRA: BlindRotationAlgo, + H: GLWEAutomorphismKeyHelper, + K: GGLWEPreparedToRef + GGLWEInfos + GetGaloisElement, M: ModuleLogN + GLWERotate + GLWETrace + GLWESub + GLWEAdd + GLWECopy, Scratch: ScratchTakeBDD, { @@ -241,7 +241,7 @@ impl FheUint { trace_start, module.log_n(), self, - &keys.cbt.atk, + keys, scratch_1, ); @@ -263,7 +263,7 @@ impl FheUint { &mut tmp_fhe_uint_byte, trace_start, module.log_n(), - &keys.cbt.atk, + keys, scratch_1, ); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index c11c496..31a7f3f 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -8,6 +8,7 @@ use crate::tfhe::{ }, }; +use poulpy_core::layouts::{GLWEAutomorphismKeyHelper, GLWEAutomorphismKeyPrepared}; use poulpy_core::{ GLWEToLWESwitchingKeyEncryptSk, GetDistribution, LWEFromGLWE, ScratchTakeCore, layouts::{ @@ -134,6 +135,18 @@ where pub(crate) ks: GLWEToLWEKeyPrepared, } +impl GLWEAutomorphismKeyHelper, BE> + for BDDKeyPrepared +{ + fn automorphism_key_infos(&self) -> poulpy_core::layouts::GGLWELayout { + self.cbt.automorphism_key_infos() + } + + fn get_automorphism_key(&self, k: i64) -> Option<&GLWEAutomorphismKeyPrepared> { + self.cbt.get_automorphism_key(k) + } +} + pub trait BDDKeyPreparedFactory where Self: Sized + CircuitBootstrappingKeyPreparedFactory + GLWEToLWEKeyPreparedFactory, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index a5adc52..a4691fa 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -8,11 +8,12 @@ use poulpy_hal::{ use poulpy_core::{ GGSWFromGGLWE, GLWEDecrypt, GLWEPacking, GLWERotate, GLWETrace, ScratchTakeCore, layouts::{ - Dsize, GGLWELayout, GGSWInfos, GGSWToMut, GLWEInfos, GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, LWEInfos, LWEToRef, + Dsize, GGLWEInfos, GGLWELayout, GGLWEPreparedToRef, GGSWInfos, GGSWToMut, GLWEAutomorphismKeyHelper, GLWEInfos, + GLWESecretPreparedFactory, GLWEToMut, GLWEToRef, GetGaloisElement, LWEInfos, LWEToRef, }, }; -use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::GLWEAutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE}; use crate::tfhe::{ blind_rotation::{ @@ -323,18 +324,20 @@ pub fn circuit_bootstrap_core( } #[allow(clippy::too_many_arguments)] -fn post_process( +fn post_process( module: &M, res: &mut R, a: &A, log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, BE>>, + auto_keys: &H, scratch: &mut Scratch, ) where R: GLWEToMut, A: GLWEToRef, + H: GLWEAutomorphismKeyHelper, + K: GGLWEPreparedToRef + GetGaloisElement + GGLWEInfos, M: ModuleLogN + GLWETrace + GLWEPacking + GLWERotate, Scratch: ScratchTakeCore, { diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs index c611846..aff3016 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key_prepared.rs @@ -1,8 +1,8 @@ use poulpy_core::{ layouts::{ - GGLWEInfos, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedFactory, GGSWInfos, GLWEAutomorphismKeyLayout, - GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory, LWEInfos, - prepared::GLWEAutomorphismKeyPrepared, + GGLWEInfos, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedFactory, GGSWInfos, GLWEAutomorphismKeyHelper, + GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWEInfos, GLWETensorKeyLayout, + GLWETensorKeyPreparedFactory, LWEInfos, prepared::GLWEAutomorphismKeyPrepared, }, trace_galois_elements, }; @@ -105,8 +105,20 @@ where pub struct CircuitBootstrappingKeyPrepared { pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: GGLWEToGGSWKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, + pub(crate) tsk: GGLWEToGGSWKeyPrepared, + pub(crate) atk: HashMap>, +} + +impl GLWEAutomorphismKeyHelper, BE> + for CircuitBootstrappingKeyPrepared +{ + fn get_automorphism_key(&self, k: i64) -> Option<&GLWEAutomorphismKeyPrepared> { + self.atk.get_automorphism_key(k) + } + + fn automorphism_key_infos(&self) -> poulpy_core::layouts::GGLWELayout { + self.atk.automorphism_key_infos() + } } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared {