diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 360a9f4..5027f3b 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/poulpy-core/benches/external_product_glwe_fft64.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box; @@ -62,13 +62,13 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); let mut ct_glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) + GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); @@ -98,7 +98,7 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); @@ -163,12 +163,12 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ct_ggsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(&module, &ggsw_layout) + GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout), ); @@ -198,7 +198,7 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ggsw_prepared: GGSWCiphertextPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); + let ggsw_prepared: GGSWPrepared, FFT64Spqlios> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { let scratch_borrow = scratch.borrow(); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 2da2032..8316949 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/poulpy-core/benches/keyswitch_glwe_fft64.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWESwitchingKey, GGLWESwitchingKeyLayout, - GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWECiphertextLayout, GLWESecret, + GLWESwitchingKey, GLWESwitchingKeyLayout, Rank, TorusPrecision, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration}; @@ -39,7 +39,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct_in.div_ceil(p.base2k.0 * dsize.0).into(); - let gglwe_atk_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let gglwe_atk_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n, base2k, k: k_gglwe, @@ -62,12 +62,12 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&gglwe_atk_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc(&gglwe_atk_layout); let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) + GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) | GLWECiphertext::keyswitch_scratch_space( &module, @@ -102,7 +102,7 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWEAutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: AutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); @@ -157,7 +157,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let dnum: Dnum = p.k_ct.div_ceil(p.base2k).into(); - let gglwe_layout: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_layout: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n, base2k, k: k_ksk, @@ -174,11 +174,11 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_layout); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) + GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout) | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout) | GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout), ); @@ -211,7 +211,7 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, FFT64Spqlios> = ksk.prepare_alloc(&module, scratch.borrow()); move || { ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 9b08e68..57324e3 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::AutomorphismKeyPrepared}; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -36,16 +36,16 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWEAutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) + AutomorphismKey::automorphism_scratch_space(module, out_infos, out_infos, key_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn automorphism( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWEAutomorphismKeyPrepared, + lhs: &AutomorphismKey, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -133,7 +133,7 @@ impl GGLWEAutomorphismKey { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index eef3082..09e2f94 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -9,11 +9,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared}, + GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, + prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, }; -impl GGSWCiphertext> { +impl GGSW> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -40,7 +40,7 @@ impl GGSWCiphertext> { &in_infos.glwe_layout(), key_infos, ); - let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); ci_dft + (ks_internal | expand) } @@ -60,17 +60,17 @@ impl GGSWCiphertext> { + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSWCiphertext::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) + GGSW::automorphism_scratch_space(module, out_infos, out_infos, key_infos, tsk_infos) } } -impl GGSWCiphertext { +impl GGSW { pub fn automorphism( &mut self, module: &Module, - lhs: &GGSWCiphertext, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + lhs: &GGSW, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -121,7 +121,7 @@ impl GGSWCiphertext { self.rank(), tensor_key.rank_out() ); - assert!(scratch.available() >= GGSWCiphertext::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) + assert!(scratch.available() >= GGSW::automorphism_scratch_space(module, self, lhs, auto_key, tensor_key)) }; // Keyswitch the j-th row of the col 0 @@ -137,8 +137,8 @@ impl GGSWCiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - auto_key: &GGLWEAutomorphismKeyPrepared, - tensor_key: &GGLWETensorKeyPrepared, + auto_key: &AutomorphismKeyPrepared, + tensor_key: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index 79fcb12..f94b9bc 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; impl GLWECiphertext> { pub fn automorphism_scratch_space( @@ -41,7 +41,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -67,7 +67,7 @@ impl GLWECiphertext { pub fn automorphism_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -94,7 +94,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -135,7 +135,7 @@ impl GLWECiphertext { pub fn automorphism_add_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -177,7 +177,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -219,7 +219,7 @@ impl GLWECiphertext { pub fn automorphism_sub_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -262,7 +262,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -304,7 +304,7 @@ impl GLWECiphertext { pub fn automorphism_sub_negate_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index f202223..9665c19 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -9,18 +9,19 @@ use crate::{ encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GGLWEAutomorphismKeyCompressed, GGLWEAutomorphismKeyCompressedToMut, GGLWEKeyCompressed}, + compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed}, }, }; -impl GGLWEAutomorphismKeyCompressed> { +impl AutomorphismKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { assert_eq!(module.n() as u32, infos.n()); - GGLWEKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) } } @@ -34,7 +35,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEAutomorphismKeyCompressedToMut, + R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef; } @@ -53,10 +54,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEAutomorphismKeyCompressedToMut, + R: AutomorphismKeyCompressedToMut, S: GLWESecretToRef, { - let res: &mut GGLWEAutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -65,10 +66,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), + scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}", scratch.available(), - GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) + AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res) ) } @@ -92,7 +93,7 @@ where } } -impl GGLWEAutomorphismKeyCompressed { +impl AutomorphismKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index c2b4de3..4939fe5 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ TakeGLWEPt, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGLWECiphertext, GGLWEInfos, LWEInfos, + GGLWE, GGLWEInfos, LWEInfos, compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -40,7 +40,7 @@ impl GGLWECiphertextCompressed> { A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GGLWECiphertext::encrypt_sk_scratch_space(module, infos) + GGLWE::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index e5b3716..91f1856 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -11,25 +11,25 @@ use crate::{ TakeGLWESecretPrepared, encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk, layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, - compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut}, + Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, + compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut}, prepared::GLWESecretPrepared, }, }; -impl GGLWEKeyCompressed> { +impl GLWESwitchingKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out()) } } -impl GGLWEKeyCompressed { +impl GLWESwitchingKeyCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -56,7 +56,7 @@ pub trait GGLWEKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEKeyCompressedToMut, + R: GLWESwitchingKeyCompressedToMut, SI: GLWESecretToRef, SO: GLWESecretToRef; } @@ -80,25 +80,25 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWEKeyCompressedToMut, + R: GLWESwitchingKeyCompressedToMut, SI: GLWESecretToRef, SO: GLWESecretToRef, { - let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref(); let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref(); #[cfg(debug_assertions)] { - use crate::layouts::GGLWESwitchingKey; + use crate::layouts::GLWESwitchingKey; assert!(sk_in.n().0 <= self.n() as u32); assert!(sk_out.n().0 <= self.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res), + scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(self, res) + GLWESwitchingKey::encrypt_sk_scratch_space(self, res) ) } diff --git a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs index be78f3e..65012d6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_tsk.rs @@ -11,20 +11,19 @@ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk, layouts::{ - GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, - compressed::{GGLWETensorKeyCompressed, GGLWETensorKeyCompressedToMut}, - prepared::Prepare, + GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey, + compressed::{TensorKeyCompressed, TensorKeyCompressedToMut}, }, }; -impl GGLWETensorKeyCompressed> { +impl TensorKeyCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GGLWETensorKey::encrypt_sk_scratch_space(module, infos) + TensorKey::encrypt_sk_scratch_space(module, infos) } } @@ -37,7 +36,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWETensorKeyCompressedToMut, + R: TensorKeyCompressedToMut, S: GLWESecretToRef; } @@ -59,10 +58,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWETensorKeyCompressedToMut, + R: TensorKeyCompressedToMut, S: GLWESecretToRef, { - let res: &mut GGLWETensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -119,7 +118,7 @@ where } } -impl GGLWETensorKeyCompressed { +impl TensorKeyCompressed { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index bdad6be..e9e5017 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -8,7 +8,7 @@ use crate::{ TakeGLWEPt, encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GLWEInfos, LWEInfos, compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, @@ -20,7 +20,7 @@ impl GGSWCiphertextCompressed> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 834f968..86f86cc 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -1,17 +1,15 @@ use poulpy_hal::{ - api::{ - ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, - VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, - VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, - }, + api::{VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes}, layouts::{Backend, DataMut, DataRef, Module, Scratch}, source::Source, }; use crate::{ - encryption::{SIGMA, glwe_ct::glwe_encrypt_sk_internal}, + encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared, + GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut}, + prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -25,6 +23,60 @@ impl GLWECiphertextCompressed> { } } +pub trait GLWECompressedEncryptSk { + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextCompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef; +} + +impl GLWECompressedEncryptSk for Module +where + Module: GLWEEncryptSkInternal, +{ + fn glwe_compressed_encrypt_sk( + &self, + res: &mut R, + pt: &P, + sk: &S, + seed_xa: [u8; 32], + source_xe: &mut Source, + scratch: &mut Scratch, + ) where + R: GLWECiphertextCompressedToMut, + P: GLWEPlaintextToRef, + S: GLWESecretPreparedToRef, + { + let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let mut source_xa: Source = Source::new(seed_xa); + let cols: usize = (res.rank() + 1).into(); + + self.glwe_encrypt_sk_internal( + res.base2k().into(), + res.k().into(), + &mut res.data, + cols, + true, + Some((pt, 0)), + sk, + &mut source_xa, + source_xe, + SIGMA, + scratch, + ); + + res.seed = seed_xa; + } +} + impl GLWECiphertextCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( @@ -36,65 +88,8 @@ impl GLWECiphertextCompressed { source_xe: &mut Source, scratch: &mut Scratch, ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, + Module: GLWECompressedEncryptSk, { - self.encrypt_sk_internal(module, Some((pt, 0)), sk, seed_xa, source_xe, scratch); - } - - #[allow(clippy::too_many_arguments)] - pub(crate) fn encrypt_sk_internal( - &mut self, - module: &Module, - pt: Option<(&GLWEPlaintext, usize)>, - sk: &GLWESecretPrepared, - seed_xa: [u8; 32], - source_xe: &mut Source, - scratch: &mut Scratch, - ) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, - { - let mut source_xa = Source::new(seed_xa); - let cols: usize = (self.rank() + 1).into(); - glwe_encrypt_sk_internal( - module, - self.base2k().into(), - self.k().into(), - &mut self.data, - cols, - true, - pt, - sk, - &mut source_xa, - source_xe, - SIGMA, - scratch, - ); - self.seed = seed_xa; + module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch); } } diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index 9065c2e..a269847 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -12,12 +12,11 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyToMut, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, GLWESecretToRef, - LWEInfos, + AutomorphismKey, AutomorphismKeyToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, }, }; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -28,7 +27,7 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) } pub fn encrypt_pk_scratch_space(module: &Module, _infos: &A) -> usize @@ -40,7 +39,7 @@ impl GGLWEAutomorphismKey> { _infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) + GLWESwitchingKey::encrypt_pk_scratch_space(module, _infos) } } @@ -54,13 +53,13 @@ pub trait GGLWEAutomorphismKeyEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - A: GGLWEAutomorphismKeyToMut, + A: AutomorphismKeyToMut, B: GLWESecretToRef; } -impl GGLWEAutomorphismKey +impl AutomorphismKey where - Self: GGLWEAutomorphismKeyToMut, + Self: AutomorphismKeyToMut, { pub fn encrypt_sk( &mut self, @@ -109,10 +108,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - A: GGLWEAutomorphismKeyToMut, + A: AutomorphismKeyToMut, B: GLWESecretToRef, { - let res: &mut GGLWEAutomorphismKey<&mut [u8]> = &mut res.to_mut(); + let res: &mut AutomorphismKey<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecret<&[u8]> = &sk.to_ref(); #[cfg(debug_assertions)] @@ -123,10 +122,10 @@ where assert_eq!(res.rank_out(), res.rank_in()); assert_eq!(sk.rank(), res.rank_out()); assert!( - scratch.available() >= GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res), + scratch.available() >= AutomorphismKey::encrypt_sk_scratch_space(self, res), "scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {:?}", scratch.available(), - GGLWEAutomorphismKey::encrypt_sk_scratch_space(self, res) + AutomorphismKey::encrypt_sk_scratch_space(self, res) ) } diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index 3928cf4..a7a98ce 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/poulpy-core/src/encryption/gglwe_ct.rs @@ -11,12 +11,12 @@ use crate::{ TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, + GGLWE, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGLWECiphertext> { +impl GGLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -68,7 +68,7 @@ where P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); #[cfg(debug_assertions)] @@ -93,12 +93,12 @@ where assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertext::encrypt_sk_scratch_space(self, res), + scratch.available() >= GGLWE::encrypt_sk_scratch_space(self, res), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}", scratch.available(), res.rank_out(), res.size(), - GGLWECiphertext::encrypt_sk_scratch_space(self, res) + GGLWE::encrypt_sk_scratch_space(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0, @@ -148,7 +148,7 @@ where } } -impl GGLWECiphertext { +impl GGLWE { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 0629bec..ce10a51 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -11,18 +11,16 @@ use poulpy_hal::{ use crate::{ TakeGLWESecretPrepared, - layouts::{ - Degree, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, GLWESecret, LWEInfos, prepared::GLWESecretPrepared, - }, + layouts::{Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, prepared::GLWESecretPrepared}, }; -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1)) + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into()) + GLWESecretPrepared::alloc_bytes(module, &infos.glwe_layout()) } @@ -31,11 +29,11 @@ impl GGLWESwitchingKey> { where A: GGLWEInfos, { - GGLWECiphertext::encrypt_pk_scratch_space(module, _infos) + GGLWE::encrypt_pk_scratch_space(module, _infos) } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -70,10 +68,10 @@ impl GGLWESwitchingKey { assert!(sk_in.n().0 <= module.n() as u32); assert!(sk_out.n().0 <= module.n() as u32); assert!( - scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(module, self), + scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(module, self), "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}", scratch.available(), - GGLWESwitchingKey::encrypt_sk_scratch_space(module, self) + GLWESwitchingKey::encrypt_sk_scratch_space(module, self) ) } diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 1946929..50a646a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -12,12 +12,11 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GLWEInfos, GLWESecret, LWEInfos, Rank, - prepared::{GLWESecretPrepared, Prepare}, + Degree, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, Rank, TensorKey, prepared::GLWESecretPrepared, }, }; -impl GGLWETensorKey> { +impl TensorKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -29,11 +28,11 @@ impl GGLWETensorKey> { + module.vec_znx_big_alloc_bytes(1, 1) + module.vec_znx_dft_alloc_bytes(1, 1) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } -impl GGLWETensorKey { +impl TensorKey { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index d9e35bd..ad713b5 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/poulpy-core/src/encryption/ggsw_ct.rs @@ -8,12 +8,12 @@ use crate::{ SIGMA, TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ - GGSWCiphertext, GGSWCiphertextToMut, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWECiphertext, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertext> { +impl GGSW> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, @@ -37,7 +37,7 @@ pub trait GGSWEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextToMut, + R: GGSWToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -56,11 +56,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextToMut, + R: GGSWToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - let res: &mut GGSWCiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); @@ -106,7 +106,7 @@ where } } -impl GGSWCiphertext { +impl GGSW { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index b65ce4e..aee218d 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -11,22 +11,24 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{GGLWEInfos, GGLWESwitchingKey, GLWESecret, GLWEToLWEKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared}, + layouts::{ + GGLWEInfos, GLWESecret, GLWESwitchingKey, GLWEToLWESwitchingKey, LWEInfos, LWESecret, Rank, prepared::GLWESecretPrepared, + }, }; -impl GLWEToLWEKey> { +impl GLWEToLWESwitchingKey> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { GLWESecretPrepared::alloc_bytes_with(module, infos.rank_in()) - + (GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) } } -impl GLWEToLWEKey { +impl GLWEToLWESwitchingKey { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 66ae685..4c8b866 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -12,7 +12,7 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, layouts::{ - Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWESwitchingKey, Rank, + Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWESwitchingKey, Rank, prepared::GLWESecretPrepared, }, }; @@ -40,7 +40,7 @@ impl LWESwitchingKey> { ); GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) + GLWESecretPrepared::alloc_bytes_with(module, Rank(1)) - + GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 204e84b..5cb9339 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -11,7 +11,7 @@ use poulpy_hal::{ use crate::{ TakeGLWESecret, TakeGLWESecretPrepared, - layouts::{Degree, GGLWEInfos, GGLWESwitchingKey, GLWESecret, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, + layouts::{Degree, GGLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, LWESecret, LWEToGLWESwitchingKey, Rank}, }; impl LWEToGLWESwitchingKey> { @@ -25,7 +25,7 @@ impl LWEToGLWESwitchingKey> { Rank(1), "rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) } } diff --git a/poulpy-core/src/external_product/gglwe_atk.rs b/poulpy-core/src/external_product/gglwe_atk.rs index cb35a4c..7228c28 100644 --- a/poulpy-core/src/external_product/gglwe_atk.rs +++ b/poulpy-core/src/external_product/gglwe_atk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch}, }; -use crate::layouts::{GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GGSWInfos, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GGSWInfos, GLWESwitchingKey, prepared::GGSWPrepared}; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn external_product_scratch_space( module: &Module, out_infos: &OUT, @@ -22,7 +22,7 @@ impl GGLWEAutomorphismKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) + GLWESwitchingKey::external_product_scratch_space(module, out_infos, in_infos, ggsw_infos) } pub fn external_product_inplace_scratch_space( @@ -35,16 +35,16 @@ impl GGLWEAutomorphismKey> { GGSW: GGSWInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) + GLWESwitchingKey::external_product_inplace_scratch_space(module, out_infos, ggsw_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn external_product( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGSWCiphertextPrepared, + lhs: &AutomorphismKey, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -64,7 +64,7 @@ impl GGLWEAutomorphismKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index 2eff45c..bfeb146 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGLWESwitchingKey, GGSWInfos, GLWECiphertext, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{GGLWEInfos, GGSWInfos, GLWECiphertext, GLWESwitchingKey, prepared::GGSWPrepared}; -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn external_product_scratch_space( module: &Module, out_infos: &OUT, @@ -44,12 +44,12 @@ impl GGLWESwitchingKey> { } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn external_product( &mut self, module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGSWCiphertextPrepared, + lhs: &GLWESwitchingKey, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -107,7 +107,7 @@ impl GGLWESwitchingKey { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index a458de1..1610cef 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWCiphertextPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWPrepared}; -impl GGSWCiphertext> { +impl GGSW> { #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, @@ -45,12 +45,12 @@ impl GGSWCiphertext> { } } -impl GGSWCiphertext { +impl GGSW { pub fn external_product( &mut self, module: &Module, - lhs: &GGSWCiphertext, - rhs: &GGSWCiphertextPrepared, + lhs: &GGSW, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -86,7 +86,7 @@ impl GGSWCiphertext { rhs.rank() ); - assert!(scratch.available() >= GGSWCiphertext::external_product_scratch_space(module, self, lhs, rhs)) + assert!(scratch.available() >= GGSW::external_product_scratch_space(module, self, lhs, rhs)) } let min_dnum: usize = self.dnum().min(lhs.dnum()).into(); @@ -105,7 +105,7 @@ impl GGSWCiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index d764507..5a72049 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -11,7 +11,7 @@ use crate::{ GLWEExternalProduct, GLWEExternalProductInplace, layouts::{ GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, }; @@ -74,7 +74,7 @@ impl GLWECiphertext { &mut self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProduct, @@ -85,7 +85,7 @@ impl GLWECiphertext { pub fn external_product_inplace( &mut self, module: &Module, - rhs: &GGSWCiphertextPrepared, + rhs: &GGSWPrepared, scratch: &mut Scratch, ) where Module: GLWEExternalProductInplace, @@ -113,7 +113,7 @@ where D: GGSWCiphertextPreparedToRef, { let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &ggsw.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); let basek_in: usize = res.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); @@ -237,7 +237,7 @@ where let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); - let rhs: &GGSWCiphertextPrepared<&[u8], BE> = &rhs.to_ref(); + let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref(); let basek_in: usize = lhs.base2k().into(); let basek_ggsw: usize = rhs.base2k().into(); diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 3da962a..927e098 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared}, + layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -116,7 +116,7 @@ impl GLWEPacker { &mut self, module: &Module, a: Option<&GLWECiphertext>, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -191,7 +191,7 @@ fn pack_core( a: Option<&GLWECiphertext>, accumulators: &mut [Accumulator], i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -285,7 +285,7 @@ fn combine( acc: &mut Accumulator, b: Option<&GLWECiphertext>, i: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -397,7 +397,7 @@ pub fn glwe_packing( module: &Module, cts: &mut HashMap>, log_gap_out: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, @@ -439,7 +439,7 @@ pub fn glwe_packing( (0..log_n - log_gap_out).for_each(|i| { let t: usize = (1 << log_n).min(1 << (log_n - 1 - i)); - let auto_key: &GGLWEAutomorphismKeyPrepared = if i == 0 { + let auto_key: &AutomorphismKeyPrepared = if i == 0 { auto_keys.get(&-1).unwrap() } else { auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() @@ -466,7 +466,7 @@ fn pack_internal( a: &mut Option<&mut GLWECiphertext>, b: &mut Option<&mut GLWECiphertext>, i: usize, - auto_key: &GGLWEAutomorphismKeyPrepared, + auto_key: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxRotateInplace diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 4e1769e..397a7e1 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -11,9 +11,7 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::GGLWEAutomorphismKeyPrepared, - }, + layouts::{Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; @@ -72,7 +70,7 @@ impl GLWECiphertext { start: usize, end: usize, lhs: &GLWECiphertext, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -100,7 +98,7 @@ impl GLWECiphertext { module: &Module, start: usize, end: usize, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index 9f6caa5..ee86cbc 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -8,11 +8,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEAutomorphismKey, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWESwitchingKeyPrepared}, + AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, + prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, }; -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -25,7 +25,7 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) + GLWESwitchingKey::keyswitch_scratch_space(module, out_infos, in_infos, key_infos) } pub fn keyswitch_inplace_scratch_space(module: &Module, out_infos: &OUT, key_infos: &KEY) -> usize @@ -34,16 +34,16 @@ impl GGLWEAutomorphismKey> { KEY: GGLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GGLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) + GLWESwitchingKey::keyswitch_inplace_scratch_space(module, out_infos, key_infos) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGLWEAutomorphismKey, - rhs: &GGLWESwitchingKeyPrepared, + lhs: &AutomorphismKey, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -65,7 +65,7 @@ impl GGLWEAutomorphismKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWEAutomorphismKeyPrepared, + rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -85,7 +85,7 @@ impl GGLWEAutomorphismKey { } } -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -111,12 +111,12 @@ impl GGLWESwitchingKey> { } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGLWESwitchingKey, - rhs: &GGLWESwitchingKeyPrepared, + lhs: &GLWESwitchingKey, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -187,7 +187,7 @@ impl GGLWESwitchingKey { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index d261f03..3ec738e 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -10,13 +10,13 @@ use poulpy_hal::{ use crate::{ layouts::{ - GGLWECiphertext, GGLWEInfos, GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared}, + GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, operations::GLWEOperations, }; -impl GGSWCiphertext> { +impl GGSW> { pub(crate) fn expand_row_scratch_space(module: &Module, out_infos: &OUT, tsk_infos: &TSK) -> usize where OUT: GGSWInfos, @@ -77,7 +77,7 @@ impl GGSWCiphertext> { let res_znx: usize = VecZnx::alloc_bytes(module.n(), rank + 1, size_out); let ci_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, apply_infos); - let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_infos, tsk_infos); + let expand_rows: usize = GGSW::expand_row_scratch_space(module, out_infos, tsk_infos); let res_dft: usize = module.vec_znx_dft_alloc_bytes(rank + 1, size_out); if in_infos.base2k() == tsk_infos.base2k() { @@ -109,16 +109,16 @@ impl GGSWCiphertext> { + VecZnxNormalizeTmpBytes + VecZnxBigNormalizeTmpBytes, { - GGSWCiphertext::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) + GGSW::keyswitch_scratch_space(module, out_infos, out_infos, apply_infos, tsk_infos) } } -impl GGSWCiphertext { +impl GGSW { pub fn from_gglwe( &mut self, module: &Module, - a: &GGLWECiphertext, - tsk: &GGLWETensorKeyPrepared, + a: &GGLWE, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where DataA: DataRef, @@ -157,9 +157,9 @@ impl GGSWCiphertext { pub fn keyswitch( &mut self, module: &Module, - lhs: &GGSWCiphertext, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, + lhs: &GGSW, + ksk: &GLWESwitchingKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -192,8 +192,8 @@ impl GGSWCiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - ksk: &GGLWESwitchingKeyPrepared, - tsk: &GGLWETensorKeyPrepared, + ksk: &GLWESwitchingKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -226,7 +226,7 @@ impl GGSWCiphertext { pub fn expand_row( &mut self, module: &Module, - tsk: &GGLWETensorKeyPrepared, + tsk: &TensorKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -246,7 +246,7 @@ impl GGSWCiphertext { let basek_in: usize = self.base2k().into(); let basek_tsk: usize = tsk.base2k().into(); - assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self, tsk)); + assert!(scratch.available() >= GGSW::expand_row_scratch_space(module, self, tsk)); let n: usize = self.n().into(); let rank: usize = self.rank().into(); diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index 07d95e9..e7afcbe 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GGLWESwitchingKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; impl GLWECiphertext> { pub fn keyswitch_scratch_space( @@ -68,7 +68,7 @@ impl GLWECiphertext { &self, module: &Module, lhs: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where DataLhs: DataRef, @@ -117,7 +117,7 @@ impl GLWECiphertext { pub(crate) fn assert_keyswitch_inplace( &self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where DataRhs: DataRef, @@ -149,7 +149,7 @@ impl GLWECiphertext { &mut self, module: &Module, glwe_in: &GLWECiphertext, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -191,7 +191,7 @@ impl GLWECiphertext { pub fn keyswitch_inplace( &mut self, module: &Module, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where Module: VecZnxDftAllocBytes @@ -237,7 +237,7 @@ impl GLWECiphertext { &self, module: &Module, res_dft: VecZnxDft, - rhs: &GGLWESwitchingKeyPrepared, + rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) -> VecZnxBig where diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index 2b6a54d..a62e4a7 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_atk.rs @@ -5,19 +5,19 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKeyCompressed { - pub(crate) key: GGLWEKeyCompressed, +pub struct AutomorphismKeyCompressed { + pub(crate) key: GLWESwitchingKeyCompressed, pub(crate) p: i64, } -impl LWEInfos for GGLWEAutomorphismKeyCompressed { +impl LWEInfos for AutomorphismKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -34,13 +34,13 @@ impl LWEInfos for GGLWEAutomorphismKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GLWEInfos for AutomorphismKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { +impl GGLWEInfos for AutomorphismKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -58,39 +58,39 @@ impl GGLWEInfos for GGLWEAutomorphismKeyCompressed { } } -impl fmt::Debug for GGLWEAutomorphismKeyCompressed { +impl fmt::Debug for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKeyCompressed { +impl FillUniform for AutomorphismKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKeyCompressed { +impl fmt::Display for AutomorphismKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKeyCompressed> { +impl AutomorphismKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); Self { - key: GGLWEKeyCompressed::alloc(infos), + key: GLWESwitchingKeyCompressed::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { Self { - key: GGLWEKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), + key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -100,64 +100,64 @@ impl GGLWEAutomorphismKeyCompressed> { A: GGLWEInfos, { debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) } } -impl ReaderFrom for GGLWEAutomorphismKeyCompressed { +impl ReaderFrom for AutomorphismKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::()? as i64; self.key.read_from(reader) } } -impl WriterTo for GGLWEAutomorphismKeyCompressed { +impl WriterTo for AutomorphismKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) } } -impl Decompress> for GGLWEAutomorphismKey +impl Decompress> for AutomorphismKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWEAutomorphismKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &AutomorphismKeyCompressed) { self.key.decompress(module, &other.key); self.p = other.p; } } -pub trait GGLWEAutomorphismKeyCompressedToRef { - fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]>; +pub trait AutomorphismKeyCompressedToRef { + fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>; } -impl GGLWEAutomorphismKeyCompressedToRef for GGLWEAutomorphismKeyCompressed +impl AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToRef, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { - fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]> { - GGLWEAutomorphismKeyCompressed { + fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> { + AutomorphismKeyCompressed { key: self.key.to_ref(), p: self.p, } } } -pub trait GGLWEAutomorphismKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]>; +pub trait AutomorphismKeyCompressedToMut { + fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>; } -impl GGLWEAutomorphismKeyCompressedToMut for GGLWEAutomorphismKeyCompressed +impl AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToMut, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { - fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]> { - GGLWEAutomorphismKeyCompressed { + fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> { + AutomorphismKeyCompressed { p: self.p, key: self.key.to_mut(), } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index 6e14756..a75a9a8 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GLWECiphertextCompressed}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -235,7 +235,7 @@ impl WriterTo for GGLWECiphertextCompressed { } } -impl Decompress> for GGLWECiphertext +impl Decompress> for GGLWE where Module: VecZnxFillUniform + VecZnxCopy, { @@ -313,7 +313,7 @@ pub trait GGLWECiphertextCompressedToRef { fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; } -impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { +impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { GGLWECiphertextCompressed { k: self.k(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index cbb2f8c..76d60a7 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs @@ -5,20 +5,20 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEKeyCompressed { +pub struct GLWESwitchingKeyCompressed { pub(crate) key: GGLWECiphertextCompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWEKeyCompressed { +impl LWEInfos for GLWESwitchingKeyCompressed { fn n(&self) -> Degree { self.key.n() } @@ -35,13 +35,13 @@ impl LWEInfos for GGLWEKeyCompressed { self.key.size() } } -impl GLWEInfos for GGLWEKeyCompressed { +impl GLWEInfos for GLWESwitchingKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEKeyCompressed { +impl GGLWEInfos for GLWESwitchingKeyCompressed { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -59,19 +59,19 @@ impl GGLWEInfos for GGLWEKeyCompressed { } } -impl fmt::Debug for GGLWEKeyCompressed { +impl fmt::Debug for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEKeyCompressed { +impl FillUniform for GLWESwitchingKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEKeyCompressed { +impl fmt::Display for GLWESwitchingKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -81,12 +81,12 @@ impl fmt::Display for GGLWEKeyCompressed { } } -impl GGLWEKeyCompressed> { +impl GLWESwitchingKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { - GGLWEKeyCompressed { + GLWESwitchingKeyCompressed { key: GGLWECiphertextCompressed::alloc(infos), sk_in_n: 0, sk_out_n: 0, @@ -102,7 +102,7 @@ impl GGLWEKeyCompressed> { dnum: Dnum, dsize: Dsize, ) -> Self { - GGLWEKeyCompressed { + GLWESwitchingKeyCompressed { key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, @@ -121,7 +121,7 @@ impl GGLWEKeyCompressed> { } } -impl ReaderFrom for GGLWEKeyCompressed { +impl ReaderFrom for GLWESwitchingKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -129,7 +129,7 @@ impl ReaderFrom for GGLWEKeyCompressed { } } -impl WriterTo for GGLWEKeyCompressed { +impl WriterTo for GLWESwitchingKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; @@ -137,27 +137,27 @@ impl WriterTo for GGLWEKeyCompressed { } } -impl Decompress> for GGLWESwitchingKey +impl Decompress> for GLWESwitchingKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWEKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &GLWESwitchingKeyCompressed) { self.key.decompress(module, &other.key); self.sk_in_n = other.sk_in_n; self.sk_out_n = other.sk_out_n; } } -pub trait GGLWEKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]>; +pub trait GLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>; } -impl GGLWEKeyCompressedToMut for GGLWEKeyCompressed +impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed where GGLWECiphertextCompressed: GGLWECiphertextCompressedToMut, { - fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]> { - GGLWEKeyCompressed { + fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { + GLWESwitchingKeyCompressed { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_mut(), @@ -165,16 +165,16 @@ where } } -pub trait GGLWEKeyCompressedToRef { - fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]>; +pub trait GLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>; } -impl GGLWEKeyCompressedToRef for GGLWEKeyCompressed +impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed where GGLWECiphertextCompressed: GGLWECiphertextCompressedToRef, { - fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]> { - GGLWEKeyCompressed { + fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { + GLWESwitchingKeyCompressed { sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, key: self.key.to_ref(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 0206788..7255594 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs @@ -5,18 +5,18 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TorusPrecision, + compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKeyCompressed { - pub(crate) keys: Vec>, +pub struct TensorKeyCompressed { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyCompressed { +impl LWEInfos for TensorKeyCompressed { fn n(&self) -> Degree { self.keys[0].n() } @@ -32,13 +32,13 @@ impl LWEInfos for GGLWETensorKeyCompressed { self.keys[0].size() } } -impl GLWEInfos for GGLWETensorKeyCompressed { +impl GLWEInfos for TensorKeyCompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyCompressed { +impl GGLWEInfos for TensorKeyCompressed { fn rank_in(&self) -> Rank { self.rank_out() } @@ -56,21 +56,21 @@ impl GGLWEInfos for GGLWETensorKeyCompressed { } } -impl fmt::Debug for GGLWETensorKeyCompressed { +impl fmt::Debug for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKeyCompressed { +impl FillUniform for TensorKeyCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWEKeyCompressed| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKeyCompressed| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKeyCompressed { +impl fmt::Display for TensorKeyCompressed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKeyCompressed)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -80,7 +80,7 @@ impl fmt::Display for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed> { +impl TensorKeyCompressed> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -101,10 +101,10 @@ impl GGLWETensorKeyCompressed> { } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); + let mut keys: Vec>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWEKeyCompressed::alloc_with( + keys.push(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -129,7 +129,7 @@ impl GGLWETensorKeyCompressed> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWEKeyCompressed::alloc_bytes_with( + * GLWESwitchingKeyCompressed::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), @@ -141,11 +141,11 @@ impl GGLWETensorKeyCompressed> { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) + pairs * GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) } } -impl ReaderFrom for GGLWETensorKeyCompressed { +impl ReaderFrom for TensorKeyCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -161,7 +161,7 @@ impl ReaderFrom for GGLWETensorKeyCompressed { } } -impl WriterTo for GGLWETensorKeyCompressed { +impl WriterTo for TensorKeyCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -171,8 +171,8 @@ impl WriterTo for GGLWETensorKeyCompressed { } } -impl GGLWETensorKeyCompressed { - pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEKeyCompressed { +impl TensorKeyCompressed { + pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -181,11 +181,11 @@ impl GGLWETensorKeyCompressed { } } -impl Decompress> for GGLWETensorKey +impl Decompress> for TensorKey where Module: VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GGLWETensorKeyCompressed) { + fn decompress(&mut self, module: &Module, other: &TensorKeyCompressed) { #[cfg(debug_assertions)] { assert_eq!( @@ -206,31 +206,31 @@ where } } -pub trait GGLWETensorKeyCompressedToMut { - fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]>; +pub trait TensorKeyCompressedToMut { + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]>; } -impl GGLWETensorKeyCompressedToMut for GGLWETensorKeyCompressed +impl TensorKeyCompressedToMut for TensorKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToMut, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, { - fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]> { - GGLWETensorKeyCompressed { + fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> { + TensorKeyCompressed { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), } } } -pub trait GGLWETensorKeyCompressedToRef { - fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]>; +pub trait TensorKeyCompressedToRef { + fn to_ref(&self) -> TensorKeyCompressed<&[u8]>; } -impl GGLWETensorKeyCompressedToRef for GGLWETensorKeyCompressed +impl TensorKeyCompressedToRef for TensorKeyCompressed where - GGLWEKeyCompressed: GGLWEKeyCompressedToRef, + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, { - fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]> { - GGLWETensorKeyCompressed { + fn to_ref(&self) -> TensorKeyCompressed<&[u8]> { + TensorKeyCompressed { keys: self.keys.iter().map(|c| c.to_ref()).collect(), } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index b751bfc..3d15e3e 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/poulpy-core/src/layouts/compressed/ggsw_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::{Decompress, GLWECiphertextCompressed}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -217,7 +217,7 @@ impl WriterTo for GGSWCiphertextCompressed { } } -impl Decompress> for GGSWCiphertext +impl Decompress> for GGSW where Module: VecZnxFillUniform + VecZnxCopy, { diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 30a3733..576774d 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -1,10 +1,14 @@ use poulpy_hal::{ api::{VecZnxCopy, VecZnxFillUniform}, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; +use crate::layouts::{ + Base2K, BuildError, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; @@ -96,6 +100,101 @@ impl GLWECiphertextCompressed> { } } +pub struct GLWECiphertextCompressedBuilder { + data: Option>, + base2k: Option, + k: Option, + rank: Option, +} + +impl GLWECiphertextCompressed { + #[inline] + pub fn builder() -> GLWECiphertextCompressedBuilder { + GLWECiphertextCompressedBuilder { + data: None, + base2k: None, + k: None, + rank: None, + } + } +} + +impl GLWECiphertextCompressedBuilder> { + #[inline] + pub fn layout(mut self, layout: &A) -> Self + where + A: GLWEInfos, + { + self.data = Some(VecZnx::alloc(layout.n().into(), 1, layout.size())); + self.base2k = Some(layout.base2k()); + self.k = Some(layout.k()); + self.rank = Some(layout.rank()); + self + } +} + +impl GLWECiphertextCompressedBuilder { + #[inline] + pub fn data(mut self, data: VecZnx) -> Self { + self.data = Some(data); + self + } + #[inline] + pub fn base2k(mut self, base2k: Base2K) -> Self { + self.base2k = Some(base2k); + self + } + #[inline] + pub fn k(mut self, k: TorusPrecision) -> Self { + self.k = Some(k); + self + } + #[inline] + pub fn rank(mut self, rank: Rank) -> Self { + self.rank = Some(rank); + self + } + + pub fn build(self) -> Result, BuildError> { + let data: VecZnx = self.data.ok_or(BuildError::MissingData)?; + let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; + let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; + let rank: Rank = self.rank.ok_or(BuildError::MissingK)?; + + if base2k == 0_u32 { + return Err(BuildError::ZeroBase2K); + } + + if k == 0_u32 { + return Err(BuildError::ZeroTorusPrecision); + } + + if rank == 0_u32 { + return Err(BuildError::ZeroRank); + } + + if data.n() == 0 { + return Err(BuildError::ZeroDegree); + } + + if data.cols() != 1 { + return Err(BuildError::VecZnxColsNotOne); + } + + if data.size() == 0 { + return Err(BuildError::ZeroLimbs); + } + + Ok(GLWECiphertextCompressed { + data, + base2k, + k, + rank, + seed: [0u8; 32], + }) + } +} + impl ReaderFrom for GLWECiphertextCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -176,3 +275,35 @@ impl GLWECiphertext { self.k = other.k; } } + +pub trait GLWECiphertextCompressedToRef { + fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>; +} + +impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { + fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { + GLWECiphertextCompressed::builder() + .k(self.k()) + .base2k(self.base2k()) + .rank(self.rank()) + .data(self.data.to_ref()) + .build() + .unwrap() + } +} + +pub trait GLWECiphertextCompressedToMut { + fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>; +} + +impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { + fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { + GLWECiphertextCompressed::builder() + .k(self.k()) + .base2k(self.base2k()) + .rank(self.rank()) + .data(self.data.to_mut()) + .build() + .unwrap() + } +} diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 317e0e5..c3969b2 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWEKeyCompressed, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed, }; #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct GLWEToLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for GLWEToLWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -98,11 +98,11 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -127,10 +127,10 @@ impl GLWEToLWESwitchingKeyCompressed> { 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index 9d45d0a..d7ea93b 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed}, + compressed::{Decompress, GLWESwitchingKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct LWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWESwitchingKeyCompressed { fn base2k(&self) -> Base2K { @@ -103,11 +103,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -137,11 +137,11 @@ impl LWESwitchingKeyCompressed> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index bcc69a4..c1000ff 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GGLWEKeyCompressed}, + compressed::{Decompress, GLWESwitchingKeyCompressed}, }; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GGLWEKeyCompressed); +pub struct LWEToGLWESwitchingKeyCompressed(pub(crate) GLWESwitchingKeyCompressed); impl LWEInfos for LWEToGLWESwitchingKeyCompressed { fn n(&self) -> Degree { @@ -98,11 +98,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GGLWEKeyCompressed::alloc(infos)) + Self(GLWESwitchingKeyCompressed::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWEKeyCompressed::alloc_with( + Self(GLWESwitchingKeyCompressed::alloc_with( n, base2k, k, @@ -127,11 +127,11 @@ impl LWEToGLWESwitchingKeyCompressed> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWEKeyCompressed::alloc_bytes(infos) + GLWESwitchingKeyCompressed::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) } } diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index a88db86..f9244e1 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/poulpy-core/src/layouts/gglwe_atk.rs @@ -4,15 +4,15 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GGLWESwitchingKeyToMut, GLWECiphertext, GLWEInfos, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWEAutomorphismKeyLayout { +pub struct AutomorphismKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -22,18 +22,18 @@ pub struct GGLWEAutomorphismKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWEAutomorphismKey { - pub(crate) key: GGLWESwitchingKey, +pub struct AutomorphismKey { + pub(crate) key: GLWESwitchingKey, pub(crate) p: i64, } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for GGLWEAutomorphismKey { +impl LWEInfos for AutomorphismKey { fn n(&self) -> Degree { self.key.n() } @@ -51,13 +51,13 @@ impl LWEInfos for GGLWEAutomorphismKey { } } -impl GLWEInfos for GGLWEAutomorphismKey { +impl GLWEInfos for AutomorphismKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKey { +impl GGLWEInfos for AutomorphismKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -75,7 +75,7 @@ impl GGLWEInfos for GGLWEAutomorphismKey { } } -impl LWEInfos for GGLWEAutomorphismKeyLayout { +impl LWEInfos for AutomorphismKeyLayout { fn base2k(&self) -> Base2K { self.base2k } @@ -89,13 +89,13 @@ impl LWEInfos for GGLWEAutomorphismKeyLayout { } } -impl GLWEInfos for GGLWEAutomorphismKeyLayout { +impl GLWEInfos for AutomorphismKeyLayout { fn rank(&self) -> Rank { self.rank } } -impl GGLWEInfos for GGLWEAutomorphismKeyLayout { +impl GGLWEInfos for AutomorphismKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -113,25 +113,25 @@ impl GGLWEInfos for GGLWEAutomorphismKeyLayout { } } -impl fmt::Debug for GGLWEAutomorphismKey { +impl fmt::Debug for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWEAutomorphismKey { +impl FillUniform for AutomorphismKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWEAutomorphismKey { +impl fmt::Display for AutomorphismKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key) } } -impl GGLWEAutomorphismKey> { +impl AutomorphismKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -141,15 +141,15 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc(infos), + AutomorphismKey { + key: GLWESwitchingKey::alloc(infos), p: 0, } } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - GGLWEAutomorphismKey { - key: GGLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), + AutomorphismKey { + key: GLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -163,54 +163,66 @@ impl GGLWEAutomorphismKey> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) } } -pub trait GGLWEAutomorphismKeyToMut { - fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]>; +pub trait AutomorphismKeyToMut { + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>; } -impl GGLWEAutomorphismKeyToMut for GGLWEAutomorphismKey +impl AutomorphismKeyToMut for AutomorphismKey where - GGLWESwitchingKey: GGLWESwitchingKeyToMut, + GLWESwitchingKey: GLWESwitchingKeyToMut, { - fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]> { - GGLWEAutomorphismKey { + fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> { + AutomorphismKey { key: self.key.to_mut(), p: self.p, } } } -pub trait GGLWEAutomorphismKeyToRef { - fn to_ref(&self) -> GGLWEAutomorphismKey<&[u8]>; +pub trait AutomorphismKeyToRef { + fn to_ref(&self) -> AutomorphismKey<&[u8]>; } -impl GGLWEAutomorphismKey { +impl AutomorphismKeyToRef for AutomorphismKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> AutomorphismKey<&[u8]> { + AutomorphismKey { + p: self.p, + key: self.key.to_ref(), + } + } +} + +impl AutomorphismKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } -impl GGLWEAutomorphismKey { +impl AutomorphismKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWEAutomorphismKey { +impl ReaderFrom for AutomorphismKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.p = reader.read_u64::()? as i64; self.key.read_from(reader) } } -impl WriterTo for GGLWEAutomorphismKey { +impl WriterTo for AutomorphismKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.p as u64)?; self.key.write_to(writer) diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 713df90..8546499 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/poulpy-core/src/layouts/gglwe_ct.rs @@ -29,6 +29,10 @@ where } } +pub trait SetGGLWEInfos { + fn set_dsize(&mut self, dsize: usize); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct GGLWECiphertextLayout { pub n: Degree, @@ -79,14 +83,14 @@ impl GGLWEInfos for GGLWECiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertext { +pub struct GGLWE { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertext { +impl LWEInfos for GGLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -104,13 +108,13 @@ impl LWEInfos for GGLWECiphertext { } } -impl GLWEInfos for GGLWECiphertext { +impl GLWEInfos for GGLWE { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertext { +impl GGLWEInfos for GGLWE { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -135,7 +139,7 @@ pub struct GGLWECiphertextBuilder { dsize: Option, } -impl GGLWECiphertext { +impl GGLWE { #[inline] pub fn builder() -> GGLWECiphertextBuilder { GGLWECiphertextBuilder { @@ -190,7 +194,7 @@ impl GGLWECiphertextBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -220,7 +224,7 @@ impl GGLWECiphertextBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGLWECiphertext { + Ok(GGLWE { data, base2k, k, @@ -229,31 +233,31 @@ impl GGLWECiphertextBuilder { } } -impl GGLWECiphertext { +impl GGLWE { pub fn data(&self) -> &MatZnx { &self.data } } -impl GGLWECiphertext { +impl GGLWE { pub fn data_mut(&mut self) -> &mut MatZnx { &mut self.data } } -impl fmt::Debug for GGLWECiphertext { +impl fmt::Debug for GGLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertext { +impl FillUniform for GGLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertext { +impl fmt::Display for GGLWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -266,7 +270,7 @@ impl fmt::Display for GGLWECiphertext { } } -impl GGLWECiphertext { +impl GGLWE { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.at(row, col)) @@ -277,7 +281,7 @@ impl GGLWECiphertext { } } -impl GGLWECiphertext { +impl GGLWE { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .base2k(self.base2k()) @@ -288,7 +292,7 @@ impl GGLWECiphertext { } } -impl GGLWECiphertext> { +impl GGLWE> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -390,12 +394,12 @@ impl GGLWECiphertext> { } pub trait GGLWECiphertextToMut { - fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]>; + fn to_mut(&mut self) -> GGLWE<&mut [u8]>; } -impl GGLWECiphertextToMut for GGLWECiphertext { - fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]> { - GGLWECiphertext { +impl GGLWECiphertextToMut for GGLWE { + fn to_mut(&mut self) -> GGLWE<&mut [u8]> { + GGLWE { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -404,13 +408,13 @@ impl GGLWECiphertextToMut for GGLWECiphertext { } } -pub trait GGLWECiphertextToRef { - fn to_ref(&self) -> GGLWECiphertext<&[u8]>; +pub trait GGLWEToRef { + fn to_ref(&self) -> GGLWE<&[u8]>; } -impl GGLWECiphertextToRef for GGLWECiphertext { - fn to_ref(&self) -> GGLWECiphertext<&[u8]> { - GGLWECiphertext { +impl GGLWEToRef for GGLWE { + fn to_ref(&self) -> GGLWE<&[u8]> { + GGLWE { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -419,7 +423,7 @@ impl GGLWECiphertextToRef for GGLWECiphertext { } } -impl ReaderFrom for GGLWECiphertext { +impl ReaderFrom for GGLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -428,7 +432,7 @@ impl ReaderFrom for GGLWECiphertext { } } -impl WriterTo for GGLWECiphertext { +impl WriterTo for GGLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 64b1694..e8a8b92 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/gglwe_ksk.rs @@ -4,7 +4,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -12,7 +12,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWESwitchingKeyLayout { +pub struct GLWESwitchingKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -22,7 +22,7 @@ pub struct GGLWESwitchingKeyLayout { pub dsize: Dsize, } -impl LWEInfos for GGLWESwitchingKeyLayout { +impl LWEInfos for GLWESwitchingKeyLayout { fn n(&self) -> Degree { self.n } @@ -36,13 +36,13 @@ impl LWEInfos for GGLWESwitchingKeyLayout { } } -impl GLWEInfos for GGLWESwitchingKeyLayout { +impl GLWEInfos for GLWESwitchingKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyLayout { +impl GGLWEInfos for GLWESwitchingKeyLayout { fn rank_in(&self) -> Rank { self.rank_in } @@ -61,13 +61,13 @@ impl GGLWEInfos for GGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWESwitchingKey { - pub(crate) key: GGLWECiphertext, +pub struct GLWESwitchingKey { + pub(crate) key: GGLWE, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKey { +impl LWEInfos for GLWESwitchingKey { fn n(&self) -> Degree { self.key.n() } @@ -85,13 +85,13 @@ impl LWEInfos for GGLWESwitchingKey { } } -impl GLWEInfos for GGLWESwitchingKey { +impl GLWEInfos for GLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKey { +impl GGLWEInfos for GLWESwitchingKey { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -109,13 +109,13 @@ impl GGLWEInfos for GGLWESwitchingKey { } } -impl fmt::Debug for GGLWESwitchingKey { +impl fmt::Debug for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GGLWESwitchingKey { +impl fmt::Display for GLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -127,19 +127,19 @@ impl fmt::Display for GGLWESwitchingKey { } } -impl FillUniform for GGLWESwitchingKey { +impl FillUniform for GLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.key.fill_uniform(log_bound, source); } } -impl GGLWESwitchingKey> { +impl GLWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc(infos), + GLWESwitchingKey { + key: GGLWE::alloc(infos), sk_in_n: 0, sk_out_n: 0, } @@ -154,8 +154,8 @@ impl GGLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> Self { - GGLWESwitchingKey { - key: GGLWECiphertext::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + GLWESwitchingKey { + key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } @@ -165,7 +165,7 @@ impl GGLWESwitchingKey> { where A: GGLWEInfos, { - GGLWECiphertext::alloc_bytes(infos) + GGLWE::alloc_bytes(infos) } pub fn alloc_bytes_with( @@ -177,20 +177,20 @@ impl GGLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> usize { - GGLWECiphertext::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) } } -pub trait GGLWESwitchingKeyToMut { - fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]>; +pub trait GLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>; } -impl GGLWESwitchingKeyToMut for GGLWESwitchingKey +impl GLWESwitchingKeyToMut for GLWESwitchingKey where - GGLWECiphertext: GGLWECiphertextToMut, + GGLWE: GGLWECiphertextToMut, { - fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]> { - GGLWESwitchingKey { + fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { + GLWESwitchingKey { key: self.key.to_mut(), sk_in_n: self.sk_in_n, sk_out_n: self.sk_out_n, @@ -198,19 +198,36 @@ where } } -impl GGLWESwitchingKey { +pub trait GLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWESwitchingKey<&[u8]>; +} + +impl GLWESwitchingKeyToRef for GLWESwitchingKey +where + GGLWE: GGLWEToRef, +{ + fn to_ref(&self) -> GLWESwitchingKey<&[u8]> { + GLWESwitchingKey { + key: self.key.to_ref(), + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + } + } +} + +impl GLWESwitchingKey { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { self.key.at(row, col) } } -impl GGLWESwitchingKey { +impl GLWESwitchingKey { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { self.key.at_mut(row, col) } } -impl ReaderFrom for GGLWESwitchingKey { +impl ReaderFrom for GLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.sk_in_n = reader.read_u64::()? as usize; self.sk_out_n = reader.read_u64::()? as usize; @@ -218,7 +235,7 @@ impl ReaderFrom for GGLWESwitchingKey { } } -impl WriterTo for GGLWESwitchingKey { +impl WriterTo for GLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.sk_in_n as u64)?; writer.write_u64::(self.sk_out_n as u64)?; diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index a949b7e..e6eae2d 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/gglwe_tsk.rs @@ -3,13 +3,16 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GGLWETensorKeyLayout { +pub struct TensorKeyLayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, @@ -19,11 +22,11 @@ pub struct GGLWETensorKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGLWETensorKey { - pub(crate) keys: Vec>, +pub struct TensorKey { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKey { +impl LWEInfos for TensorKey { fn n(&self) -> Degree { self.keys[0].n() } @@ -41,13 +44,13 @@ impl LWEInfos for GGLWETensorKey { } } -impl GLWEInfos for GGLWETensorKey { +impl GLWEInfos for TensorKey { fn rank(&self) -> Rank { self.keys[0].rank_out() } } -impl GGLWEInfos for GGLWETensorKey { +impl GGLWEInfos for TensorKey { fn rank_in(&self) -> Rank { self.rank_out() } @@ -65,7 +68,7 @@ impl GGLWEInfos for GGLWETensorKey { } } -impl LWEInfos for GGLWETensorKeyLayout { +impl LWEInfos for TensorKeyLayout { fn n(&self) -> Degree { self.n } @@ -79,13 +82,13 @@ impl LWEInfos for GGLWETensorKeyLayout { } } -impl GLWEInfos for GGLWETensorKeyLayout { +impl GLWEInfos for TensorKeyLayout { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyLayout { +impl GGLWEInfos for TensorKeyLayout { fn rank_in(&self) -> Rank { self.rank } @@ -103,21 +106,21 @@ impl GGLWEInfos for GGLWETensorKeyLayout { } } -impl fmt::Debug for GGLWETensorKey { +impl fmt::Debug for TensorKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWETensorKey { +impl FillUniform for TensorKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.keys .iter_mut() - .for_each(|key: &mut GGLWESwitchingKey| key.fill_uniform(log_bound, source)) + .for_each(|key: &mut GLWESwitchingKey| key.fill_uniform(log_bound, source)) } } -impl fmt::Display for GGLWETensorKey { +impl fmt::Display for TensorKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "(GLWETensorKey)",)?; for (i, key) in self.keys.iter().enumerate() { @@ -127,7 +130,7 @@ impl fmt::Display for GGLWETensorKey { } } -impl GGLWETensorKey> { +impl TensorKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -148,10 +151,10 @@ impl GGLWETensorKey> { } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec>> = Vec::new(); + let mut keys: Vec>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKey::alloc_with( + keys.push(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -176,7 +179,7 @@ impl GGLWETensorKey> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWESwitchingKey::alloc_bytes_with( + * GLWESwitchingKey::alloc_bytes_with( infos.n(), infos.base2k(), infos.k(), @@ -189,13 +192,13 @@ impl GGLWETensorKey> { pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) } } -impl GGLWETensorKey { +impl TensorKey { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKey { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -204,9 +207,9 @@ impl GGLWETensorKey { } } -impl GGLWETensorKey { +impl TensorKey { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKey { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -215,7 +218,7 @@ impl GGLWETensorKey { } } -impl ReaderFrom for GGLWETensorKey { +impl ReaderFrom for TensorKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { let len: usize = reader.read_u64::()? as usize; if self.keys.len() != len { @@ -231,7 +234,7 @@ impl ReaderFrom for GGLWETensorKey { } } -impl WriterTo for GGLWETensorKey { +impl WriterTo for TensorKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u64::(self.keys.len() as u64)?; for key in &self.keys { @@ -240,3 +243,33 @@ impl WriterTo for GGLWETensorKey { Ok(()) } } + +pub trait TensorKeyToRef { + fn to_ref(&self) -> TensorKey<&[u8]>; +} + +impl TensorKeyToRef for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> TensorKey<&[u8]> { + TensorKey { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } + } +} + +pub trait TensorKeyToMut { + fn to_ref(&mut self) -> TensorKey<&mut [u8]>; +} + +impl TensorKeyToMut for TensorKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_ref(&mut self) -> TensorKey<&mut [u8]> { + TensorKey { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index dc64bce..2d8b271 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/poulpy-core/src/layouts/ggsw_ct.rs @@ -64,14 +64,14 @@ impl GGSWInfos for GGSWCiphertextLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertext { +pub struct GGSW { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertext { +impl LWEInfos for GGSW { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -89,13 +89,13 @@ impl LWEInfos for GGSWCiphertext { } } -impl GLWEInfos for GGSWCiphertext { +impl GLWEInfos for GGSW { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertext { +impl GGSWInfos for GGSW { fn dsize(&self) -> Dsize { self.dsize } @@ -112,7 +112,7 @@ pub struct GGSWCiphertextBuilder { dsize: Option, } -impl GGSWCiphertext { +impl GGSW { #[inline] pub fn builder() -> GGSWCiphertextBuilder { GGSWCiphertextBuilder { @@ -182,7 +182,7 @@ impl GGSWCiphertextBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: MatZnx = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -212,7 +212,7 @@ impl GGSWCiphertextBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGSWCiphertext { + Ok(GGSW { data, base2k, k, @@ -221,13 +221,13 @@ impl GGSWCiphertextBuilder { } } -impl fmt::Debug for GGSWCiphertext { +impl fmt::Debug for GGSW { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertext { +impl fmt::Display for GGSW { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -240,13 +240,13 @@ impl fmt::Display for GGSWCiphertext { } } -impl FillUniform for GGSWCiphertext { +impl FillUniform for GGSW { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertext { +impl GGSW { pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { GLWECiphertext::builder() .data(self.data.at(row, col)) @@ -257,7 +257,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext { +impl GGSW { pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { GLWECiphertext::builder() .base2k(self.base2k()) @@ -268,7 +268,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext> { +impl GGSW> { pub fn alloc(infos: &A) -> Self where A: GGSWInfos, @@ -353,7 +353,7 @@ impl GGSWCiphertext> { use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for GGSWCiphertext { +impl ReaderFrom for GGSW { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -362,7 +362,7 @@ impl ReaderFrom for GGSWCiphertext { } } -impl WriterTo for GGSWCiphertext { +impl WriterTo for GGSW { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -371,34 +371,32 @@ impl WriterTo for GGSWCiphertext { } } -pub trait GGSWCiphertextToMut { - fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]>; +pub trait GGSWToMut { + fn to_mut(&mut self) -> GGSW<&mut [u8]>; } -impl GGSWCiphertextToMut for GGSWCiphertext { - fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]> { - GGSWCiphertext::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl GGSWToMut for GGSW { + fn to_mut(&mut self) -> GGSW<&mut [u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } -pub trait GGSWCiphertextToRef { - fn to_ref(&self) -> GGSWCiphertext<&[u8]>; +pub trait GGSWToRef { + fn to_ref(&self) -> GGSW<&[u8]>; } -impl GGSWCiphertextToRef for GGSWCiphertext { - fn to_ref(&self) -> GGSWCiphertext<&[u8]> { - GGSWCiphertext::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GGSWToRef for GGSW { + fn to_ref(&self) -> GGSW<&[u8]> { + GGSW { + dsize: self.dsize, + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 23b6ef9..c8cfa4f 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/poulpy-core/src/layouts/glwe_ct.rs @@ -253,36 +253,6 @@ impl GLWECiphertext> { } } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; -} - -impl GLWECiphertextToRef for GLWECiphertext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_ref()) - .build() - .unwrap() - } -} - -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; -} - -impl GLWECiphertextToMut for GLWECiphertext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext::builder() - .k(self.k()) - .base2k(self.base2k()) - .data(self.data.to_mut()) - .build() - .unwrap() - } -} - impl ReaderFrom for GLWECiphertext { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); @@ -298,3 +268,31 @@ impl WriterTo for GLWECiphertext { self.data.write_to(writer) } } + +pub trait GLWECiphertextToRef { + fn to_ref(&self) -> GLWECiphertext<&[u8]>; +} + +impl GLWECiphertextToRef for GLWECiphertext { + fn to_ref(&self) -> GLWECiphertext<&[u8]> { + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } + } +} + +pub trait GLWECiphertextToMut { + fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; +} + +impl GLWECiphertextToMut for GLWECiphertext { + fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { + GLWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index f227c9c..ec5a4a3 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs @@ -3,7 +3,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; use std::fmt; @@ -56,9 +59,9 @@ impl GGLWEInfos for GLWEToLWEKeyLayout { /// A special [GLWESwitchingKey] required to for the conversion from [GLWECiphertext] to [LWECiphertext]. #[derive(PartialEq, Eq, Clone)] -pub struct GLWEToLWEKey(pub(crate) GGLWESwitchingKey); +pub struct GLWEToLWESwitchingKey(pub(crate) GLWESwitchingKey); -impl LWEInfos for GLWEToLWEKey { +impl LWEInfos for GLWEToLWESwitchingKey { fn base2k(&self) -> Base2K { self.0.base2k() } @@ -76,12 +79,12 @@ impl LWEInfos for GLWEToLWEKey { } } -impl GLWEInfos for GLWEToLWEKey { +impl GLWEInfos for GLWEToLWESwitchingKey { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GLWEToLWEKey { +impl GGLWEInfos for GLWEToLWESwitchingKey { fn rank_in(&self) -> Rank { self.0.rank_in() } @@ -99,37 +102,37 @@ impl GGLWEInfos for GLWEToLWEKey { } } -impl fmt::Debug for GLWEToLWEKey { +impl fmt::Debug for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GLWEToLWEKey { +impl FillUniform for GLWEToLWESwitchingKey { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.0.fill_uniform(log_bound, source); } } -impl fmt::Display for GLWEToLWEKey { +impl fmt::Display for GLWEToLWESwitchingKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "(GLWEToLWESwitchingKey) {}", self.0) } } -impl ReaderFrom for GLWEToLWEKey { +impl ReaderFrom for GLWEToLWESwitchingKey { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.0.read_from(reader) } } -impl WriterTo for GLWEToLWEKey { +impl WriterTo for GLWEToLWESwitchingKey { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { self.0.write_to(writer) } } -impl GLWEToLWEKey> { +impl GLWEToLWESwitchingKey> { pub fn alloc(infos: &A) -> Self where A: GGLWEInfos, @@ -144,11 +147,11 @@ impl GLWEToLWEKey> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -173,10 +176,36 @@ impl GLWEToLWEKey> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + } +} + +pub trait GLWEToLWESwitchingKeyToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>; +} + +impl GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> { + GLWEToLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> { + GLWEToLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 1560ea4..ce5b13a 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/poulpy-core/src/layouts/lwe_ct.rs @@ -27,6 +27,11 @@ pub trait LWEInfos { } } +pub trait SetLWEInfos { + fn set_k(&mut self, k: TorusPrecision); + fn set_base2k(&mut self, base2k: Base2K); +} + #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWECiphertextLayout { pub n: Degree, @@ -47,7 +52,6 @@ impl LWEInfos for LWECiphertextLayout { self.n } } - #[derive(PartialEq, Eq, Clone)] pub struct LWECiphertext { pub(crate) data: Zn, @@ -72,6 +76,16 @@ impl LWEInfos for LWECiphertext { } } +impl SetLWEInfos for LWECiphertext { + fn set_base2k(&mut self, base2k: Base2K) { + self.base2k = base2k + } + + fn set_k(&mut self, k: TorusPrecision) { + self.k = k + } +} + impl LWECiphertext { pub fn data(&self) -> &Zn { &self.data @@ -221,12 +235,11 @@ pub trait LWECiphertextToRef { impl LWECiphertextToRef for LWECiphertext { fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() + LWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_ref(), + } } } @@ -237,12 +250,11 @@ pub trait LWECiphertextToMut { impl LWECiphertextToMut for LWECiphertext { fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext::builder() - .base2k(self.base2k()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() + LWECiphertext { + k: self.k, + base2k: self.base2k, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 314322c..8f4a2ed 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_ksk.rs @@ -5,7 +5,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWESwitchingKeyLayout { @@ -54,7 +57,7 @@ impl GGLWEInfos for LWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWESwitchingKey { fn base2k(&self) -> Base2K { @@ -118,11 +121,11 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -152,11 +155,11 @@ impl LWESwitchingKey> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } @@ -189,3 +192,29 @@ impl WriterTo for LWESwitchingKey { self.0.write_to(writer) } } + +pub trait LWEToLWEKeyToRef { + fn to_ref(&self) -> LWESwitchingKey<&[u8]>; +} + +impl LWEToLWEKeyToRef for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWESwitchingKey<&[u8]> { + LWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWEToLWEKeyToMut { + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>; +} + +impl LWEToLWEKeyToMut for LWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> { + LWESwitchingKey(self.0.to_mut()) + } +} diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index a5b7d4e..f2ba869 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -84,3 +84,29 @@ impl LWESecret { self.dist = Distribution::ZERO; } } + +pub trait LWESecretToRef { + fn to_ref(&self) -> LWESecret<&[u8]>; +} + +impl LWESecretToRef for LWESecret { + fn to_ref(&self) -> LWESecret<&[u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_ref(), + } + } +} + +pub trait LWESecretToMut { + fn to_mut(&mut self) -> LWESecret<&mut [u8]>; +} + +impl LWESecretToMut for LWESecret { + fn to_mut(&mut self) -> LWESecret<&mut [u8]> { + LWESecret { + dist: self.dist, + data: self.data.to_mut(), + } + } +} diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index b3ba74b..947e79d 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs @@ -5,7 +5,10 @@ use poulpy_hal::{ source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{ + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, + Rank, TorusPrecision, +}; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub struct LWEToGLWESwitchingKeyLayout { @@ -55,7 +58,7 @@ impl GGLWEInfos for LWEToGLWESwitchingKeyLayout { } #[derive(PartialEq, Eq, Clone)] -pub struct LWEToGLWESwitchingKey(pub(crate) GGLWESwitchingKey); +pub struct LWEToGLWESwitchingKey(pub(crate) GLWESwitchingKey); impl LWEInfos for LWEToGLWESwitchingKey { fn base2k(&self) -> Base2K { @@ -143,11 +146,11 @@ impl LWEToGLWESwitchingKey> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GGLWESwitchingKey::alloc(infos)) + Self(GLWESwitchingKey::alloc(infos)) } pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GGLWESwitchingKey::alloc_with( + Self(GLWESwitchingKey::alloc_with( n, base2k, k, @@ -172,10 +175,36 @@ impl LWEToGLWESwitchingKey> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKey::alloc_bytes(infos) + GLWESwitchingKey::alloc_bytes(infos) } pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { - GGLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + } +} + +pub trait LWEToGLWESwitchingKeyToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>; +} + +impl LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> { + LWEToGLWESwitchingKey(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey +where + GLWESwitchingKey: GLWESwitchingKeyToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> { + LWEToGLWESwitchingKey(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/mod.rs b/poulpy-core/src/layouts/mod.rs index 2b13751..775fb6f 100644 --- a/poulpy-core/src/layouts/mod.rs +++ b/poulpy-core/src/layouts/mod.rs @@ -47,6 +47,7 @@ pub enum BuildError { ZeroLimbs, ZeroRank, ZeroDigits, + VecZnxColsNotOne, } /// Newtype over `u32` with arithmetic and comparisons against same type and `u32`. diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index 594aa0a..2eda4e1 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -4,23 +4,25 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + prepared::{ + GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWEAutomorphismKeyPrepared { - pub(crate) key: GGLWESwitchingKeyPrepared, +pub struct AutomorphismKeyPrepared { + pub(crate) key: GLWESwitchingKeyPrepared, pub(crate) p: i64, } -impl GGLWEAutomorphismKeyPrepared { +impl AutomorphismKeyPrepared { pub fn p(&self) -> i64 { self.p } } -impl LWEInfos for GGLWEAutomorphismKeyPrepared { +impl LWEInfos for AutomorphismKeyPrepared { fn n(&self) -> Degree { self.key.n() } @@ -38,13 +40,23 @@ impl LWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GLWEInfos for GGLWEAutomorphismKeyPrepared { +pub trait SetP { + fn set_p(&mut self, p: i64); +} + +impl SetP for AutomorphismKeyPrepared { + fn set_p(&mut self, p: i64) { + self.p = p + } +} + +impl GLWEInfos for AutomorphismKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { +impl GGLWEInfos for AutomorphismKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -62,7 +74,7 @@ impl GGLWEInfos for GGLWEAutomorphismKeyPrepared { } } -impl GGLWEAutomorphismKeyPrepared, B> { +impl AutomorphismKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -73,8 +85,8 @@ impl GGLWEAutomorphismKeyPrepared, B> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - GGLWEAutomorphismKeyPrepared::, B> { - key: GGLWESwitchingKeyPrepared::alloc(module, infos), + AutomorphismKeyPrepared::, B> { + key: GLWESwitchingKeyPrepared::alloc(module, infos), p: 0, } } @@ -83,8 +95,8 @@ impl GGLWEAutomorphismKeyPrepared, B> { where Module: VmpPMatAlloc, { - GGLWEAutomorphismKeyPrepared { - key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), + AutomorphismKeyPrepared { + key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize), p: 0, } } @@ -99,43 +111,104 @@ impl GGLWEAutomorphismKeyPrepared, B> { infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize) } } -impl PrepareScratchSpace for GGLWEAutomorphismKeyPrepared, B> +pub trait AutomorphismKeyPrepareTmpBytes { + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos; +} + +impl AutomorphismKeyPrepareTmpBytes for Module where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: GLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) + where + A: GGLWEInfos, + { + self.glwe_switching_key_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWEAutomorphismKeyPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWEAutomorphismKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); +impl AutomorphismKeyPrepared { + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize + where + Module: AutomorphismKeyPrepareTmpBytes, + { + module.automorphism_key_prepare_tmp_bytes(self); + } +} + +pub trait AutomorphismKeyPrepare { + fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) + where + R: AutomorphismKeyPreparedToMut, + O: AutomorphismKeyToRef; +} + +impl AutomorphismKeyPrepare for Module { + fn automorphism_key_prepare(&self, res: &R, other: &O, scratch: &Scratch) + where + R: AutomorphismKeyPreparedToMut, + O: AutomorphismKeyToRef, + { + self.key.prepare(self, &other.to_ref().key, scratch); self.p = other.p; } } -impl PrepareAlloc, B>> for GGLWEAutomorphismKey +pub trait AutomorphismKeyPrepareAlloc { + fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> + where + O: AutomorphismKeyToRef; +} + +impl AutomorphismKeyPrepareAlloc for Module where Module: VmpPMatAlloc + VmpPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWEAutomorphismKeyPrepared, B> { - let mut atk_prepared: GGLWEAutomorphismKeyPrepared, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); + fn automorphism_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> AutomorphismKeyPrepared, B> + where + O: AutomorphismKeyToRef, + { + let mut atk_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref()); + atk_prepared.prepare(self, &other.to_ref(), scratch); atk_prepared } } + +pub trait AutomorphismKeyPreparedToMut { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>; +} + +impl AutomorphismKeyPreparedToMut for AutomorphismKeyPrepared { + fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_mut(), + } + } +} + +pub trait AutomorphismKeyPreparedToRef { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>; +} + +impl AutomorphismKeyPreparedToRef for AutomorphismKeyPrepared { + fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> { + AutomorphismKeyPrepared { + p: self.p, + key: self.key.to_ref(), + } + } +} diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 4f22e6e..a19fd92 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -1,23 +1,22 @@ use poulpy_hal::{ api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes}, - layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos}, oep::VmpPMatAllocBytesImpl, }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, BuildError, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGLWECiphertextPrepared { +pub struct GGLWEPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGLWECiphertextPrepared { +impl LWEInfos for GGLWEPrepared { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -35,13 +34,13 @@ impl LWEInfos for GGLWECiphertextPrepared { } } -impl GLWEInfos for GGLWECiphertextPrepared { +impl GLWEInfos for GGLWEPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextPrepared { +impl GGLWEInfos for GGLWEPrepared { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -59,17 +58,17 @@ impl GGLWEInfos for GGLWECiphertextPrepared { } } -pub struct GGLWECiphertextPreparedBuilder { +pub struct GGLWEPreparedBuilder { data: Option>, base2k: Option, k: Option, dsize: Option, } -impl GGLWECiphertextPrepared { +impl GGLWEPrepared { #[inline] - pub fn builder() -> GGLWECiphertextPreparedBuilder { - GGLWECiphertextPreparedBuilder { + pub fn builder() -> GGLWEPreparedBuilder { + GGLWEPreparedBuilder { data: None, base2k: None, k: None, @@ -78,7 +77,7 @@ impl GGLWECiphertextPrepared { } } -impl GGLWECiphertextPreparedBuilder, B> { +impl GGLWEPreparedBuilder, B> { #[inline] pub fn layout(mut self, infos: &A) -> Self where @@ -99,7 +98,7 @@ impl GGLWECiphertextPreparedBuilder, B> { } } -impl GGLWECiphertextPreparedBuilder { +impl GGLWEPreparedBuilder { #[inline] pub fn data(mut self, data: VmpPMat) -> Self { self.data = Some(data); @@ -122,7 +121,7 @@ impl GGLWECiphertextPreparedBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -152,7 +151,7 @@ impl GGLWECiphertextPreparedBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGLWECiphertextPrepared { + Ok(GGLWEPrepared { data, base2k, k, @@ -161,7 +160,7 @@ impl GGLWECiphertextPreparedBuilder { } } -impl GGLWECiphertextPrepared, B> { +impl GGLWEPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -260,12 +259,21 @@ impl GGLWECiphertextPrepared, B> { } } -impl PrepareScratchSpace for GGLWECiphertextPrepared, B> +pub trait GGLWEPrepareTmpBytes { + fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GGLWEPrepareTmpBytes for Module where Module: VmpPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.vmp_prepare_tmp_bytes( infos.dnum().into(), infos.rank_in().into(), (infos.rank() + 1).into(), @@ -274,25 +282,106 @@ where } } -impl Prepare> for GGLWECiphertextPrepared -where - Module: VmpPrepare, -{ - fn prepare(&mut self, module: &Module, other: &GGLWECiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; +impl GGLWEPrepared, B> { + pub fn prepare_tmp_bytes(&self, module: &Module) + where + Module: GGLWEPrepareTmpBytes, + { + module.gglwe_prepare_tmp_bytes(self) } } -impl PrepareAlloc, B>> for GGLWECiphertext +pub trait GGLWEPrepare { + fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut, + O: GGLWEToRef; +} + +impl GGLWEPrepare for Module +where + Module: VmpPrepare, +{ + fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGLWEPreparedToMut, + O: GGLWEToRef, + { + let mut res: GGLWEPrepared<&mut [u8], B> = self.to_mut(); + let other: GGLWE<&[u8]> = other.to_ref(); + + assert_eq!(res.n(), self.n() as u32); + assert_eq!(other.n(), self.n() as u32); + assert_eq!(res.base2k, other.base2k); + assert_eq!(res.k, other.k); + assert_eq!(res.dsize, other.dsize); + + self.vmp_prepare(&mut res.data, &other.data, scratch); + } +} + +impl GGLWEPrepared +where + Module: GGLWEPrepare, +{ + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GGLWEToRef, + { + module.gglwe_prepare(self, other, scratch); + } +} + +pub trait GGLWEPrepareAlloc { + fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B>; +} + +impl GGLWEPrepareAlloc for Module where Module: VmpPMatAlloc + VmpPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWECiphertextPrepared, B> { - let mut atk_prepared: GGLWECiphertextPrepared, B> = GGLWECiphertextPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared + fn gglwe_prepare_alloc(&self, other: &O, scratch: &mut Scratch) -> GGLWEPrepared, B> { + let mut ct_prepared: GGLWEPrepared, B> = GGLWEPrepared::alloc(self, &other.to_ref()); + ct_prepared.prepare(self, &other.to_ref(), scratch); + ct_prepared + } +} + +impl GGLWE { + fn prepare_alloc(&self, module: &Module, scratch: &Scratch) -> GGLWEPrepared, B> + where + Module: GGLWEPrepareAlloc, + { + module.gglwe_prepare_alloc(self, scratch) + } +} + +pub trait GGLWEPreparedToMut { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B>; +} + +impl GGLWEPreparedToMut for GGLWEPrepared { + fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_mut(), + } + } +} + +pub trait GGLWEPreparedToRef { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B>; +} + +impl GGLWEPreparedToRef for GGLWEPrepared { + fn to_ref(&self) -> GGLWEPrepared<&[u8], B> { + GGLWEPrepared { + k: self.k, + base2k: self.base2k, + dsize: self.dsize, + data: self.data.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index c9110c1..7f2f887 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -1,21 +1,36 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWECiphertextPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, + prepared::{GGLWEPrepare, GGLWEPrepareTmpBytes, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef}, }; #[derive(PartialEq, Eq)] -pub struct GGLWESwitchingKeyPrepared { - pub(crate) key: GGLWECiphertextPrepared, +pub struct GLWESwitchingKeyPrepared { + pub(crate) key: GGLWEPrepared, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } -impl LWEInfos for GGLWESwitchingKeyPrepared { +pub(crate) trait GLWESwitchingKeyPreparedSetMetaData { + fn set_sk_in_n(&mut self, sk_in_n: usize); + fn set_sk_out_n(&mut self, sk_out_n: usize); +} + +impl GLWESwitchingKeyPreparedSetMetaData for GLWESwitchingKeyPrepared { + fn set_sk_in_n(&mut self, sk_in_n: usize) { + self.sk_in_n = sk_in_n + } + + fn set_sk_out_n(&mut self, sk_out_n: usize) { + self.sk_out_n = self.sk_out_n + } +} + +impl LWEInfos for GLWESwitchingKeyPrepared { fn n(&self) -> Degree { self.key.n() } @@ -33,13 +48,13 @@ impl LWEInfos for GGLWESwitchingKeyPrepared { } } -impl GLWEInfos for GGLWESwitchingKeyPrepared { +impl GLWEInfos for GLWESwitchingKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWESwitchingKeyPrepared { +impl GGLWEInfos for GLWESwitchingKeyPrepared { fn rank_in(&self) -> Rank { self.key.rank_in() } @@ -57,15 +72,15 @@ impl GGLWEInfos for GGLWESwitchingKeyPrepared { } } -impl GGLWESwitchingKeyPrepared, B> { +impl GLWESwitchingKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, Module: VmpPMatAlloc, { debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc(module, infos), + GLWESwitchingKeyPrepared::, B> { + key: GGLWEPrepared::alloc(module, infos), sk_in_n: 0, sk_out_n: 0, } @@ -83,8 +98,8 @@ impl GGLWESwitchingKeyPrepared, B> { where Module: VmpPMatAlloc, { - GGLWESwitchingKeyPrepared::, B> { - key: GGLWECiphertextPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), + GLWESwitchingKeyPrepared::, B> { + key: GGLWEPrepared::alloc_with(module, base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } @@ -96,7 +111,7 @@ impl GGLWESwitchingKeyPrepared, B> { Module: VmpPMatAllocBytes, { debug_assert_eq!(module.n() as u32, infos.n(), "module.n() != infos.n()"); - GGLWECiphertextPrepared::alloc_bytes(module, infos) + GGLWEPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with( @@ -111,37 +126,122 @@ impl GGLWESwitchingKeyPrepared, B> { where Module: VmpPMatAllocBytes, { - GGLWECiphertextPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) + GGLWEPrepared::alloc_bytes_with(module, base2k, k, rank_in, rank_out, dnum, dsize) } } -impl PrepareScratchSpace for GGLWESwitchingKeyPrepared, B> +pub trait GLWESwitchingKeyPrepareTmpBytes { + fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos; +} + +impl GLWESwitchingKeyPrepareTmpBytes for Module where - GGLWECiphertextPrepared, B>: PrepareScratchSpace, + Module: GGLWEPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWECiphertextPrepared::prepare_scratch_space(module, infos) + fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.gglwe_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWESwitchingKeyPrepared +impl GLWESwitchingKeyPrepared, B> where - Module: VmpPrepare, + Module: GGLWEPrepareTmpBytes, { - fn prepare(&mut self, module: &Module, other: &GGLWESwitchingKey, scratch: &mut Scratch) { - self.key.prepare(module, &other.key, scratch); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { + module.gglwe_prepare_tmp_bytes(self) } } -impl PrepareAlloc, B>> for GGLWESwitchingKey +pub trait GLWESwitchingKeyPrepare { + fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + where + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, + O: GLWESwitchingKeyToRef; +} + +impl GLWESwitchingKeyPrepare for Module where - Module: VmpPMatAlloc + VmpPrepare, + Module: GGLWEPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWESwitchingKeyPrepared, B> { - let mut atk_prepared: GGLWESwitchingKeyPrepared, B> = GGLWESwitchingKeyPrepared::alloc(module, self); - atk_prepared.prepare(module, self, scratch); - atk_prepared + fn glwe_switching_prepare(&self, res: &R, other: &O, scratch: &mut Scratch) + where + R: GLWESwitchingKeyPreparedToMut + GLWESwitchingKeyPreparedSetMetaData, + O: GLWESwitchingKeyToRef, + { + self.gglwe_prepare(&res.to_mut(), other, scratch); + res.set_sk_in_n(other.sk_in_n); + res.set_sk_out_n(other.sk_out_n); + } +} + +impl GLWESwitchingKeyPrepared { + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef, + Module: GLWESwitchingKeyPrepare, + { + module.glwe_switching_prepare(self, other, scratch); + } +} + +pub trait GLWESwitchingKeyPrepareAlloc { + fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef; +} + +impl GLWESwitchingKeyPrepareAlloc for Module +where + Module: GLWESwitchingKeyPrepare, +{ + fn glwe_switching_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GLWESwitchingKeyToRef, + { + let mut ct_prepared: GLWESwitchingKeyPrepared, B> = GLWESwitchingKeyPrepared::alloc(self, self); + self.glwe_switching_prepare(&mut ct_prepared, other, scratch); + ct_prepared + } +} + +impl GLWESwitchingKey { + pub fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GLWESwitchingKeyPrepared, B> + where + Module: GLWESwitchingKeyPrepareAlloc, + { + module.glwe_switching_key_prepare_alloc(self, scratch); + } +} + +pub trait GLWESwitchingKeyPreparedToMut { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>; +} + +impl GLWESwitchingKeyPreparedToMut for GLWESwitchingKeyPrepared { + fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_mut(), + } + } +} + +pub trait GLWESwitchingKeyPreparedToRef { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>; +} + +impl GLWESwitchingKeyPreparedToRef for GLWESwitchingKeyPrepared { + fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> { + GLWESwitchingKeyPrepared { + sk_in_n: self.sk_in_n, + sk_out_n: self.sk_out_n, + key: self.key.to_ref(), + } } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index 4343e30..eed6ff2 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -1,19 +1,23 @@ use poulpy_hal::{ - api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare}, + api::{VmpPMatAlloc, VmpPMatAllocBytes}, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision, + compressed::TensorKeyCompressedToMut, + prepared::{ + GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, + GLWESwitchingKeyPreparedToRef, + }, }; #[derive(PartialEq, Eq)] -pub struct GGLWETensorKeyPrepared { - pub(crate) keys: Vec>, +pub struct TensorKeyPrepared { + pub(crate) keys: Vec>, } -impl LWEInfos for GGLWETensorKeyPrepared { +impl LWEInfos for TensorKeyPrepared { fn n(&self) -> Degree { self.keys[0].n() } @@ -31,13 +35,13 @@ impl LWEInfos for GGLWETensorKeyPrepared { } } -impl GLWEInfos for GGLWETensorKeyPrepared { +impl GLWEInfos for TensorKeyPrepared { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWETensorKeyPrepared { +impl GGLWEInfos for TensorKeyPrepared { fn rank_in(&self) -> Rank { self.rank_out() } @@ -55,7 +59,7 @@ impl GGLWEInfos for GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared, B> { +impl TensorKeyPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGLWEInfos, @@ -80,10 +84,10 @@ impl GGLWETensorKeyPrepared, B> { where Module: VmpPMatAlloc, { - let mut keys: Vec, B>> = Vec::new(); + let mut keys: Vec, B>> = Vec::new(); let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); (0..pairs).for_each(|_| { - keys.push(GGLWESwitchingKeyPrepared::alloc_with( + keys.push(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -109,7 +113,7 @@ impl GGLWETensorKeyPrepared, B> { let rank_out: usize = infos.rank_out().into(); let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); pairs - * GGLWESwitchingKeyPrepared::alloc_bytes_with( + * GLWESwitchingKeyPrepared::alloc_bytes_with( module, infos.base2k(), infos.k(), @@ -125,13 +129,13 @@ impl GGLWETensorKeyPrepared, B> { Module: VmpPMatAllocBytes, { let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize; - pairs * GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) + pairs * GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank, dnum, dsize) } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPrepared { // Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared { + pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -140,9 +144,9 @@ impl GGLWETensorKeyPrepared { } } -impl GGLWETensorKeyPrepared { +impl TensorKeyPrepared { // Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j]) - pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared { + pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared { if i > j { std::mem::swap(&mut i, &mut j); }; @@ -151,40 +155,125 @@ impl GGLWETensorKeyPrepared { } } -impl PrepareScratchSpace for GGLWETensorKeyPrepared, B> +pub trait TensorKeyPrepareTmpBytes { + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize; +} + +impl TensorKeyPrepareTmpBytes for Module where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + Module: GLWESwitchingKeyPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize { + self.glwe_switching_key_prepare_tmp_bytes(infos) } } -impl Prepare> for GGLWETensorKeyPrepared +impl TensorKeyPrepared, B> where - Module: VmpPrepare, + Module: TensorKeyPrepareTmpBytes, { - fn prepare(&mut self, module: &Module, other: &GGLWETensorKey, scratch: &mut Scratch) { - #[cfg(debug_assertions)] - { - assert_eq!(self.keys.len(), other.keys.len()); + fn prepare_tmp_bytes(&self, module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + { + module.tensor_key_prepare_tmp_bytes(infos) + } +} + +pub trait TensorKeyPrepare { + fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef; +} + +impl TensorKeyPrepare for Module +where + Module: GLWESwitchingKeyPrepare, +{ + fn tensor_key_prepare(&self, res: &mut R, other: &O, scratch: &Scratch) + where + R: TensorKeyPreparedToMut, + O: TensorKeyToRef, + { + let res = res.to_mut(); + let other = other.to_ref(); + + assert_eq!(self.keys.len(), other.keys.len()); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.glwe_switching_prepare(a, b, scratch); } - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.prepare(module, b, scratch); - }); } } -impl PrepareAlloc, B>> for GGLWETensorKey +impl TensorKeyPrepared where - Module: VmpPMatAlloc + VmpPrepare, + Module: TensorKeyPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGLWETensorKeyPrepared, B> { - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, self); - tsk_prepared.prepare(module, self, scratch); - tsk_prepared + fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef, + { + module.tensor_key_prepare(self, other, scratch); + } +} + +pub trait TensorKeyPrepareAlloc { + fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef; +} + +impl TensorKeyPrepareAlloc for Module +where + Module: TensorKeyPrepare, +{ + fn tensor_key_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: TensorKeyToRef, + { + let mut ct_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(self, other); + self.tensor_key_prepare(ct_prepared, other, scratch); + ct_prepared + } +} + +impl TensorKey { + pub fn prepare_alloc(&self, module: &Module, scratch: &Scratch) + where + Module: TensorKeyPrepareAlloc, + { + module.tensor_key_prepare_alloc(self, scratch); + } +} + +pub trait TensorKeyPreparedToMut { + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>; +} + +impl TensorKeyPreparedToMut for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToMut, +{ + fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> { + TensorKeyPrepared { + keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), + } + } +} + +pub trait TensorKeyPreparedToRef { + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>; +} + +impl TensorKeyPreparedToRef for TensorKeyPrepared +where + GLWESwitchingKeyPrepared: GLWESwitchingKeyPreparedToRef, +{ + fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> { + TensorKeyPrepared { + keys: self.keys.iter().map(|c| c.to_ref()).collect(), + } } } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index a3871f6..120c9ed 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -5,19 +5,18 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, BuildError, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, BuildError, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq)] -pub struct GGSWCiphertextPrepared { +pub struct GGSWPrepared { pub(crate) data: VmpPMat, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) dsize: Dsize, } -impl LWEInfos for GGSWCiphertextPrepared { +impl LWEInfos for GGSWPrepared { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -35,13 +34,13 @@ impl LWEInfos for GGSWCiphertextPrepared { } } -impl GLWEInfos for GGSWCiphertextPrepared { +impl GLWEInfos for GGSWPrepared { fn rank(&self) -> Rank { Rank(self.data.cols_out() as u32 - 1) } } -impl GGSWInfos for GGSWCiphertextPrepared { +impl GGSWInfos for GGSWPrepared { fn dsize(&self) -> Dsize { self.dsize } @@ -58,7 +57,7 @@ pub struct GGSWCiphertextPreparedBuilder { dsize: Option, } -impl GGSWCiphertextPrepared { +impl GGSWPrepared { #[inline] pub fn builder() -> GGSWCiphertextPreparedBuilder { GGSWCiphertextPreparedBuilder { @@ -129,7 +128,7 @@ impl GGSWCiphertextPreparedBuilder { self } - pub fn build(self) -> Result, BuildError> { + pub fn build(self) -> Result, BuildError> { let data: VmpPMat = self.data.ok_or(BuildError::MissingData)?; let base2k: Base2K = self.base2k.ok_or(BuildError::MissingBase2K)?; let k: TorusPrecision = self.k.ok_or(BuildError::MissingK)?; @@ -159,7 +158,7 @@ impl GGSWCiphertextPreparedBuilder { return Err(BuildError::ZeroLimbs); } - Ok(GGSWCiphertextPrepared { + Ok(GGSWPrepared { data, base2k, k, @@ -168,7 +167,7 @@ impl GGSWCiphertextPreparedBuilder { } } -impl GGSWCiphertextPrepared, B> { +impl GGSWPrepared, B> { pub fn alloc(module: &Module, infos: &A) -> Self where A: GGSWInfos, @@ -252,18 +251,27 @@ impl GGSWCiphertextPrepared, B> { } } -impl GGSWCiphertextPrepared { +impl GGSWPrepared { pub fn data(&self) -> &VmpPMat { &self.data } } -impl PrepareScratchSpace for GGSWCiphertextPrepared, B> +pub trait GGSWPrepareTmpBytes { + fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos; +} + +impl GGSWPrepareTmpBytes for Module where Module: VmpPrepareTmpBytes, { - fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - module.vmp_prepare_tmp_bytes( + fn ggsw_prepare_tmp_bytes(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.vmp_prepare_tmp_bytes( infos.dnum().into(), (infos.rank() + 1).into(), (infos.rank() + 1).into(), @@ -272,57 +280,110 @@ where } } -impl Prepare> for GGSWCiphertextPrepared +impl GGSWPrepared, B> +where + Module: GGSWPrepareTmpBytes, +{ + pub fn prepare_tmp_bytes(&self, module: Module, infos: &A) -> usize + where + A: GGSWInfos, + { + module.ggsw_prepare_tmp_bytes(self) + } +} + +pub trait GGSWPrepare { + fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGSWPreparedToMut, + O: GGSWToRef; +} + +impl GGSWPrepare for Module where Module: VmpPrepare, { - fn prepare(&mut self, module: &Module, other: &GGSWCiphertext, scratch: &mut Scratch) { - module.vmp_prepare(&mut self.data, &other.data, scratch); - self.k = other.k; - self.base2k = other.base2k; - self.dsize = other.dsize; + fn ggsw_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + where + R: GGSWPreparedToMut, + O: GGSWToRef, + { + let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut(); + let other: GGSW<&[u8]> = other.to_ref(); + assert_eq!(res.k, other.k); + assert_eq!(res.base2k, other.base2k); + assert_eq!(res.dsize, other.dsize); + self.vmp_prepare(&mut res.data, &other.data, scratch); } } -impl PrepareAlloc, B>> for GGSWCiphertext +impl GGSWPrepared where - Module: VmpPMatAlloc + VmpPrepare, + Module: GGSWPrepare, { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> GGSWCiphertextPrepared, B> { - let mut ggsw_prepared: GGSWCiphertextPrepared, B> = GGSWCiphertextPrepared::alloc(module, self); - ggsw_prepared.prepare(module, self, scratch); - ggsw_prepared + pub fn prepare(&mut self, module: &Module, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef, + { + module.ggsw_prepare(self, other, scratch); } } -pub trait GGSWCiphertextPreparedToMut { - fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B>; +pub trait GGSWPrepareAlloc { + fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef; } -impl GGSWCiphertextPreparedToMut for GGSWCiphertextPrepared { - fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B> { - GGSWCiphertextPrepared::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_mut()) - .build() - .unwrap() +impl GGSWPrepareAlloc for Module +where + Module: GGSWPrepare, +{ + fn ggsw_prepare_alloc(&self, other: &O, scratch: &mut Scratch) + where + O: GGSWToRef, + { + let mut ct_prepared: GGSWPrepared, B> = GGSWPrepared::alloc(self, other); + self.ggsw_prepare(&mut ct_prepared, other, scratch); + ct_prepared + } +} + +impl GGSW { + fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) + where + Module: GGSWPrepareAlloc, + { + module.ggsw_prepare_alloc(self, scratch); + } +} + +pub trait GGSWPreparedToMut { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>; +} + +impl GGSWPreparedToMut for GGSWPrepared { + fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_mut(), + } } } pub trait GGSWCiphertextPreparedToRef { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>; + fn to_ref(&self) -> GGSWPrepared<&[u8], B>; } -impl GGSWCiphertextPreparedToRef for GGSWCiphertextPrepared { - fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B> { - GGSWCiphertextPrepared::builder() - .base2k(self.base2k()) - .dsize(self.dsize()) - .k(self.k()) - .data(self.data.to_ref()) - .build() - .unwrap() +impl GGSWCiphertextPreparedToRef for GGSWPrepared { + fn to_ref(&self) -> GGSWPrepared<&[u8], B> { + GGSWPrepared { + base2k: self.base2k, + k: self.k, + dsize: self.dsize, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 3211017..0cbbdd0 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -6,10 +6,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{ - Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, - }, + layouts::{Base2K, BuildError, Degree, GLWEInfos, GLWEPublicKey, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq)] diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 234ea2a..a8e436d 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -5,10 +5,7 @@ use poulpy_hal::{ use crate::{ dist::Distribution, - layouts::{ - Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision, - prepared::{Prepare, PrepareAlloc, PrepareScratchSpace}, - }, + layouts::{Base2K, Degree, GLWEInfos, GLWESecret, LWEInfos, Rank, TorusPrecision}, }; pub struct GLWESecretPrepared { @@ -128,11 +125,11 @@ impl GLWESecretPreparedToRef for GLWESecretPrepared { - fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B>; + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B>; } impl GLWESecretPreparedToMut for GLWESecretPrepared { - fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B> { + fn to_mut(&mut self) -> GLWESecretPrepared<&mut [u8], B> { GLWESecretPrepared { dist: self.dist, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index f241c6d..cea8b41 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -4,12 +4,12 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWEKey, LWEInfos, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision, + prepared::GLWESwitchingKeyPrepared, }; #[derive(PartialEq, Eq)] -pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct GLWEToLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for GLWEToLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -69,14 +69,14 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -102,27 +102,27 @@ impl GLWEToLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } } impl PrepareScratchSpace for GLWEToLWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } -impl PrepareAlloc, B>> for GLWEToLWEKey +impl PrepareAlloc, B>> for GLWEToLWESwitchingKey where Module: VmpPrepare + VmpPMatAlloc, { @@ -133,11 +133,11 @@ where } } -impl Prepare> for GLWEToLWESwitchingKeyPrepared +impl Prepare> for GLWEToLWESwitchingKeyPrepared where Module: VmpPrepare, { - fn prepare(&mut self, module: &Module, other: &GLWEToLWEKey, scratch: &mut Scratch) { + fn prepare(&mut self, module: &Module, other: &GLWEToLWESwitchingKey, scratch: &mut Scratch) { self.0.prepare(module, &other.0, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 5f0cf14..8e21f23 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -5,11 +5,11 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + prepared::GLWESwitchingKeyPrepared, }; #[derive(PartialEq, Eq)] -pub struct LWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -73,14 +73,14 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -111,23 +111,23 @@ impl LWESwitchingKeyPrepared, B> { 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } } impl PrepareScratchSpace for LWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 7c2023a..253c8cc 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -5,12 +5,12 @@ use poulpy_hal::{ use crate::layouts::{ Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace}, + prepared::GLWESwitchingKeyPrepared, }; /// A special [GLWESwitchingKey] required to for the conversion from [LWECiphertext] to [GLWECiphertext]. #[derive(PartialEq, Eq)] -pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GGLWESwitchingKeyPrepared); +pub struct LWEToGLWESwitchingKeyPrepared(pub(crate) GLWESwitchingKeyPrepared); impl LWEInfos for LWEToGLWESwitchingKeyPrepared { fn base2k(&self) -> Base2K { @@ -70,14 +70,14 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GGLWESwitchingKeyPrepared::alloc(module, infos)) + Self(GLWESwitchingKeyPrepared::alloc(module, infos)) } pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self where Module: VmpPMatAlloc, { - Self(GGLWESwitchingKeyPrepared::alloc_with( + Self(GLWESwitchingKeyPrepared::alloc_with( module, base2k, k, @@ -103,23 +103,23 @@ impl LWEToGLWESwitchingKeyPrepared, B> { 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GGLWESwitchingKeyPrepared::alloc_bytes(module, infos) + GLWESwitchingKeyPrepared::alloc_bytes(module, infos) } pub fn alloc_bytes_with(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize where Module: VmpPMatAllocBytes, { - GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } } impl PrepareScratchSpace for LWEToGLWESwitchingKeyPrepared, B> where - GGLWESwitchingKeyPrepared, B>: PrepareScratchSpace, + GLWESwitchingKeyPrepared, B>: PrepareScratchSpace, { fn prepare_scratch_space(module: &Module, infos: &A) -> usize { - GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) + GLWESwitchingKeyPrepared::prepare_scratch_space(module, infos) } } diff --git a/poulpy-core/src/layouts/prepared/mod.rs b/poulpy-core/src/layouts/prepared/mod.rs index eb47848..296144a 100644 --- a/poulpy-core/src/layouts/prepared/mod.rs +++ b/poulpy-core/src/layouts/prepared/mod.rs @@ -19,16 +19,3 @@ pub use glwe_sk::*; pub use glwe_to_lwe_ksk::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; -use poulpy_hal::layouts::{Backend, Module, Scratch}; - -pub trait PrepareScratchSpace { - fn prepare_scratch_space(module: &Module, infos: &T) -> usize; -} - -pub trait PrepareAlloc { - fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> T; -} - -pub trait Prepare { - fn prepare(&mut self, module: &Module, other: &T, scratch: &mut Scratch); -} diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 0712b7f..70a8ef3 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGLWE, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGLWECiphertext { +impl GGLWE { pub fn assert_noise( &self, module: &Module, diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 03bb0c0..84a2f87 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -9,11 +9,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{ - GGSWCiphertext, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, -}; +use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GGSWCiphertext { +impl GGSW { pub fn assert_noise( &self, module: &Module, @@ -87,7 +85,7 @@ impl GGSWCiphertext { } } -impl GGSWCiphertext { +impl GGSW { pub fn print_noise( &self, module: &Module, diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 1a5a6ce..0a94b39 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,11 +6,11 @@ use poulpy_hal::{ use crate::{ dist::Distribution, layouts::{ - Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, + GLWESecret, GLWESwitchingKey, Rank, TensorKey, prepared::{ - GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, - GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, + GLWESwitchingKeyPrepared, TensorKeyPrepared, }, }, }; @@ -34,31 +34,31 @@ pub trait TakeGLWEPt { } pub trait TakeGGLWE { - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEPrepared { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGSW { - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos; } pub trait TakeGGSWPrepared { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos; } pub trait TakeGGSWPreparedSlice { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GGSWInfos; } @@ -84,37 +84,37 @@ pub trait TakeGLWEPkPrepared { } pub trait TakeGLWESwitchingKey { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWESwitchingKeyPrepared { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeTensorKey { - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWETensorKeyPrepared { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEAutomorphismKey { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos; } pub trait TakeGGLWEAutomorphismKeyPrepared { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos; } @@ -184,7 +184,7 @@ impl TakeGGLWE for Scratch where Scratch: TakeMatZnx, { - fn take_gglwe(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self) + fn take_gglwe(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self) where A: GGLWEInfos, { @@ -196,7 +196,7 @@ where infos.size(), ); ( - GGLWECiphertext::builder() + GGLWE::builder() .base2k(infos.base2k()) .k(infos.k()) .dsize(infos.dsize()) @@ -212,7 +212,7 @@ impl TakeGGLWEPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_prepared(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { @@ -224,7 +224,7 @@ where infos.size(), ); ( - GGLWECiphertextPrepared::builder() + GGLWEPrepared::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -240,7 +240,7 @@ impl TakeGGSW for Scratch where Scratch: TakeMatZnx, { - fn take_ggsw(&mut self, infos: &A) -> (GGSWCiphertext<&mut [u8]>, &mut Self) + fn take_ggsw(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self) where A: GGSWInfos, { @@ -252,7 +252,7 @@ where infos.size(), ); ( - GGSWCiphertext::builder() + GGSW::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -268,7 +268,7 @@ impl TakeGGSWPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWCiphertextPrepared<&mut [u8], B>, &mut Self) + fn take_ggsw_prepared(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self) where A: GGSWInfos, { @@ -280,7 +280,7 @@ where infos.size(), ); ( - GGSWCiphertextPrepared::builder() + GGSWPrepared::builder() .base2k(infos.base2k()) .dsize(infos.dsize()) .k(infos.k()) @@ -296,12 +296,12 @@ impl TakeGGSWPreparedSlice for Scratch where Scratch: TakeGGSWPrepared, { - fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_ggsw_prepared_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GGSWInfos, { let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { let (ct, new_scratch) = scratch.take_ggsw_prepared(infos); scratch = new_scratch; @@ -390,13 +390,13 @@ impl TakeGLWESwitchingKey for Scratch where Scratch: TakeMatZnx, { - fn take_glwe_switching_key(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self) + fn take_glwe_switching_key(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe(infos); ( - GGLWESwitchingKey { + GLWESwitchingKey { key: data, sk_in_n: 0, sk_out_n: 0, @@ -410,13 +410,13 @@ impl TakeGGLWESwitchingKeyPrepared for Scratch where Scratch: TakeGGLWEPrepared, { - fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_switching_key_prepared(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe_prepared(infos); ( - GGLWESwitchingKeyPrepared { + GLWESwitchingKeyPrepared { key: data, sk_in_n: 0, sk_out_n: 0, @@ -430,12 +430,12 @@ impl TakeGGLWEAutomorphismKey for Scratch where Scratch: TakeMatZnx, { - fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self) + fn take_gglwe_automorphism_key(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_glwe_switching_key(infos); - (GGLWEAutomorphismKey { key: data, p: 0 }, scratch) + (AutomorphismKey { key: data, p: 0 }, scratch) } } @@ -443,12 +443,12 @@ impl TakeGGLWEAutomorphismKeyPrepared for Scratch where Scratch: TakeGGLWESwitchingKeyPrepared, { - fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_automorphism_key_prepared(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { let (data, scratch) = self.take_gglwe_switching_key_prepared(infos); - (GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch) + (AutomorphismKeyPrepared { key: data, p: 0 }, scratch) } } @@ -456,7 +456,7 @@ impl TakeTensorKey for Scratch where Scratch: TakeMatZnx, { - fn take_tensor_key(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self) + fn take_tensor_key(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self) where A: GGLWEInfos, { @@ -465,7 +465,7 @@ where infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Scratch = self; @@ -483,7 +483,7 @@ where scratch = s; keys.push(gglwe); } - (GGLWETensorKey { keys }, scratch) + (TensorKey { keys }, scratch) } } @@ -491,7 +491,7 @@ impl TakeGGLWETensorKeyPrepared for Scratch where Scratch: TakeVmpPMat, { - fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self) + fn take_gglwe_tensor_key_prepared(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self) where A: GGLWEInfos, { @@ -501,7 +501,7 @@ where "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - let mut keys: Vec> = Vec::new(); + let mut keys: Vec> = Vec::new(); let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize; let mut scratch: &mut Scratch = self; @@ -519,6 +519,6 @@ where scratch = s; keys.push(gglwe); } - (GGLWETensorKeyPrepared { keys }, scratch) + (TensorKeyPrepared { keys }, scratch) } } diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 6e08395..50dc0d6 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,12 +1,12 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWECiphertext, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, - GLWECiphertext, GLWEToLWEKey, LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TorusPrecision, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey, + LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - GGLWEAutomorphismKeyCompressed, GGLWECiphertextCompressed, GGLWEKeyCompressed, GGLWETensorKeyCompressed, - GGSWCiphertextCompressed, GLWECiphertextCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, - LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, + GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, + LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed, }, }; @@ -44,7 +44,7 @@ fn lwe_compressed_serialization() { #[test] fn test_gglwe_serialization() { - let original: GGLWECiphertext> = GGLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } @@ -57,44 +57,45 @@ fn test_gglwe_compressed_serialization() { #[test] fn test_glwe_switching_key_serialization() { - let original: GGLWESwitchingKey> = GGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { - let original: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKeyCompressed> = + GLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_serialization() { - let original: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKey> = AutomorphismKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_automorphism_key_compressed_serialization() { - let original: GGLWEAutomorphismKeyCompressed> = - GGLWEAutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = + AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: GGLWETensorKey> = GGLWETensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWEKey> = GLWEToLWEKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } @@ -132,7 +133,7 @@ fn lwe_switching_key_compressed_serialization() { #[test] fn ggsw_serialization() { - let original: GGSWCiphertext> = GGSWCiphertext::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs index 1dd3e58..34fccab 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -84,7 +84,7 @@ where let dnum_out: usize = k_out / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_in_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_in_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -93,7 +93,7 @@ where rank: rank.into(), }; - let auto_key_out_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_out_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -102,7 +102,7 @@ where rank: rank.into(), }; - let auto_key_apply_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -111,18 +111,18 @@ where rank: rank.into(), }; - let mut auto_key_in: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) - | GGLWEAutomorphismKey::automorphism_scratch_space( + AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_in_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply_infos) + | AutomorphismKey::automorphism_scratch_space( module, &auto_key_out_infos, &auto_key_in_infos, @@ -153,8 +153,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_apply_infos); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); @@ -293,7 +293,7 @@ where let dnum_in: usize = k_in / (base2k * di); let dnum_apply: usize = k_in.div_ceil(base2k * di); - let auto_key_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -302,7 +302,7 @@ where rank: rank.into(), }; - let auto_key_apply_layout: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let auto_key_apply_layout: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_apply.into(), @@ -311,17 +311,17 @@ where rank: rank.into(), }; - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) - | GGLWEAutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), + AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply) + | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), ); let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key); @@ -347,8 +347,8 @@ where scratch.borrow(), ); - let mut auto_key_apply_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); + let mut auto_key_apply_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_apply_layout); auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index 2fd7151..8d34288 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -19,8 +19,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -110,7 +110,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -119,7 +119,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -128,10 +128,10 @@ where rank: rank.into(), }; - let mut ct_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_layout); - let mut ct_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct_in: GGSW> = GGSW::alloc(&ggsw_in_layout); + let mut ct_out: GGSW> = GGSW::alloc(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -139,10 +139,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct_in) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), + GGSW::encrypt_sk_scratch_space(module, &ct_in) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) + | GGSW::automorphism_scratch_space(module, &ct_out, &ct_in, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; @@ -178,11 +178,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct_out.automorphism( @@ -291,7 +291,7 @@ where rank: rank.into(), }; - let tensor_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -300,7 +300,7 @@ where rank: rank.into(), }; - let auto_key_layout: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let auto_key_layout: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -309,9 +309,9 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_layout); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_layout); - let mut auto_key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&auto_key_layout); + let mut ct: GGSW> = GGSW::alloc(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -319,10 +319,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ct) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tensor_key) - | GGSWCiphertext::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), + GGSW::encrypt_sk_scratch_space(module, &ct) + | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key) + | TensorKey::encrypt_sk_scratch_space(module, &tensor_key) + | GGSW::automorphism_inplace_scratch_space(module, &ct, &auto_key, &tensor_key), ); let var_xs: f64 = 0.5; @@ -358,11 +358,11 @@ where scratch.borrow(), ); - let mut auto_key_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &auto_key_layout); + let mut auto_key_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &auto_key_layout); auto_key_prepared.prepare(module, &auto_key, scratch.borrow()); - let mut tsk_prepared: GGLWETensorKeyPrepared, B> = GGLWETensorKeyPrepared::alloc(module, &tensor_key_layout); + let mut tsk_prepared: TensorKeyPrepared, B> = TensorKeyPrepared::alloc(module, &tensor_key_layout); tsk_prepared.prepare(module, &tensor_key, scratch.borrow()); ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs index 5828c48..a15b2c1 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -91,7 +91,7 @@ where rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -100,7 +100,7 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&ct_in_infos); let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); @@ -112,7 +112,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) + AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) | GLWECiphertext::decrypt_scratch_space(module, &ct_out) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in) | GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), @@ -140,8 +140,8 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey_infos); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = + AutomorphismKeyPrepared::alloc(module, &autokey_infos); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct_out.automorphism(module, &ct_in, &autokey_prepared, scratch.borrow()); @@ -226,7 +226,7 @@ where rank: rank.into(), }; - let autokey_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let autokey_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -235,7 +235,7 @@ where dsize: di.into(), }; - let mut autokey: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&autokey_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); @@ -246,7 +246,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &autokey) + AutomorphismKey::encrypt_sk_scratch_space(module, &autokey) | GLWECiphertext::decrypt_scratch_space(module, &ct) | GLWECiphertext::encrypt_sk_scratch_space(module, &ct) | GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey), @@ -274,8 +274,7 @@ where scratch.borrow(), ); - let mut autokey_prepared: GGLWEAutomorphismKeyPrepared, B> = - GGLWEAutomorphismKeyPrepared::alloc(module, &autokey); + let mut autokey_prepared: AutomorphismKeyPrepared, B> = AutomorphismKeyPrepared::alloc(module, &autokey); autokey_prepared.prepare(module, &autokey, scratch.borrow()); ct.automorphism_inplace(module, &autokey_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index c2c81dc..7e3beba 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -16,9 +16,9 @@ use poulpy_hal::{ }; use crate::layouts::{ - Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, - TorusPrecision, + Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, + GLWEToLWESwitchingKey, LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, + LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, }; @@ -214,7 +214,7 @@ where let mut source_xe: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GLWEToLWEKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) + GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos) | LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), ); @@ -241,7 +241,7 @@ where scratch.borrow(), ); - let mut ksk: GLWEToLWEKey> = GLWEToLWEKey::alloc(&glwe_to_lwe_infos); + let mut ksk: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos); ksk.encrypt_sk( module, diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index e9b7c93..476abd1 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWEInfos, GLWESecret, - compressed::{Decompress, GGLWEAutomorphismKeyCompressed}, + AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, + compressed::{AutomorphismKeyCompressed, Decompress}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -75,7 +75,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -84,13 +84,13 @@ where rank: rank.into(), }; - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( module, &atk_infos, )); @@ -178,7 +178,7 @@ where let n: usize = module.n(); let dnum: usize = (k_ksk - di * base2k) / (di * base2k); - let atk_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let atk_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -187,12 +187,12 @@ where rank: rank.into(), }; - let mut atk_compressed: GGLWEAutomorphismKeyCompressed> = GGLWEAutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEAutomorphismKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space( module, &atk_infos, )); @@ -217,7 +217,7 @@ where }); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs index 249161b..b0a8a8b 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWECiphertextLayout, GGLWESwitchingKey, GLWESecret, - compressed::{Decompress, GGLWEKeyCompressed}, + GGLWECiphertextLayout, GLWESecret, GLWESwitchingKey, + compressed::{Decompress, GLWESwitchingKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -81,13 +81,13 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::encrypt_sk_scratch_space( module, &gglwe_infos, )); @@ -173,12 +173,12 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GGLWEKeyCompressed> = GGLWEKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed> = GLWESwitchingKeyCompressed::alloc(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWEKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKeyCompressed::encrypt_sk_scratch_space( module, &gglwe_infos, )); @@ -201,7 +201,7 @@ where scratch.borrow(), ); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs index 9eacf38..13ed1de 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs @@ -17,53 +17,16 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, + GGSW, GGSWCiphertextLayout, GLWESecret, compressed::{Decompress, GGSWCiphertextCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; -pub fn test_ggsw_encrypt_sk(module: &Module) +pub fn test_ggsw_encrypt_sk(module: &Module) where - Module: VecZnxDftAllocBytes - + VecZnxBigNormalize - + VecZnxDftApply - + SvpApplyDftToDftInplace - + VecZnxIdftApplyConsume - + VecZnxNormalizeTmpBytes - + VecZnxFillUniform - + VecZnxSubInplace - + VecZnxAddInplace - + VecZnxNormalizeInplace - + VecZnxAddNormal - + VecZnxNormalize - + VecZnxSub - + SvpPrepare - + SvpPPolAllocBytes - + SvpPPolAlloc - + VecZnxBigAddSmallInplace - + VecZnxAddScalarInplace - + VecZnxBigAllocBytes - + VecZnxBigAddInplace - + VecZnxCopy - + VmpPMatAlloc - + VmpPrepare - + VecZnxBigAlloc - + VecZnxDftAlloc - + VecZnxBigNormalizeTmpBytes - + VecZnxIdftApplyTmpA, - B: Backend - + TakeVecZnxDftImpl - + TakeVecZnxBigImpl - + TakeSvpPPolImpl - + ScratchOwnedAllocImpl - + ScratchOwnedBorrowImpl - + ScratchAvailableImpl - + TakeScalarZnxImpl - + TakeVecZnxImpl - + VecZnxDftAllocBytesImpl - + VecZnxBigAllocBytesImpl - + TakeSvpPPolImpl, + ScratchOwned: ScratchOwnedAlloc, + Module: SvpPrepare, { let base2k: usize = 12; let k: usize = 54; @@ -82,7 +45,7 @@ where rank: rank.into(), }; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -92,10 +55,7 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertext::encrypt_sk_scratch_space( - module, - &ggsw_infos, - )); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); @@ -206,7 +166,7 @@ where let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index 8bb9730..2944d59 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - Dsize, GGLWETensorKey, GGLWETensorKeyLayout, GLWEPlaintext, GLWESecret, - compressed::{Decompress, GGLWETensorKeyCompressed}, + Dsize, GLWEPlaintext, GLWESecret, TensorKey, TensorKeyLayout, + compressed::{Decompress, TensorKeyCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, }; @@ -71,7 +71,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos = GGLWETensorKeyLayout { + let tensor_key_infos = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -80,13 +80,13 @@ where rank: rank.into(), }; - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWETensorKey::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKey::encrypt_sk_scratch_space( module, &tensor_key_infos, )); @@ -190,7 +190,7 @@ where let n: usize = module.n(); let dnum: usize = k / base2k; - let tensor_key_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tensor_key_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k.into(), @@ -199,12 +199,12 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: GGLWETensorKeyCompressed> = GGLWETensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGLWETensorKeyCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(TensorKeyCompressed::encrypt_sk_scratch_space( module, &tensor_key_infos, )); @@ -217,7 +217,7 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: GGLWETensorKey> = GGLWETensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); diff --git a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs index 2cfa618..15ac048 100644 --- a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -81,7 +81,7 @@ where let dnum: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_in_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_in_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -91,7 +91,7 @@ where rank_out: rank_out.into(), }; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,9 +110,9 @@ where rank: rank_out.into(), }; - let mut ct_gglwe_in: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -121,14 +121,14 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) - | GGLWESwitchingKey::external_product_scratch_space( + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_in_infos) + | GLWESwitchingKey::external_product_scratch_space( module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos, ) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), ); let r: usize = 1; @@ -163,7 +163,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow()); @@ -263,7 +263,7 @@ where let dsize_in: usize = 1; - let gglwe_out_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_out_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -282,8 +282,8 @@ where rank: rank_out.into(), }; - let mut ct_gglwe: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -292,9 +292,9 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) - | GGLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_out_infos) + | GLWESwitchingKey::external_product_inplace_scratch_space(module, &gglwe_out_infos, &ggsw_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_infos), ); let r: usize = 1; @@ -329,7 +329,7 @@ where scratch.borrow(), ); - let ct_rgsw_prepared: GGSWCiphertextPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); + let ct_rgsw_prepared: GGSWPrepared, B> = ct_rgsw.prepare_alloc(module, scratch.borrow()); // gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k) ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs index 464eac2..32f15e9 100644 --- a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -128,9 +128,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGSWCiphertext::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) + | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); @@ -155,7 +155,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow()); @@ -265,8 +265,8 @@ where rank: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -282,9 +282,9 @@ where pt_apply.to_mut().raw_mut()[k] = 1; //X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) - | GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGSWCiphertext::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + | GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) + | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), ); let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); @@ -309,7 +309,7 @@ where scratch.borrow(), ); - let ct_rhs_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_rhs_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs index 1a21a0c..efd5af0 100644 --- a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product, }; @@ -96,7 +96,7 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -116,7 +116,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); @@ -143,7 +143,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow()); @@ -240,7 +240,7 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_apply_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); @@ -259,7 +259,7 @@ where pt_ggsw.raw_mut()[k] = 1; // X^{k} let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_apply_infos) + GGSW::encrypt_sk_scratch_space(module, &ggsw_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), ); @@ -286,7 +286,7 @@ where scratch.borrow(), ); - let ct_ggsw_prepared: GGSWCiphertextPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); + let ct_ggsw_prepared: GGSWPrepared, B> = ggsw_apply.prepare_alloc(module, scratch.borrow()); glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs index f131556..dfde51e 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -80,7 +80,7 @@ where let dnum_apply: usize = k_in.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), @@ -90,7 +90,7 @@ where rank_out: rank_out_s0s1.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -100,7 +100,7 @@ where rank_out: rank_out_s1s2.into(), }; - let gglwe_s0s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -110,20 +110,20 @@ where rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_scratch_space( module, &gglwe_s0s1_infos, &gglwe_s0s2_infos, @@ -160,7 +160,7 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) @@ -246,7 +246,7 @@ where let dnum: usize = k_out.div_ceil(base2k * di); let dsize_in: usize = 1; - let gglwe_s0s1_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s0s1_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_out.into(), @@ -256,7 +256,7 @@ where rank_out: rank_out.into(), }; - let gglwe_s1s2_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let gglwe_s1s2_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -266,18 +266,18 @@ where rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch_enc: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), + GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s0s1_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &gglwe_s1s2_infos), ); - let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GGLWESwitchingKey::keyswitch_inplace_scratch_space( + let mut scratch_apply: ScratchOwned = ScratchOwned::alloc(GLWESwitchingKey::keyswitch_inplace_scratch_space( module, &gglwe_s0s1_infos, &gglwe_s1s2_infos, @@ -315,13 +315,13 @@ where scratch_enc.borrow(), ); - let gglwe_s1s2_prepared: GGLWESwitchingKeyPrepared, B> = + let gglwe_s1s2_prepared: GLWESwitchingKeyPrepared, B> = gglwe_s1s2.prepare_alloc(module, scratch_apply.borrow()); // gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) gglwe_s0s1.keyswitch_inplace(module, &gglwe_s1s2_prepared, scratch_apply.borrow()); - let gglwe_s0s2: GGLWESwitchingKey> = gglwe_s0s1; + let gglwe_s0s2: GLWESwitchingKey> = gglwe_s0s1; let max_noise: f64 = log2_std_noise_gglwe_product( n as f64, diff --git a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs index c7e7c82..91114a7 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -18,9 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GGLWETensorKey, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, - GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, TensorKey, TensorKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc, TensorKeyPrepared}, }, noise::noise_ggsw_keyswitch, }; @@ -103,7 +102,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -112,7 +111,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -122,10 +121,10 @@ where rank_out: rank.into(), }; - let mut ggsw_in: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -133,10 +132,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_in_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_scratch_space( + GGSW::encrypt_sk_scratch_space(module, &ggsw_in_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) + | GGSW::keyswitch_scratch_space( module, &ggsw_out_infos, &ggsw_in_infos, @@ -182,8 +181,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch( module, @@ -282,7 +281,7 @@ where rank: rank.into(), }; - let tsk_infos: GGLWETensorKeyLayout = GGLWETensorKeyLayout { + let tsk_infos: TensorKeyLayout = TensorKeyLayout { n: n.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -291,7 +290,7 @@ where rank: rank.into(), }; - let ksk_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let ksk_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -301,9 +300,9 @@ where rank_out: rank.into(), }; - let mut ggsw_out: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_out_infos); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&tsk_infos); - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -311,10 +310,10 @@ where let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGSWCiphertext::encrypt_sk_scratch_space(module, &ggsw_out_infos) - | GGLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) - | GGLWETensorKey::encrypt_sk_scratch_space(module, &tsk_infos) - | GGSWCiphertext::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), + GGSW::encrypt_sk_scratch_space(module, &ggsw_out_infos) + | GLWESwitchingKey::encrypt_sk_scratch_space(module, &ksk_apply_infos) + | TensorKey::encrypt_sk_scratch_space(module, &tsk_infos) + | GGSW::keyswitch_inplace_scratch_space(module, &ggsw_out_infos, &ksk_apply_infos, &tsk_infos), ); let var_xs: f64 = 0.5; @@ -354,8 +353,8 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); - let tsk_prepared: GGLWETensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let tsk_prepared: TensorKeyPrepared, B> = tsk.prepare_alloc(module, scratch.borrow()); ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs index d745f1d..7c8c1ec 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -18,8 +18,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWESwitchingKey, GGLWESwitchingKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWESwitchingKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product, }; @@ -91,7 +91,7 @@ where rank: rank_out.into(), }; - let key_apply: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -101,7 +101,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply); let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); @@ -113,7 +113,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) + GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_in_infos) | GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); @@ -144,7 +144,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch(module, &glwe_in, &ksk_prepared, scratch.borrow()); @@ -224,7 +224,7 @@ where rank: rank.into(), }; - let key_apply_infos: GGLWESwitchingKeyLayout = GGLWESwitchingKeyLayout { + let key_apply_infos: GLWESwitchingKeyLayout = GLWESwitchingKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -234,7 +234,7 @@ where rank_out: rank.into(), }; - let mut key_apply: GGLWESwitchingKey> = GGLWESwitchingKey::alloc(&key_apply_infos); + let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply_infos); let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); @@ -245,7 +245,7 @@ where module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); let mut scratch: ScratchOwned = ScratchOwned::alloc( - GGLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) + GLWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), ); @@ -276,7 +276,7 @@ where scratch.borrow(), ); - let ksk_prepared: GGLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); + let ksk_prepared: GLWESwitchingKeyPrepared, B> = key_apply.prepare_alloc(module, scratch.borrow()); glwe_out.keyswitch_inplace(module, &ksk_prepared, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index de7fc9d..354738b 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -21,8 +21,8 @@ use poulpy_hal::{ use crate::{ GLWEOperations, GLWEPacker, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; @@ -95,7 +95,7 @@ where rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_ksk.into(), @@ -106,7 +106,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), ); @@ -124,8 +124,8 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); - let mut auto_keys: HashMap, B>> = HashMap::new(); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -135,7 +135,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index bf348ca..d3a8976 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -21,9 +21,8 @@ use poulpy_hal::{ use crate::{ encryption::SIGMA, layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - LWEInfos, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWEInfos, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product, }; @@ -90,7 +89,7 @@ where rank: rank.into(), }; - let key_infos: GGLWEAutomorphismKeyLayout = GGLWEAutomorphismKeyLayout { + let key_infos: AutomorphismKeyLayout = AutomorphismKeyLayout { n: n.into(), base2k: base2k.into(), k: k_autokey.into(), @@ -110,7 +109,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) - | GGLWEAutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) + | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), ); @@ -135,9 +134,9 @@ where scratch.borrow(), ); - let mut auto_keys: HashMap, B>> = HashMap::new(); + let mut auto_keys: HashMap, B>> = HashMap::new(); let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); - let mut tmp: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&key_infos); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -147,7 +146,7 @@ where &mut source_xe, scratch.borrow(), ); - let atk_prepared: GGLWEAutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); + let atk_prepared: AutomorphismKeyPrepared, B> = tmp.prepare_alloc(module, scratch.borrow()); auto_keys.insert(*gal_el, atk_prepared); }); diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 47c848e..12a44fd 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,8 +3,8 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWESecret, LWECiphertext, - LWECiphertextLayout, LWESecret, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret, + TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_hal::{ api::{ @@ -218,7 +218,7 @@ where scratch.borrow(), ); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(¶ms.ggsw_infos); + let mut res: GGSW> = GGSW::alloc(¶ms.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || { @@ -261,7 +261,7 @@ where dnum: 3_u32.into(), rank: 2_u32.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), @@ -269,7 +269,7 @@ where dsize: Dsize(1), rank: 2_u32.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: 1024_u32.into(), base2k: 13_u32.into(), k: 52_u32.into(), diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 4fec699..2ac8eb2 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/poulpy-schemes/examples/circuit_bootstrapping.rs @@ -1,9 +1,9 @@ use poulpy_core::{ GLWEOperations, layouts::{ - GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertext, GGSWCiphertextLayout, GLWECiphertext, - GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, + prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, }; use std::time::Instant; @@ -89,7 +89,7 @@ fn main() { dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_trace.into(), @@ -97,7 +97,7 @@ fn main() { dsize: 1_u32.into(), rank: rank.into(), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -187,7 +187,7 @@ fn main() { println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = @@ -249,7 +249,7 @@ fn main() { ); // Prepare GGSW output of circuit bootstrapping (opaque backend dependant write only struct) - let res_prepared: GGSWCiphertextPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); + let res_prepared: GGSWPrepared, BackendImpl> = res.prepare_alloc(&module, scratch.borrow()); // Apply GLWE x GGSW ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 3cd50a6..3213894 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -1,12 +1,10 @@ use std::marker::PhantomData; -use poulpy_core::layouts::{ - Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWCiphertextPrepared, -}; +use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared}; #[cfg(test)] use poulpy_core::{ TakeGGSW, - layouts::{GGSWCiphertext, prepared::GLWESecretPrepared}, + layouts::{GGSW, prepared::GLWESecretPrepared}, }; use poulpy_hal::{ api::VmpPMatAlloc, @@ -29,7 +27,7 @@ use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, Unsigne #[cfg(test)] pub(crate) struct FheUintBlocksPrepDebug { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -62,7 +60,7 @@ impl FheUintBlocksPrepDebug, T> { ) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertext::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) + .map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) .collect(), _base: 1, _phantom: PhantomData, @@ -72,7 +70,7 @@ impl FheUintBlocksPrepDebug, T> { /// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocksPrep { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -103,7 +101,7 @@ where { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSWCiphertextPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) + .map(|_| GGSWPrepared::alloc_with(module, base2k, k, dnum, dsize, rank)) .collect(), _base: 1, _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index cc754bc..b09b8a5 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs @@ -3,7 +3,7 @@ use poulpy_core::{ GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, layouts::{ GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, - prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared}, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; use poulpy_hal::{ @@ -32,7 +32,7 @@ impl FheUintWord { &mut self, module: &Module, mut tmp_res: Vec>, - auto_keys: &HashMap>, + auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 98e1083..13f34e1 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs @@ -3,7 +3,7 @@ use poulpy_core::{ GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, layouts::{ GLWECiphertext, GLWECiphertextToMut, LWEInfos, - prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef}, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, }; use poulpy_hal::{ @@ -164,7 +164,7 @@ pub trait Cmux { out: &mut GLWECiphertext, t: &GLWECiphertext, f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, + s: &GGSWPrepared, scratch: &mut Scratch, ) where O: DataMut, @@ -182,7 +182,7 @@ where out: &mut GLWECiphertext, t: &GLWECiphertext, f: &GLWECiphertext, - s: &GGSWCiphertextPrepared, + s: &GGSWPrepared, scratch: &mut Scratch, ) where O: DataMut, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index febabed..322a2e2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -11,7 +11,7 @@ use crate::tfhe::{ use poulpy_core::{ TakeGGSW, TakeGLWECt, layouts::{ - GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, }; @@ -56,7 +56,7 @@ where BRA: BlindRotationAlgo, { cbt: CircuitBootstrappingKey, - ks: GLWEToLWEKey, + ks: GLWEToLWESwitchingKey, } impl BDDKey, Vec, BRA> { @@ -98,7 +98,7 @@ impl BDDKey, Vec, BRA> { + VecZnxAutomorphismInplace, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol + TakeVecZnxBig, { - let mut ks: GLWEToLWEKey> = GLWEToLWEKey::alloc(&infos.ks_infos()); + let mut ks: GLWEToLWESwitchingKey> = GLWEToLWESwitchingKey::alloc(&infos.ks_infos()); ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch); Self { @@ -131,7 +131,7 @@ impl PrepareAll for BDDKey where CircuitBootstrappingKey: PrepareAlloc>, - GLWEToLWEKey: PrepareAlloc>, + GLWEToLWESwitchingKey: PrepareAlloc>, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> BDDKeyPrepared { BDDKeyPrepared { diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 6b56f79..5a7ae97 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs @@ -1,7 +1,7 @@ #[cfg(test)] use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, GLWECiphertextLayout, - GLWEToLWEKeyLayout, Rank, TorusPrecision, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank, + TensorKeyLayout, TorusPrecision, }; #[cfg(test)] @@ -53,7 +53,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), rank: Rank(TEST_RANK), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), @@ -61,7 +61,7 @@ pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout { dnum: Dnum(3), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(52), diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index fbf506b..a8fa39a 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -14,9 +14,9 @@ use std::marker::PhantomData; use poulpy_core::{ Distribution, layouts::{ - GGSWCiphertext, GGSWInfos, LWESecret, + GGSW, GGSWInfos, LWESecret, compressed::GGSWCiphertextCompressed, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + prepared::{GGSWPrepared, GLWESecretPrepared}, }, }; @@ -30,9 +30,9 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); + let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSWCiphertext::alloc(infos)); + data.push(GGSW::alloc(infos)); } Self { @@ -49,7 +49,7 @@ impl BlindRotationKey, CGGI> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertext::encrypt_sk_scratch_space(module, infos) + GGSW::encrypt_sk_scratch_space(module, infos) } } @@ -121,8 +121,8 @@ where where A: BlindRotationKeyInfos, { - let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextPrepared::alloc(module, infos))); + let mut data: Vec, B>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWPrepared::alloc(module, infos))); Self { data, dist: Distribution::NONE, diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key.rs b/poulpy-schemes/src/tfhe/blind_rotation/key.rs index 86dbd21..d9bb0a9 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key.rs @@ -8,7 +8,7 @@ use std::{fmt, marker::PhantomData}; use poulpy_core::{ Distribution, layouts::{ - Base2K, Degree, Dnum, Dsize, GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, LWESecret, Rank, TorusPrecision, prepared::GLWESecretPrepared, }, }; @@ -98,7 +98,7 @@ pub trait BlindRotationKeyEncryptSk { #[derive(Clone)] pub struct BlindRotationKey { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs index 8719de4..2001083 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -9,7 +9,7 @@ use poulpy_core::{ Distribution, layouts::{ Base2K, Degree, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - prepared::{GGSWCiphertextPrepared, Prepare, PrepareAlloc}, + prepared::{GGSWPrepared, Prepare, PrepareAlloc}, }, }; @@ -23,7 +23,7 @@ pub trait BlindRotationKeyPreparedAlloc { #[derive(PartialEq, Eq)] pub struct BlindRotationKeyPrepared { - pub(crate) data: Vec>, + pub(crate) data: Vec>, pub(crate) dist: Distribution, pub(crate) x_pow_a: Option, B>>>, pub(crate) _phantom: PhantomData, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 45b9717..22e7a7a 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -20,7 +20,7 @@ use poulpy_core::{ }; use poulpy_core::glwe_packing; -use poulpy_core::layouts::{GGSWCiphertext, GLWECiphertext, LWECiphertext, prepared::GGLWEAutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -74,7 +74,7 @@ where fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -97,7 +97,7 @@ where &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -122,7 +122,7 @@ pub fn circuit_bootstrap_core( to_exponent: bool, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -268,7 +268,7 @@ fn post_process( log_gap_in: usize, log_gap_out: usize, log_domain: usize, - auto_keys: &HashMap, B>>, + auto_keys: &HashMap, B>>, scratch: &mut Scratch, ) where DataRes: DataMut, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 427cf75..3d91943 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -1,7 +1,7 @@ use poulpy_core::layouts::{ - GGLWEAutomorphismKey, GGLWEAutomorphismKeyLayout, GGLWEInfos, GGLWETensorKey, GGLWETensorKeyLayout, GGSWInfos, - GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, - prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, + TensorKey, TensorKeyLayout, + prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap; @@ -24,19 +24,19 @@ use crate::tfhe::blind_rotation::{ pub trait CircuitBootstrappingKeyInfos { fn brk_infos(&self) -> BlindRotationKeyLayout; - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout; - fn tsk_infos(&self) -> GGLWETensorKeyLayout; + fn atk_infos(&self) -> AutomorphismKeyLayout; + fn tsk_infos(&self) -> TensorKeyLayout; } #[derive(Debug, Clone, Copy)] pub struct CircuitBootstrappingKeyLayout { pub layout_brk: BlindRotationKeyLayout, - pub layout_atk: GGLWEAutomorphismKeyLayout, - pub layout_tsk: GGLWETensorKeyLayout, + pub layout_atk: AutomorphismKeyLayout, + pub layout_tsk: TensorKeyLayout, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { self.layout_atk } @@ -44,7 +44,7 @@ impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyLayout { self.layout_brk } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { self.layout_tsk } } @@ -68,8 +68,8 @@ pub trait CircuitBootstrappingKeyEncryptSk { pub struct CircuitBootstrappingKey { pub(crate) brk: BlindRotationKey, - pub(crate) tsk: GGLWETensorKey>, - pub(crate) atk: HashMap>>, + pub(crate) tsk: TensorKey>, + pub(crate) atk: HashMap>>, } impl CircuitBootstrappingKeyEncryptSk for CircuitBootstrappingKey, BRA> @@ -117,14 +117,14 @@ where assert_eq!(sk_glwe.n(), cbt_infos.atk_infos().n()); assert_eq!(sk_glwe.n(), cbt_infos.tsk_infos().n()); - let atk_infos: GGLWEAutomorphismKeyLayout = cbt_infos.atk_infos(); + let atk_infos: AutomorphismKeyLayout = cbt_infos.atk_infos(); let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos(); - let trk_infos: GGLWETensorKeyLayout = cbt_infos.tsk_infos(); + let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); - let mut auto_keys: HashMap>> = HashMap::new(); + let mut auto_keys: HashMap>> = HashMap::new(); let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); auto_keys.insert(*gal_el, key); }); @@ -141,7 +141,7 @@ where scratch, ); - let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(&trk_infos); + let mut tsk: TensorKey> = TensorKey::alloc(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self { @@ -154,14 +154,14 @@ where pub struct CircuitBootstrappingKeyPrepared { pub(crate) brk: BlindRotationKeyPrepared, - pub(crate) tsk: GGLWETensorKeyPrepared, B>, - pub(crate) atk: HashMap, B>>, + pub(crate) tsk: TensorKeyPrepared, B>, + pub(crate) atk: HashMap, B>>, } impl CircuitBootstrappingKeyInfos for CircuitBootstrappingKeyPrepared { - fn atk_infos(&self) -> GGLWEAutomorphismKeyLayout { + fn atk_infos(&self) -> AutomorphismKeyLayout { let (_, atk) = self.atk.iter().next().expect("atk is empty"); - GGLWEAutomorphismKeyLayout { + AutomorphismKeyLayout { n: atk.n(), base2k: atk.base2k(), k: atk.k(), @@ -182,8 +182,8 @@ impl CircuitBootstrappingKeyInfo } } - fn tsk_infos(&self) -> GGLWETensorKeyLayout { - GGLWETensorKeyLayout { + fn tsk_infos(&self) -> TensorKeyLayout { + TensorKeyLayout { n: self.tsk.n(), base2k: self.tsk.base2k(), k: self.tsk.k(), @@ -199,13 +199,13 @@ impl PrepareAlloc: VmpPMatAlloc + VmpPrepare, BlindRotationKey: PrepareAlloc, BRA, B>>, - GGLWETensorKey: PrepareAlloc, B>>, - GGLWEAutomorphismKey: PrepareAlloc, B>>, + TensorKey: PrepareAlloc, B>>, + AutomorphismKey: PrepareAlloc, B>>, { fn prepare_alloc(&self, module: &Module, scratch: &mut Scratch) -> CircuitBootstrappingKeyPrepared, BRA, B> { let brk: BlindRotationKeyPrepared, BRA, B> = self.brk.prepare_alloc(module, scratch); - let tsk: GGLWETensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); - let mut atk: HashMap, B>> = HashMap::new(); + let tsk: TensorKeyPrepared, B> = self.tsk.prepare_alloc(module, scratch); + let mut atk: HashMap, B>> = HashMap::new(); for (key, value) in &self.atk { atk.insert(*key, value.prepare_alloc(module, scratch)); } diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index 86dcf5e..e316d34 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -5,7 +5,7 @@ pub mod tests; pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSWCiphertext, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWECiphertext}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; @@ -13,7 +13,7 @@ pub trait CirtuitBootstrappingExecute { fn execute_to_constant( &self, module: &Module, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, @@ -25,7 +25,7 @@ pub trait CirtuitBootstrappingExecute { &self, module: &Module, log_gap_out: usize, - res: &mut GGSWCiphertext, + res: &mut GGSW, lwe: &LWECiphertext, log_domain: usize, extension_factor: usize, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index 40f5448..a0dbb42 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -32,12 +32,12 @@ use crate::tfhe::{ }; use poulpy_core::layouts::{ - Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, LWECiphertextLayout, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc, }; use poulpy_core::layouts::{ - GGSWCiphertext, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, - prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, + GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, + prepared::{GGSWPrepared, GLWESecretPrepared}, }; pub fn test_circuit_bootstrapping_to_exponent(module: &Module) @@ -143,7 +143,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -151,7 +151,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -206,7 +206,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); let log_gap_out = 1; @@ -249,7 +249,7 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); @@ -365,7 +365,7 @@ where dnum: rows_brk.into(), rank: rank.into(), }, - layout_atk: GGLWEAutomorphismKeyLayout { + layout_atk: AutomorphismKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_atk.into(), @@ -373,7 +373,7 @@ where rank: rank.into(), dsize: Dsize(1), }, - layout_tsk: GGLWETensorKeyLayout { + layout_tsk: TensorKeyLayout { n: n_glwe.into(), base2k: base2k.into(), k: k_tsk.into(), @@ -428,7 +428,7 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSWCiphertext> = GGSWCiphertext::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); @@ -462,7 +462,7 @@ where scratch.borrow(), ); - let res_prepared: GGSWCiphertextPrepared, B> = res.prepare_alloc(module, scratch.borrow()); + let res_prepared: GGSWPrepared, B> = res.prepare_alloc(module, scratch.borrow()); ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());