arnaucube
arnaucube/poulpyPublic Self-hosted repository
wip
Pro7ech jeanphilippe.bossuat@gmail.com
Tue, 14 Oct 2025 18:46:25 +0200
commit

72dca47cbeadf66cd1c2815330c0cf60ed1c9823

parent

0533cdff8aa93711908c525d81ea5a329242519a

153 files changed, 3009 insertions(+), 1866 deletions(-)

jump to
M poulpy-backend/src/cpu_fft64_avx/scratch.rs → poulpy-backend/src/cpu_fft64_avx/scratch.rs

@@ -69,7 +69,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -82,7 +82,7 @@ where

B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>, { fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -95,7 +95,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice),

@@ -115,7 +115,7 @@ size: usize,

) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size),

@@ -136,7 +136,7 @@ size: usize,

) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); (

@@ -204,7 +204,7 @@ size: usize,

) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),

@@ -227,7 +227,7 @@ size: usize,

) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),
M poulpy-backend/src/cpu_fft64_avx/svp.rs → poulpy-backend/src/cpu_fft64_avx/svp.rs

@@ -22,7 +22,7 @@ }

} unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Avx { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::<f64>() } }
M poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs → poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs

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

}; unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Avx { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs → poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs

@@ -24,7 +24,7 @@ }

} unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Avx { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Avx as Backend>::ScalarPrep>() } }
M poulpy-backend/src/cpu_fft64_avx/vmp.rs → poulpy-backend/src/cpu_fft64_avx/vmp.rs

@@ -16,7 +16,7 @@

use crate::cpu_fft64_avx::{FFT64Avx, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Avx { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_fft64_ref/scratch.rs → poulpy-backend/src/cpu_fft64_ref/scratch.rs

@@ -69,7 +69,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -82,7 +82,7 @@ where

B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>, { fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -95,7 +95,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice),

@@ -115,7 +115,7 @@ size: usize,

) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size),

@@ -136,7 +136,7 @@ size: usize,

) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); (

@@ -204,7 +204,7 @@ size: usize,

) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),

@@ -227,7 +227,7 @@ size: usize,

) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),
M poulpy-backend/src/cpu_fft64_ref/svp.rs → poulpy-backend/src/cpu_fft64_ref/svp.rs

@@ -22,7 +22,7 @@ }

} unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Ref { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { Self::layout_prep_word_count() * n * cols * size_of::<f64>() } }
M poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs → poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs

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

}; unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Ref { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs → poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs

@@ -24,7 +24,7 @@ }

} unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Ref { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Ref as Backend>::ScalarPrep>() } }
M poulpy-backend/src/cpu_fft64_ref/vmp.rs → poulpy-backend/src/cpu_fft64_ref/vmp.rs

@@ -16,7 +16,7 @@

use crate::cpu_fft64_ref::{FFT64Ref, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Ref { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_spqlios/fft64/scratch.rs → poulpy-backend/src/cpu_spqlios/fft64/scratch.rs

@@ -69,7 +69,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols)); ( ScalarZnx::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -82,7 +82,7 @@ where

B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>, { fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols)); ( SvpPPol::from_data(take_slice, n, cols), Scratch::from_bytes(rem_slice),

@@ -95,7 +95,7 @@ where

B: ScratchFromBytesImpl<B>, { fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) { - let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size)); + let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size)); ( VecZnx::from_data(take_slice, n, cols, size), Scratch::from_bytes(rem_slice),

@@ -115,7 +115,7 @@ size: usize,

) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_big_alloc_bytes_impl(n, cols, size), + B::vec_znx_big_bytes_of_impl(n, cols, size), ); ( VecZnxBig::from_data(take_slice, n, cols, size),

@@ -136,7 +136,7 @@ size: usize,

) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vec_znx_dft_alloc_bytes_impl(n, cols, size), + B::vec_znx_dft_bytes_of_impl(n, cols, size), ); (

@@ -204,7 +204,7 @@ size: usize,

) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size), + B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size), ); ( VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),

@@ -227,7 +227,7 @@ size: usize,

) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) { let (take_slice, rem_slice) = take_slice_aligned( &mut scratch.data, - MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size), + MatZnx::bytes_of(n, rows, cols_in, cols_out, size), ); ( MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),
M poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs → poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs

@@ -27,7 +27,7 @@ }

} unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Spqlios { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { FFT64Spqlios::layout_prep_word_count() * n * cols * size_of::<f64>() } }
M poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs → poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs

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

}; unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Spqlios { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_big_word_count() * n * cols * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs → poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs

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

} unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Spqlios { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Spqlios as Backend>::ScalarPrep>() } }
M poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs → poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs

@@ -16,7 +16,7 @@ ffi::{vec_znx_dft::vec_znx_dft_t, vmp},

}; unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Spqlios { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>() } }
M poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs → poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs

@@ -18,7 +18,7 @@ }

} unsafe impl SvpPPolAllocBytesImpl<Self> for NTT120 { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize { + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size_of::<i64>() } }
M poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs → poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs

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

use crate::cpu_spqlios::NTT120; unsafe impl VecZnxBigAllocBytesImpl<NTT120> for NTT120 { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_big_word_count() * n * cols * size * size_of::<i128>() } }
M poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs → poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs

@@ -6,7 +6,7 @@

use crate::cpu_spqlios::NTT120; unsafe impl VecZnxDftAllocBytesImpl<NTT120> for NTT120 { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize { + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize { NTT120::layout_prep_word_count() * n * cols * size * size_of::<i64>() } }
M poulpy-core/benches/external_product_glwe_fft64.rs → poulpy-core/benches/external_product_glwe_fft64.rs

@@ -1,6 +1,5 @@

use poulpy_core::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWESecret, Rank, - TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWESecret, Rank, TorusPrecision, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }; use std::hint::black_box;

@@ -48,36 +47,36 @@ dsize: dsize,

rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_out, rank, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct_in, rank, }; - let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) + | GLWE::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout), ); let mut source_xs = Source::new([0u8; 32]); let mut source_xe = Source::new([0u8; 32]); let mut source_xa = Source::new([0u8; 32]); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_in_layout); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow());

@@ -156,28 +155,28 @@ dsize: dsize,

rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe, rank, }; - let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout); + let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) + | GLWE::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_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 sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow());
M poulpy-core/benches/keyswitch_glwe_fft64.rs → poulpy-core/benches/keyswitch_glwe_fft64.rs

@@ -1,6 +1,6 @@

use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWECiphertextLayout, GLWESecret, - GLWESwitchingKey, GLWESwitchingKeyLayout, Rank, TorusPrecision, + AutomorphismKey, AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GLWE, GLWELayout, GLWESecret, GLWESwitchingKey, + GLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }; use std::{hint::black_box, time::Duration};

@@ -48,28 +48,28 @@ rank,

dsize, }; - let glwe_in_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_in, rank, }; - let glwe_out_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe_out, rank, }; - let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&gglwe_atk_layout); - let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout) - | GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout) - | GLWECiphertext::keyswitch_scratch_space( + | GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout) + | GLWE::keyswitch_scratch_space( &module, &glwe_out_layout, &glwe_in_layout,

@@ -81,7 +81,7 @@ 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 sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_in_layout); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow());

@@ -167,31 +167,31 @@ rank_in: rank,

rank_out: rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct, rank, }; - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_layout); - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(&module, &glwe_layout) + | GLWE::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_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 sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk(
M poulpy-core/examples/encryption.rs → poulpy-core/examples/encryption.rs

@@ -2,8 +2,7 @@ use poulpy_backend::cpu_spqlios::FFT64Spqlios;

use poulpy_core::{ GLWEOperations, SIGMA, layouts::{ - Base2K, Degree, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWESecret, LWEInfos, Rank, TorusPrecision, prepared::{GLWESecretPrepared, PrepareAlloc}, }, };

@@ -34,7 +33,7 @@

// Instantiate Module (DFT Tables) let module: Module<FFT64Spqlios> = Module::<FFT64Spqlios>::new(n.0 as u64); - let glwe_ct_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_ct_infos: GLWELayout = GLWELayout { n, base2k, k: k_ct,

@@ -44,9 +43,9 @@

let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]);

@@ -55,12 +54,11 @@ let mut source_xa: Source = Source::new([2u8; 32]);

// Scratch space let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_ct_infos) - | GLWECiphertext::decrypt_scratch_space(&module, &glwe_ct_infos), + GLWE::encrypt_sk_scratch_space(&module, &glwe_ct_infos) | GLWE::decrypt_scratch_space(&module, &glwe_ct_infos), ); // Generate secret-key - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_ct_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret
M poulpy-core/src/automorphism/gglwe_atk.rs → poulpy-core/src/automorphism/gglwe_atk.rs

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

layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWECiphertext, prepared::AutomorphismKeyPrepared}; +use crate::layouts::{AutomorphismKey, GGLWEInfos, GLWE, prepared::AutomorphismKeyPrepared}; impl AutomorphismKey<Vec<u8>> { pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>(

@@ -22,7 +22,7 @@ IN: GGLWEInfos,

KEY: GGLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space( + GLWE::keyswitch_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(),

@@ -103,8 +103,8 @@ let p_inv: i64 = module.galois_element_inv(p);

(0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); - let lhs_ct: GLWECiphertext<&[u8]> = lhs.at(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); + let lhs_ct: GLWE<&[u8]> = lhs.at(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| {

@@ -176,7 +176,7 @@ let p_inv = module.galois_element_inv(p);

(0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_j| { - let mut res_ct: GLWECiphertext<&mut [u8]> = self.at_mut(row_j, col_i); + let mut res_ct: GLWE<&mut [u8]> = self.at_mut(row_j, col_i); // Reverts the automorphism X^{-k}: (-pi^{-1}_{k}(s)a + s, a) to (-sa + pi_{k}(s), a) (0..cols_out).for_each(|i| {
M poulpy-core/src/automorphism/ggsw_ct.rs → poulpy-core/src/automorphism/ggsw_ct.rs

@@ -9,7 +9,7 @@ layouts::{Backend, DataMut, DataRef, Module, Scratch},

}; use crate::layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, + GGLWEInfos, GGSW, GGSWInfos, GLWE, prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, };

@@ -33,8 +33,8 @@ + VecZnxNormalizeTmpBytes

+ VecZnxBigNormalizeTmpBytes, { let out_size: usize = out_infos.size(); - let ci_dft: usize = module.vec_znx_dft_alloc_bytes((key_infos.rank_out() + 1).into(), out_size); - let ks_internal: usize = GLWECiphertext::keyswitch_scratch_space( + let ci_dft: usize = module.vec_znx_dft_bytes_of((key_infos.rank_out() + 1).into(), out_size); + let ks_internal: usize = GLWE::keyswitch_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(),
M poulpy-core/src/automorphism/glwe_ct.rs → poulpy-core/src/automorphism/glwe_ct.rs

@@ -8,9 +8,9 @@ },

layouts::{Backend, DataMut, DataRef, Module, Scratch, VecZnxBig}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>( module: &Module<B>, out_infos: &OUT,

@@ -36,11 +36,11 @@ Self::keyswitch_inplace_scratch_space(module, out_infos, key_infos)

} } -impl<DataSelf: DataMut> GLWECiphertext<DataSelf> { +impl<DataSelf: DataMut> GLWE<DataSelf> { pub fn automorphism<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &AutomorphismKeyPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where

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

pub fn automorphism_add<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &AutomorphismKeyPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where

@@ -176,7 +176,7 @@

pub fn automorphism_sub_ab<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &AutomorphismKeyPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where

@@ -261,7 +261,7 @@

pub fn automorphism_sub_negate<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &AutomorphismKeyPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where
M poulpy-core/src/conversion/glwe_to_lwe.rs → poulpy-core/src/conversion/glwe_to_lwe.rs

@@ -9,13 +9,10 @@ };

use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, Rank, - prepared::GLWEToLWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, Rank, prepared::GLWEToLWESwitchingKeyPrepared}, }; -impl LWECiphertext<Vec<u8>> { +impl LWE<Vec<u8>> { pub fn from_glwe_scratch_space<B: Backend, OUT, IN, KEY>( module: &Module<B>, lwe_infos: &OUT,

@@ -28,24 +25,24 @@ IN: GLWEInfos,

KEY: GGLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: lwe_infos.base2k(), k: lwe_infos.k(), rank: Rank(1), }; - GLWECiphertext::alloc_bytes_with( + GLWE::bytes_of( module.n().into(), lwe_infos.base2k(), lwe_infos.k(), 1u32.into(), - ) + GLWECiphertext::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) + ) + GLWE::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos) } } -impl<DLwe: DataMut> LWECiphertext<DLwe> { - pub fn sample_extract<DGlwe: DataRef>(&mut self, a: &GLWECiphertext<DGlwe>) { +impl<DLwe: DataMut> LWE<DLwe> { + pub fn sample_extract<DGlwe: DataRef>(&mut self, a: &GLWE<DGlwe>) { #[cfg(debug_assertions)] { assert!(self.n() <= a.n());

@@ -66,7 +63,7 @@

pub fn from_glwe<DGlwe, DKs, B: Backend>( &mut self, module: &Module<B>, - a: &GLWECiphertext<DGlwe>, + a: &GLWE<DGlwe>, ks: &GLWEToLWESwitchingKeyPrepared<DKs, B>, scratch: &mut Scratch<B>, ) where

@@ -92,7 +89,7 @@ assert_eq!(ks.n(), module.n() as u32);

assert!(self.n() <= module.n() as u32); } - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n: module.n().into(), base2k: self.base2k(), k: self.k(),
M poulpy-core/src/conversion/lwe_to_glwe.rs → poulpy-core/src/conversion/lwe_to_glwe.rs

@@ -9,13 +9,10 @@ };

use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, - prepared::LWEToGLWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}, }; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn from_lwe_scratch_space<B: Backend, OUT, IN, KEY>( module: &Module<B>, glwe_infos: &OUT,

@@ -28,27 +25,27 @@ IN: LWEInfos,

KEY: GGLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - let ct: usize = GLWECiphertext::alloc_bytes_with( + let ct: usize = GLWE::bytes_of( module.n().into(), key_infos.base2k(), lwe_infos.k().max(glwe_infos.k()), 1u32.into(), ); - let ks: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); + let ks: usize = GLWE::keyswitch_inplace_scratch_space(module, glwe_infos, key_infos); if lwe_infos.base2k() == key_infos.base2k() { ct + ks } else { - let a_conv = VecZnx::alloc_bytes(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); + let a_conv = VecZnx::bytes_of(module.n(), 1, lwe_infos.size()) + module.vec_znx_normalize_tmp_bytes(); ct + a_conv + ks } } } -impl<D: DataMut> GLWECiphertext<D> { +impl<D: DataMut> GLWE<D> { pub fn from_lwe<DLwe, DKsk, B: Backend>( &mut self, module: &Module<B>, - lwe: &LWECiphertext<DLwe>, + lwe: &LWE<DLwe>, ksk: &LWEToGLWESwitchingKeyPrepared<DKsk, B>, scratch: &mut Scratch<B>, ) where

@@ -74,7 +71,7 @@ assert_eq!(ksk.n(), module.n() as u32);

assert!(lwe.n() <= module.n() as u32); } - let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: ksk.base2k(), k: lwe.k(),
M poulpy-core/src/decryption/glwe_ct.rs → poulpy-core/src/decryption/glwe_ct.rs

@@ -6,20 +6,20 @@ },

layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch}, }; -use crate::layouts::{GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn decrypt_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where A: GLWEInfos, Module<B>: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size: usize = infos.size(); - (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size) + (module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_bytes_of(1, size)) + module.vec_znx_dft_bytes_of(1, size) } } -impl<DataSelf: DataRef> GLWECiphertext<DataSelf> { +impl<DataSelf: DataRef> GLWE<DataSelf> { pub fn decrypt<DataPt: DataMut, DataSk: DataRef, B: Backend>( &self, module: &Module<B>,
M poulpy-core/src/decryption/lwe_ct.rs → poulpy-core/src/decryption/lwe_ct.rs

@@ -4,9 +4,9 @@ layouts::{Backend, DataMut, DataRef, Module, ScratchOwned, ZnxView, ZnxViewMut},

oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}; -impl<DataSelf> LWECiphertext<DataSelf> +impl<DataSelf> LWE<DataSelf> where DataSelf: DataRef, {
M poulpy-core/src/encryption/compressed/gglwe_atk.rs → poulpy-core/src/encryption/compressed/gglwe_atk.rs

@@ -20,8 +20,7 @@ A: GGLWEInfos,

Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { assert_eq!(module.n() as u32, infos.n()); - GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out()) + GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out()) } }
M poulpy-core/src/encryption/compressed/gglwe_ct.rs → poulpy-core/src/encryption/compressed/gglwe_ct.rs

@@ -12,12 +12,12 @@ TakeGLWEPt,

encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, - compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, + compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl<D: DataMut> GGLWECiphertextCompressed<D> { +impl<D: DataMut> GGLWECompressed<D> { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( &mut self,

@@ -34,7 +34,7 @@ module.gglwe_compressed_encrypt_sk(self, pt, sk, seed, source_xe, scratch);

} } -impl GGLWECiphertextCompressed<Vec<u8>> { +impl GGLWECompressed<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos,

@@ -54,7 +54,7 @@ seed: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>; }

@@ -78,11 +78,11 @@ seed: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>, { - let res: &mut GGLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); #[cfg(debug_assertions)]

@@ -107,10 +107,10 @@ );

assert_eq!(res.n(), sk.n()); assert_eq!(pt.n() as u32, sk.n()); assert!( - scratch.available() >= GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res), + scratch.available() >= GGLWECompressed::encrypt_sk_scratch_space(self, res), "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GGLWECiphertextCompressed::encrypt_sk_scratch_space(self, res) + GGLWECompressed::encrypt_sk_scratch_space(self, res) ); assert!( res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
M poulpy-core/src/encryption/compressed/gglwe_ksk.rs → poulpy-core/src/encryption/compressed/gglwe_ksk.rs

@@ -23,9 +23,9 @@ where

A: GGLWEInfos, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes, { - (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.rank_out()) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of(module, infos.rank_out()) } }
M poulpy-core/src/encryption/compressed/ggsw_ct.rs → poulpy-core/src/encryption/compressed/ggsw_ct.rs

@@ -9,12 +9,12 @@ TakeGLWEPt,

encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, - compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, + compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertextCompressed<Vec<u8>> { +impl GGSWCompressed<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where A: GGSWInfos,

@@ -34,7 +34,7 @@ seed_xa: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>; }

@@ -53,11 +53,11 @@ seed_xa: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>, { - let res: &mut GGSWCiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref();

@@ -113,7 +113,7 @@ }

} } -impl<DataSelf: DataMut> GGSWCiphertextCompressed<DataSelf> { +impl<DataSelf: DataMut> GGSWCompressed<DataSelf> { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( &mut self,
M poulpy-core/src/encryption/compressed/glwe_ct.rs → poulpy-core/src/encryption/compressed/glwe_ct.rs

@@ -7,19 +7,19 @@

use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, - compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut}, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + compressed::{GLWECompressed, GLWECompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertextCompressed<Vec<u8>> { +impl GLWECompressed<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where A: GLWEInfos, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, infos) + GLWE::encrypt_sk_scratch_space(module, infos) } }

@@ -33,7 +33,7 @@ seed_xa: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef<B>; }

@@ -51,11 +51,11 @@ seed_xa: [u8; 32],

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef<B>, { - let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); let mut source_xa: Source = Source::new(seed_xa); let cols: usize = (res.rank() + 1).into();

@@ -77,7 +77,7 @@ res.seed = seed_xa;

} } -impl<D: DataMut> GLWECiphertextCompressed<D> { +impl<D: DataMut> GLWECompressed<D> { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>( &mut self,
M poulpy-core/src/encryption/gglwe_atk.rs → poulpy-core/src/encryption/gglwe_atk.rs

@@ -27,7 +27,7 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKey" ); - GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout()) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(&infos.glwe_layout()) } pub fn encrypt_pk_scratch_space<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize
M poulpy-core/src/encryption/gglwe_ct.rs → poulpy-core/src/encryption/gglwe_ct.rs

@@ -11,7 +11,7 @@ use crate::{

TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSk, layouts::{ - GGLWE, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, + GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, };

@@ -22,8 +22,8 @@ where

A: GGLWEInfos, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + (GLWEPlaintext::alloc_bytes(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) + GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + + (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes()) } pub fn encrypt_pk_scratch_space<B: Backend, A>(_module: &Module<B>, _infos: &A) -> usize

@@ -44,7 +44,7 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>; }

@@ -64,7 +64,7 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef<B>, {
M poulpy-core/src/encryption/gglwe_ksk.rs → poulpy-core/src/encryption/gglwe_ksk.rs

@@ -20,9 +20,9 @@ where

A: GGLWEInfos, Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - (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_from_infos(module, &infos.glwe_layout()) + (GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1)) + + ScalarZnx::bytes_of(module.n(), infos.rank_in().into()) + + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout()) } pub fn encrypt_pk_scratch_space<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize
M poulpy-core/src/encryption/gglwe_tsk.rs → poulpy-core/src/encryption/gglwe_tsk.rs

@@ -23,11 +23,11 @@ A: GGLWEInfos,

Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes, { - GLWESecretPrepared::alloc_bytes(module, infos.rank_out()) - + module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1) - + 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)) + GLWESecretPrepared::bytes_of(module, infos.rank_out()) + + module.vec_znx_dft_bytes_of(infos.rank_out().into(), 1) + + module.vec_znx_big_bytes_of(1, 1) + + module.vec_znx_dft_bytes_of(1, 1) + + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } }
M poulpy-core/src/encryption/ggsw_ct.rs → poulpy-core/src/encryption/ggsw_ct.rs

@@ -8,7 +8,7 @@ use crate::{

SIGMA, TakeGLWEPt, encryption::glwe_ct::GLWEEncryptSkInternal, layouts::{ - GGSW, GGSWInfos, GGSWToMut, GLWECiphertext, GLWEInfos, LWEInfos, + GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, LWEInfos, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, };

@@ -20,10 +20,10 @@ A: GGSWInfos,

Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { let size = infos.size(); - GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout()) - + VecZnx::alloc_bytes(module.n(), (infos.rank() + 1).into(), size) - + VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes((infos.rank() + 1).into(), size) + GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout()) + + VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size) + + VecZnx::bytes_of(module.n(), 1, size) + + module.vec_znx_dft_bytes_of((infos.rank() + 1).into(), size) } }
M poulpy-core/src/encryption/glwe_ct.rs → poulpy-core/src/encryption/glwe_ct.rs

@@ -13,12 +13,12 @@ use crate::{

dist::Distribution, encryption::{SIGMA, SIGMA_BOUND}, layouts::{ - GLWECiphertext, GLWECiphertextToMut, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos, + GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEToMut, LWEInfos, prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where A: GLWEInfos,

@@ -26,9 +26,7 @@ Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,

{ let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - module.vec_znx_normalize_tmp_bytes() - + 2 * VecZnx::alloc_bytes(module.n(), 1, size) - + module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.vec_znx_dft_bytes_of(1, size) } pub fn encrypt_pk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize where

@@ -37,14 +35,13 @@ Module<B>: VecZnxDftAllocBytes + SvpPPolAllocBytes + VecZnxBigAllocBytes + VecZnxNormalizeTmpBytes,

{ let size: usize = infos.size(); assert_eq!(module.n() as u32, infos.n()); - ((module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_big_alloc_bytes(1, size)) - | ScalarZnx::alloc_bytes(module.n(), 1)) - + module.svp_ppol_alloc_bytes(1) + ((module.vec_znx_dft_bytes_of(1, size) + module.vec_znx_big_bytes_of(1, size)) | ScalarZnx::bytes_of(module.n(), 1)) + + module.svp_ppol_bytes_of(1) + module.vec_znx_normalize_tmp_bytes() } } -impl<D: DataMut> GLWECiphertext<D> { +impl<D: DataMut> GLWE<D> { pub fn encrypt_sk<R, P, S, B: Backend>( &mut self, module: &Module<B>,

@@ -116,7 +113,7 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef<B>; }

@@ -135,11 +132,11 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef<B>, { - let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + let mut res: GLWE<&mut [u8]> = res.to_mut(); let pt: GLWEPlaintext<&[u8]> = pt.to_ref(); #[cfg(debug_assertions)]

@@ -150,10 +147,10 @@ assert_eq!(res.n(), self.n() as u32);

assert_eq!(sk.n(), self.n() as u32); assert_eq!(pt.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), + scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_scratch_space(self, &res) ) }

@@ -183,7 +180,7 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef<B>; }

@@ -200,10 +197,10 @@ source_xa: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef<B>, { - let mut res: GLWECiphertext<&mut [u8]> = res.to_mut(); + let mut res: GLWE<&mut [u8]> = res.to_mut(); #[cfg(debug_assertions)] {

@@ -212,10 +209,10 @@ assert_eq!(res.rank(), sk.rank());

assert_eq!(res.n(), self.n() as u32); assert_eq!(sk.n(), self.n() as u32); assert!( - scratch.available() >= GLWECiphertext::encrypt_sk_scratch_space(self, &res), + scratch.available() >= GLWE::encrypt_sk_scratch_space(self, &res), "scratch.available(): {} < GLWECiphertext::encrypt_sk_scratch_space: {}", scratch.available(), - GLWECiphertext::encrypt_sk_scratch_space(self, &res) + GLWE::encrypt_sk_scratch_space(self, &res) ) }

@@ -246,7 +243,7 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef<B>; }

@@ -264,7 +261,7 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef<B>, {

@@ -281,7 +278,7 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef<B>; }

@@ -297,7 +294,7 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef<B>, { self.glwe_encrypt_pk_internal(

@@ -321,7 +318,7 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef<B>; }

@@ -345,11 +342,11 @@ source_xu: &mut Source,

source_xe: &mut Source, scratch: &mut Scratch<B>, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef<B>, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref(); #[cfg(debug_assertions)]
M poulpy-core/src/encryption/glwe_pk.rs → poulpy-core/src/encryption/glwe_pk.rs

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

use crate::{ encryption::glwe_ct::GLWEEncryptZeroSk, layouts::{ - GLWECiphertext, GLWEPublicKey, GLWEPublicKeyToMut, + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, };

@@ -45,9 +45,9 @@ }

} // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(self, res)); + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::encrypt_sk_scratch_space(self, res)); - let mut tmp: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(res); + let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist;
M poulpy-core/src/encryption/glwe_to_lwe_ksk.rs → poulpy-core/src/encryption/glwe_to_lwe_ksk.rs

@@ -22,9 +22,8 @@ where

A: GGLWEInfos, Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes, { - GLWESecretPrepared::alloc_bytes(module, infos.rank_in()) - + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - | GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in())) + GLWESecretPrepared::bytes_of(module, infos.rank_in()) + + (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in())) } }
M poulpy-core/src/encryption/lwe_ct.rs → poulpy-core/src/encryption/lwe_ct.rs

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

use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}, }; -impl<DataSelf: DataMut> LWECiphertext<DataSelf> { +impl<DataSelf: DataMut> LWE<DataSelf> { pub fn encrypt_sk<DataPt, DataSk, B>( &mut self, module: &Module<B>,
M poulpy-core/src/encryption/lwe_ksk.rs → poulpy-core/src/encryption/lwe_ksk.rs

@@ -38,8 +38,8 @@ infos.rank_out().0,

1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1)) - + GLWESecretPrepared::alloc_bytes(module, Rank(1)) + GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1)) + + GLWESecretPrepared::bytes_of(module, Rank(1)) + GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) } }
M poulpy-core/src/encryption/lwe_to_glwe_ksk.rs → poulpy-core/src/encryption/lwe_to_glwe_ksk.rs

@@ -26,7 +26,7 @@ Rank(1),

"rank_in != 1 is not supported for LWEToGLWESwitchingKey" ); GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) - + GLWESecret::alloc_bytes_with(Degree(module.n() as u32), infos.rank_in()) + + GLWESecret::bytes_of(Degree(module.n() as u32), infos.rank_in()) } }
M poulpy-core/src/external_product/gglwe_ksk.rs → poulpy-core/src/external_product/gglwe_ksk.rs

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

layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGLWEInfos, GGSWInfos, GLWECiphertext, GLWESwitchingKey, prepared::GGSWPrepared}; +use crate::layouts::{GGLWEInfos, GGSWInfos, GLWE, GLWESwitchingKey, prepared::GGSWPrepared}; impl GLWESwitchingKey<Vec<u8>> { pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(

@@ -22,7 +22,7 @@ IN: GGLWEInfos,

GGSW: GGSWInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space( + GLWE::external_product_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(),

@@ -40,7 +40,7 @@ OUT: GGLWEInfos,

GGSW: GGSWInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) + GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos) } }
M poulpy-core/src/external_product/ggsw_ct.rs → poulpy-core/src/external_product/ggsw_ct.rs

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

layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, prepared::GGSWPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, prepared::GGSWPrepared}; impl GGSW<Vec<u8>> { #[allow(clippy::too_many_arguments)]

@@ -23,7 +23,7 @@ IN: GGSWInfos,

GGSW: GGSWInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_scratch_space( + GLWE::external_product_scratch_space( module, &out_infos.glwe_layout(), &in_infos.glwe_layout(),

@@ -41,7 +41,7 @@ OUT: GGSWInfos,

GGSW: GGSWInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) + GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos) } }
M poulpy-core/src/external_product/glwe_ct.rs → poulpy-core/src/external_product/glwe_ct.rs

@@ -7,15 +7,12 @@ },

layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig}, }; -use crate::{ - GLWEExternalProduct, GLWEExternalProductInplace, - layouts::{ - GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, LWEInfos, - prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, - }, +use crate::layouts::{ + GGSWInfos, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, + prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>( module: &Module<B>,

@@ -35,8 +32,8 @@ .div_ceil(apply_infos.base2k())

.div_ceil(apply_infos.dsize().into()) as usize; let out_size: usize = out_infos.size(); let ggsw_size: usize = apply_infos.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), ggsw_size); - let a_dft: usize = module.vec_znx_dft_alloc_bytes((apply_infos.rank() + 1).into(), in_size); + let res_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), ggsw_size); + let a_dft: usize = module.vec_znx_dft_bytes_of((apply_infos.rank() + 1).into(), in_size); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size,

@@ -50,7 +47,7 @@

if in_infos.base2k() == apply_infos.base2k() { res_dft + a_dft + (vmp | normalize_big) } else { - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (apply_infos.rank() + 1).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), (apply_infos.rank() + 1).into(), in_size); res_dft + ((a_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } }

@@ -69,11 +66,11 @@ Self::external_product_scratch_space(module, out_infos, out_infos, apply_infos)

} } -impl<DataSelf: DataMut> GLWECiphertext<DataSelf> { +impl<DataSelf: DataMut> GLWE<DataSelf> { pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &GGSWPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where

@@ -88,15 +85,15 @@ module: &Module<B>,

rhs: &GGSWPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where - Module<B>: GLWEExternalProductInplace<B>, + Module<B>: GLWEExternalProduct<B>, { module.external_product_inplace(self, rhs, scratch); } } -impl<BE: Backend> GLWEExternalProductInplace<BE> for Module<BE> +pub trait GLWEExternalProduct<BE: Backend> where - Module<BE>: VecZnxDftAllocBytes + Self: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply<BE>

@@ -107,12 +104,12 @@ + VecZnxBigNormalize<BE>

+ VecZnxNormalize<BE>, Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx, { - fn external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>) + fn glwe_external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>) where - R: GLWECiphertextToMut, + R: GLWEToMut, D: GGSWCiphertextPreparedToRef<BE>, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref(); let basek_in: usize = res.base2k().into();

@@ -124,7 +121,7 @@ use poulpy_hal::api::ScratchAvailable;

assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_inplace_scratch_space(self, res, rhs)); + assert!(scratch.available() >= GLWE::external_product_inplace_scratch_space(self, res, rhs)); } let cols: usize = (rhs.rank() + 1).into();

@@ -213,29 +210,15 @@ scratch_1,

); } } -} -impl<BE: Backend> GLWEExternalProduct<BE> for Module<BE> -where - Module<BE>: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply<BE> - + VmpApplyDftToDft<BE> - + VmpApplyDftToDftAdd<BE> - + VecZnxIdftApplyConsume<BE> - + VecZnxBigNormalize<BE> - + VecZnxNormalize<BE>, - Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx, -{ - fn external_product<R, A, D>(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch<BE>) + fn glwe_external_product<R, A, D>(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch<BE>) where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, + R: GLWEToMut, + A: GLWEToRef, D: GGSWCiphertextPreparedToRef<BE>, { - let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut(); - let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref(); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let lhs: &GLWE<&[u8]> = &lhs.to_ref(); let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref();

@@ -251,7 +234,7 @@ assert_eq!(rhs.rank(), lhs.rank());

assert_eq!(rhs.rank(), res.rank()); assert_eq!(rhs.n(), res.n()); assert_eq!(lhs.n(), res.n()); - assert!(scratch.available() >= GLWECiphertext::external_product_scratch_space(self, res, lhs, rhs)); + assert!(scratch.available() >= GLWE::external_product_scratch_space(self, res, lhs, rhs)); } let cols: usize = (rhs.rank() + 1).into();

@@ -342,3 +325,18 @@ );

}); } } + +impl<BE: Backend> GLWEExternalProduct<BE> for Module<BE> +where + Self: VecZnxDftAllocBytes + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftApply<BE> + + VmpApplyDftToDft<BE> + + VmpApplyDftToDftAdd<BE> + + VecZnxIdftApplyConsume<BE> + + VecZnxBigNormalize<BE> + + VecZnxNormalize<BE>, + Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx, +{ +}
M poulpy-core/src/external_product/mod.rs → poulpy-core/src/external_product/mod.rs

@@ -1,23 +1,8 @@

use poulpy_hal::layouts::{Backend, Scratch}; -use crate::layouts::{GLWECiphertextToMut, GLWECiphertextToRef, prepared::GGSWCiphertextPreparedToRef}; +use crate::layouts::{GLWEToMut, GLWEToRef, prepared::GGSWCiphertextPreparedToRef}; mod gglwe_atk; mod gglwe_ksk; mod ggsw_ct; mod glwe_ct; - -pub trait GLWEExternalProduct<BE: Backend> { - fn external_product<R, A, D>(&self, res: &mut R, a: &A, ggsw: &D, scratch: &mut Scratch<BE>) - where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, - D: GGSWCiphertextPreparedToRef<BE>; -} - -pub trait GLWEExternalProductInplace<BE: Backend> { - fn external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>) - where - R: GLWECiphertextToMut, - D: GGSWCiphertextPreparedToRef<BE>; -}
M poulpy-core/src/glwe_packing.rs → poulpy-core/src/glwe_packing.rs

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

use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing

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

/// [Accumulator] stores intermediate packing result. /// There are Log(N) such accumulators in a [GLWEPacker]. struct Accumulator { - data: GLWECiphertext<Vec<u8>>, + data: GLWE<Vec<u8>>, value: bool, // Implicit flag for zero ciphertext control: bool, // Can be combined with incoming value }

@@ -48,7 +48,7 @@ where

A: GLWEInfos, { Self { - data: GLWECiphertext::alloc(infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, }

@@ -100,7 +100,7 @@ pack_core_scratch_space(module, out_infos, key_infos)

} pub fn galois_elements<B: Backend>(module: &Module<B>) -> Vec<i64> { - GLWECiphertext::trace_galois_elements(module) + GLWE::trace_galois_elements(module) } /// Adds a GLWE ciphertext to the [GLWEPacker].

@@ -115,7 +115,7 @@ /// * `scratch`: scratch space of size at least [Self::scratch_space].

pub fn add<DataA: DataRef, DataAK: DataRef, B: Backend>( &mut self, module: &Module<B>, - a: Option<&GLWECiphertext<DataA>>, + a: Option<&GLWE<DataA>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>, scratch: &mut Scratch<B>, ) where

@@ -162,7 +162,7 @@ self.counter += 1 << self.log_batch;

} /// Flush result to`res`. - pub fn flush<Data: DataMut, B: Backend>(&mut self, module: &Module<B>, res: &mut GLWECiphertext<Data>) + pub fn flush<Data: DataMut, B: Backend>(&mut self, module: &Module<B>, res: &mut GLWE<Data>) where Module<B>: VecZnxCopy, {

@@ -188,7 +188,7 @@ }

fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>( module: &Module<B>, - a: Option<&GLWECiphertext<D>>, + a: Option<&GLWE<D>>, accumulators: &mut [Accumulator], i: usize, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,

@@ -258,7 +258,7 @@ );

} else { pack_core( module, - None::<&GLWECiphertext<Vec<u8>>>, + None::<&GLWE<Vec<u8>>>, acc_next, i + 1, auto_keys,

@@ -274,16 +274,15 @@ OUT: GLWEInfos,

KEY: GGLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::alloc_bytes(out_infos) - + (GLWECiphertext::rsh_scratch_space(module.n()) - | GLWECiphertext::automorphism_inplace_scratch_space(module, out_infos, key_infos)) + GLWE::bytes_of(out_infos) + + (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos)) } /// [combine] merges two ciphertexts together. fn combine<D: DataRef, DataAK: DataRef, B: Backend>( module: &Module<B>, acc: &mut Accumulator, - b: Option<&GLWECiphertext<D>>, + b: Option<&GLWE<D>>, i: usize, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>, scratch: &mut Scratch<B>,

@@ -314,7 +313,7 @@ + VecZnxNormalizeTmpBytes,

Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeGLWECt, { let log_n: usize = acc.data.n().log2(); - let a: &mut GLWECiphertext<Vec<u8>> = &mut acc.data; + let a: &mut GLWE<Vec<u8>> = &mut acc.data; let gal_el: i64 = if i == 0 { -1

@@ -395,7 +394,7 @@ /// Packs [x_0: GLWE(m_0), x_1: GLWE(m_1), ..., x_i: GLWE(m_i)]

/// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] pub fn glwe_packing<D: DataMut, ATK, B: Backend>( module: &Module<B>, - cts: &mut HashMap<usize, &mut GLWECiphertext<D>>, + cts: &mut HashMap<usize, &mut GLWE<D>>, log_gap_out: usize, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, B>>, scratch: &mut Scratch<B>,

@@ -446,8 +445,8 @@ auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap()

}; (0..t).for_each(|j| { - let mut a: Option<&mut GLWECiphertext<D>> = cts.remove(&j); - let mut b: Option<&mut GLWECiphertext<D>> = cts.remove(&(j + t)); + let mut a: Option<&mut GLWE<D>> = cts.remove(&j); + let mut b: Option<&mut GLWE<D>> = cts.remove(&(j + t)); pack_internal(module, &mut a, &mut b, i, auto_key, scratch);

@@ -463,8 +462,8 @@

#[allow(clippy::too_many_arguments)] fn pack_internal<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>( module: &Module<B>, - a: &mut Option<&mut GLWECiphertext<A>>, - b: &mut Option<&mut GLWECiphertext<D>>, + a: &mut Option<&mut GLWE<A>>, + b: &mut Option<&mut GLWE<D>>, i: usize, auto_key: &AutomorphismKeyPrepared<DataAK, B>, scratch: &mut Scratch<B>,
M poulpy-core/src/glwe_trace.rs → poulpy-core/src/glwe_trace.rs

@@ -11,11 +11,11 @@ };

use crate::{ TakeGLWECt, - layouts::{Base2K, GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{Base2K, GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWEInfos, prepared::AutomorphismKeyPrepared}, operations::GLWEOperations, }; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn trace_galois_elements<B: Backend>(module: &Module<B>) -> Vec<i64> { let mut gal_els: Vec<i64> = Vec::new(); (0..module.log_n()).for_each(|i| {

@@ -42,7 +42,7 @@ Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,

{ let trace: usize = Self::automorphism_inplace_scratch_space(module, out_infos, key_infos); if in_infos.base2k() != key_infos.base2k() { - let glwe_conv: usize = VecZnx::alloc_bytes( + let glwe_conv: usize = VecZnx::bytes_of( module.n(), (key_infos.rank_out() + 1).into(), out_infos.k().min(in_infos.k()).div_ceil(key_infos.base2k()) as usize,

@@ -63,13 +63,13 @@ Self::trace_scratch_space(module, out_infos, out_infos, key_infos)

} } -impl<DataSelf: DataMut> GLWECiphertext<DataSelf> { +impl<DataSelf: DataMut> GLWE<DataSelf> { pub fn trace<DataLhs: DataRef, DataAK: DataRef, B: Backend>( &mut self, module: &Module<B>, start: usize, end: usize, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>, scratch: &mut Scratch<B>, ) where

@@ -135,7 +135,7 @@ }

} if self.base2k() != basek_ksk { - let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut self_conv, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: module.n().into(), base2k: basek_ksk, k: self.k(),
M poulpy-core/src/keyswitching/gglwe_ct.rs → poulpy-core/src/keyswitching/gglwe_ct.rs

@@ -8,7 +8,7 @@ layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero},

}; use crate::layouts::{ - AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, + AutomorphismKey, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, };

@@ -98,7 +98,7 @@ IN: GGLWEInfos,

KEY: GGLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) + GLWE::keyswitch_scratch_space(module, out_infos, in_infos, key_apply) } pub fn keyswitch_inplace_scratch_space<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_apply: &KEY) -> usize

@@ -107,7 +107,7 @@ OUT: GGLWEInfos + GLWEInfos,

KEY: GGLWEInfos + GLWEInfos, Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_inplace_scratch_space(module, out_infos, key_apply) + GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply) } }
M poulpy-core/src/keyswitching/ggsw_ct.rs → poulpy-core/src/keyswitching/ggsw_ct.rs

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

use crate::{ layouts::{ - GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, LWEInfos, + GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, LWEInfos, prepared::{GLWESwitchingKeyPrepared, TensorKeyPrepared}, }, operations::GLWEOperations,

@@ -29,8 +29,8 @@ .k()

.div_ceil(tsk_infos.base2k()) .div_ceil(tsk_infos.dsize().into()) as usize; - let tmp_dft_i: usize = module.vec_znx_dft_alloc_bytes((tsk_infos.rank_out() + 1).into(), tsk_size); - let tmp_a: usize = module.vec_znx_dft_alloc_bytes(1, size_in); + let tmp_dft_i: usize = module.vec_znx_dft_bytes_of((tsk_infos.rank_out() + 1).into(), tsk_size); + let tmp_a: usize = module.vec_znx_dft_bytes_of(1, size_in); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( tsk_size, size_in,

@@ -39,7 +39,7 @@ (tsk_infos.rank_in()).into(), // Verify if rank+1

(tsk_infos.rank_out()).into(), // Verify if rank+1 tsk_size, ); - let tmp_idft: usize = module.vec_znx_big_alloc_bytes(1, tsk_size); + let tmp_idft: usize = module.vec_znx_big_bytes_of(1, tsk_size); let norm: usize = module.vec_znx_normalize_tmp_bytes(); tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm))

@@ -74,16 +74,16 @@

let rank: usize = apply_infos.rank_out().into(); let size_out: usize = out_infos.k().div_ceil(out_infos.base2k()) as usize; - 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 res_znx: usize = VecZnx::bytes_of(module.n(), rank + 1, size_out); + let ci_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); + let ks: usize = GLWE::keyswitch_scratch_space(module, out_infos, in_infos, apply_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); + let res_dft: usize = module.vec_znx_dft_bytes_of(rank + 1, size_out); if in_infos.base2k() == tsk_infos.base2k() { res_znx + ci_dft + (ks | expand_rows | res_dft) } else { - let a_conv: usize = VecZnx::alloc_bytes( + let a_conv: usize = VecZnx::bytes_of( module.n(), 1, out_infos.k().div_ceil(tsk_infos.base2k()) as usize,
M poulpy-core/src/keyswitching/glwe_ct.rs → poulpy-core/src/keyswitching/glwe_ct.rs

@@ -7,9 +7,9 @@ },

layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch, VecZnx, VecZnxBig, VecZnxDft, VmpPMat, ZnxInfos}, }; -use crate::layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; +use crate::layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::GLWESwitchingKeyPrepared}; -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>( module: &Module<B>, out_infos: &OUT,

@@ -28,8 +28,8 @@ .div_ceil(key_apply.base2k())

.div_ceil(key_apply.dsize().into()) as usize; let out_size: usize = out_infos.size(); let ksk_size: usize = key_apply.size(); - let res_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE - let ai_dft: usize = module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); + let res_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_out() + 1).into(), ksk_size); // TODO OPTIMIZE + let ai_dft: usize = module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes( out_size, in_size,

@@ -37,17 +37,17 @@ in_size,

(key_apply.rank_in()).into(), (key_apply.rank_out() + 1).into(), ksk_size, - ) + module.vec_znx_dft_alloc_bytes((key_apply.rank_in()).into(), in_size); + ) + module.vec_znx_dft_bytes_of((key_apply.rank_in()).into(), in_size); let normalize_big: usize = module.vec_znx_big_normalize_tmp_bytes(); if in_infos.base2k() == key_apply.base2k() { res_dft + ((ai_dft + vmp) | normalize_big) } else if key_apply.dsize() == 1 { // In this case, we only need one column, temporary, that we can drop once a_dft is computed. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), 1, in_size) + module.vec_znx_normalize_tmp_bytes(); res_dft + (((ai_dft + normalize_conv) | vmp) | normalize_big) } else { // Since we stride over a to get a_dft when dsize > 1, we need to store the full columns of a with in the base conversion. - let normalize_conv: usize = VecZnx::alloc_bytes(module.n(), (key_apply.rank_in()).into(), in_size); + let normalize_conv: usize = VecZnx::bytes_of(module.n(), (key_apply.rank_in()).into(), in_size); res_dft + ((ai_dft + normalize_conv + (module.vec_znx_normalize_tmp_bytes() | vmp)) | normalize_big) } }

@@ -62,12 +62,12 @@ Self::keyswitch_scratch_space(module, out_infos, out_infos, key_apply)

} } -impl<DataSelf: DataRef> GLWECiphertext<DataSelf> { +impl<DataSelf: DataRef> GLWE<DataSelf> { #[allow(dead_code)] pub(crate) fn assert_keyswitch<B: Backend, DataLhs, DataRhs>( &self, module: &Module<B>, - lhs: &GLWECiphertext<DataLhs>, + lhs: &GLWE<DataLhs>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>, scratch: &Scratch<B>, ) where

@@ -93,7 +93,7 @@ );

assert_eq!(rhs.n(), self.n()); assert_eq!(lhs.n(), self.n()); - let scrach_needed: usize = GLWECiphertext::keyswitch_scratch_space(module, self, lhs, rhs); + let scrach_needed: usize = GLWE::keyswitch_scratch_space(module, self, lhs, rhs); assert!( scratch.available() >= scrach_needed,

@@ -134,7 +134,7 @@ );

assert_eq!(rhs.n(), self.n()); - let scrach_needed: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, self, rhs); + let scrach_needed: usize = GLWE::keyswitch_inplace_scratch_space(module, self, rhs); assert!( scratch.available() >= scrach_needed,

@@ -144,11 +144,11 @@ );

} } -impl<DataSelf: DataMut> GLWECiphertext<DataSelf> { +impl<DataSelf: DataMut> GLWE<DataSelf> { pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>( &mut self, module: &Module<B>, - glwe_in: &GLWECiphertext<DataLhs>, + glwe_in: &GLWE<DataLhs>, rhs: &GLWESwitchingKeyPrepared<DataRhs, B>, scratch: &mut Scratch<B>, ) where

@@ -232,7 +232,7 @@ })

} } -impl<D: DataRef> GLWECiphertext<D> { +impl<D: DataRef> GLWE<D> { pub(crate) fn keyswitch_internal<B: Backend, DataRes, DataKey>( &self, module: &Module<B>,
M poulpy-core/src/keyswitching/lwe_ct.rs → poulpy-core/src/keyswitching/lwe_ct.rs

@@ -9,13 +9,10 @@ };

use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, LWECiphertext, LWEInfos, Rank, TorusPrecision, - prepared::LWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWELayout, LWE, LWEInfos, Rank, TorusPrecision, prepared::LWESwitchingKeyPrepared}, }; -impl LWECiphertext<Vec<u8>> { +impl LWE<Vec<u8>> { pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>( module: &Module<B>, out_infos: &OUT,

@@ -40,33 +37,33 @@ + VecZnxNormalizeTmpBytes,

{ let max_k: TorusPrecision = in_infos.k().max(out_infos.k()); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: module.n().into(), base2k: in_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: module.n().into(), base2k: out_infos.base2k(), k: max_k, rank: Rank(1), }; - let glwe_in: usize = GLWECiphertext::alloc_bytes(&glwe_in_infos); - let glwe_out: usize = GLWECiphertext::alloc_bytes(&glwe_out_infos); - let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); + let glwe_in: usize = GLWE::bytes_of(&glwe_in_infos); + let glwe_out: usize = GLWE::bytes_of(&glwe_out_infos); + let ks: usize = GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos); glwe_in + glwe_out + ks } } -impl<DLwe: DataMut> LWECiphertext<DLwe> { +impl<DLwe: DataMut> LWE<DLwe> { pub fn keyswitch<A, DKs, B: Backend>( &mut self, module: &Module<B>, - a: &LWECiphertext<A>, + a: &LWE<A>, ksk: &LWESwitchingKeyPrepared<DKs, B>, scratch: &mut Scratch<B>, ) where

@@ -90,14 +87,14 @@ #[cfg(debug_assertions)]

{ assert!(self.n() <= module.n() as u32); assert!(a.n() <= module.n() as u32); - assert!(scratch.available() >= LWECiphertext::keyswitch_scratch_space(module, self, a, ksk)); + assert!(scratch.available() >= LWE::keyswitch_scratch_space(module, self, a, ksk)); } let max_k: TorusPrecision = self.k().max(a.k()); let a_size: usize = a.k().div_ceil(ksk.base2k()) as usize; - let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe_in, scratch_1) = scratch.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: a.base2k(), k: max_k,

@@ -105,7 +102,7 @@ rank: Rank(1),

}); glwe_in.data.zero(); - let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWECiphertextLayout { + let (mut glwe_out, scratch_1) = scratch_1.take_glwe_ct(&GLWELayout { n: ksk.n(), base2k: self.base2k(), k: max_k,
M poulpy-core/src/layouts/compressed/gglwe_atk.rs → poulpy-core/src/layouts/compressed/gglwe_atk.rs

@@ -1,12 +1,15 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, + AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, + prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt;

@@ -76,35 +79,100 @@ write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)

} } +pub trait AutomorphismKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyCompressed<Vec<u8>> { + AutomorphismKeyCompressed { + key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>> + where + A: GGLWEInfos, + { + assert_eq!(infos.rank_in(), infos.rank_out()); + self.alloc_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize) + } + + fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.rank_in(), infos.rank_out()); + self.bytes_of_automorphism_key_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + impl AutomorphismKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self where A: GGLWEInfos, + Module<B>: AutomorphismKeyCompressedAlloc, { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - Self { - key: GLWESwitchingKeyCompressed::alloc(infos), - p: 0, - } + module.alloc_automorphism_key_compressed_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - Self { - key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module<B>: AutomorphismKeyCompressedAlloc, + { + module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize where A: GGLWEInfos, + Module<B>: AutomorphismKeyCompressedAlloc, { - debug_assert_eq!(infos.rank_in(), infos.rank_out()); - GLWESwitchingKeyCompressed::alloc_bytes(infos) + module.bytes_of_automorphism_key_compressed_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize) + pub fn bytes_of<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: AutomorphismKeyCompressedAlloc, + { + module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } }

@@ -122,13 +190,32 @@ self.key.write_to(writer)

} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, AutomorphismKeyCompressed<DR>> for AutomorphismKey<D> +pub trait AutomorphismKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O) + where + R: AutomorphismKeyToMut + SetAutomorphismGaloisElement, + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + { + self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key); + res.set_p(other.p()); + } +} + +impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: AutomorphismKeyDecompress {} + +impl<D: DataMut> AutomorphismKey<D> where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: SetAutomorphismGaloisElement, { - fn decompress(&mut self, module: &Module<B>, other: &AutomorphismKeyCompressed<DR>) { - self.key.decompress(module, &other.key); - self.p = other.p; + pub fn decompressed<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + Module<B>: AutomorphismKeyDecompress, + { + module.decompress_automorphism_key(self, other); } }
M poulpy-core/src/layouts/compressed/gglwe_ct.rs → poulpy-core/src/layouts/compressed/gglwe_ct.rs

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

}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGLWECiphertextCompressed<D: Data> { +pub struct GGLWECompressed<D: Data> { pub(crate) data: MatZnx<D>, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision,

@@ -23,7 +23,7 @@ pub(crate) dsize: Dsize,

pub(crate) seed: Vec<[u8; 32]>, } -impl<D: Data> LWEInfos for GGLWECiphertextCompressed<D> { +impl<D: Data> LWEInfos for GGLWECompressed<D> { fn n(&self) -> Degree { Degree(self.data.n() as u32) }

@@ -40,13 +40,13 @@ fn size(&self) -> usize {

self.data.size() } } -impl<D: Data> GLWEInfos for GGLWECiphertextCompressed<D> { +impl<D: Data> GLWEInfos for GGLWECompressed<D> { fn rank(&self) -> Rank { self.rank_out() } } -impl<D: Data> GGLWEInfos for GGLWECiphertextCompressed<D> { +impl<D: Data> GGLWEInfos for GGLWECompressed<D> { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) }

@@ -64,53 +64,41 @@ Dnum(self.data.rows() as u32)

} } -impl<D: DataRef> fmt::Debug for GGLWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Debug for GGLWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataMut> FillUniform for GGLWECiphertextCompressed<D> { +impl<D: DataMut> FillUniform for GGLWECompressed<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl<D: DataRef> fmt::Display for GGLWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Display for GGLWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGLWECiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGLWECompressed: base2k={} k={} dsize={}) {}", self.base2k.0, self.k.0, self.dsize.0, self.data ) } } -impl GGLWECiphertextCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWECompressedAlloc +where + Self: GetDegree, +{ + fn alloc_gglwe_compressed( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWECompressed<Vec<u8>> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -125,9 +113,9 @@ dnum.0,

dsize.0, ); - Self { + GGLWECompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), rank_in.into(), 1,

@@ -141,21 +129,22 @@ seed: vec![[0u8; 32]; (dnum.0 * rank_in.0) as usize],

} } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn alloc_gglwe_compressed_from_infos<A>(&self, infos: &A) -> GGLWECompressed<Vec<u8>> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + assert_eq!(infos.n(), self.n()); + self.alloc_gglwe_compressed( infos.base2k(), infos.k(), infos.rank_in(), + infos.rank_out(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -170,20 +159,83 @@ dnum.0,

dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), rank_in.into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_compressed_from_infos<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!(infos.n(), self.n()); + self.bytes_of_gglwe_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.dnum(), + infos.dsize(), + ) + } } -impl<D: DataRef> GGLWECiphertextCompressed<D> { - pub(crate) fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl<B: Backend> GGLWECompressedAlloc for Module<B> where Self: GetDegree {} + +impl GGLWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed_from_infos(infos) + } + + pub fn alloc<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module<B>: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed_from_infos(infos) + } + + pub fn byte_of<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } +} + +impl<D: DataRef> GGLWECompressed<D> { + pub(crate) fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k,

@@ -193,10 +245,10 @@ }

} } -impl<D: DataMut> GGLWECiphertextCompressed<D> { - pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl<D: DataMut> GGLWECompressed<D> { + pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank_in: usize = self.rank_in().into(); - GLWECiphertextCompressed { + GLWECompressed { k: self.k, base2k: self.base2k, rank: self.rank_out,

@@ -206,7 +258,7 @@ }

} } -impl<D: DataMut> ReaderFrom for GGLWECiphertextCompressed<D> { +impl<D: DataMut> ReaderFrom for GGLWECompressed<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -221,7 +273,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for GGLWECiphertextCompressed<D> { +impl<D: DataRef> WriterTo for GGLWECompressed<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;

@@ -235,70 +287,50 @@ self.data.write_to(writer)

} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWE<D> +pub trait GGLWEDecompress where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module<B>, other: &GGLWECiphertextCompressed<DR>) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", - self.n(), - other.n() - ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank_in(), - other.rank_in(), - "invalid receiver: self.rank_in()={} != other.rank_in()={}", - self.rank_in(), - other.rank_in() - ); - assert_eq!( - self.rank_out(), - other.rank_out(), - "invalid receiver: self.rank_out()={} != other.rank_out()={}", - self.rank_out(), - other.rank_out() - ); + fn decompress_gglwe<R, O>(&self, res: &mut R, other: &O) + where + R: GGLWEToMut, + O: GGLWECompressedToRef, + { + let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GGLWECompressed<&[u8]> = &other.to_ref(); + + assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + + let rank_in: usize = res.rank_in().into(); + let dnum: usize = res.dnum().into(); - assert_eq!( - self.dnum(), - other.dnum(), - "invalid receiver: self.dnum()={} != other.dnum()={}", - self.dnum(), - other.dnum() - ); + for row_i in 0..dnum { + for col_i in 0..rank_in { + self.decompress_glwe(&mut res.at_mut(row_i, col_i), &other.at(row_i, col_i)); + } } + } +} - let rank_in: usize = self.rank_in().into(); - let dnum: usize = self.dnum().into(); +impl<B: Backend> GGLWEDecompress for Module<B> where Self: VecZnxFillUniform + VecZnxCopy {} - (0..rank_in).for_each(|col_i| { - (0..dnum).for_each(|row_i| { - self.at_mut(row_i, col_i) - .decompress(module, &other.at(row_i, col_i)); - }); - }); +impl<D: DataMut> GGLWE<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: GGLWECompressedToRef, + Module<B>: GGLWEDecompress, + { + module.decompress_gglwe(self, other); } } -pub trait GGLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]>; +pub trait GGLWECompressedToMut { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]>; } -impl<D: DataMut> GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed<D> { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> { - GGLWECiphertextCompressed { +impl<D: DataMut> GGLWECompressedToMut for GGLWECompressed<D> { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(),

@@ -309,13 +341,13 @@ }

} } -pub trait GGLWECiphertextCompressedToRef { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; +pub trait GGLWECompressedToRef { + fn to_ref(&self) -> GGLWECompressed<&[u8]>; } -impl<D: DataRef> GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed<D> { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { - GGLWECiphertextCompressed { +impl<D: DataRef> GGLWECompressedToRef for GGLWECompressed<D> { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(),
M poulpy-core/src/layouts/compressed/gglwe_ksk.rs → poulpy-core/src/layouts/compressed/gglwe_ksk.rs

@@ -1,19 +1,19 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut, + LWEInfos, Rank, TorusPrecision, + compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] pub struct GLWESwitchingKeyCompressed<D: Data> { - pub(crate) key: GGLWECiphertextCompressed<D>, + pub(crate) key: GGLWECompressed<D>, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out }

@@ -81,43 +81,103 @@ )

} } +pub trait GLWESwitchingKeyCompressedAlloc +where + Self: GGLWECompressedAlloc, +{ + fn alloc_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GLWESwitchingKeyCompressed<Vec<u8>> { + GLWESwitchingKeyCompressed { + key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } + } + + fn alloc_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyCompressed<Vec<u8>> + where + A: GGLWEInfos, + { + self.alloc_glwe_switching_key_compressed( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe_compressed_from_infos(infos) + } +} + impl GLWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self where A: GGLWEInfos, + Module<B>: GLWESwitchingKeyCompressedAlloc, { - GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } + module.alloc_glwe_switching_key_compressed_from_infos(infos) } - pub fn alloc_with( - n: Degree, + pub fn alloc<B: Backend>( + module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { - GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } + ) -> Self + where + Module<B>: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, + Module<B>: GLWESwitchingKeyCompressedAlloc, { - GGLWECiphertextCompressed::alloc_bytes(infos) + module.bytes_of_glwe_switching_key_compressed_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, dsize) + pub fn bytes_of<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) } }

@@ -137,14 +197,31 @@ self.key.write_to(writer)

} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GLWESwitchingKeyCompressed<DR>> for GLWESwitchingKey<D> +pub trait GLWESwitchingKeyDecompress where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: GGLWEDecompress, { - fn decompress(&mut self, module: &Module<B>, other: &GLWESwitchingKeyCompressed<DR>) { - self.key.decompress(module, &other.key); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + fn decompress_glwe_switching_key<R, O>(&self, res: &mut R, other: &O) + where + R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData, + O: GLWESwitchingKeyCompressedToRef, + { + let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref(); + self.decompress_gglwe(&mut res.to_mut().key, &other.key); + res.set_sk_in_n(other.sk_in_n); + res.set_sk_out_n(other.sk_out_n); + } +} + +impl<B: Backend> GLWESwitchingKeyDecompress for Module<B> where Self: GGLWEDecompress {} + +impl<D: DataMut> GLWESwitchingKey<D> { + pub fn decompress<O, B: Backend>(&mut self, module: Module<B>, other: &O) + where + O: GLWESwitchingKeyCompressedToRef, + Module<B>: GGLWEDecompress, + { + module.decompress_glwe_switching_key(self, other); } }

@@ -154,7 +231,7 @@ }

impl<D: DataMut> GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed<D> where - GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToMut, + GGLWECompressed<D>: GGLWECompressedToMut, { fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { GLWESwitchingKeyCompressed {

@@ -171,7 +248,7 @@ }

impl<D: DataRef> GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed<D> where - GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToRef, + GGLWECompressed<D>: GGLWECompressedToRef, { fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { GLWESwitchingKeyCompressed {
M poulpy-core/src/layouts/compressed/gglwe_tsk.rs → poulpy-core/src/layouts/compressed/gglwe_tsk.rs

@@ -1,12 +1,14 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt;

@@ -80,8 +82,27 @@ Ok(())

} } -impl TensorKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait TensorKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn tensor_key_compressed_alloc( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> TensorKeyCompressed<Vec<u8>> { + let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); + TensorKeyCompressed { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn tensor_key_compressed_alloc_from_infos<A>(&self, infos: &A) -> TensorKeyCompressed<Vec<u8>> where A: GGLWEInfos, {

@@ -90,58 +111,70 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" ); - Self::alloc_with( - infos.n(), + self.tensor_key_compressed_alloc( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec<GLWESwitchingKeyCompressed<Vec<u8>>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn tensor_key_compressed_bytes_of(&self, 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 * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn tensor_key_compressed_bytes_of_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWETensorKeyCompressed" - ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKeyCompressed::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.dnum(), - infos.dsize(), - ) + self.tensor_key_compressed_bytes_of( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } +} - 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 * GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize) +impl TensorKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module<B>: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of(base2k, k, rank, dnum, dsize) } }

@@ -181,28 +214,41 @@ &mut self.keys[i * rank + j - (i * (i + 1) / 2)]

} } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, TensorKeyCompressed<DR>> for TensorKey<D> +pub trait TensorKeyDecompress where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module<B>, other: &TensorKeyCompressed<DR>) { - #[cfg(debug_assertions)] - { - assert_eq!( - self.keys.len(), - other.keys.len(), - "invalid receiver: self.keys.len()={} != other.keys.len()={}", - self.keys.len(), - other.keys.len() - ); + fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O) + where + R: TensorKeyToMut, + O: TensorKeyCompressedToRef, + { + let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut(); + let other: &TensorKeyCompressed<&[u8]> = &other.to_ref(); + + assert_eq!( + res.keys.len(), + other.keys.len(), + "invalid receiver: res.keys.len()={} != other.keys.len()={}", + res.keys.len(), + other.keys.len() + ); + + for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) { + self.decompress_glwe_switching_key(a, b); } + } +} - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.decompress(module, b); - }); +impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} + +impl<D: DataMut> TensorKey<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: TensorKeyCompressedToRef, + Module<B>: GLWESwitchingKeyDecompress, + { + module.decompress_tensor_key(self, other); } }
M poulpy-core/src/layouts/compressed/ggsw_ct.rs → poulpy-core/src/layouts/compressed/ggsw_ct.rs

@@ -1,5 +1,4 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{ Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, },

@@ -7,14 +6,14 @@ source::Source,

}; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, - compressed::{Decompress, GLWECiphertextCompressed}, + Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision, + compressed::{GLWECompressed, GLWEDecompress}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GGSWCiphertextCompressed<D: Data> { +pub struct GGSWCompressed<D: Data> { pub(crate) data: MatZnx<D>, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K,

@@ -23,7 +22,7 @@ pub(crate) rank: Rank,

pub(crate) seed: Vec<[u8; 32]>, } -impl<D: Data> LWEInfos for GGSWCiphertextCompressed<D> { +impl<D: Data> LWEInfos for GGSWCompressed<D> { fn n(&self) -> Degree { Degree(self.data.n() as u32) }

@@ -39,13 +38,13 @@ fn size(&self) -> usize {

self.data.size() } } -impl<D: Data> GLWEInfos for GGSWCiphertextCompressed<D> { +impl<D: Data> GLWEInfos for GGSWCompressed<D> { fn rank(&self) -> Rank { self.rank } } -impl<D: Data> GGSWInfos for GGSWCiphertextCompressed<D> { +impl<D: Data> GGSWInfos for GGSWCompressed<D> { fn dsize(&self) -> Dsize { self.dsize }

@@ -55,46 +54,42 @@ Dnum(self.data.rows() as u32)

} } -impl<D: DataRef> fmt::Debug for GGSWCiphertextCompressed<D> { +impl<D: DataRef> fmt::Debug for GGSWCompressed<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl<D: DataRef> fmt::Display for GGSWCiphertextCompressed<D> { +impl<D: DataRef> fmt::Display for GGSWCompressed<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "(GGSWCiphertextCompressed: base2k={} k={} dsize={}) {}", + "(GGSWCompressed: base2k={} k={} dsize={}) {}", self.base2k, self.k, self.dsize, self.data ) } } -impl<D: DataMut> FillUniform for GGSWCiphertextCompressed<D> { +impl<D: DataMut> FillUniform for GGSWCompressed<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertextCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWCompressedAlloc +where + Self: GetDegree, +{ + fn alloc_ggsw_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GGSWCompressed<Vec<u8>> { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0

@@ -107,9 +102,9 @@ dnum.0,

dsize.0, ); - Self { + GGSWCompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), 1,

@@ -123,12 +118,11 @@ seed: Vec::new(),

} } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn alloc_ggsw_compressed_from_infos<A>(&self, infos: &A) -> GGSWCompressed<Vec<u8>> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw_compressed( infos.base2k(), infos.k(), infos.rank(),

@@ -137,9 +131,9 @@ infos.dsize(),

) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; - debug_assert!( + assert!( size as u32 > dsize.0, "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}", dsize.0

@@ -152,20 +146,72 @@ dnum.0,

dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), (rank + 1).into(), 1, k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_ggsw_compressed_key_from_infos<A>(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } } -impl<D: DataRef> GGSWCiphertextCompressed<D> { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGSWCompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGSWInfos, + Module<B>: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module<B>: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGSWInfos, + Module<B>: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed_key_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl<D: DataRef> GGSWCompressed<D> { + pub fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at(row, col), k: self.k, base2k: self.base2k,

@@ -175,10 +221,10 @@ }

} } -impl<D: DataMut> GGSWCiphertextCompressed<D> { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl<D: DataMut> GGSWCompressed<D> { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> { let rank: usize = self.rank().into(); - GLWECiphertextCompressed { + GLWECompressed { data: self.data.at_mut(row, col), k: self.k, base2k: self.base2k,

@@ -188,7 +234,7 @@ }

} } -impl<D: DataMut> ReaderFrom for GGSWCiphertextCompressed<D> { +impl<D: DataMut> ReaderFrom for GGSWCompressed<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -203,7 +249,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for GGSWCiphertextCompressed<D> { +impl<D: DataRef> WriterTo for GGSWCompressed<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;

@@ -217,34 +263,49 @@ self.data.write_to(writer)

} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSW<D> +pub trait GGSWDecompress where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module<B>, other: &GGSWCiphertextCompressed<DR>) { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), other.rank()) + fn decompress_ggsw<R, O>(&self, res: &mut R, other: &O) + where + R: GGSWToMut, + O: GGSWCompressedToRef, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let other: &GGSWCompressed<&[u8]> = &other.to_ref(); + + assert_eq!(res.rank(), other.rank()); + let dnum: usize = res.dnum().into(); + let rank: usize = res.rank().into(); + + for row_i in 0..dnum { + for col_j in 0..rank + 1 { + self.decompress_glwe(&mut res.at_mut(row_i, col_j), &other.at(row_i, col_j)); + } } + } +} - let dnum: usize = self.dnum().into(); - let rank: usize = self.rank().into(); - (0..dnum).for_each(|row_i| { - (0..rank + 1).for_each(|col_j| { - self.at_mut(row_i, col_j) - .decompress(module, &other.at(row_i, col_j)); - }); - }); +impl<B: Backend> GGSWDecompress for Module<B> where Self: GGSWDecompress {} + +impl<D: DataMut> GGSW<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: GGSWCompressedToRef, + Module<B>: GGSWDecompress, + { + module.decompress_ggsw(self, other); } } -pub trait GGSWCiphertextCompressedToMut { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]>; +pub trait GGSWCompressedToMut { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]>; } -impl<D: DataMut> GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed<D> { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> { - GGSWCiphertextCompressed { +impl<D: DataMut> GGSWCompressedToMut for GGSWCompressed<D> { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(),

@@ -255,13 +316,13 @@ }

} } -pub trait GGSWCiphertextCompressedToRef { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>; +pub trait GGSWCompressedToRef { + fn to_ref(&self) -> GGSWCompressed<&[u8]>; } -impl<D: DataRef> GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed<D> { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> { - GGSWCiphertextCompressed { +impl<D: DataRef> GGSWCompressedToRef for GGSWCompressed<D> { + fn to_ref(&self) -> GGSWCompressed<&[u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(),
M poulpy-core/src/layouts/compressed/glwe_ct.rs → poulpy-core/src/layouts/compressed/glwe_ct.rs

@@ -6,12 +6,12 @@ },

source::Source, }; -use crate::layouts::{Base2K, Degree, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::Decompress}; +use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt; #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertextCompressed<D: Data> { +pub struct GLWECompressed<D: Data> { pub(crate) data: VecZnx<D>, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision,

@@ -19,7 +19,7 @@ pub(crate) rank: Rank,

pub(crate) seed: [u8; 32], } -impl<D: Data> LWEInfos for GLWECiphertextCompressed<D> { +impl<D: Data> LWEInfos for GLWECompressed<D> { fn base2k(&self) -> Base2K { self.base2k }

@@ -36,23 +36,23 @@ fn n(&self) -> Degree {

Degree(self.data.n() as u32) } } -impl<D: Data> GLWEInfos for GLWECiphertextCompressed<D> { +impl<D: Data> GLWEInfos for GLWECompressed<D> { fn rank(&self) -> Rank { self.rank } } -impl<D: DataRef> fmt::Debug for GLWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Debug for GLWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataRef> fmt::Display for GLWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Display for GLWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertextCompressed: base2k={} k={} rank={} seed={:?}: {}", + "GLWECompressed: base2k={} k={} rank={} seed={:?}: {}", self.base2k(), self.k(), self.rank(),

@@ -62,23 +62,19 @@ )

} } -impl<D: DataMut> FillUniform for GLWECiphertextCompressed<D> { +impl<D: DataMut> FillUniform for GLWECompressed<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertextCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize), +pub trait GLWECompressedAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed<Vec<u8>> { + GLWECompressed { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, rank,

@@ -86,19 +82,62 @@ seed: [0u8; 32],

} } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn alloc_glwe_compressed_from_infos<A>(&self, infos: &A) -> GLWECompressed<Vec<u8>> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + assert_eq!(self.n(), infos.n()); + self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - VecZnx::alloc_bytes(n.into(), 1, k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_glwe_compressed_from_infos<A>(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + assert_eq!(self.n(), infos.n()); + self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) } } -impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> { +impl<B: Backend> GLWECompressedAlloc for Module<B> where Self: GetDegree {} + +impl GLWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GLWEInfos, + Module<B>: GLWECompressedAlloc, + { + module.alloc_glwe_compressed_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module<B>: GLWECompressedAlloc, + { + module.alloc_glwe_compressed(base2k, k, rank) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GLWEInfos, + Module<B>: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> usize + where + Module<B>: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed(base2k, k) + } +} + +impl<D: DataMut> ReaderFrom for GLWECompressed<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -108,7 +147,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for GLWECiphertextCompressed<D> { +impl<D: DataRef> WriterTo for GLWECompressed<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;

@@ -118,74 +157,61 @@ self.data.write_to(writer)

} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GLWECiphertextCompressed<DR>> for GLWECiphertext<D> +pub trait GLWEDecompress where - Module<B>: VecZnxFillUniform + VecZnxCopy, + Self: GetDegree + VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module<B>, other: &GLWECiphertextCompressed<DR>) { - #[cfg(debug_assertions)] + fn decompress_glwe<R, O>(&self, res: &mut R, other: &O) + where + R: GLWEToMut + SetGLWEInfos, + O: GLWECompressedToRef + GLWEInfos, + { { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GLWECompressed<&[u8]> = &other.to_ref(); assert_eq!( - self.n(), - other.n(), - "invalid receiver: self.n()={} != other.n()={}", + res.n(), self.n(), - other.n() + "invalid receiver: res.n()={} != other.n()={}", + res.n(), + self.n() ); - assert_eq!( - self.size(), - other.size(), - "invalid receiver: self.size()={} != other.size()={}", - self.size(), - other.size() - ); - assert_eq!( - self.rank(), - other.rank(), - "invalid receiver: self.rank()={} != other.rank()={}", - self.rank(), - other.rank() - ); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + assert_eq!(res.glwe_layout(), other.glwe_layout()); + + let mut source: Source = Source::new(other.seed); + + self.vec_znx_copy(&mut res.data, 0, &other.data, 0); + (1..(other.rank() + 1).into()).for_each(|i| { + self.vec_znx_fill_uniform(other.base2k.into(), &mut res.data, i, &mut source); + }); } - let mut source: Source = Source::new(other.seed); - self.decompress_internal(module, other, &mut source); + res.set_base2k(other.base2k()); + res.set_k(other.k()); } } -impl<D: DataMut> GLWECiphertext<D> { - pub(crate) fn decompress_internal<DataOther, B: Backend>( - &mut self, - module: &Module<B>, - other: &GLWECiphertextCompressed<DataOther>, - source: &mut Source, - ) where - DataOther: DataRef, - Module<B>: VecZnxCopy + VecZnxFillUniform, - { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), other.rank()); - debug_assert_eq!(self.size(), other.size()); - } +impl<B: Backend> GLWEDecompress for Module<B> where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} - module.vec_znx_copy(&mut self.data, 0, &other.data, 0); - (1..(other.rank() + 1).into()).for_each(|i| { - module.vec_znx_fill_uniform(other.base2k.into(), &mut self.data, i, source); - }); - - self.base2k = other.base2k; - self.k = other.k; +impl<D: DataMut> GLWE<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: GLWECompressedToRef + GLWEInfos, + Module<B>: GLWEDecompress, + { + module.decompress_glwe(self, other); } } -pub trait GLWECiphertextCompressedToRef { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>; +pub trait GLWECompressedToRef { + fn to_ref(&self) -> GLWECompressed<&[u8]>; } -impl<D: DataRef> GLWECiphertextCompressedToRef for GLWECiphertextCompressed<D> { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { - GLWECiphertextCompressed { +impl<D: DataRef> GLWECompressedToRef for GLWECompressed<D> { + fn to_ref(&self) -> GLWECompressed<&[u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k,

@@ -195,13 +221,13 @@ }

} } -pub trait GLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>; +pub trait GLWECompressedToMut { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]>; } -impl<D: DataMut> GLWECiphertextCompressedToMut for GLWECiphertextCompressed<D> { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { - GLWECiphertextCompressed { +impl<D: DataMut> GLWECompressedToMut for GLWECompressed<D> { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k,
M poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs → poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs

@@ -1,12 +1,17 @@

use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GLWESwitchingKeyCompressed, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; #[derive(PartialEq, Eq, Clone)]

@@ -83,54 +88,146 @@ self.0.write_to(writer)

} } -impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKeyCompressed<Vec<u8>> { + GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_glwe_to_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } +} + +impl<B: Backend> GLWEToLWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {} + +impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module<B>: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize + where + Module<B>: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } +} + +pub trait GLWEToLWESwitchingKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O) + where + R: GLWEToLWESwitchingKeyToMut, + O: GLWEToLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl<B: Backend> GLWEToLWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} + +impl<D: DataMut> GLWEToLWESwitchingKey<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: GLWEToLWESwitchingKeyCompressedToRef, + Module<B>: GLWEToLWESwitchingKeyDecompress, + { + module.decompress_glwe_to_lwe_switching_key(self, other); + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; +} + +impl<D: DataRef> GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed<D> +where + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) } +} + +pub trait GLWEToLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; +} - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1)) +impl<D: DataMut> GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed<D> +where + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) } }
M poulpy-core/src/layouts/compressed/lwe_ct.rs → poulpy-core/src/layouts/compressed/lwe_ct.rs

@@ -2,21 +2,24 @@ use std::fmt;

use poulpy_hal::{ api::ZnFillUniform, - layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnxInfos, ZnxView, ZnxViewMut}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos, ZnxView, + ZnxViewMut, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, LWECiphertext, LWEInfos, TorusPrecision, compressed::Decompress}; +use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision}; #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertextCompressed<D: Data> { +pub struct LWECompressed<D: Data> { pub(crate) data: Zn<D>, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) seed: [u8; 32], } -impl<D: Data> LWEInfos for LWECiphertextCompressed<D> { +impl<D: Data> LWEInfos for LWECompressed<D> { fn base2k(&self) -> Base2K { self.base2k }

@@ -34,17 +37,17 @@ self.data.size()

} } -impl<D: DataRef> fmt::Debug for LWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Debug for LWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataRef> fmt::Display for LWECiphertextCompressed<D> { +impl<D: DataRef> fmt::Display for LWECompressed<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "LWECiphertextCompressed: base2k={} k={} seed={:?}: {}", + "LWECompressed: base2k={} k={} seed={:?}: {}", self.base2k(), self.k(), self.seed,

@@ -53,44 +56,76 @@ )

} } -impl<D: DataMut> FillUniform for LWECiphertextCompressed<D> { +impl<D: DataMut> FillUniform for LWECompressed<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl LWECiphertextCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWECompressedAlloc { + fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed<Vec<u8>> { + LWECompressed { + data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), + k, + base2k, + seed: [0u8; 32], + } + } + + fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>> + where + A: LWEInfos, + { + self.alloc_lwe_compressed(infos.base2k(), infos.k()) + } + + fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_lwe_compressed_from_infos<A>(&self, infos: &A) -> usize where A: LWEInfos, { - Self::alloc_with(infos.base2k(), infos.k()) + self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) } +} - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { - data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), - k, - base2k, - seed: [0u8; 32], - } +impl LWECompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: LWEInfos, + Module<B>: LWECompressedAlloc, + { + module.alloc_lwe_compressed_from_infos(infos) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> Self + where + Module<B>: LWECompressedAlloc, + { + module.alloc_lwe_compressed(base2k, k) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize where A: LWEInfos, + Module<B>: LWECompressedAlloc, { - Self::alloc_bytes_with(infos.base2k(), infos.k()) + module.bytes_of_lwe_compressed_from_infos(infos) } - pub fn alloc_bytes_with(base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes(1, 1, k.0.div_ceil(base2k.0) as usize) + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> usize + where + Module<B>: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed(base2k, k) } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl<D: DataMut> ReaderFrom for LWECiphertextCompressed<D> { +impl<D: DataMut> ReaderFrom for LWECompressed<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -99,7 +134,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for LWECiphertextCompressed<D> { +impl<D: DataRef> WriterTo for LWECompressed<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;

@@ -108,22 +143,72 @@ self.data.write_to(writer)

} } -impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, LWECiphertextCompressed<DR>> for LWECiphertext<D> +pub trait LWEDecompress where - Module<B>: ZnFillUniform, + Self: ZnFillUniform, { - fn decompress(&mut self, module: &Module<B>, other: &LWECiphertextCompressed<DR>) { - debug_assert_eq!(self.size(), other.size()); + fn decompress_lwe<R, O>(&self, res: &mut R, other: &O) + where + R: LWEToMut, + O: LWECompressedToRef, + { + let res: &mut LWE<&mut [u8]> = &mut res.to_mut(); + let other: &LWECompressed<&[u8]> = &other.to_ref(); + + assert_eq!(res.lwe_layout(), other.lwe_layout()); + let mut source: Source = Source::new(other.seed); - module.zn_fill_uniform( - self.n().into(), + self.zn_fill_uniform( + res.n().into(), other.base2k().into(), - &mut self.data, + &mut res.data, 0, &mut source, ); - (0..self.size()).for_each(|i| { - self.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; - }); + for i in 0..res.size() { + res.data.at_mut(0, i)[0] = other.data.at(0, i)[0]; + } + } +} + +impl<B: Backend> LWEDecompress for Module<B> where Self: ZnFillUniform {} + +impl<D: DataMut> LWE<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: LWECompressedToRef, + Module<B>: LWEDecompress, + { + module.decompress_lwe(self, other); + } +} + +pub trait LWECompressedToRef { + fn to_ref(&self) -> LWECompressed<&[u8]>; +} + +impl<D: DataRef> LWECompressedToRef for LWECompressed<D> { + fn to_ref(&self) -> LWECompressed<&[u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_ref(), + } + } +} + +pub trait LWECompressedToMut { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]>; +} + +impl<D: DataMut> LWECompressedToMut for LWECompressed<D> { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_mut(), + } } }
M poulpy-core/src/layouts/compressed/lwe_ksk.rs → poulpy-core/src/layouts/compressed/lwe_ksk.rs

@@ -1,12 +1,14 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt;

@@ -83,73 +85,147 @@ self.0.write_to(writer)

} } -impl LWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyCompressed<Vec<u8>> { + LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyCompressed<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, - "dsize > 1 is not supported for LWESwitchingKey" + "dsize > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, - "rank_in > 1 is not supported for LWESwitchingKey" + "rank_in > 1 is not supported for LWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, - "rank_out > 1 is not supported for LWESwitchingKey" + "rank_out > 1 is not supported for LWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) } +} - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) +impl LWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module<B>: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module<B>: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) } } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWESwitchingKeyCompressed<DR>> for LWESwitchingKey<D> +pub trait LWESwitchingKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_lwe_switching_key<R, O>(&self, res: &mut R, other: &O) + where + R: LWESwitchingKeyToMut, + O: LWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl<B: Backend> LWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} + +impl<D: DataMut> LWESwitchingKey<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: LWESwitchingKeyCompressedToRef, + Module<B>: LWESwitchingKeyDecompress, + { + module.decompress_lwe_switching_key(self, other); + } +} + +pub trait LWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; +} + +impl<D: DataRef> LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed<D> +where + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { + LWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; +} + +impl<D: DataMut> LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed<D> where - Module<B>: VecZnxFillUniform + VecZnxCopy, + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut, { - fn decompress(&mut self, module: &Module<B>, other: &LWESwitchingKeyCompressed<DR>) { - self.0.decompress(module, &other.0); + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { + LWESwitchingKeyCompressed(self.0.to_mut()) } }
M poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs → poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs

@@ -1,12 +1,15 @@

use poulpy_hal::{ - api::{VecZnxCopy, VecZnxFillUniform}, layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision, - compressed::{Decompress, GLWESwitchingKeyCompressed}, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank, + TorusPrecision, + compressed::{ + GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut, + GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress, + }, }; use std::fmt;

@@ -83,63 +86,138 @@ self.0.write_to(writer)

} } -impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_to_glwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>> { + LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - Self(GLWESwitchingKeyCompressed::alloc(infos)) + self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKeyCompressed::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( - infos.rank_in().0, + assert_eq!( + infos.dsize().0, 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - debug_assert_eq!( - infos.dsize().0, + assert_eq!( + infos.rank_in().0, 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(infos) + self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } +} - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1)) +impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> { + pub fn alloc<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc_with<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module<B>: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module<B>: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) } } -impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWEToGLWESwitchingKeyCompressed<DR>> for LWEToGLWESwitchingKey<D> +pub trait LWEToGLWESwitchingKeyDecompress +where + Self: GLWESwitchingKeyDecompress, +{ + fn decompress_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O) + where + R: LWEToGLWESwitchingKeyToMut, + O: LWEToGLWESwitchingKeyCompressedToRef, + { + self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0); + } +} + +impl<B: Backend> LWEToGLWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {} + +impl<D: DataMut> LWEToGLWESwitchingKey<D> { + pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O) + where + O: LWEToGLWESwitchingKeyCompressedToRef, + Module<B>: LWEToGLWESwitchingKeyDecompress, + { + module.decompress_lwe_to_glwe_switching_key(self, other); + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; +} + +impl<D: DataRef> LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed<D> +where + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl<D: DataMut> LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed<D> where - Module<B>: VecZnxFillUniform + VecZnxCopy, + GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut, { - fn decompress(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKeyCompressed<DR>) { - self.0.decompress(module, &other.0); + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) } }
M poulpy-core/src/layouts/compressed/mod.rs → poulpy-core/src/layouts/compressed/mod.rs

@@ -19,9 +19,3 @@ pub use glwe_to_lwe_ksk::*;

pub use lwe_ct::*; pub use lwe_ksk::*; pub use lwe_to_glwe_ksk::*; - -use poulpy_hal::layouts::{Backend, Module}; - -pub trait Decompress<B: Backend, C> { - fn decompress(&mut self, module: &Module<B>, other: &C); -}
M poulpy-core/src/layouts/gglwe_atk.rs → poulpy-core/src/layouts/gglwe_atk.rs

@@ -1,10 +1,10 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

@@ -131,43 +131,105 @@ write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key)

} } -impl AutomorphismKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GGLWEInfos, - { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); +impl<B: Backend> AutomorphismKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} + +pub trait AutomorphismKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_automorphism_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKey<Vec<u8>> { AutomorphismKey { - key: GLWESwitchingKey::alloc(infos), + key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), p: 0, } } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - AutomorphismKey { - key: GLWESwitchingKey::alloc_with(n, base2k, k, rank, rank, dnum, dsize), - p: 0, - } + fn alloc_automorphism_key_from_infos<A>(&self, infos: &A) -> AutomorphismKey<Vec<u8>> + where + A: GGLWEInfos, + { + self.alloc_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_automorphism_key_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!( infos.rank_in(), infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" + "rank_in != rank_out is not supported for AutomorphismKey" ); - GLWESwitchingKey::alloc_bytes(infos) + self.bytes_of_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } +} - pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank, rank, dnum, dsize) +impl AutomorphismKey<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key_from_infos(infos) + } + + pub fn alloc_with<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module<B>: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: &Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) } }

@@ -204,13 +266,13 @@ }

} impl<D: DataRef> AutomorphismKey<D> { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } impl<D: DataMut> AutomorphismKey<D> { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } }
M poulpy-core/src/layouts/gglwe_ct.rs → poulpy-core/src/layouts/gglwe_ct.rs

@@ -1,9 +1,11 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt;

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

fn dsize(&self) -> Dsize; fn rank_in(&self) -> Rank; fn rank_out(&self) -> Rank; - fn layout(&self) -> GGLWECiphertextLayout { + fn gglwe_layout(&self) -> GGLWECiphertextLayout { GGLWECiphertextLayout { n: self.n(), base2k: self.base2k(),

@@ -170,8 +172,8 @@ }

} impl<D: DataRef> GGLWE<D> { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at(row, col),

@@ -180,8 +182,8 @@ }

} impl<D: DataMut> GGLWE<D> { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at_mut(row, col),

@@ -189,31 +191,19 @@ }

} } -impl GGLWE<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GGLWEInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank_in(), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) - } - - pub fn alloc_with( - n: Degree, +pub trait GGLWEAlloc +where + Self: GetDegree, +{ + fn alloc_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { + ) -> GGLWE<Vec<u8>> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -228,9 +218,9 @@ dnum.0,

dsize.0, ); - Self { + GGLWE { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(),

@@ -242,12 +232,11 @@ dsize,

} } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GGLWE<Vec<u8>> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_gglwe( infos.base2k(), infos.k(), infos.rank_in(),

@@ -257,8 +246,8 @@ infos.dsize(),

) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank,

@@ -280,21 +269,85 @@ dnum.0,

dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), rank_in.into(), (rank_out + 1).into(), k.0.div_ceil(base2k.0) as usize, ) } + + fn bytes_of_gglwe_from_infos<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } } -pub trait GGLWECiphertextToMut { +impl<B: Backend> GGLWEAlloc for Module<B> where Self: GetDegree {} + +impl GGLWE<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: GGLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module<B>: GGLWEAlloc, + { + module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: GGLWEAlloc, + { + module.bytes_of_gglwe_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: GGLWEAlloc, + { + module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } +} + +pub trait GGLWEToMut { fn to_mut(&mut self) -> GGLWE<&mut [u8]>; } -impl<D: DataMut> GGLWECiphertextToMut for GGLWE<D> { +impl<D: DataMut> GGLWEToMut for GGLWE<D> { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { GGLWE { k: self.k(),
M poulpy-core/src/layouts/gglwe_ksk.rs → poulpy-core/src/layouts/gglwe_ksk.rs

@@ -1,10 +1,10 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWE, GGLWECiphertextToMut, GGLWEInfos, GGLWEToRef, GLWECiphertext, GLWEInfos, LWEInfos, Rank, + Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

@@ -163,51 +163,112 @@ self.key.fill_uniform(log_bound, source);

} } +pub trait GLWESwitchingKeyAlloc +where + Self: GGLWEAlloc, +{ + fn alloc_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> GLWESwitchingKey<Vec<u8>> { + GLWESwitchingKey { + key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), + sk_in_n: 0, + sk_out_n: 0, + } + } + + fn alloc_glwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWESwitchingKey<Vec<u8>> + where + A: GGLWEInfos, + { + self.alloc_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } + + fn bytes_of_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize { + self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) + } +} + impl GLWESwitchingKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self where A: GGLWEInfos, + Module<B>: GLWESwitchingKeyAlloc, { - GLWESwitchingKey { - key: GGLWE::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } + module.alloc_glwe_switching_key_from_infos(infos) } - pub fn alloc_with( - n: Degree, + pub fn alloc<B: Backend>( + module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> Self { - GLWESwitchingKey { - key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), - sk_in_n: 0, - sk_out_n: 0, - } + ) -> Self + where + Module<B>: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, + Module<B>: GLWESwitchingKeyAlloc, { - GGLWE::alloc_bytes(infos) + module.bytes_of_glwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with( - n: Degree, + pub fn bytes_of<B: Backend>( + module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize, - ) -> usize { - GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + ) -> usize + where + Module<B>: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } }

@@ -217,7 +278,7 @@ }

impl<D: DataMut> GLWESwitchingKeyToMut for GLWESwitchingKey<D> where - GGLWE<D>: GGLWECiphertextToMut, + GGLWE<D>: GGLWEToMut, { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { GLWESwitchingKey {

@@ -246,13 +307,13 @@ }

} impl<D: DataRef> GLWESwitchingKey<D> { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { self.key.at(row, col) } } impl<D: DataMut> GLWESwitchingKey<D> { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { self.key.at_mut(row, col) } }
M poulpy-core/src/layouts/gglwe_tsk.rs → poulpy-core/src/layouts/gglwe_tsk.rs

@@ -1,11 +1,11 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

@@ -130,8 +130,20 @@ Ok(())

} } -impl TensorKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait TensorKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey<Vec<u8>> { + let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); + TensorKey { + keys: (0..pairs) + .map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)) + .collect(), + } + } + + fn alloc_tensor_key_from_infos<A>(&self, infos: &A) -> TensorKey<Vec<u8>> where A: GGLWEInfos, {

@@ -140,34 +152,21 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - Self::alloc_with( - infos.n(), + self.alloc_tensor_key( infos.base2k(), infos.k(), - infos.rank_out(), + infos.rank(), infos.dnum(), infos.dsize(), ) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { - let mut keys: Vec<GLWESwitchingKey<Vec<u8>>> = Vec::new(); - let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1); - (0..pairs).for_each(|_| { - keys.push(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank, - dnum, - dsize, - )); - }); - Self { keys } + fn bytes_of_tensor_key(&self, 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 * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_tensor_key_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -176,23 +175,54 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKey" ); - let rank_out: usize = infos.rank_out().into(); - let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1); - pairs - * GLWESwitchingKey::alloc_bytes_with( - infos.n(), - infos.base2k(), - infos.k(), - Rank(1), - infos.rank_out(), - infos.dnum(), - infos.dsize(), - ) + self.bytes_of_tensor_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) } +} - 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 * GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank, dnum, dsize) +impl<B: Backend> TensorKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} + +impl TensorKey<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: TensorKeyAlloc, + { + module.alloc_tensor_key_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module<B>: TensorKeyAlloc, + { + module.alloc_tensor_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: TensorKeyAlloc, + { + module.bytes_of_tensor_key_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: TensorKeyAlloc, + { + module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) } }

@@ -260,14 +290,14 @@ }

} pub trait TensorKeyToMut { - fn to_ref(&mut self) -> TensorKey<&mut [u8]>; + fn to_mut(&mut self) -> TensorKey<&mut [u8]>; } impl<D: DataMut> TensorKeyToMut for TensorKey<D> where GLWESwitchingKey<D>: GLWESwitchingKeyToMut, { - fn to_ref(&mut self) -> TensorKey<&mut [u8]> { + fn to_mut(&mut self) -> TensorKey<&mut [u8]> { TensorKey { keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(), }
M poulpy-core/src/layouts/ggsw_ct.rs → poulpy-core/src/layouts/ggsw_ct.rs

@@ -1,10 +1,12 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos}, + layouts::{ + Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos, + }, source::Source, }; use std::fmt; -use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}; pub trait GGSWInfos where

@@ -131,8 +133,8 @@ }

} impl<D: DataRef> GGSW<D> { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> { - GLWECiphertext { + pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at(row, col),

@@ -141,8 +143,8 @@ }

} impl<D: DataMut> GGSW<D> { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { + pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.at_mut(row, col),

@@ -150,22 +152,13 @@ }

} } -impl GGSW<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } +impl<B: Backend> GGSWAlloc for Module<B> where Self: GetDegree {} - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self { +pub trait GGSWAlloc +where + Self: GetDegree, +{ + fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW<Vec<u8>> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -180,9 +173,9 @@ dnum.0,

dsize.0, ); - Self { + GGSW { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(),

@@ -194,12 +187,11 @@ dsize,

} } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn alloc_ggsw_from_infos<A>(&self, infos: &A) -> GGSW<Vec<u8>> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw( infos.base2k(), infos.k(), infos.rank(),

@@ -208,7 +200,7 @@ infos.dsize(),

) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -223,13 +215,65 @@ dnum.0,

dsize.0, ); - MatZnx::alloc_bytes( - n.into(), + MatZnx::bytes_of( + self.n().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize, ) + } + + fn bytes_of_ggsw_from_infos<A>(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGSW<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GGSWInfos, + Module<B>: GGSWAlloc, + { + module.alloc_ggsw_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module<B>: GGSWAlloc, + { + module.alloc_ggsw(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize + where + A: GGSWInfos, + Module<B>: GGSWAlloc, + { + module.bytes_of_ggsw_from_infos(infos) + } + + pub fn bytes_of<B: Backend>( + module: Module<B>, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module<B>: GGSWAlloc, + { + module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) } }
M poulpy-core/src/layouts/glwe_ct.rs → poulpy-core/src/layouts/glwe_ct.rs

@@ -1,11 +1,12 @@

use poulpy_hal::{ layouts::{ - Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, + Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, + WriterTo, ZnxInfos, }, source::Source, }; -use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision}; +use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use std::fmt;

@@ -14,8 +15,8 @@ where

Self: LWEInfos, { fn rank(&self) -> Rank; - fn glwe_layout(&self) -> GLWECiphertextLayout { - GLWECiphertextLayout { + fn glwe_layout(&self) -> GLWELayout { + GLWELayout { n: self.n(), base2k: self.base2k(), k: self.k(),

@@ -24,20 +25,20 @@ }

} } -pub trait GLWELayoutSet { +pub trait SetGLWEInfos { fn set_k(&mut self, k: TorusPrecision); - fn set_basek(&mut self, base2k: Base2K); + fn set_base2k(&mut self, base2k: Base2K); } #[derive(PartialEq, Eq, Copy, Clone, Debug)] -pub struct GLWECiphertextLayout { +pub struct GLWELayout { pub n: Degree, pub base2k: Base2K, pub k: TorusPrecision, pub rank: Rank, } -impl LWEInfos for GLWECiphertextLayout { +impl LWEInfos for GLWELayout { fn n(&self) -> Degree { self.n }

@@ -51,21 +52,21 @@ self.k

} } -impl GLWEInfos for GLWECiphertextLayout { +impl GLWEInfos for GLWELayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertext<D: Data> { +pub struct GLWE<D: Data> { pub(crate) data: VecZnx<D>, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, } -impl<D: DataMut> GLWELayoutSet for GLWECiphertext<D> { - fn set_basek(&mut self, base2k: Base2K) { +impl<D: DataMut> SetGLWEInfos for GLWE<D> { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k }

@@ -74,19 +75,19 @@ self.k = k

} } -impl<D: DataRef> GLWECiphertext<D> { +impl<D: DataRef> GLWE<D> { pub fn data(&self) -> &VecZnx<D> { &self.data } } -impl<D: DataMut> GLWECiphertext<D> { +impl<D: DataMut> GLWE<D> { pub fn data_mut(&mut self) -> &mut VecZnx<D> { &mut self.data } } -impl<D: Data> LWEInfos for GLWECiphertext<D> { +impl<D: Data> LWEInfos for GLWE<D> { fn base2k(&self) -> Base2K { self.base2k }

@@ -104,16 +105,16 @@ self.data.size()

} } -impl<D: Data> GLWEInfos for GLWECiphertext<D> { +impl<D: Data> GLWEInfos for GLWE<D> { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } -impl<D: DataRef> ToOwnedDeep for GLWECiphertext<D> { - type Owned = GLWECiphertext<Vec<u8>>; +impl<D: DataRef> ToOwnedDeep for GLWE<D> { + type Owned = GLWE<Vec<u8>>; fn to_owned_deep(&self) -> Self::Owned { - GLWECiphertext { + GLWE { data: self.data.to_owned_deep(), k: self.k, base2k: self.base2k,

@@ -121,17 +122,17 @@ }

} } -impl<D: DataRef> fmt::Debug for GLWECiphertext<D> { +impl<D: DataRef> fmt::Debug for GLWE<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataRef> fmt::Display for GLWECiphertext<D> { +impl<D: DataRef> fmt::Display for GLWE<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "GLWECiphertext: base2k={} k={}: {}", + "GLWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data

@@ -139,41 +140,86 @@ )

} } -impl<D: DataMut> FillUniform for GLWECiphertext<D> { +impl<D: DataMut> FillUniform for GLWE<D> { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertext<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait GLWEAlloc +where + Self: GetDegree, +{ + fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE<Vec<u8>> { + GLWE { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), + base2k, + k, + } + } + + fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GLWE<Vec<u8>> where A: GLWEInfos, { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - } + fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_glwe_from_infos<A>(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) + } +} + +impl<B: Backend> GLWEAlloc for Module<B> where Self: GetDegree {} + +impl GLWE<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self + where + A: GLWEInfos, + Module<B>: GLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module<B>: GLWEAlloc, + { + module.alloc_glwe(base2k, k, rank) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize where A: GLWEInfos, + Module<B>: GLWEAlloc, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + module.bytes_of_glwe_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module<B>: GLWEAlloc, + { + module.bytes_of_glwe(base2k, k, rank) } } -impl<D: DataMut> ReaderFrom for GLWECiphertext<D> { +impl<D: DataMut> ReaderFrom for GLWE<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -181,7 +227,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for GLWECiphertext<D> { +impl<D: DataRef> WriterTo for GLWE<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.0)?; writer.write_u32::<LittleEndian>(self.base2k.0)?;

@@ -189,13 +235,13 @@ self.data.write_to(writer)

} } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; +pub trait GLWEToRef { + fn to_ref(&self) -> GLWE<&[u8]>; } -impl<D: DataRef> GLWECiphertextToRef for GLWECiphertext<D> { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl<D: DataRef> GLWEToRef for GLWE<D> { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(),

@@ -203,13 +249,13 @@ }

} } -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; +pub trait GLWEToMut { + fn to_mut(&mut self) -> GLWE<&mut [u8]>; } -impl<D: DataMut> GLWECiphertextToMut for GLWECiphertext<D> { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl<D: DataMut> GLWEToMut for GLWE<D> { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(),
M poulpy-core/src/layouts/glwe_pk.rs → poulpy-core/src/layouts/glwe_pk.rs

@@ -1,8 +1,10 @@

-use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos}; +use poulpy_hal::layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos, +}; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

@@ -80,32 +82,75 @@ self.rank

} } +pub trait GLWEPublicKeyAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey<Vec<u8>> { + GLWEPublicKey { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), + base2k, + k, + dist: Distribution::NONE, + } + } + + fn alloc_glwe_public_key_from_infos<A>(&self, infos: &A) -> GLWEPublicKey<Vec<u8>> + where + A: GLWEInfos, + { + self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + } + + fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + VecZnx::bytes_of( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ) + } + + fn bytes_of_glwe_public_key_from_infos<A>(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + } +} + impl GLWEPublicKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self where A: GLWEInfos, + Module<B>: GLWEPublicKeyAlloc, { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + module.alloc_glwe_public_key_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - dist: Distribution::NONE, - } + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module<B>: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key(base2k, k, rank) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize where A: GLWEInfos, + Module<B>: GLWEPublicKeyAlloc, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + module.bytes_of_glwe_public_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module<B>: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key(base2k, k, rank) } }
M poulpy-core/src/layouts/glwe_pt.rs → poulpy-core/src/layouts/glwe_pt.rs

@@ -1,10 +1,9 @@

use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos}; use crate::layouts::{ - Base2K, Degree, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, LWEInfos, Rank, - TorusPrecision, + Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)]

@@ -40,8 +39,8 @@ pub base2k: Base2K,

pub k: TorusPrecision, } -impl<D: DataMut> GLWELayoutSet for GLWEPlaintext<D> { - fn set_basek(&mut self, base2k: Base2K) { +impl<D: DataMut> SetGLWEInfos for GLWEPlaintext<D> { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k }

@@ -86,39 +85,72 @@ )

} } +pub trait GLWEPlaintextAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> { + GLWEPlaintext { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), + base2k, + k, + } + } + + fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>> + where + A: GLWEInfos, + { + self.alloc_glwe_plaintext(infos.base2k(), infos.k()) + } + + fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize { + VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize) + } + + fn bytes_of_glwe_plaintext_from_infos<A>(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) + } +} + impl GLWEPlaintext<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self where A: GLWEInfos, + Module<B>: GLWEPlaintextAlloc, { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) + module.alloc_glwe_plaintext_from_infos(infos) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - debug_assert!(rank.0 == 0); - Self { - data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), - base2k, - k, - } + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> Self + where + Module<B>: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext(base2k, k) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize where A: GLWEInfos, + Module<B>: GLWEPlaintextAlloc, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) + module.bytes_of_glwe_plaintext_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - debug_assert!(rank.0 == 0); - VecZnx::alloc_bytes(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> usize + where + Module<B>: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext(base2k, k) } } -impl<D: DataRef> GLWECiphertextToRef for GLWEPlaintext<D> { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl<D: DataRef> GLWEToRef for GLWEPlaintext<D> { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(),

@@ -126,9 +158,9 @@ }

} } -impl<D: DataMut> GLWECiphertextToMut for GLWEPlaintext<D> { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl<D: DataMut> GLWEToMut for GLWEPlaintext<D> { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(),
M poulpy-core/src/layouts/glwe_sk.rs → poulpy-core/src/layouts/glwe_sk.rs

@@ -1,11 +1,14 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero}, + layouts::{ + Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, + ZnxZero, + }, source::Source, }; use crate::{ dist::Distribution, - layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision}, + layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision}, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)]

@@ -67,30 +70,65 @@ Rank(self.data.cols() as u32)

} } +pub trait GLWESecretAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret<Vec<u8>> { + GLWESecret { + data: ScalarZnx::alloc(self.n().into(), rank.into()), + dist: Distribution::NONE, + } + } + + fn alloc_glwe_secret_from_infos<A>(&self, infos: &A) -> GLWESecret<Vec<u8>> + where + A: GLWEInfos, + { + self.alloc_glwe_secret(infos.rank()) + } + + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + ScalarZnx::bytes_of(self.n().into(), rank.into()) + } + + fn bytes_of_glwe_secret_from_infos<A>(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_secret(infos.rank()) + } +} + impl GLWESecret<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self where A: GLWEInfos, + Module<B>: GLWESecretAlloc, { - Self::alloc_with(infos.n(), infos.rank()) + module.alloc_glwe_secret_from_infos(infos) } - pub fn alloc_with(n: Degree, rank: Rank) -> Self { - Self { - data: ScalarZnx::alloc(n.into(), rank.into()), - dist: Distribution::NONE, - } + pub fn alloc<B: Backend>(module: Module<B>, rank: Rank) -> Self + where + Module<B>: GLWESecretAlloc, + { + module.alloc_glwe_secret(rank) } - pub fn alloc_bytes<A>(infos: &A) -> usize + pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize where A: GLWEInfos, + Module<B>: GLWESecretAlloc, { - Self::alloc_bytes_with(infos.n(), infos.rank()) + module.bytes_of_glwe_secret_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, rank: Rank) -> usize { - ScalarZnx::alloc_bytes(n.into(), rank.into()) + pub fn bytes_of<B: Backend>(module: Module<B>, rank: Rank) -> usize + where + Module<B>: GLWESecretAlloc, + { + module.bytes_of_glwe_secret(rank) } }
M poulpy-core/src/layouts/glwe_to_lwe_ksk.rs → poulpy-core/src/layouts/glwe_to_lwe_ksk.rs

@@ -1,11 +1,11 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; use std::fmt;

@@ -132,55 +132,88 @@ self.0.write_to(writer)

} } -impl GLWEToLWESwitchingKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait GLWEToLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_glwe_to_lwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKey<Vec<u8>> { + GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKey<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - Self(GLWESwitchingKey::alloc(infos)) + self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - rank_in, - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for GLWEToLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for GLWEToLWESwitchingKey" ); - GLWESwitchingKey::alloc_bytes(infos) + self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } +} + +impl GLWEToLWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module<B>: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + where + Module<B>: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } }
M poulpy-core/src/layouts/lwe_ct.rs → poulpy-core/src/layouts/lwe_ct.rs

@@ -1,7 +1,7 @@

use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos}, source::Source, };

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

} } #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertext<D: Data> { +pub struct LWE<D: Data> { pub(crate) data: Zn<D>, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, } -impl<D: Data> LWEInfos for LWECiphertext<D> { +impl<D: Data> LWEInfos for LWE<D> { fn base2k(&self) -> Base2K { self.base2k }

@@ -76,7 +76,7 @@ self.data.size()

} } -impl<D: Data> SetLWEInfos for LWECiphertext<D> { +impl<D: Data> SetLWEInfos for LWE<D> { fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k }

@@ -86,25 +86,25 @@ self.k = k

} } -impl<D: DataRef> LWECiphertext<D> { +impl<D: DataRef> LWE<D> { pub fn data(&self) -> &Zn<D> { &self.data } } -impl<D: DataMut> LWECiphertext<D> { +impl<D: DataMut> LWE<D> { pub fn data_mut(&mut self) -> &Zn<D> { &mut self.data } } -impl<D: DataRef> fmt::Debug for LWECiphertext<D> { +impl<D: DataRef> fmt::Debug for LWE<D> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl<D: DataRef> fmt::Display for LWECiphertext<D> { +impl<D: DataRef> fmt::Display for LWE<D> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f,

@@ -116,7 +116,7 @@ )

} } -impl<D: DataMut> FillUniform for LWECiphertext<D> +impl<D: DataMut> FillUniform for LWE<D> where Zn<D>: FillUniform, {

@@ -125,41 +125,73 @@ self.data.fill_uniform(log_bound, source);

} } -impl LWECiphertext<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWEAlloc { + fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE<Vec<u8>> { + LWE { + data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), + k, + base2k, + } + } + + fn alloc_lwe_from_infos<A>(&self, infos: &A) -> LWE<Vec<u8>> where A: LWEInfos, { - Self::alloc_with(infos.n(), infos.base2k(), infos.k()) + self.alloc_lwe(infos.n(), infos.base2k(), infos.k()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { - Self { - data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), - k, - base2k, - } + fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { + Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_lwe_from_infos<A>(&self, infos: &A) -> usize where A: LWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) } +} - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize) +impl LWE<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: LWEInfos, + Module<B>: LWEAlloc, + { + module.alloc_lwe_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self + where + Module<B>: LWEAlloc, + { + module.alloc_lwe(n, base2k, k) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: LWEInfos, + Module<B>: LWEAlloc, + { + module.bytes_of_lwe_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: &Module<B>, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize + where + Module<B>: LWEAlloc, + { + module.bytes_of_lwe(n, base2k, k) } } pub trait LWECiphertextToRef { - fn to_ref(&self) -> LWECiphertext<&[u8]>; + fn to_ref(&self) -> LWE<&[u8]>; } -impl<D: DataRef> LWECiphertextToRef for LWECiphertext<D> { - fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext { +impl<D: DataRef> LWECiphertextToRef for LWE<D> { + fn to_ref(&self) -> LWE<&[u8]> { + LWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(),

@@ -167,14 +199,14 @@ }

} } -pub trait LWECiphertextToMut { +pub trait LWEToMut { #[allow(dead_code)] - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]>; + fn to_mut(&mut self) -> LWE<&mut [u8]>; } -impl<D: DataMut> LWECiphertextToMut for LWECiphertext<D> { - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext { +impl<D: DataMut> LWEToMut for LWE<D> { + fn to_mut(&mut self) -> LWE<&mut [u8]> { + LWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(),

@@ -182,7 +214,7 @@ }

} } -impl<D: DataMut> ReaderFrom for LWECiphertext<D> { +impl<D: DataMut> ReaderFrom for LWE<D> { fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?); self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);

@@ -190,7 +222,7 @@ self.data.read_from(reader)

} } -impl<D: DataRef> WriterTo for LWECiphertext<D> { +impl<D: DataRef> WriterTo for LWE<D> { fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::<LittleEndian>(self.k.into())?; writer.write_u32::<LittleEndian>(self.base2k.into())?;
M poulpy-core/src/layouts/lwe_ksk.rs → poulpy-core/src/layouts/lwe_ksk.rs

@@ -1,13 +1,13 @@

use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)]

@@ -101,65 +101,94 @@ self.0.dnum()

} } -impl LWESwitchingKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey<Vec<u8>> { + LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_from_infos<A>(&self, infos: &A) -> LWESwitchingKey<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - Self(GLWESwitchingKey::alloc(infos)) + self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - Rank(1), - dnum, - Dsize(1), - )) + fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.rank_out().0, 1, "rank_out > 1 is not supported for LWESwitchingKey" ); - GLWESwitchingKey::alloc_bytes(infos) + self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) } +} - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) +impl<B: Backend> LWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} + +impl LWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: LWESwitchingKeyAlloc, + { + module.alloc_lwe_switching_key_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module<B>: LWESwitchingKeyAlloc, + { + module.alloc_lwe_switching_key(base2k, k, dnum) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: LWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key_from_infos(infos) + } + + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module<B>: LWESwitchingKeyAlloc, + { + module.bytes_of_lwe_switching_key(base2k, k, dnum) } }
M poulpy-core/src/layouts/lwe_pt.rs → poulpy-core/src/layouts/lwe_pt.rs

@@ -1,6 +1,6 @@

use std::fmt; -use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos}; +use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos}; use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision};

@@ -52,20 +52,35 @@ self.data.size()

} } +pub trait LWEPlaintextAlloc { + fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext<Vec<u8>> { + LWEPlaintext { + data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), + k, + base2k, + } + } + + fn alloc_lwe_plaintext_from_infos<A>(&self, infos: &A) -> LWEPlaintext<Vec<u8>> + where + A: LWEInfos, + { + self.alloc_lwe_plaintext(infos.base2k(), infos.k()) + } +} + +impl<B: Backend> LWEPlaintextAlloc for Module<B> {} + impl LWEPlaintext<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self + pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self where A: LWEInfos, { - Self::alloc_with(infos.base2k(), infos.k()) + module.alloc_lwe_plaintext_from_infos(infos) } - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { - data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), - k, - base2k, - } + pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> Self { + module.alloc_lwe_plaintext(base2k, k) } }
M poulpy-core/src/layouts/lwe_sk.rs → poulpy-core/src/layouts/lwe_sk.rs

@@ -1,5 +1,5 @@

use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, };

@@ -13,12 +13,23 @@ pub(crate) data: ScalarZnx<D>,

pub(crate) dist: Distribution, } -impl LWESecret<Vec<u8>> { - pub fn alloc(n: Degree) -> Self { - Self { +pub trait LWESecretAlloc { + fn alloc_lwe_secret(&self, n: Degree) -> LWESecret<Vec<u8>> { + LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, } + } +} + +impl<B: Backend> LWESecretAlloc for Module<B> {} + +impl LWESecret<Vec<u8>> { + pub fn alloc<B: Backend>(module: &Module<B>, n: Degree) -> Self + where + Module<B>: LWESecretAlloc, + { + module.alloc_lwe_secret(n) } }
M poulpy-core/src/layouts/lwe_to_glwe_ksk.rs → poulpy-core/src/layouts/lwe_to_glwe_ksk.rs

@@ -1,13 +1,13 @@

use std::fmt; use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo}, + layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo}, source::Source, }; use crate::layouts::{ - Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos, - Rank, TorusPrecision, + Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut, + GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision, }; #[derive(PartialEq, Eq, Copy, Clone, Debug)]

@@ -131,55 +131,91 @@ self.0.write_to(writer)

} } -impl LWEToGLWESwitchingKey<Vec<u8>> { - pub fn alloc<A>(infos: &A) -> Self +pub trait LWEToGLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_to_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKey<Vec<u8>> { + LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKey<Vec<u8>> where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - Self(GLWESwitchingKey::alloc(infos)) + + self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self { - Self(GLWESwitchingKey::alloc_with( - n, - base2k, - k, - Rank(1), - rank_out, - dnum, - Dsize(1), - )) + fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - pub fn alloc_bytes<A>(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_assert_eq!( + assert_eq!( infos.rank_in().0, 1, "rank_in > 1 is not supported for LWEToGLWESwitchingKey" ); - debug_assert_eq!( + assert_eq!( infos.dsize().0, 1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - GLWESwitchingKey::alloc_bytes(infos) + self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } +} + +impl<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {} + +impl LWEToGLWESwitchingKey<Vec<u8>> { + pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self + where + A: GGLWEInfos, + Module<B>: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key_from_infos(infos) + } + + pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module<B>: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize + where + A: GGLWEInfos, + Module<B>: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos) } - pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize { - GLWESwitchingKey::alloc_bytes_with(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + where + Module<B>: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } }
M poulpy-core/src/layouts/prepared/gglwe_atk.rs → poulpy-core/src/layouts/prepared/gglwe_atk.rs

@@ -80,7 +80,7 @@ pub trait AutomorphismKeyPreparedAlloc<B: Backend>

where Self: GLWESwitchingKeyPreparedAlloc<B>, { - fn automorphism_key_prepared_alloc( + fn alloc_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -89,12 +89,12 @@ dnum: Dnum,

dsize: Dsize, ) -> AutomorphismKeyPrepared<Vec<u8>, B> { AutomorphismKeyPrepared::<Vec<u8>, B> { - key: self.glwe_switching_key_prepared_alloc(base2k, k, rank, rank, dnum, dsize), + key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize), p: 0, } } - fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B> + fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B> where A: GGLWEInfos, {

@@ -103,7 +103,7 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for AutomorphismKeyPrepared" ); - self.automorphism_key_prepared_alloc( + self.alloc_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(),

@@ -112,7 +112,7 @@ infos.dsize(),

) } - fn automorphism_key_prepared_alloc_bytes( + fn bytes_of_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -120,10 +120,10 @@ rank: Rank,

dnum: Dnum, dsize: Dsize, ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize) + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize) } - fn automorphism_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -132,7 +132,7 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared" ); - self.automorphism_key_prepared_alloc_bytes( + self.bytes_of_automorphism_key_prepared( infos.base2k(), infos.k(), infos.rank(),

@@ -150,29 +150,29 @@ where

A: GGLWEInfos, Module<B>: AutomorphismKeyPreparedAlloc<B>, { - module.automorphism_key_prepared_alloc_from_infos(infos) + module.alloc_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where Module<B>: AutomorphismKeyPreparedAlloc<B>, { - module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize) + module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, Module<B>: AutomorphismKeyPreparedAlloc<B>, { - module.automorphism_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module<B>: AutomorphismKeyPreparedAlloc<B>, { - module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) + module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } }

@@ -180,19 +180,19 @@ pub trait AutomorphismKeyPrepare<B: Backend>

where Self: GLWESwitchingKeyPrepare<B>, { - fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize + fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos) + self.prepare_glwe_switching_key_tmp_bytes(infos) } - fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) + fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement, O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, { - self.glwe_switching_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_p(other.p()); } }

@@ -204,7 +204,7 @@ pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize

where Module<B>: GLWESwitchingKeyPrepare<B>, { - module.automorphism_key_prepare_tmp_bytes(self) + module.prepare_automorphism_key_tmp_bytes(self) } }

@@ -214,7 +214,7 @@ where

O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, Module<B>: AutomorphismKeyPrepare<B>, { - module.automorphism_key_prepare(self, other, scratch); + module.prepare_automorphism_key(self, other, scratch); } }
M poulpy-core/src/layouts/prepared/gglwe_ct.rs → poulpy-core/src/layouts/prepared/gglwe_ct.rs

@@ -61,7 +61,7 @@ pub trait GGLWEPreparedAlloc<B: Backend>

where Self: GetDegree + VmpPMatAlloc<B> + VmpPMatAllocBytes, { - fn gglwe_prepared_alloc( + fn alloc_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -92,12 +92,12 @@ dsize,

} } - fn gglwe_prepared_alloc_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B> + fn alloc_gglwe_prepared_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B> where A: GGLWEInfos, { assert_eq!(self.n(), infos.n()); - self.gglwe_prepared_alloc( + self.alloc_gglwe_prepared( infos.base2k(), infos.k(), infos.rank_in(),

@@ -107,7 +107,7 @@ infos.dsize(),

) } - fn gglwe_prepared_alloc_bytes( + fn bytes_of_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -130,15 +130,15 @@ dnum.0,

dsize.0, ); - self.vmp_pmat_alloc_bytes(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) + self.vmp_pmat_bytes_of(dnum.into(), rank_in.into(), (rank_out + 1).into(), size) } - fn gglwe_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_gglwe_prepared_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { assert_eq!(self.n(), infos.n()); - self.gglwe_prepared_alloc_bytes( + self.bytes_of_gglwe_prepared( infos.base2k(), infos.k(), infos.rank_in(),

@@ -159,7 +159,7 @@ pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self

where A: GGLWEInfos, { - module.gglwe_prepared_alloc_from_infos(infos) + module.alloc_gglwe_prepared_from_infos(infos) } pub fn alloc(

@@ -171,17 +171,17 @@ rank_out: Rank,

dnum: Dnum, dsize: Dsize, ) -> Self { - module.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) + module.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.gglwe_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_gglwe_prepared_from_infos(infos) } - pub fn alloc_bytes( + pub fn bytes_of( module: &Module<B>, base2k: Base2K, k: TorusPrecision,

@@ -190,7 +190,7 @@ rank_out: Rank,

dnum: Dnum, dsize: Dsize, ) -> usize { - module.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + module.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } }

@@ -198,7 +198,7 @@ pub trait GGLWEPrepare<B: Backend>

where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B>, { - fn gglwe_prepare_tmp_bytes<A>(&self, infos: &A) -> usize + fn prepare_gglwe_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -210,7 +210,7 @@ infos.size(),

) } - fn gglwe_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) + fn prepare_gglwe<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where R: GGLWEPreparedToMut<B>, O: GGLWEToRef,

@@ -238,7 +238,7 @@ pub fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)

where O: GGLWEToRef, { - module.gglwe_prepare(self, other, scratch); + module.prepare_gglwe(self, other, scratch); } }

@@ -247,7 +247,7 @@ pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize

where Module<B>: GGLWEPrepare<B>, { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } }
M poulpy-core/src/layouts/prepared/gglwe_ksk.rs → poulpy-core/src/layouts/prepared/gglwe_ksk.rs

@@ -79,7 +79,7 @@ pub trait GLWESwitchingKeyPreparedAlloc<B: Backend>

where Self: GGLWEPreparedAlloc<B>, { - fn glwe_switching_key_prepared_alloc( + fn alloc_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -89,7 +89,7 @@ dnum: Dnum,

dsize: Dsize, ) -> GLWESwitchingKeyPrepared<Vec<u8>, B> { GLWESwitchingKeyPrepared::<Vec<u8>, B> { - key: self.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, }

@@ -99,7 +99,7 @@ fn glwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyPrepared<Vec<u8>, B>

where A: GGLWEInfos, { - self.glwe_switching_key_prepared_alloc( + self.alloc_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(),

@@ -109,7 +109,7 @@ infos.dsize(),

) } - fn glwe_switching_key_prepared_alloc_bytes( + fn bytes_of_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -118,14 +118,14 @@ rank_out: Rank,

dnum: Dnum, dsize: Dsize, ) -> usize { - self.gglwe_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + self.bytes_of_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - fn glwe_switching_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn glwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepared_alloc_bytes( + self.bytes_of_glwe_switching_key_prepared( infos.base2k(), infos.k(), infos.rank_in(),

@@ -158,17 +158,17 @@ rank_out: Rank,

dnum: Dnum, dsize: Dsize, ) -> Self { - module.glwe_switching_key_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize) + module.alloc_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.glwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes( + pub fn bytes_of( module: &Module<B>, base2k: Base2K, k: TorusPrecision,

@@ -177,7 +177,7 @@ rank_out: Rank,

dnum: Dnum, dsize: Dsize, ) -> usize { - module.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, rank_out, dnum, dsize) + module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize) } }

@@ -185,19 +185,19 @@ pub trait GLWESwitchingKeyPrepare<B: Backend>

where Self: GGLWEPrepare<B>, { - fn glwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize + fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.gglwe_prepare_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } - fn glwe_switching_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) + fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData, O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, { - self.gglwe_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch); + self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch); res.set_sk_in_n(other.sk_in_n()); res.set_sk_out_n(other.sk_out_n()); }

@@ -211,7 +211,7 @@ where

O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, Module<B>: GLWESwitchingKeyPrepare<B>, { - module.glwe_switching_prepare(self, other, scratch); + module.prepare_glwe_switching(self, other, scratch); } }

@@ -220,7 +220,7 @@ where

Module<B>: GLWESwitchingKeyPrepare<B>, { pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } }
M poulpy-core/src/layouts/prepared/gglwe_tsk.rs → poulpy-core/src/layouts/prepared/gglwe_tsk.rs

@@ -59,7 +59,7 @@ pub trait TensorKeyPreparedAlloc<B: Backend>

where Self: GLWESwitchingKeyPreparedAlloc<B>, { - fn tensor_key_prepared_alloc( + fn alloc_tensor_key_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -70,12 +70,12 @@ ) -> TensorKeyPrepared<Vec<u8>, B> {

let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1); TensorKeyPrepared { keys: (0..pairs) - .map(|_| self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank, dnum, dsize)) + .map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)) .collect(), } } - fn tensor_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B> + fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B> where A: GGLWEInfos, {

@@ -84,7 +84,7 @@ infos.rank_in(),

infos.rank_out(), "rank_in != rank_out is not supported for GGLWETensorKeyPrepared" ); - self.tensor_key_prepared_alloc( + self.alloc_tensor_key_prepared( infos.base2k(), infos.k(), infos.dnum(),

@@ -93,16 +93,16 @@ infos.rank_out(),

) } - fn tensor_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + fn bytes_of_tensor_key_prepared(&self, 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 * self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank, dnum, dsize) + pairs * self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize) } - fn tensor_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.tensor_key_prepared_alloc_bytes( + self.bytes_of_tensor_key_prepared( infos.base2k(), infos.k(), infos.rank(),

@@ -122,29 +122,22 @@ pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self

where A: GGLWEInfos, { - module.tensor_key_prepared_alloc_from_infos(infos) + module.alloc_tensor_key_prepared_from_infos(infos) } pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { - module.tensor_key_prepared_alloc(base2k, k, dnum, dsize, rank) + module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank) } - pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.tensor_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_tensor_key_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module<B>, - base2k: Base2K, - k: TorusPrecision, - rank: Rank, - dnum: Dnum, - dsize: Dsize, - ) -> usize { - module.tensor_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize { + module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize) } }

@@ -174,14 +167,14 @@ pub trait TensorKeyPrepare<B: Backend>

where Self: GLWESwitchingKeyPrepare<B>, { - fn tensor_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize + fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos) + self.prepare_glwe_switching_key_tmp_bytes(infos) } - fn tensor_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) + fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where R: TensorKeyPreparedToMut<B>, O: TensorKeyToRef,

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

assert_eq!(res.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.prepare_glwe_switching(a, b, scratch); } } }

@@ -207,7 +200,7 @@ fn prepare_tmp_bytes<A>(&self, module: &Module<B>, infos: &A) -> usize

where A: GGLWEInfos, { - module.tensor_key_prepare_tmp_bytes(infos) + module.prepare_tensor_key_tmp_bytes(infos) } }

@@ -219,7 +212,7 @@ fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)

where O: TensorKeyToRef, { - module.tensor_key_prepare(self, other, scratch); + module.prepare_tensor_key(self, other, scratch); } }
M poulpy-core/src/layouts/prepared/ggsw_ct.rs → poulpy-core/src/layouts/prepared/ggsw_ct.rs

@@ -53,7 +53,7 @@ pub trait GGSWPreparedAlloc<B: Backend>

where Self: GetDegree + VmpPMatAlloc<B> + VmpPMatAllocBytes, { - fn ggsw_prepared_alloc( + fn alloc_ggsw_prepared( &self, base2k: Base2K, k: TorusPrecision,

@@ -88,12 +88,12 @@ dsize,

} } - fn ggsw_prepared_alloc_from_infos<A>(&self, infos: &A) -> GGSWPrepared<Vec<u8>, B> + fn alloc_ggsw_prepared_from_infos<A>(&self, infos: &A) -> GGSWPrepared<Vec<u8>, B> where A: GGSWInfos, { assert_eq!(self.n(), infos.n()); - self.ggsw_prepared_alloc( + self.alloc_ggsw_prepared( infos.base2k(), infos.k(), infos.dnum(),

@@ -102,7 +102,7 @@ infos.rank(),

) } - fn ggsw_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + fn bytes_of_ggsw_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0,

@@ -117,15 +117,15 @@ dnum.0,

dsize.0, ); - self.vmp_pmat_alloc_bytes(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) + self.vmp_pmat_bytes_of(dnum.into(), (rank + 1).into(), (rank + 1).into(), size) } - fn ggsw_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_ggsw_prepared_from_infos<A>(&self, infos: &A) -> usize where A: GGSWInfos, { assert_eq!(self.n(), infos.n()); - self.ggsw_prepared_alloc_bytes( + self.bytes_of_ggsw_prepared( infos.base2k(), infos.k(), infos.dnum(),

@@ -145,29 +145,22 @@ pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self

where A: GGSWInfos, { - module.ggsw_prepared_alloc_from_infos(infos) + module.alloc_ggsw_prepared_from_infos(infos) } pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self { - module.ggsw_prepared_alloc(base2k, k, dnum, dsize, rank) + module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank) } - pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGSWInfos, { - module.ggsw_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_ggsw_prepared_from_infos(infos) } - pub fn alloc_bytes_with( - module: &Module<B>, - base2k: Base2K, - k: TorusPrecision, - dnum: Dnum, - dsize: Dsize, - rank: Rank, - ) -> usize { - module.ggsw_prepared_alloc_bytes(base2k, k, dnum, dsize, rank) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) } }
M poulpy-core/src/layouts/prepared/glwe_pk.rs → poulpy-core/src/layouts/prepared/glwe_pk.rs

@@ -54,7 +54,7 @@ pub trait GLWEPublicKeyPreparedAlloc<B: Backend>

where Self: GetDegree + VecZnxDftAlloc<B> + VecZnxDftAllocBytes, { - fn glwe_public_key_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared<Vec<u8>, B> { + fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared<Vec<u8>, B> { GLWEPublicKeyPrepared { data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k,

@@ -63,22 +63,22 @@ dist: Distribution::NONE,

} } - fn glwe_public_key_alloc_from_infos<A>(&self, infos: &A) -> GLWEPublicKeyPrepared<Vec<u8>, B> + fn alloc_glwe_public_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEPublicKeyPrepared<Vec<u8>, B> where A: GLWEInfos, { - self.glwe_public_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } - fn glwe_public_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - self.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) + fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + self.vec_znx_dft_bytes_of((rank + 1).into(), k.0.div_ceil(base2k.0) as usize) } - fn glwe_public_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_glwe_public_key_prepared_from_infos<A>(&self, infos: &A) -> usize where A: GLWEInfos, { - self.glwe_public_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank()) + self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank()) } }

@@ -92,22 +92,22 @@ pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self

where A: GLWEInfos, { - module.glwe_public_key_alloc_from_infos(infos) + module.alloc_glwe_public_key_prepared_from_infos(infos) } pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { - module.glwe_public_key_prepared_alloc(base2k, k, rank) + module.alloc_glwe_public_key_prepared(base2k, k, rank) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GLWEInfos, { - module.glwe_public_key_prepared_alloc_bytes_from_infos(infos) + module.bytes_of_glwe_public_key_prepared_from_infos(infos) } - pub fn alloc_bytes(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } }

@@ -115,7 +115,7 @@ pub trait GLWEPublicKeyPrepare<B: Backend>

where Self: GetDegree + VecZnxDftApply<B>, { - fn glwe_public_key_prepare<R, O>(&self, res: &mut R, other: &O) + fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut<B> + SetDist, O: GLWEPublicKeyToRef + GetDist,

@@ -149,7 +149,7 @@ pub fn prepare<O>(&mut self, module: &Module<B>, other: &O)

where O: GLWEPublicKeyToRef + GetDist, { - module.glwe_public_key_prepare(self, other); + module.prepare_glwe_public_key(self, other); } }
M poulpy-core/src/layouts/prepared/glwe_sk.rs → poulpy-core/src/layouts/prepared/glwe_sk.rs

@@ -49,29 +49,29 @@ pub trait GLWESecretPreparedAlloc<B: Backend>

where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc<B>, { - fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared<Vec<u8>, B> { + fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared<Vec<u8>, B> { GLWESecretPrepared { data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - fn glwe_secret_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWESecretPrepared<Vec<u8>, B> + fn alloc_glwe_secret_prepared_from_infos<A>(&self, infos: &A) -> GLWESecretPrepared<Vec<u8>, B> where A: GLWEInfos, { assert_eq!(self.n(), infos.n()); - self.glwe_secret_prepared_alloc(infos.rank()) + self.alloc_glwe_secret_prepared(infos.rank()) } - fn glwe_secret_alloc_bytes(&self, rank: Rank) -> usize { - self.svp_ppol_alloc_bytes(rank.into()) + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + self.svp_ppol_bytes_of(rank.into()) } - fn glwe_secret_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn bytes_of_glwe_secret_from_infos<A>(&self, infos: &A) -> usize where A: GLWEInfos, { assert_eq!(self.n(), infos.n()); - self.glwe_secret_alloc_bytes(infos.rank()) + self.bytes_of_glwe_secret(infos.rank()) } }

@@ -85,22 +85,22 @@ pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self

where A: GLWEInfos, { - module.glwe_secret_prepared_alloc_from_infos(infos) + module.alloc_glwe_secret_prepared_from_infos(infos) } pub fn alloc(module: &Module<B>, rank: Rank) -> Self { - module.glwe_secret_prepared_alloc(rank) + module.alloc_glwe_secret_prepared(rank) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GLWEInfos, { - module.glwe_secret_alloc_bytes_from_infos(infos) + module.bytes_of_glwe_secret_from_infos(infos) } - pub fn alloc_bytes(module: &Module<B>, rank: Rank) -> usize { - module.glwe_secret_alloc_bytes(rank) + pub fn bytes_of(module: &Module<B>, rank: Rank) -> usize { + module.bytes_of_glwe_secret(rank) } }

@@ -118,7 +118,7 @@ pub trait GLWESecretPrepare<B: Backend>

where Self: SvpPrepare<B>, { - fn glwe_secret_prepare<R, O>(&self, res: &mut R, other: &O) + fn prepare_glwe_secret<R, O>(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut<B> + SetDist, O: GLWESecretToRef + GetDist,
M poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs → poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs

@@ -64,7 +64,7 @@ k: TorusPrecision,

rank_in: Rank, dnum: Dnum, ) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> { - GLWEToLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) } fn glwe_to_lwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> where

@@ -83,17 +83,11 @@ );

self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } - fn glwe_to_lwe_switching_key_prepared_alloc_bytes( - &self, - base2k: Base2K, - k: TorusPrecision, - rank_in: Rank, - dnum: Dnum, - ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) + fn glwe_to_lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)) } - fn glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -107,7 +101,7 @@ infos.dsize().0,

1, "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared" ); - self.glwe_to_lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + self.glwe_to_lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) } }

@@ -128,15 +122,15 @@ pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {

module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { - module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize { + module.glwe_to_lwe_switching_key_prepared_bytes_of(base2k, k, rank_in, dnum) } }

@@ -148,7 +142,7 @@ fn glwe_to_lwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A)

where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn glwe_to_lwe_switching_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)

@@ -156,7 +150,7 @@ where

R: GLWEToLWESwitchingKeyPreparedToMut<B>, O: GLWEToLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } }
M poulpy-core/src/layouts/prepared/lwe_ksk.rs → poulpy-core/src/layouts/prepared/lwe_ksk.rs

@@ -56,13 +56,13 @@ pub trait LWESwitchingKeyPreparedAlloc<B: Backend>

where Self: GLWESwitchingKeyPreparedAlloc<B>, { - fn lwe_switching_key_prepared_alloc( + fn alloc_lwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, ) -> LWESwitchingKeyPrepared<Vec<u8>, B> { - LWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) } fn lwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyPrepared<Vec<u8>, B>

@@ -84,14 +84,14 @@ infos.rank_out().0,

1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.dnum()) + self.alloc_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum()) } - fn lwe_switching_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) + fn lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)) } - fn lwe_switching_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn lwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -110,7 +110,7 @@ infos.rank_out().0,

1, "rank_out > 1 is not supported for LWESwitchingKey" ); - self.lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.dnum()) + self.lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.dnum()) } }

@@ -128,18 +128,18 @@ module.lwe_switching_key_prepared_alloc_from_infos(infos)

} pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self { - module.lwe_switching_key_prepared_alloc(base2k, k, dnum) + module.alloc_lwe_switching_key_prepared(base2k, k, dnum) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.lwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.lwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + module.lwe_switching_key_prepared_bytes_of(base2k, k, dnum) } }

@@ -151,14 +151,14 @@ fn lwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A)

where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn lwe_switching_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>) where R: LWESwitchingKeyPreparedToMut<B>, O: LWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } }
M poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs → poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs

@@ -65,7 +65,7 @@ k: TorusPrecision,

rank_out: Rank, dnum: Dnum, ) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> { - LWEToGLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) } fn lwe_to_glwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> where

@@ -84,17 +84,17 @@ );

self.lwe_to_glwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } - fn lwe_to_glwe_switching_key_prepared_alloc_bytes( + fn lwe_to_glwe_switching_key_prepared_bytes_of( &self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum, ) -> usize { - self.glwe_switching_key_prepared_alloc_bytes(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) + self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)) } - fn lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize + fn lwe_to_glwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize where A: GGLWEInfos, {

@@ -108,7 +108,7 @@ infos.dsize().0,

1, "dsize > 1 is not supported for LWEToGLWESwitchingKey" ); - self.lwe_to_glwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + self.lwe_to_glwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) } }

@@ -129,15 +129,15 @@ pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {

module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) } - pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize + pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize where A: GGLWEInfos, { - module.lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(infos) + module.lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(infos) } - pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { - module.lwe_to_glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_out, dnum) + pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize { + module.lwe_to_glwe_switching_key_prepared_bytes_of(base2k, k, rank_out, dnum) } }

@@ -149,7 +149,7 @@ fn lwe_to_glwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A)

where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn lwe_to_glwe_switching_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)

@@ -157,7 +157,7 @@ where

R: LWEToGLWESwitchingKeyPreparedToMut<B>, O: LWEToGLWESwitchingKeyToRef, { - self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch); + self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch); } }
M poulpy-core/src/noise/gglwe_ct.rs → poulpy-core/src/noise/gglwe_ct.rs

@@ -8,7 +8,7 @@ layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, ZnxZero},

oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGLWE, GGLWEInfos, GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGLWE, GGLWEInfos, GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl<D: DataRef> GGLWE<D> { pub fn assert_noise<B, DataSk, DataWant>(

@@ -35,8 +35,8 @@ {

let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_i| {
M poulpy-core/src/noise/ggsw_ct.rs → poulpy-core/src/noise/ggsw_ct.rs

@@ -9,7 +9,7 @@ layouts::{Backend, DataRef, Module, ScalarZnx, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},

oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; impl<D: DataRef> GGSW<D> { pub fn assert_noise<B, DataSk, DataScalar, F>(

@@ -42,13 +42,13 @@ {

let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned<B> = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| {

@@ -114,13 +114,13 @@ {

let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned<B> = - ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); + ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes()); (0..(self.rank() + 1).into()).for_each(|col_j| { (0..self.dnum().into()).for_each(|row_i| {
M poulpy-core/src/noise/glwe_ct.rs → poulpy-core/src/noise/glwe_ct.rs

@@ -8,9 +8,9 @@ layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},

oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl<D: DataRef> GLWECiphertext<D> { +impl<D: DataRef> GLWE<D> { pub fn noise<B, DataSk, DataPt>( &self, module: &Module<B>,

@@ -32,7 +32,7 @@ + VecZnxBigAddSmallInplace<B>

+ VecZnxBigNormalize<B>, Scratch<B>: TakeVecZnxDft<B> + TakeVecZnxBig<B>, { - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self); self.decrypt(module, &mut pt_have, sk_prepared, scratch); module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch);

@@ -61,7 +61,7 @@ + VecZnxSubInplace

+ VecZnxNormalizeInplace<B>, B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>, { - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow()); assert!(noise_have <= max_noise, "{noise_have} {max_noise}"); }
M poulpy-core/src/operations/glwe.rs → poulpy-core/src/operations/glwe.rs

@@ -7,24 +7,22 @@ },

layouts::{Backend, DataMut, Module, Scratch, VecZnx, ZnxZero}, }; -use crate::layouts::{ - GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEInfos, GLWELayoutSet, GLWEPlaintext, LWEInfos, TorusPrecision, -}; +use crate::layouts::{GLWE, GLWEInfos, GLWEPlaintext, GLWEToMut, GLWEToRef, LWEInfos, SetGLWEInfos, TorusPrecision}; impl<D> GLWEOperations for GLWEPlaintext<D> where D: DataMut, - GLWEPlaintext<D>: GLWECiphertextToMut + GLWEInfos, + GLWEPlaintext<D>: GLWEToMut + GLWEInfos, { } -impl<D: DataMut> GLWEOperations for GLWECiphertext<D> where GLWECiphertext<D>: GLWECiphertextToMut + GLWEInfos {} +impl<D: DataMut> GLWEOperations for GLWE<D> where GLWE<D>: GLWEToMut + GLWEInfos {} -pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Sized { +pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { fn add<A, B, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module<BACKEND>: VecZnxAdd + VecZnxCopy, { #[cfg(debug_assertions)]

@@ -39,9 +37,9 @@ let min_col: usize = (a.rank().min(b.rank()) + 1).into();

let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_add(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i);

@@ -64,13 +62,13 @@ self_mut.data.zero_at(i, j);

}); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn add_inplace<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<BACKEND>: VecZnxAddInplace, { #[cfg(debug_assertions)]

@@ -80,8 +78,8 @@ assert_eq!(self.base2k(), a.base2k());

assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_add_inplace(&mut self_mut.data, i, &a_ref.data, i);

@@ -92,8 +90,8 @@ }

fn sub<A, B, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module<BACKEND>: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, { #[cfg(debug_assertions)]

@@ -108,9 +106,9 @@ let min_col: usize = (a.rank().min(b.rank()) + 1).into();

let max_col: usize = (a.rank().max(b.rank() + 1)).into(); let self_col: usize = (self.rank() + 1).into(); - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); - let b_ref: &GLWECiphertext<&[u8]> = &b.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); + let b_ref: &GLWE<&[u8]> = &b.to_ref(); (0..min_col).for_each(|i| { module.vec_znx_sub(&mut self_mut.data, i, &a_ref.data, i, &b_ref.data, i);

@@ -134,13 +132,13 @@ self_mut.data.zero_at(i, j);

}); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn sub_inplace_ab<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<BACKEND>: VecZnxSubInplace, { #[cfg(debug_assertions)]

@@ -150,8 +148,8 @@ assert_eq!(self.base2k(), a.base2k());

assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_inplace(&mut self_mut.data, i, &a_ref.data, i);

@@ -162,7 +160,7 @@ }

fn sub_inplace_ba<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<BACKEND>: VecZnxSubNegateInplace, { #[cfg(debug_assertions)]

@@ -172,8 +170,8 @@ assert_eq!(self.base2k(), a.base2k());

assert!(self.rank() >= a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_sub_negate_inplace(&mut self_mut.data, i, &a_ref.data, i);

@@ -184,7 +182,7 @@ }

fn rotate<A, B: Backend>(&mut self, module: &Module<B>, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<B>: VecZnxRotate, { #[cfg(debug_assertions)]

@@ -193,14 +191,14 @@ assert_eq!(a.n(), self.n());

assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) }

@@ -208,7 +206,7 @@ fn rotate_inplace<B: Backend>(&mut self, module: &Module<B>, k: i64, scratch: &mut Scratch<B>)

where Module<B>: VecZnxRotateInplace<B>, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_rotate_inplace(k, &mut self_mut.data, i, scratch);

@@ -217,7 +215,7 @@ }

fn mul_xp_minus_one<A, B: Backend>(&mut self, module: &Module<B>, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<B>: VecZnxMulXpMinusOne, { #[cfg(debug_assertions)]

@@ -226,14 +224,14 @@ assert_eq!(a.n(), self.n());

assert_eq!(self.rank(), a.rank()) } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(a.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one(k, &mut self_mut.data, i, &a_ref.data, i); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_unary(self, a)) }

@@ -241,7 +239,7 @@ fn mul_xp_minus_one_inplace<B: Backend>(&mut self, module: &Module<B>, k: i64, scratch: &mut Scratch<B>)

where Module<B>: VecZnxMulXpMinusOneInplace<B>, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_mul_xp_minus_one_inplace(k, &mut self_mut.data, i, scratch);

@@ -250,7 +248,7 @@ }

fn copy<A, B: Backend>(&mut self, module: &Module<B>, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<B>: VecZnxCopy, { #[cfg(debug_assertions)]

@@ -259,15 +257,15 @@ assert_eq!(self.n(), a.n());

assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_copy(&mut self_mut.data, i, &a_ref.data, i); }); self.set_k(a.k().min(self.max_k())); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); } fn rsh<B: Backend>(&mut self, module: &Module<B>, k: usize, scratch: &mut Scratch<B>)

@@ -282,7 +280,7 @@ }

fn normalize<A, B: Backend>(&mut self, module: &Module<B>, a: &A, scratch: &mut Scratch<B>) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module<B>: VecZnxNormalize<B>, { #[cfg(debug_assertions)]

@@ -291,8 +289,8 @@ assert_eq!(self.n(), a.n());

assert_eq!(self.rank(), a.rank()); } - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); - let a_ref: &GLWECiphertext<&[u8]> = &a.to_ref(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); + let a_ref: &GLWE<&[u8]> = &a.to_ref(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize(

@@ -305,7 +303,7 @@ i,

scratch, ); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(a.k().min(self.k())); }

@@ -313,14 +311,14 @@ fn normalize_inplace<B: Backend>(&mut self, module: &Module<B>, scratch: &mut Scratch<B>)

where Module<B>: VecZnxNormalizeInplace<B>, { - let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut(); + let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut(); (0..(self_mut.rank() + 1).into()).for_each(|i| { module.vec_znx_normalize_inplace(self_mut.base2k().into(), &mut self_mut.data, i, scratch); }); } } -impl GLWECiphertext<Vec<u8>> { +impl GLWE<Vec<u8>> { pub fn rsh_scratch_space(n: usize) -> usize { VecZnx::rsh_scratch_space(n) }
M poulpy-core/src/scratch.rs → poulpy-core/src/scratch.rs

@@ -6,8 +6,8 @@

use crate::{ dist::Distribution, layouts::{ - AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, - GLWESecret, GLWESwitchingKey, Rank, TensorKey, + AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, + GLWESwitchingKey, Rank, TensorKey, prepared::{ AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared, GLWESwitchingKeyPrepared, TensorKeyPrepared,

@@ -16,13 +16,13 @@ },

}; pub trait TakeGLWECt { - fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos; } pub trait TakeGLWECtSlice { - fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWECiphertext<&mut [u8]>>, &mut Self) + fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self) where A: GLWEInfos; }

@@ -123,13 +123,13 @@ impl<B: Backend> TakeGLWECt for Scratch<B>

where Scratch<B>: TakeVecZnx, { - fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos, { let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size()); ( - GLWECiphertext { + GLWE { k: infos.k(), base2k: infos.base2k(), data,

@@ -143,12 +143,12 @@ impl<B: Backend> TakeGLWECtSlice for Scratch<B>

where Scratch<B>: TakeVecZnx, { - fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWECiphertext<&mut [u8]>>, &mut Self) + fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self) where A: GLWEInfos, { let mut scratch: &mut Scratch<B> = self; - let mut cts: Vec<GLWECiphertext<&mut [u8]>> = Vec::with_capacity(size); + let mut cts: Vec<GLWE<&mut [u8]>> = Vec::with_capacity(size); for _ in 0..size { let (ct, new_scratch) = scratch.take_glwe_ct(infos); scratch = new_scratch;

@@ -463,7 +463,7 @@ let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;

let mut scratch: &mut Scratch<B> = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 {

@@ -499,7 +499,7 @@ let pairs: usize = (((infos.rank_out().0 + 1) * infos.rank_out().0) >> 1).max(1) as usize;

let mut scratch: &mut Scratch<B> = self; - let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout(); + let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout(); ksk_infos.rank_in = Rank(1); if pairs != 0 {
M poulpy-core/src/tests/serialization.rs → poulpy-core/src/tests/serialization.rs

@@ -1,12 +1,12 @@

use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey, - LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, + AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE, + LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision, compressed::{ - AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed, - GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed, - LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed, + AutomorphismKeyCompressed, GGLWECompressed, GGSWCompressed, GLWECompressed, GLWESwitchingKeyCompressed, + GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed, + TensorKeyCompressed, }, };

@@ -20,51 +20,50 @@ const DSIZE: Dsize = Dsize(1);

#[test] fn glwe_serialization() { - let original: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWE<Vec<u8>> = GLWE::alloc(N_GLWE, BASE2K, K, RANK); poulpy_hal::test_suite::serialization::test_reader_writer_interface(original); } #[test] fn glwe_compressed_serialization() { - let original: GLWECiphertextCompressed<Vec<u8>> = GLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let original: LWECiphertext<Vec<u8>> = LWECiphertext::alloc_with(N_LWE, BASE2K, K); + let original: LWE<Vec<u8>> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let original: LWECiphertextCompressed<Vec<u8>> = LWECiphertextCompressed::alloc_with(BASE2K, K); + let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let original: GGLWE<Vec<u8>> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE<Vec<u8>> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let original: GGLWECiphertextCompressed<Vec<u8>> = - GGLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed<Vec<u8>> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_serialization() { - let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_glwe_switching_key_compressed_serialization() { let original: GLWESwitchingKeyCompressed<Vec<u8>> = - GLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); }

@@ -76,39 +75,38 @@ }

#[test] fn test_automorphism_key_compressed_serialization() { - let original: AutomorphismKeyCompressed<Vec<u8>> = - AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: TensorKey<Vec<u8>> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey<Vec<u8>> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_serialization() { - let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn glwe_to_lwe_switching_key_compressed_serialization() { let original: GLWEToLWESwitchingKeyCompressed<Vec<u8>> = - GLWEToLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_to_glwe_switching_key_serialization() { - let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); }

@@ -121,24 +119,24 @@ }

#[test] fn lwe_switching_key_serialization() { - let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let original: GGSW<Vec<u8>> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW<Vec<u8>> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let original: GGSWCiphertextCompressed<Vec<u8>> = GGSWCiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); }
M poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs → poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs

@@ -111,9 +111,9 @@ dsize: di.into(),

rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -130,7 +130,7 @@ &auto_key_apply_infos,

), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key_in); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key_in); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1

@@ -166,9 +166,9 @@ &auto_key_apply_prepared,

scratch.borrow(), ); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&auto_key_out_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos); - let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key_out_infos); + let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key_out_infos); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { module.vec_znx_automorphism(

@@ -311,8 +311,8 @@ dsize: di.into(),

rank: rank.into(), }; - let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -324,7 +324,7 @@ | AutomorphismKey::encrypt_sk_scratch_space(module, &auto_key_apply)

| AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1

@@ -355,9 +355,9 @@

// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0) auto_key.automorphism_inplace(module, &auto_key_apply_prepared, scratch.borrow()); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&auto_key); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key); - let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key); + let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank {
M poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs → poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs

@@ -128,10 +128,10 @@ dsize: di.into(),

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

@@ -147,7 +147,7 @@ );

let var_xs: f64 = 0.5; - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -309,9 +309,9 @@ dsize: di.into(),

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

@@ -327,7 +327,7 @@ );

let var_xs: f64 = 0.5; - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs → poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs

@@ -18,7 +18,7 @@

use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product,

@@ -77,14 +77,14 @@

let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let ct_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -100,10 +100,10 @@ dnum: dnum.into(),

dsize: di.into(), }; - let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos); - let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_in_infos); - let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_in_infos); + let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -113,12 +113,12 @@ module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::decrypt_scratch_space(module, &ct_out) + | GLWE::encrypt_sk_scratch_space(module, &ct_in) + | GLWE::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -219,7 +219,7 @@

let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let ct_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let ct_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -235,9 +235,9 @@ dnum: dnum.into(),

dsize: di.into(), }; - let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos); - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -247,12 +247,12 @@ module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::decrypt_scratch_space(module, &ct) + | GLWE::encrypt_sk_scratch_space(module, &ct) + | GLWE::automorphism_inplace_scratch_space(module, &ct, &autokey), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/conversion.rs → poulpy-core/src/tests/test_suite/conversion.rs

@@ -16,9 +16,8 @@ source::Source,

}; use crate::layouts::{ - Base2K, Degree, Dnum, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, - GLWEToLWESwitchingKey, LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, - LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, + Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, + LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision, prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, };

@@ -83,7 +82,7 @@ dnum: Dnum(2),

rank_out: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34),

@@ -98,11 +97,11 @@ };

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, &lwe_to_glwe_infos) - | GLWECiphertext::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + | GLWE::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());

@@ -112,13 +111,13 @@ sk_lwe.fill_ternary_prob(0.5, &mut source_xs);

let data: i64 = 17; - let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_pt.encode_i64(data, k_lwe_pt); - let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe); - let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(&lwe_to_glwe_infos); + let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos); ksk.encrypt_sk( module,

@@ -129,13 +128,13 @@ &mut source_xe,

scratch.borrow(), ); - let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); - let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow()); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);

@@ -196,7 +195,7 @@ dnum: Dnum(2),

rank_in: rank, }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe, base2k: Base2K(17), k: TorusPrecision(34),

@@ -215,11 +214,11 @@ let mut source_xe: Source = Source::new([0u8; 32]);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | LWE::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());

@@ -228,10 +227,10 @@ let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);

sk_lwe.fill_ternary_prob(0.5, &mut source_xs); let data: i64 = 17; - let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0); - let mut glwe_ct = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos); glwe_ct.encrypt_sk( module, &glwe_pt,

@@ -252,13 +251,13 @@ &mut source_xe,

scratch.borrow(), ); - let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); - let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe); assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
M poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs → poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs

@@ -84,7 +84,7 @@ dsize: di.into(),

rank: rank.into(), }; - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -94,7 +94,7 @@ let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(

module, &atk_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5;

@@ -187,7 +187,7 @@ dsize: di.into(),

rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -196,7 +196,7 @@ let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_scratch_space(

module, &atk_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5;

@@ -217,7 +217,7 @@ );

}); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key
M poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs → poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs

@@ -81,7 +81,7 @@ rank_in: rank_in.into(),

rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -92,10 +92,10 @@ module,

&gglwe_infos, )); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());

@@ -173,7 +173,8 @@ rank_in: rank_in.into(),

rank_out: rank_out.into(), }; - let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> = GLWESwitchingKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed<Vec<u8>> = + GLWESwitchingKeyCompressed::alloc_from_infos(&gglwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -183,10 +184,10 @@ module,

&gglwe_infos, )); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());

@@ -201,7 +202,7 @@ &mut source_xe,

scratch.borrow(), ); - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key
M poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs → poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs

@@ -18,7 +18,7 @@ use crate::{

encryption::SIGMA, layouts::{ GGSW, GGSWCiphertextLayout, GLWESecret, - compressed::{Decompress, GGSWCiphertextCompressed}, + compressed::{Decompress, GGSWCompressed}, prepared::{GLWESecretPrepared, PrepareAlloc}, }, };

@@ -45,7 +45,7 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -57,7 +57,7 @@ pt_scalar.fill_ternary_hw(0, n, &mut source_xs);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -135,7 +135,7 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ct_compressed: GGSWCiphertextCompressed<Vec<u8>> = GGSWCiphertextCompressed::alloc(&ggsw_infos); + let mut ct_compressed: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -144,12 +144,12 @@ let mut source_xe: Source = Source::new([0u8; 32]);

pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCiphertextCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_scratch_space( module, &ggsw_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

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

let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f);
M poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs → poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs

@@ -17,8 +17,8 @@

use crate::{ encryption::SIGMA, layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, - compressed::{Decompress, GLWECiphertextCompressed}, + GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, + compressed::{Decompress, GLWECompressed}, prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, operations::GLWEOperations,

@@ -64,7 +64,7 @@

for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(),

@@ -77,20 +77,19 @@ base2k: base2k.into(),

k: k_pt.into(), }; - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&pt_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_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<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -157,7 +156,7 @@

for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(),

@@ -170,21 +169,20 @@ base2k: base2k.into(),

k: k_pt.into(), }; - let mut ct_compressed: GLWECiphertextCompressed<Vec<u8>> = GLWECiphertextCompressed::alloc(&glwe_infos); + let mut ct_compressed: GLWECompressed<Vec<u8>> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&pt_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_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<B> = ScratchOwned::alloc( - GLWECiphertextCompressed::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos), + GLWECompressed::encrypt_sk_scratch_space(module, &glwe_infos) | GLWE::decrypt_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -201,7 +199,7 @@ &mut source_xe,

scratch.borrow(), ); - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());

@@ -258,29 +256,28 @@

for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos), + GLWE::decrypt_scratch_space(module, &glwe_infos) | GLWE::encrypt_sk_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module,

@@ -336,16 +333,16 @@

for rank in 1_usize..3 { let n: usize = module.n(); - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(), rank: rank.into(), }; - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -353,16 +350,16 @@ let mut source_xa: Source = Source::new([0u8; 32]);

let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_infos) - | GLWECiphertext::encrypt_pk_scratch_space(module, &glwe_infos), + GLWE::encrypt_sk_scratch_space(module, &glwe_infos) + | GLWE::decrypt_scratch_space(module, &glwe_infos) + | GLWE::encrypt_pk_scratch_space(module, &glwe_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc(&glwe_infos); + let mut pk: GLWEPublicKey<Vec<u8>> = GLWEPublicKey::alloc_from_infos(&glwe_infos); pk.generate(module, &sk_prepared, &mut source_xa, &mut source_xe); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
M poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs → poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs

@@ -80,7 +80,7 @@ dsize: Dsize(1),

rank: rank.into(), }; - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -91,7 +91,7 @@ module,

&tensor_key_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -103,11 +103,11 @@ &mut source_xe,

scratch.borrow(), ); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank {

@@ -199,7 +199,7 @@ dsize: Dsize(1),

rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_from_infos(&tensor_key_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -209,7 +209,7 @@ module,

&tensor_key_infos, )); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -217,14 +217,14 @@ let seed_xa: [u8; 32] = [1u8; 32];

tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos); let mut sk_ij_dft = module.vec_znx_dft_alloc(1, 1); let mut sk_ij_big = module.vec_znx_big_alloc(1, 1); - let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank {
M poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs → poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs

@@ -110,9 +110,9 @@ dsize: di.into(),

rank: rank_out.into(), }; - let mut ct_gglwe_in: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -137,10 +137,10 @@ pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}

let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());

@@ -282,8 +282,8 @@ dsize: di.into(),

rank: rank_out.into(), }; - let mut ct_gglwe: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -303,10 +303,10 @@ pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}

let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs → poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs

@@ -111,9 +111,9 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -133,7 +133,7 @@ | 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<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -265,8 +265,8 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);

@@ -287,7 +287,7 @@ | 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<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs → poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs

@@ -17,7 +17,7 @@

use crate::{ encryption::SIGMA, layouts::{ - GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::noise_ggsw_product,

@@ -73,14 +73,14 @@

let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -96,11 +96,11 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -117,11 +117,11 @@ pt_ggsw.raw_mut()[k] = 1; // X^{k}

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) + | GLWE::external_product_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -224,7 +224,7 @@

let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -240,10 +240,10 @@ dsize: di.into(),

rank: rank.into(), }; - let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -260,11 +260,11 @@ pt_ggsw.raw_mut()[k] = 1; // X^{k}

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::external_product_inplace_scratch_space(module, &glwe_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs

@@ -110,9 +110,9 @@ rank_in: rank_in_s0s1.into(),

rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -130,13 +130,13 @@ &gglwe_s0s2_infos,

&gglwe_s1s2_infos, )); - let mut sk0: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in_s0s1.into()); + let mut sk0: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in_s0s1.into()); sk0.fill_ternary_prob(0.5, &mut source_xs); - let mut sk1: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out_s0s1.into()); + let mut sk1: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out_s0s1.into()); sk1.fill_ternary_prob(0.5, &mut source_xs); - let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out_s1s2.into()); + let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out_s1s2.into()); sk2.fill_ternary_prob(0.5, &mut source_xs); let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());

@@ -266,8 +266,8 @@ rank_in: rank_out.into(),

rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -285,13 +285,13 @@ ));

let var_xs: f64 = 0.5; - let mut sk0: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk0: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk0.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk1: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk1: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk1.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk2: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk2.fill_ternary_prob(var_xs, &mut source_xs); let sk2_prepared: GLWESecretPrepared<Vec<u8>, B> = sk2.prepare_alloc(module, scratch_apply.borrow());
M poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs

@@ -121,10 +121,10 @@ rank_in: rank.into(),

rank_out: rank.into(), }; - let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]);

@@ -146,11 +146,11 @@ );

let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());

@@ -300,9 +300,9 @@ rank_in: rank.into(),

rank_out: rank.into(), }; - let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]);

@@ -318,11 +318,11 @@ );

let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs

@@ -18,7 +18,7 @@

use crate::{ encryption::SIGMA, layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout, prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, PrepareAlloc}, }, noise::log2_std_noise_gglwe_product,

@@ -77,14 +77,14 @@

let n: usize = module.n(); let dnum: usize = k_in.div_ceil(base2k * dsize); - let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_in_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_in.into(), rank: rank_in.into(), }; - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -101,10 +101,10 @@ rank_in: rank_in.into(),

rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&key_apply); - let mut glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_in_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -114,15 +114,15 @@ module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(module, &glwe_in_infos) + | GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, &key_apply), ); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());

@@ -217,7 +217,7 @@

let n: usize = module.n(); let dnum: usize = k_out.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_out.into(),

@@ -234,9 +234,9 @@ rank_in: rank.into(),

rank_out: rank.into(), }; - let mut key_apply: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(&key_apply_infos); - let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut key_apply: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);

@@ -246,15 +246,15 @@ module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( 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), + | GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::keyswitch_inplace_scratch_space(module, &glwe_out_infos, &key_apply_infos), ); - let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow());
M poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs → poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs

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

}; use crate::layouts::{ - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, + LWE, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, };

@@ -100,7 +100,7 @@ };

let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( LWESwitchingKey::encrypt_sk_scratch_space(module, &key_apply_infos) - | LWECiphertext::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), + | LWE::keyswitch_scratch_space(module, &lwe_out_infos, &lwe_in_infos, &key_apply_infos), ); let mut sk_lwe_in: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe_in.into());

@@ -111,10 +111,10 @@ sk_lwe_out.fill_ternary_prob(0.5, &mut source_xs);

let data: i64 = 17; - let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); lwe_pt_in.encode_i64(data, k_lwe_pt.into()); - let mut lwe_ct_in: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_in_infos); + let mut lwe_ct_in: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_in_infos); lwe_ct_in.encrypt_sk( module, &lwe_pt_in,

@@ -123,7 +123,7 @@ &mut source_xa,

&mut source_xe, ); - let mut ksk: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(&key_apply_infos); + let mut ksk: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc_from_infos(&key_apply_infos); ksk.encrypt_sk( module,

@@ -134,13 +134,13 @@ &mut source_xe,

scratch.borrow(), ); - let mut lwe_ct_out: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_out_infos); + let mut lwe_ct_out: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_out_infos); let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow()); - let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_out_infos); + let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_out_infos); lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out); assert_eq!(lwe_pt_in.data.at(0, 0)[0], lwe_pt_out.data.at(0, 0)[0]);
M poulpy-core/src/tests/test_suite/packing.rs → poulpy-core/src/tests/test_suite/packing.rs

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

use crate::{ GLWEOperations, GLWEPacker, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, };

@@ -88,7 +88,7 @@ let k_ksk: usize = k_ct + base2k * dsize;

let dnum: usize = k_ct.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k_ct.into(),

@@ -105,16 +105,16 @@ dnum: dnum.into(),

}; let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) + GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) | GLWEPacker::scratch_space(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec<i64> = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { *x = i as i64;

@@ -125,7 +125,7 @@

let gal_els: Vec<i64> = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap<i64, AutomorphismKeyPrepared<Vec<u8>, B>> = HashMap::new(); - let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&key_infos); + let mut tmp: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module,

@@ -143,7 +143,7 @@ let log_batch: usize = 0;

let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); - let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); ct.encrypt_sk( module,

@@ -171,19 +171,14 @@

if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); } else { - packer.add( - module, - None::<&GLWECiphertext<Vec<u8>>>, - &auto_keys, - scratch.borrow(), - ) + packer.add(module, None::<&GLWE<Vec<u8>>>, &auto_keys, scratch.borrow()) } }); - let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); + let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); packer.flush(module, &mut res); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec<i64> = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { if i.is_multiple_of(5) {
M poulpy-core/src/tests/test_suite/trace.rs → poulpy-core/src/tests/test_suite/trace.rs

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

use crate::{ encryption::SIGMA, layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWEInfos, + AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWEInfos, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }, noise::var_noise_gglwe_product,

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

let dsize: usize = 1; let dnum: usize = k.div_ceil(base2k * dsize); - let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_out_infos: GLWELayout = GLWELayout { n: n.into(), base2k: base2k.into(), k: k.into(),

@@ -98,22 +98,22 @@ dsize: dsize.into(),

dnum: dnum.into(), }; - let mut glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_out_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<B> = ScratchOwned::alloc( - GLWECiphertext::encrypt_sk_scratch_space(module, &glwe_out_infos) - | GLWECiphertext::decrypt_scratch_space(module, &glwe_out_infos) + GLWE::encrypt_sk_scratch_space(module, &glwe_out_infos) + | GLWE::decrypt_scratch_space(module, &glwe_out_infos) | AutomorphismKey::encrypt_sk_scratch_space(module, &key_infos) - | GLWECiphertext::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), + | GLWE::trace_inplace_scratch_space(module, &glwe_out_infos, &key_infos), ); - let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

@@ -135,8 +135,8 @@ scratch.borrow(),

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

@@ -9,7 +9,7 @@ }

/// Returns the size in bytes to allocate a [crate::layouts::SvpPPol]. pub trait SvpPPolAllocBytes { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize; + fn svp_ppol_bytes_of(&self, cols: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::MatZnx].
M poulpy-hal/src/api/vec_znx_big.rs → poulpy-hal/src/api/vec_znx_big.rs

@@ -17,7 +17,7 @@ }

/// Returns the size in bytes to allocate a [crate::layouts::VecZnxBig]. pub trait VecZnxBigAllocBytes { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize; + fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize; } /// Consume a vector of bytes into a [crate::layouts::VecZnxBig].
M poulpy-hal/src/api/vec_znx_dft.rs → poulpy-hal/src/api/vec_znx_dft.rs

@@ -11,7 +11,7 @@ fn vec_znx_dft_from_bytes(&self, cols: usize, size: usize, bytes: Vec<u8>) -> VecZnxDftOwned<B>;

} pub trait VecZnxDftAllocBytes { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize; + fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize; } pub trait VecZnxDftApply<B: Backend> {
M poulpy-hal/src/api/vmp_pmat.rs → poulpy-hal/src/api/vmp_pmat.rs

@@ -7,7 +7,7 @@ fn vmp_pmat_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned<B>;

} pub trait VmpPMatAllocBytes { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } pub trait VmpPMatFromBytes<B: Backend> {
M poulpy-hal/src/delegates/svp_ppol.rs → poulpy-hal/src/delegates/svp_ppol.rs

@@ -34,8 +34,8 @@ impl<B> SvpPPolAllocBytes for Module<B>

where B: Backend + SvpPPolAllocBytesImpl<B>, { - fn svp_ppol_alloc_bytes(&self, cols: usize) -> usize { - B::svp_ppol_alloc_bytes_impl(self.n(), cols) + fn svp_ppol_bytes_of(&self, cols: usize) -> usize { + B::svp_ppol_bytes_of_impl(self.n(), cols) } }
M poulpy-hal/src/delegates/vec_znx_big.rs → poulpy-hal/src/delegates/vec_znx_big.rs

@@ -53,8 +53,8 @@ impl<B> VecZnxBigAllocBytes for Module<B>

where B: Backend + VecZnxBigAllocBytesImpl<B>, { - fn vec_znx_big_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_big_alloc_bytes_impl(self.n(), cols, size) + fn vec_znx_big_bytes_of(&self, cols: usize, size: usize) -> usize { + B::vec_znx_big_bytes_of_impl(self.n(), cols, size) } }
M poulpy-hal/src/delegates/vec_znx_dft.rs → poulpy-hal/src/delegates/vec_znx_dft.rs

@@ -28,8 +28,8 @@ impl<B> VecZnxDftAllocBytes for Module<B>

where B: Backend + VecZnxDftAllocBytesImpl<B>, { - fn vec_znx_dft_alloc_bytes(&self, cols: usize, size: usize) -> usize { - B::vec_znx_dft_alloc_bytes_impl(self.n(), cols, size) + fn vec_znx_dft_bytes_of(&self, cols: usize, size: usize) -> usize { + B::vec_znx_dft_bytes_of_impl(self.n(), cols, size) } }
M poulpy-hal/src/delegates/vmp_pmat.rs → poulpy-hal/src/delegates/vmp_pmat.rs

@@ -27,8 +27,8 @@ impl<B> VmpPMatAllocBytes for Module<B>

where B: Backend + VmpPMatAllocBytesImpl<B>, { - fn vmp_pmat_alloc_bytes(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - B::vmp_pmat_alloc_bytes_impl(self.n(), rows, cols_in, cols_out, size) + fn vmp_pmat_bytes_of(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + B::vmp_pmat_bytes_of_impl(self.n(), rows, cols_in, cols_out, size) } }
M poulpy-hal/src/layouts/mat_znx.rs → poulpy-hal/src/layouts/mat_znx.rs

@@ -114,12 +114,12 @@ }

} impl MatZnx<Vec<u8>> { - pub fn alloc_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - rows * cols_in * VecZnx::<Vec<u8>>::alloc_bytes(n, cols_out, size) + pub fn bytes_of(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { + rows * cols_in * VecZnx::<Vec<u8>>::bytes_of(n, cols_out, size) } pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec<u8> = alloc_aligned(Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + let data: Vec<u8> = alloc_aligned(Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n,

@@ -132,7 +132,7 @@ }

pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + assert!(data.len() == Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n,

@@ -153,7 +153,7 @@ assert!(col < self.cols_in(), "cols: {} >= {}", col, self.cols_in());

} let self_ref: MatZnx<&[u8]> = self.to_ref(); - let nb_bytes: usize = VecZnx::<Vec<u8>>::alloc_bytes(self.n, self.cols_out, self.size); + let nb_bytes: usize = VecZnx::<Vec<u8>>::bytes_of(self.n, self.cols_out, self.size); let start: usize = nb_bytes * self.cols() * row + col * nb_bytes; let end: usize = start + nb_bytes;

@@ -181,7 +181,7 @@ let cols_in: usize = self.cols_in();

let size: usize = self.size(); let self_ref: MatZnx<&mut [u8]> = self.to_mut(); - let nb_bytes: usize = VecZnx::<Vec<u8>>::alloc_bytes(n, cols_out, size); + let nb_bytes: usize = VecZnx::<Vec<u8>>::bytes_of(n, cols_out, size); let start: usize = nb_bytes * cols_in * row + col * nb_bytes; let end: usize = start + nb_bytes;
M poulpy-hal/src/layouts/scalar_znx.rs → poulpy-hal/src/layouts/scalar_znx.rs

@@ -132,18 +132,18 @@ }

} impl ScalarZnx<Vec<u8>> { - pub fn alloc_bytes(n: usize, cols: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize) -> usize { n * cols * size_of::<i64>() } pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec<u8> = alloc_aligned::<u8>(Self::alloc_bytes(n, cols)); + let data: Vec<u8> = alloc_aligned::<u8>(Self::bytes_of(n, cols)); Self { data, n, cols } } pub fn from_bytes(n: usize, cols: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data, n, cols } } }
M poulpy-hal/src/layouts/svp_ppol.rs → poulpy-hal/src/layouts/svp_ppol.rs

@@ -77,7 +77,7 @@ where

B: SvpPPolAllocBytesImpl<B>, { pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec<u8> = alloc_aligned::<u8>(B::svp_ppol_alloc_bytes_impl(n, cols)); + let data: Vec<u8> = alloc_aligned::<u8>(B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n,

@@ -88,7 +88,7 @@ }

pub fn from_bytes(n: usize, cols: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == B::svp_ppol_alloc_bytes_impl(n, cols)); + assert!(data.len() == B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n,
M poulpy-hal/src/layouts/vec_znx.rs → poulpy-hal/src/layouts/vec_znx.rs

@@ -125,12 +125,12 @@ }

} impl VecZnx<Vec<u8>> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::<i64>() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec<u8> = alloc_aligned::<u8>(Self::alloc_bytes(n, cols, size)); + let data: Vec<u8> = alloc_aligned::<u8>(Self::bytes_of(n, cols, size)); Self { data, n,

@@ -142,7 +142,7 @@ }

pub fn from_bytes<Scalar: Sized>(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n,
M poulpy-hal/src/layouts/vec_znx_big.rs → poulpy-hal/src/layouts/vec_znx_big.rs

@@ -96,7 +96,7 @@ where

B: VecZnxBigAllocBytesImpl<B>, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data = alloc_aligned::<u8>(B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + let data = alloc_aligned::<u8>(B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n,

@@ -109,7 +109,7 @@ }

pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n,
M poulpy-hal/src/layouts/vec_znx_dft.rs → poulpy-hal/src/layouts/vec_znx_dft.rs

@@ -116,7 +116,7 @@ where

B: VecZnxDftAllocBytesImpl<B>, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec<u8> = alloc_aligned::<u8>(B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let data: Vec<u8> = alloc_aligned::<u8>(B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n,

@@ -129,7 +129,7 @@ }

pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + assert!(data.len() == B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n,
M poulpy-hal/src/layouts/vmp_pmat.rs → poulpy-hal/src/layouts/vmp_pmat.rs

@@ -88,9 +88,7 @@ where

B: VmpPMatAllocBytesImpl<B>, { pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec<u8> = alloc_aligned(B::vmp_pmat_alloc_bytes_impl( - n, rows, cols_in, cols_out, size, - )); + let data: Vec<u8> = alloc_aligned(B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n,

@@ -104,7 +102,7 @@ }

pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size)); + assert!(data.len() == B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n,
M poulpy-hal/src/layouts/zn.rs → poulpy-hal/src/layouts/zn.rs

@@ -113,12 +113,12 @@ }

} impl Zn<Vec<u8>> { - pub fn alloc_bytes(n: usize, cols: usize, size: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize, size: usize) -> usize { n * cols * size * size_of::<i64>() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec<u8> = alloc_aligned::<u8>(Self::alloc_bytes(n, cols, size)); + let data: Vec<u8> = alloc_aligned::<u8>(Self::bytes_of(n, cols, size)); Self { data, n,

@@ -130,7 +130,7 @@ }

pub fn from_bytes<Scalar: Sized>(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n,
M poulpy-hal/src/oep/svp_ppol.rs → poulpy-hal/src/oep/svp_ppol.rs

@@ -23,7 +23,7 @@ /// * See the [poulpy-backend/src/cpu_fft64_ref/svp.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/svp.rs) reference implementation.

/// * See [crate::api::SvpPPolAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocBytesImpl<B: Backend> { - fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize; + fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
M poulpy-hal/src/oep/vec_znx_big.rs → poulpy-hal/src/oep/vec_znx_big.rs

@@ -35,7 +35,7 @@ /// * See the [poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs) reference implementation.

/// * See [crate::api::VecZnxBigAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocBytesImpl<B: Backend> { - fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } #[allow(clippy::too_many_arguments)]
M poulpy-hal/src/oep/vec_znx_dft.rs → poulpy-hal/src/oep/vec_znx_dft.rs

@@ -42,7 +42,7 @@ /// * See the [poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs) reference implementation.

/// * See [crate::api::VecZnxDftAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocBytesImpl<B: Backend> { - fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; + fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
M poulpy-hal/src/oep/vmp_pmat.rs → poulpy-hal/src/oep/vmp_pmat.rs

@@ -15,7 +15,7 @@ /// * See [poulpy-backend/src/cpu_fft64_ref/vmp.rs](https://github.com/phantomzone-org/poulpy/blob/main/poulpy-backend/src/cpu_fft64_ref/vmp.rs) for reference implementation.

/// * See [crate::api::VmpPMatAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocBytesImpl<B: Backend> { - fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; + fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe)
M poulpy-hal/src/reference/fft64/vmp.rs → poulpy-hal/src/reference/fft64/vmp.rs

@@ -140,7 +140,7 @@ assert!(tmp_bytes.len() >= vmp_apply_dft_tmp_bytes(n, size, pmat.rows(), cols));

assert!(a.cols() <= cols); } - let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let (data, tmp_bytes) = tmp_bytes.split_at_mut(BE::vec_znx_dft_bytes_of_impl(n, cols, size)); let mut a_dft: VecZnxDft<&mut [u8], BE> = VecZnxDft::from_data(cast_mut(data), n, cols, size);
M poulpy-schemes/benches/circuit_bootstrapping.rs → poulpy-schemes/benches/circuit_bootstrapping.rs

@@ -3,8 +3,8 @@

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret, - TensorKeyLayout, prepared::PrepareAlloc, + AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWE, LWECiphertextLayout, LWESecret, TensorKeyLayout, + prepared::PrepareAlloc, }; use poulpy_hal::{ api::{

@@ -202,10 +202,10 @@ let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);

sk_lwe.fill_binary_block(params.block_size, &mut source_xs); sk_lwe.fill_zero(); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe, rank); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&params.lwe_infos); + let ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&params.lwe_infos); // Circuit bootstrapping evaluation key let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk(

@@ -218,7 +218,7 @@ &mut source_xe,

scratch.borrow(), ); - let mut res: GGSW<Vec<u8>> = GGSW::alloc(&params.ggsw_infos); + let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&params.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || {
M poulpy-schemes/examples/circuit_bootstrapping.rs → poulpy-schemes/examples/circuit_bootstrapping.rs

@@ -1,8 +1,8 @@

use poulpy_core::{ GLWEOperations, layouts::{ - AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, - LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, + AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, + LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout, prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc}, }, };

@@ -140,7 +140,7 @@ sk_lwe.fill_binary_block(block_size, &mut source_xs);

sk_lwe.fill_zero(); // GLWE secret - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); // sk_glwe.fill_zero();

@@ -151,7 +151,7 @@ // Plaintext value to circuit bootstrap

let data: i64 = 1 % (1 << k_lwe_pt); // LWE plaintext - let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); // LWE plaintext(data * 2^{- (k_lwe_pt - 1)}) pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); // +1 for padding bit

@@ -167,7 +167,7 @@ );

println!("pt_lwe: {pt_lwe}"); // LWE ciphertext - let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); // Encrypt LWE Plaintext ct_lwe.encrypt_sk(&module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);

@@ -187,7 +187,7 @@ );

println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> =

@@ -214,7 +214,7 @@ res.print_noise(&module, &sk_glwe_prepared, &pt_ggsw);

// Tests RLWE(1) * GGSW(data) - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: (k_ggsw_res - base2k).into(),

@@ -222,11 +222,11 @@ rank: rank.into(),

}; // GLWE ciphertext modulus - let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); + let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); // Some GLWE plaintext with signed data let k_glwe_pt: usize = 3; - let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut data_vec: Vec<i64> = vec![0i64; n_glwe]; data_vec .iter_mut()

@@ -255,7 +255,7 @@ // Apply GLWE x GGSW

ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); // Decrypt - let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); ct_glwe.decrypt(&module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); println!("pt_res: {:?}", &pt_res.data.at(0, 0)[..64]);
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs

@@ -1,6 +1,6 @@

use std::marker::PhantomData; -use poulpy_core::layouts::{Base2K, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; +use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared}; use poulpy_hal::api::VecZnxBigAllocBytes;

@@ -24,7 +24,7 @@ use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};

/// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocks<D: Data, T: UnsignedInteger> { - pub(crate) blocks: Vec<GLWECiphertext<D>>, + pub(crate) blocks: Vec<GLWE<D>>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData<T>, }

@@ -62,7 +62,7 @@ #[allow(dead_code)]

pub(crate) fn alloc_with<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GLWECiphertext::alloc_with(module.n().into(), base2k, k, rank)) + .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) .collect(), _base: 1, _phantom: PhantomData,
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs

@@ -60,7 +60,7 @@ rank: Rank,

) -> Self { Self { blocks: (0..T::WORD_SIZE) - .map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize)) + .map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize)) .collect(), _base: 1, _phantom: PhantomData,
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs

@@ -2,7 +2,7 @@ use itertools::Itertools;

use poulpy_core::{ GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, layouts::{ - GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, + GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, };

@@ -24,14 +24,14 @@

use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// A FHE ciphertext encrypting a [UnsignedInteger]. -pub struct FheUintWord<D: Data, T: UnsignedInteger>(pub(crate) GLWECiphertext<D>, pub(crate) PhantomData<T>); +pub struct FheUintWord<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crate) PhantomData<T>); impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> { #[allow(dead_code)] fn post_process<ATK, BE: Backend>( &mut self, module: &Module<BE>, - mut tmp_res: Vec<GLWECiphertext<&mut [u8]>>, + mut tmp_res: Vec<GLWE<&mut [u8]>>, auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, BE>>, scratch: &mut Scratch<BE>, ) where

@@ -67,7 +67,7 @@ {

// Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; - let mut cts: HashMap<usize, &mut GLWECiphertext<&mut [u8]>> = HashMap::new(); + let mut cts: HashMap<usize, &mut GLWE<&mut [u8]>> = HashMap::new(); for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); }
M poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs

@@ -2,7 +2,7 @@ use itertools::Itertools;

use poulpy_core::{ GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice, layouts::{ - GLWECiphertext, GLWECiphertextToMut, LWEInfos, + GLWE, GLWEToMut, LWEInfos, prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared}, }, };

@@ -38,7 +38,7 @@ {

fn execute<O>( &self, module: &Module<BE>, - out: &mut [GLWECiphertext<O>], + out: &mut [GLWE<O>], inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>], scratch: &mut Scratch<BE>, ) where

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

fn execute<O>( &self, module: &Module<BE>, - out: &mut [GLWECiphertext<O>], + out: &mut [GLWE<O>], inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>], scratch: &mut Scratch<BE>, ) where

@@ -159,14 +159,8 @@ }

} pub trait Cmux<BE: Backend> { - fn cmux<O, T, F, S>( - &self, - out: &mut GLWECiphertext<O>, - t: &GLWECiphertext<T>, - f: &GLWECiphertext<F>, - s: &GGSWPrepared<S, BE>, - scratch: &mut Scratch<BE>, - ) where + fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>) + where O: DataMut, T: DataRef, F: DataRef,

@@ -177,14 +171,8 @@ impl<BE: Backend> Cmux<BE> for Module<BE>

where Module<BE>: GLWEExternalProductInplace<BE> + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, { - fn cmux<O, T, F, S>( - &self, - out: &mut GLWECiphertext<O>, - t: &GLWECiphertext<T>, - f: &GLWECiphertext<F>, - s: &GGSWPrepared<S, BE>, - scratch: &mut Scratch<BE>, - ) where + fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>) + where O: DataMut, T: DataRef, F: DataRef,
M poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs

@@ -11,7 +11,7 @@ };

use poulpy_core::{ TakeGGSW, TakeGLWECt, layouts::{ - GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, };

@@ -182,7 +182,7 @@ #[cfg(debug_assertions)]

{ assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE<Vec<u8>> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out); for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch_1);

@@ -231,7 +231,7 @@ #[cfg(debug_assertions)]

{ assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE<Vec<u8>> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) { lwe.from_glwe(module, src, &self.ks, scratch); self.cbt
M poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs

@@ -1,6 +1,6 @@

#[cfg(test)] use poulpy_core::layouts::{ - AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank, + AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWELayout, GLWEToLWEKeyLayout, Rank, TensorKeyLayout, TorusPrecision, };

@@ -25,7 +25,7 @@ #[cfg(test)]

pub(crate) const TEST_RANK: u32 = 2; #[cfg(test)] -pub(crate) static TEST_GLWE_INFOS: GLWECiphertextLayout = GLWECiphertextLayout { +pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout { n: Degree(TEST_N_GLWE), base2k: Base2K(TEST_BASE2K), k: TorusPrecision(TEST_K_GLWE),
M poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs

@@ -4,7 +4,7 @@ use poulpy_backend::FFT64Ref;

use poulpy_core::{ TakeGGSW, TakeGLWEPt, layouts::{ - GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret, + GGSWCiphertextLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, }, };

@@ -107,7 +107,7 @@ BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<BE, BlindRotationKeyPrepared<Vec<u8>, BRA, BE>>,

BlindRotationKeyPrepared<Vec<u8>, BRA, BE>: BlincRotationExecute<BE>, BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>, { - let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS; + let glwe_infos: GLWELayout = TEST_GLWE_INFOS; let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS; let n_glwe: usize = glwe_infos.n().into();

@@ -120,7 +120,7 @@ let mut source_xe: Source = Source::new([3u8; 32]);

let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow());
M poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs → poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs

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

use poulpy_core::{ Distribution, GLWEOperations, TakeGLWECt, - layouts::{GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWEInfos, LWECiphertext, LWECiphertextToRef, LWEInfos}, + layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWECiphertextToRef, LWEInfos}, }; use crate::tfhe::blind_rotation::{

@@ -43,14 +43,14 @@

if block_size > 1 { let cols: usize = (brk_infos.rank() + 1).into(); let dnum: usize = brk_infos.dnum().into(); - let acc_dft: usize = module.vec_znx_dft_alloc_bytes(cols, dnum) * extension_factor; - let acc_big: usize = module.vec_znx_big_alloc_bytes(1, brk_size); - let vmp_res: usize = module.vec_znx_dft_alloc_bytes(cols, brk_size) * extension_factor; - let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(1, brk_size); + let acc_dft: usize = module.vec_znx_dft_bytes_of(cols, dnum) * extension_factor; + let acc_big: usize = module.vec_znx_big_bytes_of(1, brk_size); + let vmp_res: usize = module.vec_znx_dft_bytes_of(cols, brk_size) * extension_factor; + let vmp_xai: usize = module.vec_znx_dft_bytes_of(1, brk_size); let acc_dft_add: usize = vmp_res; let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) let acc: usize = if extension_factor > 1 { - VecZnx::alloc_bytes(module.n(), cols, glwe_infos.size()) * extension_factor + VecZnx::bytes_of(module.n(), cols, glwe_infos.size()) * extension_factor } else { 0 };

@@ -61,8 +61,7 @@ + vmp_res

+ vmp_xai + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes()))) } else { - GLWECiphertext::alloc_bytes(glwe_infos) - + GLWECiphertext::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) + GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_scratch_space(module, glwe_infos, brk_infos) } }

@@ -99,8 +98,8 @@ {

fn execute<DR: DataMut, DI: DataRef>( &self, module: &Module<B>, - res: &mut GLWECiphertext<DR>, - lwe: &LWECiphertext<DI>, + res: &mut GLWE<DR>, + lwe: &LWE<DI>, lut: &LookUpTable, scratch: &mut Scratch<B>, ) {

@@ -121,8 +120,8 @@ }

fn execute_block_binary_extended<DataRes, DataIn, DataBrk, B: Backend>( module: &Module<B>, - res: &mut GLWECiphertext<DataRes>, - lwe: &LWECiphertext<DataIn>, + res: &mut GLWE<DataRes>, + lwe: &LWE<DataIn>, lut: &LookUpTable, brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>, scratch: &mut Scratch<B>,

@@ -179,7 +178,7 @@ panic!("invalid key: x_pow_a has not been initialized")

} let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).as_usize()]; // TODO: from scratch space - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = 2 * n_glwe; let two_n_ext: usize = 2 * lut.domain_size();

@@ -288,8 +287,8 @@ }

fn execute_block_binary<DataRes, DataIn, DataBrk, B: Backend>( module: &Module<B>, - res: &mut GLWECiphertext<DataRes>, - lwe: &LWECiphertext<DataIn>, + res: &mut GLWE<DataRes>, + lwe: &LWE<DataIn>, lut: &LookUpTable, brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>, scratch: &mut Scratch<B>,

@@ -324,8 +323,8 @@ Scratch<B>: TakeVecZnxDftSlice<B> + TakeVecZnxDft<B> + TakeVecZnxBig<B> + TakeVecZnxSlice + ScratchAvailable + TakeVecZnx,

{ let n_glwe: usize = brk.n_glwe().into(); let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); let two_n: usize = n_glwe << 1; let base2k: usize = brk.base2k().into(); let dnum: usize = brk.dnum().into();

@@ -410,8 +409,8 @@ }

fn execute_standard<DataRes, DataIn, DataBrk, B: Backend>( module: &Module<B>, - res: &mut GLWECiphertext<DataRes>, - lwe: &LWECiphertext<DataIn>, + res: &mut GLWE<DataRes>, + lwe: &LWE<DataIn>, lut: &LookUpTable, brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>, scratch: &mut Scratch<B>,

@@ -480,8 +479,8 @@ );

} let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space - let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); - let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref(); + let mut out_mut: GLWE<&mut [u8]> = res.to_mut(); + let lwe_ref: LWE<&[u8]> = lwe.to_ref(); mod_switch_2n( 2 * lut.domain_size(),

@@ -519,7 +518,7 @@ // on top of each others, thus ~ 2^{63-base2k} additions are supported before overflow.

out_mut.normalize_inplace(module, scratch_1); } -pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8]>, rot_dir: LookUpTableRotationDirection) { +pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) { let base2k: usize = lwe.base2k().into(); let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1;
M poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs → poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs

@@ -15,7 +15,7 @@ use poulpy_core::{

Distribution, layouts::{ GGSW, GGSWInfos, LWESecret, - compressed::GGSWCiphertextCompressed, + compressed::GGSWCompressed, prepared::{GGSWPrepared, GLWESecretPrepared}, }, };

@@ -32,7 +32,7 @@ A: BlindRotationKeyInfos,

{ let mut data: Vec<GGSW<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into()); for _ in 0..infos.n_lwe().as_usize() { - data.push(GGSW::alloc(infos)); + data.push(GGSW::alloc_from_infos(infos)); } Self {

@@ -137,8 +137,8 @@ pub fn alloc<A>(infos: &A) -> Self

where A: BlindRotationKeyInfos, { - let mut data: Vec<GGSWCiphertextCompressed<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos))); + let mut data: Vec<GGSWCompressed<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into()); + (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos))); Self { keys: data, dist: Distribution::NONE,

@@ -151,7 +151,7 @@ where

A: GGSWInfos, Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos) + GGSWCompressed::encrypt_sk_scratch_space(module, infos) } }
M poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs → poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs

@@ -8,14 +8,14 @@

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use poulpy_core::{ Distribution, - layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCiphertextCompressed}, + layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed}, }; use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos}; #[derive(Clone)] pub struct BlindRotationKeyCompressed<D: Data, BRT: BlindRotationAlgo> { - pub(crate) keys: Vec<GGSWCiphertextCompressed<D>>, + pub(crate) keys: Vec<GGSWCompressed<D>>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData<BRT>, }
M poulpy-schemes/src/tfhe/blind_rotation/mod.rs → poulpy-schemes/src/tfhe/blind_rotation/mod.rs

@@ -14,7 +14,7 @@ pub use lut::*;

pub mod tests; -use poulpy_core::layouts::{GLWECiphertext, LWECiphertext}; +use poulpy_core::layouts::{GLWE, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; pub trait BlindRotationAlgo {}

@@ -27,8 +27,8 @@ pub trait BlincRotationExecute<B: Backend> {

fn execute<DR: DataMut, DI: DataRef>( &self, module: &Module<B>, - res: &mut GLWECiphertext<DR>, - lwe: &LWECiphertext<DI>, + res: &mut GLWE<DR>, + lwe: &LWE<DI>, lut: &LookUpTable, scratch: &mut Scratch<B>, );
M poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs → poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs

@@ -23,8 +23,7 @@ BlindRotationKeyPrepared, CGGI, LookUpTable, cggi_blind_rotate_scratch_space, mod_switch_2n,

}; use poulpy_core::layouts::{ - GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWECiphertextToRef, - LWEInfos, LWEPlaintext, LWESecret, + GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, LWECiphertextToRef, LWEInfos, LWEPlaintext, LWESecret, prepared::{GLWESecretPrepared, PrepareAlloc}, };

@@ -111,7 +110,7 @@ dnum: rows_brk.into(),

rank: rank.into(), }; - let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_infos: GLWELayout = GLWELayout { n: n_glwe.into(), base2k: base2k.into(), k: k_res.into(),

@@ -128,7 +127,7 @@ let mut scratch: ScratchOwned<B> = ScratchOwned::<B>::alloc(BlindRotationKey::generate_from_sk_scratch_space(

module, &brk_infos, )); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_dft: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());

@@ -154,9 +153,9 @@ &mut source_xe,

scratch.borrow(), ); - let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); - let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos); + let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos); let x: i64 = 15 % (message_modulus as i64);

@@ -175,13 +174,13 @@

let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor); lut.set(module, &f_vec, log_message_modulus + 1); - let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos); + let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); let brk_prepared: BlindRotationKeyPrepared<Vec<u8>, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); - let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos); res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());
M poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs

@@ -20,7 +20,7 @@ layouts::{Dsize, GGLWECiphertextLayout, GGSWInfos, GLWEInfos, LWEInfos},

}; use poulpy_core::glwe_packing; -use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{

@@ -75,7 +75,7 @@ fn execute_to_constant<DM: DataMut, DR: DataRef>(

&self, module: &Module<B>, res: &mut GGSW<DM>, - lwe: &LWECiphertext<DR>, + lwe: &LWE<DR>, log_domain: usize, extension_factor: usize, scratch: &mut Scratch<B>,

@@ -98,7 +98,7 @@ &self,

module: &Module<B>, log_gap_out: usize, res: &mut GGSW<DM>, - lwe: &LWECiphertext<DR>, + lwe: &LWE<DR>, log_domain: usize, extension_factor: usize, scratch: &mut Scratch<B>,

@@ -123,7 +123,7 @@ to_exponent: bool,

module: &Module<B>, log_gap_out: usize, res: &mut GGSW<DRes>, - lwe: &LWECiphertext<DLwe>, + lwe: &LWE<DLwe>, log_domain: usize, extension_factor: usize, key: &CircuitBootstrappingKeyPrepared<DBrk, BRA, B>,

@@ -233,7 +233,7 @@

let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _; (0..dnum).for_each(|i| { - let mut tmp_glwe: GLWECiphertext<&mut [u8]> = tmp_gglwe.at_mut(i, 0); + let mut tmp_glwe: GLWE<&mut [u8]> = tmp_gglwe.at_mut(i, 0); if to_exponent { // Isolates i-th LUT and moves coefficients according to requested gap.

@@ -263,8 +263,8 @@

#[allow(clippy::too_many_arguments)] fn post_process<DataRes, DataA, B: Backend>( module: &Module<B>, - res: &mut GLWECiphertext<DataRes>, - a: &GLWECiphertext<DataA>, + res: &mut GLWE<DataRes>, + a: &GLWE<DataA>, log_gap_in: usize, log_gap_out: usize, log_domain: usize,

@@ -303,7 +303,7 @@ Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,

{ let log_n: usize = module.log_n(); - let mut cts: HashMap<usize, &mut GLWECiphertext<Vec<u8>>> = HashMap::new(); + let mut cts: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new(); // First partial trace, vanishes all coefficients which are not multiples of gap_in // [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0]

@@ -322,7 +322,7 @@ if log_gap_in != log_gap_out {

let steps: usize = 1 << log_domain; // TODO: from Scratch - let mut cts_vec: Vec<GLWECiphertext<Vec<u8>>> = Vec::new(); + let mut cts_vec: Vec<GLWE<Vec<u8>>> = Vec::new(); for i in 0..steps { if i != 0 {

@@ -336,7 +336,7 @@ cts.insert(i * (1 << log_gap_out), ct);

} glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); - let packed: &mut GLWECiphertext<Vec<u8>> = cts.remove(&0).unwrap(); + let packed: &mut GLWE<Vec<u8>> = cts.remove(&0).unwrap(); res.trace( module, log_n - log_gap_out,
M poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs

@@ -1,6 +1,6 @@

use poulpy_core::layouts::{ - AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret, - TensorKey, TensorKeyLayout, + AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey, + TensorKeyLayout, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared}, }; use std::collections::HashMap;

@@ -122,9 +122,9 @@ let brk_infos: BlindRotationKeyLayout = cbt_infos.brk_infos();

let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>>> = HashMap::new(); - let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module); + let gal_els: Vec<i64> = GLWE::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch); auto_keys.insert(*gal_el, key); });

@@ -141,7 +141,7 @@ source_xe,

scratch, ); - let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&trk_infos); + let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self {
M poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs

@@ -5,7 +5,7 @@

pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSW, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};

@@ -14,7 +14,7 @@ fn execute_to_constant<DM: DataMut, DR: DataRef>(

&self, module: &Module<B>, res: &mut GGSW<DM>, - lwe: &LWECiphertext<DR>, + lwe: &LWE<DR>, log_domain: usize, extension_factor: usize, scratch: &mut Scratch<B>,

@@ -26,7 +26,7 @@ &self,

module: &Module<B>, log_gap_out: usize, res: &mut GGSW<DM>, - lwe: &LWECiphertext<DR>, + lwe: &LWE<DR>, log_domain: usize, extension_factor: usize, scratch: &mut Scratch<B>,
M poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs → poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs

@@ -36,7 +36,7 @@ AutomorphismKeyLayout, Dsize, GGSWCiphertextLayout, LWECiphertextLayout, TensorKeyLayout, prepared::PrepareAlloc,

}; use poulpy_core::layouts::{ - GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret, + GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret, prepared::{GGSWPrepared, GLWESecretPrepared}, };

@@ -179,19 +179,19 @@

let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now();

@@ -206,7 +206,7 @@ scratch.borrow(),

); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1;

@@ -236,8 +236,8 @@ );

res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - 2); ct_glwe.encrypt_sk(

@@ -253,7 +253,7 @@ let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());

ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless.

@@ -401,19 +401,19 @@

let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); println!("pt_lwe: {pt_lwe}"); - let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos); ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe); let now: Instant = Instant::now();

@@ -428,7 +428,7 @@ scratch.borrow(),

); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());

@@ -449,8 +449,8 @@ pt_ggsw.at_mut(0, 0)[0] = data;

res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - k_lwe_pt - 1); ct_glwe.encrypt_sk(

@@ -466,7 +466,7 @@ let res_prepared: GGSWPrepared<Vec<u8>, B> = res.prepare_alloc(module, scratch.borrow());

ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos); ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow()); // Parameters are set such that the first limb should be noiseless.