From 72dca47cbeadf66cd1c2815330c0cf60ed1c9823 Mon Sep 17 00:00:00 2001 From: Pro7ech Date: Tue, 14 Oct 2025 18:46:25 +0200 Subject: [PATCH] wip --- poulpy-backend/src/cpu_fft64_avx/scratch.rs | 14 +- poulpy-backend/src/cpu_fft64_avx/svp.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_big.rs | 2 +- .../src/cpu_fft64_avx/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_avx/vmp.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/scratch.rs | 14 +- poulpy-backend/src/cpu_fft64_ref/svp.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_big.rs | 2 +- .../src/cpu_fft64_ref/vec_znx_dft.rs | 2 +- poulpy-backend/src/cpu_fft64_ref/vmp.rs | 2 +- .../src/cpu_spqlios/fft64/scratch.rs | 14 +- .../src/cpu_spqlios/fft64/svp_ppol.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/fft64/vec_znx_dft.rs | 2 +- .../src/cpu_spqlios/fft64/vmp_pmat.rs | 2 +- .../src/cpu_spqlios/ntt120/svp_ppol.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_big.rs | 2 +- .../src/cpu_spqlios/ntt120/vec_znx_dft.rs | 2 +- .../benches/external_product_glwe_fft64.rs | 31 ++- poulpy-core/benches/keyswitch_glwe_fft64.rs | 34 +-- poulpy-core/examples/encryption.rs | 16 +- poulpy-core/src/automorphism/gglwe_atk.rs | 10 +- poulpy-core/src/automorphism/ggsw_ct.rs | 6 +- poulpy-core/src/automorphism/glwe_ct.rs | 14 +- poulpy-core/src/conversion/glwe_to_lwe.rs | 21 +- poulpy-core/src/conversion/lwe_to_glwe.rs | 19 +- poulpy-core/src/decryption/glwe_ct.rs | 8 +- poulpy-core/src/decryption/lwe_ct.rs | 4 +- .../src/encryption/compressed/gglwe_atk.rs | 3 +- .../src/encryption/compressed/gglwe_ct.rs | 16 +- .../src/encryption/compressed/gglwe_ksk.rs | 6 +- .../src/encryption/compressed/ggsw_ct.rs | 12 +- .../src/encryption/compressed/glwe_ct.rs | 16 +- poulpy-core/src/encryption/gglwe_atk.rs | 2 +- poulpy-core/src/encryption/gglwe_ct.rs | 10 +- poulpy-core/src/encryption/gglwe_ksk.rs | 6 +- poulpy-core/src/encryption/gglwe_tsk.rs | 10 +- poulpy-core/src/encryption/ggsw_ct.rs | 10 +- poulpy-core/src/encryption/glwe_ct.rs | 49 ++-- poulpy-core/src/encryption/glwe_pk.rs | 6 +- poulpy-core/src/encryption/glwe_to_lwe_ksk.rs | 5 +- poulpy-core/src/encryption/lwe_ct.rs | 4 +- poulpy-core/src/encryption/lwe_ksk.rs | 4 +- poulpy-core/src/encryption/lwe_to_glwe_ksk.rs | 2 +- poulpy-core/src/external_product/gglwe_ksk.rs | 6 +- poulpy-core/src/external_product/ggsw_ct.rs | 6 +- poulpy-core/src/external_product/glwe_ct.rs | 76 +++--- poulpy-core/src/external_product/mod.rs | 17 +- poulpy-core/src/glwe_packing.rs | 35 ++- poulpy-core/src/glwe_trace.rs | 12 +- poulpy-core/src/keyswitching/gglwe_ct.rs | 6 +- poulpy-core/src/keyswitching/ggsw_ct.rs | 18 +- poulpy-core/src/keyswitching/glwe_ct.rs | 28 +- poulpy-core/src/keyswitching/lwe_ct.rs | 27 +- .../src/layouts/compressed/gglwe_atk.rs | 135 ++++++++-- .../src/layouts/compressed/gglwe_ct.rs | 242 ++++++++++-------- .../src/layouts/compressed/gglwe_ksk.rs | 141 +++++++--- .../src/layouts/compressed/gglwe_tsk.rs | 170 +++++++----- poulpy-core/src/layouts/compressed/ggsw_ct.rs | 201 ++++++++++----- poulpy-core/src/layouts/compressed/glwe_ct.rs | 200 ++++++++------- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 147 +++++++++-- poulpy-core/src/layouts/compressed/lwe_ct.rs | 153 ++++++++--- poulpy-core/src/layouts/compressed/lwe_ksk.rs | 146 ++++++++--- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 148 ++++++++--- poulpy-core/src/layouts/compressed/mod.rs | 6 - poulpy-core/src/layouts/gglwe_atk.rs | 118 +++++++-- poulpy-core/src/layouts/gglwe_ct.rs | 127 ++++++--- poulpy-core/src/layouts/gglwe_ksk.rs | 113 ++++++-- poulpy-core/src/layouts/gglwe_tsk.rs | 112 +++++--- poulpy-core/src/layouts/ggsw_ct.rs | 102 +++++--- poulpy-core/src/layouts/glwe_ct.rs | 146 +++++++---- poulpy-core/src/layouts/glwe_pk.rs | 79 ++++-- poulpy-core/src/layouts/glwe_pt.rs | 88 +++++-- poulpy-core/src/layouts/glwe_sk.rs | 72 ++++-- poulpy-core/src/layouts/glwe_to_lwe_ksk.rs | 111 +++++--- poulpy-core/src/layouts/lwe_ct.rs | 100 +++++--- poulpy-core/src/layouts/lwe_ksk.rs | 127 +++++---- poulpy-core/src/layouts/lwe_pt.rs | 37 ++- poulpy-core/src/layouts/lwe_sk.rs | 19 +- poulpy-core/src/layouts/lwe_to_glwe_ksk.rs | 114 ++++++--- poulpy-core/src/layouts/prepared/gglwe_atk.rs | 42 +-- poulpy-core/src/layouts/prepared/gglwe_ct.rs | 34 +-- poulpy-core/src/layouts/prepared/gglwe_ksk.rs | 36 +-- poulpy-core/src/layouts/prepared/gglwe_tsk.rs | 47 ++-- poulpy-core/src/layouts/prepared/ggsw_ct.rs | 33 +-- poulpy-core/src/layouts/prepared/glwe_pk.rs | 30 +-- poulpy-core/src/layouts/prepared/glwe_sk.rs | 28 +- .../src/layouts/prepared/glwe_to_lwe_ksk.rs | 28 +- poulpy-core/src/layouts/prepared/lwe_ksk.rs | 28 +- .../src/layouts/prepared/lwe_to_glwe_ksk.rs | 22 +- poulpy-core/src/noise/gglwe_ct.rs | 6 +- poulpy-core/src/noise/ggsw_ct.rs | 14 +- poulpy-core/src/noise/glwe_ct.rs | 8 +- poulpy-core/src/operations/glwe.rs | 92 ++++--- poulpy-core/src/scratch.rs | 20 +- poulpy-core/src/tests/serialization.rs | 48 ++-- .../test_suite/automorphism/gglwe_atk.rs | 22 +- .../tests/test_suite/automorphism/ggsw_ct.rs | 18 +- .../tests/test_suite/automorphism/glwe_ct.rs | 38 +-- .../src/tests/test_suite/conversion.rs | 39 ++- .../tests/test_suite/encryption/gglwe_atk.rs | 10 +- .../tests/test_suite/encryption/gglwe_ct.rs | 15 +- .../tests/test_suite/encryption/ggsw_ct.rs | 14 +- .../tests/test_suite/encryption/glwe_ct.rs | 61 +++-- .../tests/test_suite/encryption/glwe_tsk.rs | 18 +- .../test_suite/external_product/gglwe_ksk.rs | 18 +- .../test_suite/external_product/ggsw_ct.rs | 14 +- .../test_suite/external_product/glwe_ct.rs | 34 +-- .../tests/test_suite/keyswitch/gglwe_ct.rs | 22 +- .../src/tests/test_suite/keyswitch/ggsw_ct.rs | 22 +- .../src/tests/test_suite/keyswitch/glwe_ct.rs | 38 +-- .../src/tests/test_suite/keyswitch/lwe_ct.rs | 14 +- poulpy-core/src/tests/test_suite/packing.rs | 25 +- poulpy-core/src/tests/test_suite/trace.rs | 22 +- poulpy-hal/src/api/svp_ppol.rs | 2 +- poulpy-hal/src/api/vec_znx_big.rs | 2 +- poulpy-hal/src/api/vec_znx_dft.rs | 2 +- poulpy-hal/src/api/vmp_pmat.rs | 2 +- poulpy-hal/src/delegates/svp_ppol.rs | 4 +- poulpy-hal/src/delegates/vec_znx_big.rs | 4 +- poulpy-hal/src/delegates/vec_znx_dft.rs | 4 +- poulpy-hal/src/delegates/vmp_pmat.rs | 4 +- poulpy-hal/src/layouts/mat_znx.rs | 12 +- poulpy-hal/src/layouts/scalar_znx.rs | 6 +- poulpy-hal/src/layouts/svp_ppol.rs | 4 +- poulpy-hal/src/layouts/vec_znx.rs | 6 +- poulpy-hal/src/layouts/vec_znx_big.rs | 4 +- poulpy-hal/src/layouts/vec_znx_dft.rs | 4 +- poulpy-hal/src/layouts/vmp_pmat.rs | 6 +- poulpy-hal/src/layouts/zn.rs | 6 +- poulpy-hal/src/oep/svp_ppol.rs | 2 +- poulpy-hal/src/oep/vec_znx_big.rs | 2 +- poulpy-hal/src/oep/vec_znx_dft.rs | 2 +- poulpy-hal/src/oep/vmp_pmat.rs | 2 +- poulpy-hal/src/reference/fft64/vmp.rs | 2 +- .../benches/circuit_bootstrapping.rs | 10 +- .../examples/circuit_bootstrapping.rs | 20 +- .../tfhe/bdd_arithmetic/ciphertexts/block.rs | 6 +- .../ciphertexts/block_prepared.rs | 2 +- .../tfhe/bdd_arithmetic/ciphertexts/word.rs | 8 +- .../src/tfhe/bdd_arithmetic/eval.rs | 26 +- poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs | 6 +- .../src/tfhe/bdd_arithmetic/parameters.rs | 4 +- .../src/tfhe/bdd_arithmetic/test.rs | 6 +- .../src/tfhe/blind_rotation/cggi_algo.rs | 43 ++-- .../src/tfhe/blind_rotation/cggi_key.rs | 10 +- .../src/tfhe/blind_rotation/key_compressed.rs | 4 +- poulpy-schemes/src/tfhe/blind_rotation/mod.rs | 6 +- .../tests/generic_blind_rotation.rs | 15 +- .../src/tfhe/circuit_bootstrapping/circuit.rs | 20 +- .../src/tfhe/circuit_bootstrapping/key.rs | 10 +- .../src/tfhe/circuit_bootstrapping/mod.rs | 6 +- .../tests/circuit_bootstrapping.rs | 30 +-- 153 files changed, 3099 insertions(+), 1956 deletions(-) diff --git a/poulpy-backend/src/cpu_fft64_avx/scratch.rs b/poulpy-backend/src/cpu_fft64_avx/scratch.rs index 922166b..2cdf85e 100644 --- a/poulpy-backend/src/cpu_fft64_avx/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_avx/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - 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 + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - 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, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - 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 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { 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), diff --git a/poulpy-backend/src/cpu_fft64_avx/svp.rs b/poulpy-backend/src/cpu_fft64_avx/svp.rs index f505597..1c2c999 100644 --- a/poulpy-backend/src/cpu_fft64_avx/svp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Avx { } unsafe impl SvpPPolAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs index 99a39fd..08ec98a 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs index 862f623..063ee26 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Avx { } unsafe impl VecZnxDftAllocBytesImpl 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::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_avx/vmp.rs b/poulpy-backend/src/cpu_fft64_avx/vmp.rs index 6b87ce1..fcb6236 100644 --- a/poulpy-backend/src/cpu_fft64_avx/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_avx/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_avx::{FFT64Avx, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/scratch.rs b/poulpy-backend/src/cpu_fft64_ref/scratch.rs index 80b228d..ad617ce 100644 --- a/poulpy-backend/src/cpu_fft64_ref/scratch.rs +++ b/poulpy-backend/src/cpu_fft64_ref/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - 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 + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - 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, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - 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 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { 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), diff --git a/poulpy-backend/src/cpu_fft64_ref/svp.rs b/poulpy-backend/src/cpu_fft64_ref/svp.rs index 06dad9e..37ca14a 100644 --- a/poulpy-backend/src/cpu_fft64_ref/svp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/svp.rs @@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Ref { } unsafe impl SvpPPolAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs index bb75c8f..348c9b6 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_big.rs @@ -27,7 +27,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs index a08b728..a2a743d 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vec_znx_dft.rs @@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Ref { } unsafe impl VecZnxDftAllocBytesImpl 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::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_fft64_ref/vmp.rs b/poulpy-backend/src/cpu_fft64_ref/vmp.rs index 2286de5..34cbf07 100644 --- a/poulpy-backend/src/cpu_fft64_ref/vmp.rs +++ b/poulpy-backend/src/cpu_fft64_ref/vmp.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ use crate::cpu_fft64_ref::{FFT64Ref, module::FFT64ModuleHandle}; unsafe impl VmpPMatAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs index 9bddcb3..2b56218 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/scratch.rs @@ -69,7 +69,7 @@ where B: ScratchFromBytesImpl, { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch) { - 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 + ScratchFromBytesImpl, { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch) { - 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, { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch) { - 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 @@ where ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (VmpPMat<&mut [u8], B>, &mut Scratch) { 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 @@ where ) -> (MatZnx<&mut [u8]>, &mut Scratch) { 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), diff --git a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs index b917400..f46b795 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/svp_ppol.rs @@ -27,7 +27,7 @@ unsafe impl SvpPPolAllocImpl for FFT64Spqlios { } unsafe impl SvpPPolAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs index 8becaf6..5021f6b 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_big.rs @@ -22,7 +22,7 @@ use poulpy_hal::{ }; unsafe impl VecZnxBigAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs index 461d327..3b67089 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vec_znx_dft.rs @@ -30,7 +30,7 @@ unsafe impl VecZnxDftFromBytesImpl for FFT64Spqlios { } unsafe impl VecZnxDftAllocBytesImpl 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::<::ScalarPrep>() } } diff --git a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs index ca64992..ff1eaa2 100644 --- a/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs +++ b/poulpy-backend/src/cpu_spqlios/fft64/vmp_pmat.rs @@ -16,7 +16,7 @@ use crate::cpu_spqlios::{ }; unsafe impl VmpPMatAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs index c98237a..8c7fdcc 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/svp_ppol.rs @@ -18,7 +18,7 @@ unsafe impl SvpPPolAllocImpl for NTT120 { } unsafe impl SvpPPolAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs index 715b432..58ddf78 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_big.rs @@ -3,7 +3,7 @@ use poulpy_hal::{layouts::Backend, oep::VecZnxBigAllocBytesImpl}; use crate::cpu_spqlios::NTT120; unsafe impl VecZnxBigAllocBytesImpl 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::() } } diff --git a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs index 53dd24f..9e1666b 100644 --- a/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs +++ b/poulpy-backend/src/cpu_spqlios/ntt120/vec_znx_dft.rs @@ -6,7 +6,7 @@ use poulpy_hal::{ use crate::cpu_spqlios::NTT120; unsafe impl VecZnxDftAllocBytesImpl 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::() } } diff --git a/poulpy-core/benches/external_product_glwe_fft64.rs b/poulpy-core/benches/external_product_glwe_fft64.rs index 5027f3b..333d68f 100644 --- a/poulpy-core/benches/external_product_glwe_fft64.rs +++ b/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 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { 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> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_in_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); @@ -156,28 +155,28 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_glwe, rank, }; - let mut ct_ggsw: GGSW> = GGSW::alloc(&ggsw_layout); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ct_ggsw: GGSW> = GGSW::alloc_from_infos(&ggsw_layout); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n.into(), 1); let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_layout); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow()); diff --git a/poulpy-core/benches/keyswitch_glwe_fft64.rs b/poulpy-core/benches/keyswitch_glwe_fft64.rs index 8316949..a1b5ea2 100644 --- a/poulpy-core/benches/keyswitch_glwe_fft64.rs +++ b/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 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { 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> = AutomorphismKey::alloc(&gglwe_atk_layout); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_layout); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_layout); + let mut ksk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_layout); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_layout); let mut scratch: ScratchOwned = 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 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); - let mut sk_in: GLWESecret> = GLWESecret::alloc(&glwe_in_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_in_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); @@ -167,31 +167,31 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { rank_out: rank, }; - let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout { + let glwe_layout: GLWELayout = GLWELayout { n, base2k, k: k_ct, rank, }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_layout); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_layout); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_layout); let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_layout); + let mut sk_in: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_dft: GLWESecretPrepared, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc(&glwe_layout); + let mut sk_out: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_layout); sk_out.fill_ternary_prob(0.5, &mut source_xs); ksk.encrypt_sk( diff --git a/poulpy-core/examples/encryption.rs b/poulpy-core/examples/encryption.rs index a65b473..f59d5a1 100644 --- a/poulpy-core/examples/encryption.rs +++ b/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 @@ fn main() { // Instantiate Module (DFT Tables) let module: Module = Module::::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 @@ fn main() { let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt }; // Allocates ciphertext & plaintexts - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_ct_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_ct_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos); // CPRNG let mut source_xs: Source = Source::new([0u8; 32]); @@ -55,12 +54,11 @@ fn main() { // Scratch space let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_ct_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_ct_infos); sk.fill_ternary_prob(0.5, &mut source_xs); // Backend-prepared secret diff --git a/poulpy-core/src/automorphism/gglwe_atk.rs b/poulpy-core/src/automorphism/gglwe_atk.rs index 57324e3..233f250 100644 --- a/poulpy-core/src/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/automorphism/gglwe_atk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ 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> { pub fn automorphism_scratch_space( @@ -22,7 +22,7 @@ impl AutomorphismKey> { KEY: GGLWEInfos, Module: 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 @@ impl AutomorphismKey { (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 @@ impl AutomorphismKey { (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| { diff --git a/poulpy-core/src/automorphism/ggsw_ct.rs b/poulpy-core/src/automorphism/ggsw_ct.rs index 09e2f94..f1a84ad 100644 --- a/poulpy-core/src/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/automorphism/ggsw_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - GGLWEInfos, GGSW, GGSWInfos, GLWECiphertext, + GGLWEInfos, GGSW, GGSWInfos, GLWE, prepared::{AutomorphismKeyPrepared, TensorKeyPrepared}, }; @@ -33,8 +33,8 @@ impl GGSW> { + 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(), diff --git a/poulpy-core/src/automorphism/glwe_ct.rs b/poulpy-core/src/automorphism/glwe_ct.rs index f94b9bc..800248a 100644 --- a/poulpy-core/src/automorphism/glwe_ct.rs +++ b/poulpy-core/src/automorphism/glwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ 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> { +impl GLWE> { pub fn automorphism_scratch_space( module: &Module, out_infos: &OUT, @@ -36,11 +36,11 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn automorphism( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -93,7 +93,7 @@ impl GLWECiphertext { pub fn automorphism_add( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -176,7 +176,7 @@ impl GLWECiphertext { pub fn automorphism_sub_ab( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where @@ -261,7 +261,7 @@ impl GLWECiphertext { pub fn automorphism_sub_negate( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &AutomorphismKeyPrepared, scratch: &mut Scratch, ) where diff --git a/poulpy-core/src/conversion/glwe_to_lwe.rs b/poulpy-core/src/conversion/glwe_to_lwe.rs index 517da90..d3e32ae 100644 --- a/poulpy-core/src/conversion/glwe_to_lwe.rs +++ b/poulpy-core/src/conversion/glwe_to_lwe.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ 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> { +impl LWE> { pub fn from_glwe_scratch_space( module: &Module, lwe_infos: &OUT, @@ -28,24 +25,24 @@ impl LWECiphertext> { KEY: GGLWEInfos, Module: 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 LWECiphertext { - pub fn sample_extract(&mut self, a: &GLWECiphertext) { +impl LWE { + pub fn sample_extract(&mut self, a: &GLWE) { #[cfg(debug_assertions)] { assert!(self.n() <= a.n()); @@ -66,7 +63,7 @@ impl LWECiphertext { pub fn from_glwe( &mut self, module: &Module, - a: &GLWECiphertext, + a: &GLWE, ks: &GLWEToLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -92,7 +89,7 @@ impl LWECiphertext { 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(), diff --git a/poulpy-core/src/conversion/lwe_to_glwe.rs b/poulpy-core/src/conversion/lwe_to_glwe.rs index d3ae616..bb5480e 100644 --- a/poulpy-core/src/conversion/lwe_to_glwe.rs +++ b/poulpy-core/src/conversion/lwe_to_glwe.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ use crate::{ TakeGLWECt, - layouts::{ - GGLWEInfos, GLWECiphertext, GLWECiphertextLayout, GLWEInfos, LWECiphertext, LWEInfos, - prepared::LWEToGLWESwitchingKeyPrepared, - }, + layouts::{GGLWEInfos, GLWE, GLWEInfos, GLWELayout, LWE, LWEInfos, prepared::LWEToGLWESwitchingKeyPrepared}, }; -impl GLWECiphertext> { +impl GLWE> { pub fn from_lwe_scratch_space( module: &Module, glwe_infos: &OUT, @@ -28,27 +25,27 @@ impl GLWECiphertext> { KEY: GGLWEInfos, Module: 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 GLWECiphertext { +impl GLWE { pub fn from_lwe( &mut self, module: &Module, - lwe: &LWECiphertext, + lwe: &LWE, ksk: &LWEToGLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -74,7 +71,7 @@ impl GLWECiphertext { 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(), diff --git a/poulpy-core/src/decryption/glwe_ct.rs b/poulpy-core/src/decryption/glwe_ct.rs index 19b4a82..92b8c29 100644 --- a/poulpy-core/src/decryption/glwe_ct.rs +++ b/poulpy-core/src/decryption/glwe_ct.rs @@ -6,20 +6,20 @@ use poulpy_hal::{ 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> { +impl GLWE> { pub fn decrypt_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: 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 GLWECiphertext { +impl GLWE { pub fn decrypt( &self, module: &Module, diff --git a/poulpy-core/src/decryption/lwe_ct.rs b/poulpy-core/src/decryption/lwe_ct.rs index 57abdc6..ade21e3 100644 --- a/poulpy-core/src/decryption/lwe_ct.rs +++ b/poulpy-core/src/decryption/lwe_ct.rs @@ -4,9 +4,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; -use crate::layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}; +use crate::layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}; -impl LWECiphertext +impl LWE where DataSelf: DataRef, { diff --git a/poulpy-core/src/encryption/compressed/gglwe_atk.rs b/poulpy-core/src/encryption/compressed/gglwe_atk.rs index 9665c19..7e717c6 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_atk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_atk.rs @@ -20,8 +20,7 @@ impl AutomorphismKeyCompressed> { Module: 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()) } } diff --git a/poulpy-core/src/encryption/compressed/gglwe_ct.rs b/poulpy-core/src/encryption/compressed/gglwe_ct.rs index 4939fe5..f5a136f 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ct.rs @@ -12,12 +12,12 @@ use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGLWE, GGLWEInfos, LWEInfos, - compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut}, + compressed::{GGLWECompressed, GGLWECompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGLWECiphertextCompressed { +impl GGLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, @@ -34,7 +34,7 @@ impl GGLWECiphertextCompressed { } } -impl GGLWECiphertextCompressed> { +impl GGLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGLWEInfos, @@ -54,7 +54,7 @@ pub trait GGLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -78,11 +78,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextCompressedToMut, + R: GGLWECompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - 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 @@ where 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, diff --git a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs index f9549a5..260c02e 100644 --- a/poulpy-core/src/encryption/compressed/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/compressed/gglwe_ksk.rs @@ -23,9 +23,9 @@ impl GLWESwitchingKeyCompressed> { A: GGLWEInfos, Module: 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()) } } diff --git a/poulpy-core/src/encryption/compressed/ggsw_ct.rs b/poulpy-core/src/encryption/compressed/ggsw_ct.rs index e9e5017..56f22ad 100644 --- a/poulpy-core/src/encryption/compressed/ggsw_ct.rs +++ b/poulpy-core/src/encryption/compressed/ggsw_ct.rs @@ -9,12 +9,12 @@ use crate::{ encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal}, layouts::{ GGSW, GGSWInfos, GLWEInfos, LWEInfos, - compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut}, + compressed::{GGSWCompressed, GGSWCompressedToMut}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; -impl GGSWCiphertextCompressed> { +impl GGSWCompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GGSWInfos, @@ -34,7 +34,7 @@ pub trait GGSWCompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -53,11 +53,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGSWCiphertextCompressedToMut, + R: GGSWCompressedToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { - 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 @@ where } } -impl GGSWCiphertextCompressed { +impl GGSWCompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/compressed/glwe_ct.rs b/poulpy-core/src/encryption/compressed/glwe_ct.rs index 86f86cc..6fadd31 100644 --- a/poulpy-core/src/encryption/compressed/glwe_ct.rs +++ b/poulpy-core/src/encryption/compressed/glwe_ct.rs @@ -7,19 +7,19 @@ use poulpy_hal::{ 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> { +impl GLWECompressed> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GLWECiphertext::encrypt_sk_scratch_space(module, infos) + GLWE::encrypt_sk_scratch_space(module, infos) } } @@ -33,7 +33,7 @@ pub trait GLWECompressedEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -51,11 +51,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextCompressedToMut, + R: GLWECompressedToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - 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 @@ where } } -impl GLWECiphertextCompressed { +impl GLWECompressed { #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, diff --git a/poulpy-core/src/encryption/gglwe_atk.rs b/poulpy-core/src/encryption/gglwe_atk.rs index a269847..18caa37 100644 --- a/poulpy-core/src/encryption/gglwe_atk.rs +++ b/poulpy-core/src/encryption/gglwe_atk.rs @@ -27,7 +27,7 @@ impl AutomorphismKey> { 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(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_ct.rs b/poulpy-core/src/encryption/gglwe_ct.rs index a7a98ce..9beed56 100644 --- a/poulpy-core/src/encryption/gglwe_ct.rs +++ b/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 @@ impl GGLWE> { A: GGLWEInfos, Module: 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(_module: &Module, _infos: &A) -> usize @@ -44,7 +44,7 @@ pub trait GGLWEEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef; } @@ -64,7 +64,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GGLWECiphertextToMut, + R: GGLWEToMut, P: ScalarZnxToRef, S: GLWESecretPreparedToRef, { diff --git a/poulpy-core/src/encryption/gglwe_ksk.rs b/poulpy-core/src/encryption/gglwe_ksk.rs index 5bea8d0..b0caa54 100644 --- a/poulpy-core/src/encryption/gglwe_ksk.rs +++ b/poulpy-core/src/encryption/gglwe_ksk.rs @@ -20,9 +20,9 @@ impl GLWESwitchingKey> { A: GGLWEInfos, Module: 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(module: &Module, _infos: &A) -> usize diff --git a/poulpy-core/src/encryption/gglwe_tsk.rs b/poulpy-core/src/encryption/gglwe_tsk.rs index 330ca0c..4de505a 100644 --- a/poulpy-core/src/encryption/gglwe_tsk.rs +++ b/poulpy-core/src/encryption/gglwe_tsk.rs @@ -23,11 +23,11 @@ impl TensorKey> { Module: 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) } } diff --git a/poulpy-core/src/encryption/ggsw_ct.rs b/poulpy-core/src/encryption/ggsw_ct.rs index ad713b5..ea200d4 100644 --- a/poulpy-core/src/encryption/ggsw_ct.rs +++ b/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 @@ impl GGSW> { Module: 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) } } diff --git a/poulpy-core/src/encryption/glwe_ct.rs b/poulpy-core/src/encryption/glwe_ct.rs index d05ffc6..9fe8411 100644 --- a/poulpy-core/src/encryption/glwe_ct.rs +++ b/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> { +impl GLWE> { pub fn encrypt_sk_scratch_space(module: &Module, infos: &A) -> usize where A: GLWEInfos, @@ -26,9 +26,7 @@ impl GLWECiphertext> { { 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(module: &Module, infos: &A) -> usize where @@ -37,14 +35,13 @@ impl GLWECiphertext> { { 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 GLWECiphertext { +impl GLWE { pub fn encrypt_sk( &mut self, module: &Module, @@ -116,7 +113,7 @@ pub trait GLWEEncryptSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef; } @@ -135,11 +132,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, S: GLWESecretPreparedToRef, { - 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 @@ where 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 @@ pub trait GLWEEncryptZeroSk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef; } @@ -200,10 +197,10 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, S: GLWESecretPreparedToRef, { - 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 @@ where 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 @@ pub trait GLWEEncryptPk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; } @@ -264,7 +261,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, { @@ -281,7 +278,7 @@ pub trait GLWEEncryptZeroPk { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef; } @@ -297,7 +294,7 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, K: GLWEPublicKeyPreparedToRef, { self.glwe_encrypt_pk_internal( @@ -321,7 +318,7 @@ pub(crate) trait GLWEEncryptPkInternal { source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef; } @@ -345,11 +342,11 @@ where source_xe: &mut Source, scratch: &mut Scratch, ) where - R: GLWECiphertextToMut, + R: GLWEToMut, P: GLWEPlaintextToRef, K: GLWEPublicKeyPreparedToRef, { - 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)] diff --git a/poulpy-core/src/encryption/glwe_pk.rs b/poulpy-core/src/encryption/glwe_pk.rs index c0de69b..d8f96c2 100644 --- a/poulpy-core/src/encryption/glwe_pk.rs +++ b/poulpy-core/src/encryption/glwe_pk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ use crate::{ encryption::glwe_ct::GLWEEncryptZeroSk, layouts::{ - GLWECiphertext, GLWEPublicKey, GLWEPublicKeyToMut, + GLWE, GLWEPublicKey, GLWEPublicKeyToMut, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, }, }; @@ -45,9 +45,9 @@ where } // Its ok to allocate scratch space here since pk is usually generated only once. - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(self, res)); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::encrypt_sk_scratch_space(self, res)); - let mut tmp: GLWECiphertext> = GLWECiphertext::alloc(res); + let mut tmp: GLWE> = GLWE::alloc_from_infos(res); tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow()); res.dist = sk.dist; diff --git a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs index ebbd0f1..56c92aa 100644 --- a/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/encryption/glwe_to_lwe_ksk.rs @@ -22,9 +22,8 @@ impl GLWEToLWESwitchingKey> { A: GGLWEInfos, Module: 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())) } } diff --git a/poulpy-core/src/encryption/lwe_ct.rs b/poulpy-core/src/encryption/lwe_ct.rs index 4dd09ac..a01d95f 100644 --- a/poulpy-core/src/encryption/lwe_ct.rs +++ b/poulpy-core/src/encryption/lwe_ct.rs @@ -7,10 +7,10 @@ use poulpy_hal::{ use crate::{ encryption::{SIGMA, SIGMA_BOUND}, - layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret}, + layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret}, }; -impl LWECiphertext { +impl LWE { pub fn encrypt_sk( &mut self, module: &Module, diff --git a/poulpy-core/src/encryption/lwe_ksk.rs b/poulpy-core/src/encryption/lwe_ksk.rs index 4408e6e..23cea9b 100644 --- a/poulpy-core/src/encryption/lwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_ksk.rs @@ -38,8 +38,8 @@ impl LWESwitchingKey> { 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) } } diff --git a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs index 5cb9339..bf60c09 100644 --- a/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/encryption/lwe_to_glwe_ksk.rs @@ -26,7 +26,7 @@ impl LWEToGLWESwitchingKey> { "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()) } } diff --git a/poulpy-core/src/external_product/gglwe_ksk.rs b/poulpy-core/src/external_product/gglwe_ksk.rs index bfeb146..c09898e 100644 --- a/poulpy-core/src/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/external_product/gglwe_ksk.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ 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> { pub fn external_product_scratch_space( @@ -22,7 +22,7 @@ impl GLWESwitchingKey> { GGSW: GGSWInfos, Module: 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 @@ impl GLWESwitchingKey> { GGSW: GGSWInfos, Module: 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) } } diff --git a/poulpy-core/src/external_product/ggsw_ct.rs b/poulpy-core/src/external_product/ggsw_ct.rs index 1610cef..8e5eccd 100644 --- a/poulpy-core/src/external_product/ggsw_ct.rs +++ b/poulpy-core/src/external_product/ggsw_ct.rs @@ -7,7 +7,7 @@ use poulpy_hal::{ 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> { #[allow(clippy::too_many_arguments)] @@ -23,7 +23,7 @@ impl GGSW> { GGSW: GGSWInfos, Module: 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 @@ impl GGSW> { GGSW: GGSWInfos, Module: 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) } } diff --git a/poulpy-core/src/external_product/glwe_ct.rs b/poulpy-core/src/external_product/glwe_ct.rs index 5a72049..221d000 100644 --- a/poulpy-core/src/external_product/glwe_ct.rs +++ b/poulpy-core/src/external_product/glwe_ct.rs @@ -7,15 +7,12 @@ use poulpy_hal::{ 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> { +impl GLWE> { #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, @@ -35,8 +32,8 @@ impl GLWECiphertext> { .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 @@ impl GLWECiphertext> { 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 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn external_product( &mut self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &GGSWPrepared, scratch: &mut Scratch, ) where @@ -88,15 +85,15 @@ impl GLWECiphertext { rhs: &GGSWPrepared, scratch: &mut Scratch, ) where - Module: GLWEExternalProductInplace, + Module: GLWEExternalProduct, { module.external_product_inplace(self, rhs, scratch); } } -impl GLWEExternalProductInplace for Module +pub trait GLWEExternalProduct where - Module: VecZnxDftAllocBytes + Self: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply @@ -107,12 +104,12 @@ where + VecZnxNormalize, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) + fn glwe_external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) where - R: GLWECiphertextToMut, + R: GLWEToMut, D: GGSWCiphertextPreparedToRef, { - 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 @@ where 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 @@ where ); } } -} -impl GLWEExternalProduct for Module -where - Module: VecZnxDftAllocBytes - + VmpApplyDftToDftTmpBytes - + VecZnxNormalizeTmpBytes - + VecZnxDftApply - + VmpApplyDftToDft - + VmpApplyDftToDftAdd - + VecZnxIdftApplyConsume - + VecZnxBigNormalize - + VecZnxNormalize, - Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, -{ - fn external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) + fn glwe_external_product(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch) where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, + R: GLWEToMut, + A: GLWEToRef, D: GGSWCiphertextPreparedToRef, { - 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 @@ where 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 @@ where }); } } + +impl GLWEExternalProduct for Module +where + Self: VecZnxDftAllocBytes + + VmpApplyDftToDftTmpBytes + + VecZnxNormalizeTmpBytes + + VecZnxDftApply + + VmpApplyDftToDft + + VmpApplyDftToDftAdd + + VecZnxIdftApplyConsume + + VecZnxBigNormalize + + VecZnxNormalize, + Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx, +{ +} diff --git a/poulpy-core/src/external_product/mod.rs b/poulpy-core/src/external_product/mod.rs index f46b82c..7e6c5ae 100644 --- a/poulpy-core/src/external_product/mod.rs +++ b/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 { - fn external_product(&self, res: &mut R, a: &A, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - A: GLWECiphertextToRef, - D: GGSWCiphertextPreparedToRef; -} - -pub trait GLWEExternalProductInplace { - fn external_product_inplace(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch) - where - R: GLWECiphertextToMut, - D: GGSWCiphertextPreparedToRef; -} diff --git a/poulpy-core/src/glwe_packing.rs b/poulpy-core/src/glwe_packing.rs index 927e098..07b61a4 100644 --- a/poulpy-core/src/glwe_packing.rs +++ b/poulpy-core/src/glwe_packing.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ use crate::{ GLWEOperations, TakeGLWECt, - layouts::{GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, + layouts::{GGLWEInfos, GLWE, GLWEInfos, LWEInfos, prepared::AutomorphismKeyPrepared}, }; /// [GLWEPacker] enables only the fly GLWE packing @@ -29,7 +29,7 @@ pub struct GLWEPacker { /// [Accumulator] stores intermediate packing result. /// There are Log(N) such accumulators in a [GLWEPacker]. struct Accumulator { - data: GLWECiphertext>, + data: GLWE>, value: bool, // Implicit flag for zero ciphertext control: bool, // Can be combined with incoming value } @@ -48,7 +48,7 @@ impl Accumulator { A: GLWEInfos, { Self { - data: GLWECiphertext::alloc(infos), + data: GLWE::alloc_from_infos(infos), value: false, control: false, } @@ -100,7 +100,7 @@ impl GLWEPacker { } pub fn galois_elements(module: &Module) -> Vec { - GLWECiphertext::trace_galois_elements(module) + GLWE::trace_galois_elements(module) } /// Adds a GLWE ciphertext to the [GLWEPacker]. @@ -115,7 +115,7 @@ impl GLWEPacker { pub fn add( &mut self, module: &Module, - a: Option<&GLWECiphertext>, + a: Option<&GLWE>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -162,7 +162,7 @@ impl GLWEPacker { } /// Flush result to`res`. - pub fn flush(&mut self, module: &Module, res: &mut GLWECiphertext) + pub fn flush(&mut self, module: &Module, res: &mut GLWE) where Module: VecZnxCopy, { @@ -188,7 +188,7 @@ where fn pack_core( module: &Module, - a: Option<&GLWECiphertext>, + a: Option<&GLWE>, accumulators: &mut [Accumulator], i: usize, auto_keys: &HashMap>, @@ -258,7 +258,7 @@ fn pack_core( } else { pack_core( module, - None::<&GLWECiphertext>>, + None::<&GLWE>>, acc_next, i + 1, auto_keys, @@ -274,16 +274,15 @@ where KEY: GGLWEInfos, Module: 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( module: &Module, acc: &mut Accumulator, - b: Option<&GLWECiphertext>, + b: Option<&GLWE>, i: usize, auto_keys: &HashMap>, scratch: &mut Scratch, @@ -314,7 +313,7 @@ fn combine( Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECt, { let log_n: usize = acc.data.n().log2(); - let a: &mut GLWECiphertext> = &mut acc.data; + let a: &mut GLWE> = &mut acc.data; let gal_el: i64 = if i == 0 { -1 @@ -395,7 +394,7 @@ fn combine( /// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)] pub fn glwe_packing( module: &Module, - cts: &mut HashMap>, + cts: &mut HashMap>, log_gap_out: usize, auto_keys: &HashMap>, scratch: &mut Scratch, @@ -446,8 +445,8 @@ pub fn glwe_packing( }; (0..t).for_each(|j| { - let mut a: Option<&mut GLWECiphertext> = cts.remove(&j); - let mut b: Option<&mut GLWECiphertext> = cts.remove(&(j + t)); + let mut a: Option<&mut GLWE> = cts.remove(&j); + let mut b: Option<&mut GLWE> = cts.remove(&(j + t)); pack_internal(module, &mut a, &mut b, i, auto_key, scratch); @@ -463,8 +462,8 @@ pub fn glwe_packing( #[allow(clippy::too_many_arguments)] fn pack_internal( module: &Module, - a: &mut Option<&mut GLWECiphertext>, - b: &mut Option<&mut GLWECiphertext>, + a: &mut Option<&mut GLWE>, + b: &mut Option<&mut GLWE>, i: usize, auto_key: &AutomorphismKeyPrepared, scratch: &mut Scratch, diff --git a/poulpy-core/src/glwe_trace.rs b/poulpy-core/src/glwe_trace.rs index 397a7e1..85f0db3 100644 --- a/poulpy-core/src/glwe_trace.rs +++ b/poulpy-core/src/glwe_trace.rs @@ -11,11 +11,11 @@ use poulpy_hal::{ 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> { +impl GLWE> { pub fn trace_galois_elements(module: &Module) -> Vec { let mut gal_els: Vec = Vec::new(); (0..module.log_n()).for_each(|i| { @@ -42,7 +42,7 @@ impl GLWECiphertext> { { 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 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { pub fn trace( &mut self, module: &Module, start: usize, end: usize, - lhs: &GLWECiphertext, + lhs: &GLWE, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -135,7 +135,7 @@ impl GLWECiphertext { } 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(), diff --git a/poulpy-core/src/keyswitching/gglwe_ct.rs b/poulpy-core/src/keyswitching/gglwe_ct.rs index ee86cbc..589c391 100644 --- a/poulpy-core/src/keyswitching/gglwe_ct.rs +++ b/poulpy-core/src/keyswitching/gglwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - AutomorphismKey, GGLWEInfos, GLWECiphertext, GLWEInfos, GLWESwitchingKey, + AutomorphismKey, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, prepared::{AutomorphismKeyPrepared, GLWESwitchingKeyPrepared}, }; @@ -98,7 +98,7 @@ impl GLWESwitchingKey> { KEY: GGLWEInfos, Module: 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(module: &Module, out_infos: &OUT, key_apply: &KEY) -> usize @@ -107,7 +107,7 @@ impl GLWESwitchingKey> { KEY: GGLWEInfos + GLWEInfos, Module: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes, { - GLWECiphertext::keyswitch_inplace_scratch_space(module, out_infos, key_apply) + GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply) } } diff --git a/poulpy-core/src/keyswitching/ggsw_ct.rs b/poulpy-core/src/keyswitching/ggsw_ct.rs index 3ec738e..eea54a2 100644 --- a/poulpy-core/src/keyswitching/ggsw_ct.rs +++ b/poulpy-core/src/keyswitching/ggsw_ct.rs @@ -10,7 +10,7 @@ use poulpy_hal::{ 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 @@ impl GGSW> { .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 @@ impl GGSW> { (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 @@ impl GGSW> { 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, diff --git a/poulpy-core/src/keyswitching/glwe_ct.rs b/poulpy-core/src/keyswitching/glwe_ct.rs index e7afcbe..f785016 100644 --- a/poulpy-core/src/keyswitching/glwe_ct.rs +++ b/poulpy-core/src/keyswitching/glwe_ct.rs @@ -7,9 +7,9 @@ use poulpy_hal::{ 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> { +impl GLWE> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -28,8 +28,8 @@ impl GLWECiphertext> { .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 @@ impl GLWECiphertext> { (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 @@ impl GLWECiphertext> { } } -impl GLWECiphertext { +impl GLWE { #[allow(dead_code)] pub(crate) fn assert_keyswitch( &self, module: &Module, - lhs: &GLWECiphertext, + lhs: &GLWE, rhs: &GLWESwitchingKeyPrepared, scratch: &Scratch, ) where @@ -93,7 +93,7 @@ impl GLWECiphertext { 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 @@ impl GLWECiphertext { 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 GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub fn keyswitch( &mut self, module: &Module, - glwe_in: &GLWECiphertext, + glwe_in: &GLWE, rhs: &GLWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -232,7 +232,7 @@ impl GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub(crate) fn keyswitch_internal( &self, module: &Module, diff --git a/poulpy-core/src/keyswitching/lwe_ct.rs b/poulpy-core/src/keyswitching/lwe_ct.rs index 7d9e08e..dd8c552 100644 --- a/poulpy-core/src/keyswitching/lwe_ct.rs +++ b/poulpy-core/src/keyswitching/lwe_ct.rs @@ -9,13 +9,10 @@ use poulpy_hal::{ 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> { +impl LWE> { pub fn keyswitch_scratch_space( module: &Module, out_infos: &OUT, @@ -40,33 +37,33 @@ impl LWECiphertext> { { 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 LWECiphertext { +impl LWE { pub fn keyswitch( &mut self, module: &Module, - a: &LWECiphertext, + a: &LWE, ksk: &LWESwitchingKeyPrepared, scratch: &mut Scratch, ) where @@ -90,14 +87,14 @@ impl LWECiphertext { { 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 @@ impl LWECiphertext { }); 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, diff --git a/poulpy-core/src/layouts/compressed/gglwe_atk.rs b/poulpy-core/src/layouts/compressed/gglwe_atk.rs index a62e4a7..29cf49b 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_atk.rs +++ b/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 @@ impl fmt::Display for AutomorphismKeyCompressed { } } +pub trait AutomorphismKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_automorphism_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> AutomorphismKeyCompressed> { + AutomorphismKeyCompressed { + key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_compressed_from_infos(&self, infos: &A) -> AutomorphismKeyCompressed> + 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(&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> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: Module, infos: &A) -> Self where A: GGLWEInfos, + Module: 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(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: AutomorphismKeyCompressedAlloc, + { + module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize where A: GGLWEInfos, + Module: 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( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: AutomorphismKeyCompressedAlloc, + { + module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize) } } @@ -122,13 +190,32 @@ impl WriterTo for AutomorphismKeyCompressed { } } -impl Decompress> for AutomorphismKey +pub trait AutomorphismKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &AutomorphismKeyCompressed) { - self.key.decompress(module, &other.key); - self.p = other.p; + fn decompress_automorphism_key(&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 AutomorphismKeyDecompress for Module where Self: AutomorphismKeyDecompress {} + +impl AutomorphismKey +where + Self: SetAutomorphismGaloisElement, +{ + pub fn decompressed(&mut self, module: &Module, other: &O) + where + O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement, + Module: AutomorphismKeyDecompress, + { + module.decompress_automorphism_key(self, other); } } diff --git a/poulpy-core/src/layouts/compressed/gglwe_ct.rs b/poulpy-core/src/layouts/compressed/gglwe_ct.rs index a75a9a8..51bb1fb 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/gglwe_ct.rs @@ -7,14 +7,14 @@ use poulpy_hal::{ }; 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 { +pub struct GGLWECompressed { pub(crate) data: MatZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -23,7 +23,7 @@ pub struct GGLWECiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGLWECiphertextCompressed { +impl LWEInfos for GGLWECompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -40,13 +40,13 @@ impl LWEInfos for GGLWECiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGLWECiphertextCompressed { +impl GLWEInfos for GGLWECompressed { fn rank(&self) -> Rank { self.rank_out() } } -impl GGLWEInfos for GGLWECiphertextCompressed { +impl GGLWEInfos for GGLWECompressed { fn rank_in(&self) -> Rank { Rank(self.data.cols_in() as u32) } @@ -64,53 +64,41 @@ impl GGLWEInfos for GGLWECiphertextCompressed { } } -impl fmt::Debug for GGLWECiphertextCompressed { +impl fmt::Debug for GGLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl FillUniform for GGLWECiphertextCompressed { +impl FillUniform for GGLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl fmt::Display for GGLWECiphertextCompressed { +impl fmt::Display for GGLWECompressed { 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> { - pub fn alloc(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> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -125,9 +113,9 @@ impl GGLWECiphertextCompressed> { dsize.0, ); - Self { + GGLWECompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), rank_in.into(), 1, @@ -141,21 +129,22 @@ impl GGLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_gglwe_compressed_from_infos(&self, infos: &A) -> GGLWECompressed> 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 @@ impl GGLWECiphertextCompressed> { 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(&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 GGLWECiphertextCompressed { - pub(crate) fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGLWECompressedAlloc for Module where Self: GetDegree {} + +impl GGLWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GGLWECompressedAlloc, + { + module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed_from_infos(infos) + } + + pub fn byte_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGLWECompressedAlloc, + { + module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize) + } +} + +impl GGLWECompressed { + 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 GGLWECiphertextCompressed { } } -impl GGLWECiphertextCompressed { - pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGLWECompressed { + 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 GGLWECiphertextCompressed { } } -impl ReaderFrom for GGLWECiphertextCompressed { +impl ReaderFrom for GGLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -221,7 +273,7 @@ impl ReaderFrom for GGLWECiphertextCompressed { } } -impl WriterTo for GGLWECiphertextCompressed { +impl WriterTo for GGLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -235,70 +287,50 @@ impl WriterTo for GGLWECiphertextCompressed { } } -impl Decompress> for GGLWE +pub trait GGLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGLWECiphertextCompressed) { - #[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(&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!( - self.dnum(), - other.dnum(), - "invalid receiver: self.dnum()={} != other.dnum()={}", - self.dnum(), - other.dnum() - ); + assert_eq!(res.gglwe_layout(), other.gglwe_layout()); + + let rank_in: usize = res.rank_in().into(); + let dnum: usize = res.dnum().into(); + + 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(); - - (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)); - }); - }); } } -pub trait GGLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]>; +impl GGLWEDecompress for Module where Self: VecZnxFillUniform + VecZnxCopy {} + +impl GGLWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GGLWECompressedToRef, + Module: GGLWEDecompress, + { + module.decompress_gglwe(self, other); + } } -impl GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed { - fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> { - GGLWECiphertextCompressed { +pub trait GGLWECompressedToMut { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]>; +} + +impl GGLWECompressedToMut for GGLWECompressed { + fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -309,13 +341,13 @@ impl GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed } } -pub trait GGLWECiphertextCompressedToRef { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>; +pub trait GGLWECompressedToRef { + fn to_ref(&self) -> GGLWECompressed<&[u8]>; } -impl GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed { - fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> { - GGLWECiphertextCompressed { +impl GGLWECompressedToRef for GGLWECompressed { + fn to_ref(&self) -> GGLWECompressed<&[u8]> { + GGLWECompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), diff --git a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs b/poulpy-core/src/layouts/compressed/gglwe_ksk.rs index 76d60a7..450c5bc 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_ksk.rs +++ b/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 { - pub(crate) key: GGLWECiphertextCompressed, + pub(crate) key: GGLWECompressed, pub(crate) sk_in_n: usize, // Degree of sk_in pub(crate) sk_out_n: usize, // Degree of sk_out } @@ -81,43 +81,103 @@ impl fmt::Display for GLWESwitchingKeyCompressed { } } -impl GLWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +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, - ) -> Self { + ) -> GLWESwitchingKeyCompressed> { GLWESwitchingKeyCompressed { - key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> GLWESwitchingKeyCompressed> where A: GGLWEInfos, { - GGLWECiphertextCompressed::alloc_bytes(infos) + self.alloc_glwe_switching_key_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 { - GGLWECiphertextCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, 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(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + self.bytes_of_gglwe_compressed_from_infos(infos) + } +} + +impl GLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize) } } @@ -137,14 +197,31 @@ impl WriterTo for GLWESwitchingKeyCompressed { } } -impl Decompress> for GLWESwitchingKey +pub trait GLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GGLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GLWESwitchingKeyCompressed) { - self.key.decompress(module, &other.key); - self.sk_in_n = other.sk_in_n; - self.sk_out_n = other.sk_out_n; + fn decompress_glwe_switching_key(&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 GLWESwitchingKeyDecompress for Module where Self: GGLWEDecompress {} + +impl GLWESwitchingKey { + pub fn decompress(&mut self, module: Module, other: &O) + where + O: GLWESwitchingKeyCompressedToRef, + Module: GGLWEDecompress, + { + module.decompress_glwe_switching_key(self, other); } } @@ -154,7 +231,7 @@ pub trait GLWESwitchingKeyCompressedToMut { impl GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed where - GGLWECiphertextCompressed: GGLWECiphertextCompressedToMut, + GGLWECompressed: GGLWECompressedToMut, { fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> { GLWESwitchingKeyCompressed { @@ -171,7 +248,7 @@ pub trait GLWESwitchingKeyCompressedToRef { impl GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed where - GGLWECiphertextCompressed: GGLWECiphertextCompressedToRef, + GGLWECompressed: GGLWECompressedToRef, { fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> { GLWESwitchingKeyCompressed { diff --git a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs b/poulpy-core/src/layouts/compressed/gglwe_tsk.rs index 7255594..4c51b6a 100644 --- a/poulpy-core/src/layouts/compressed/gglwe_tsk.rs +++ b/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 @@ impl fmt::Display for TensorKeyCompressed { } } -impl TensorKeyCompressed> { - pub fn alloc(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> { + 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(&self, infos: &A) -> TensorKeyCompressed> where A: GGLWEInfos, { @@ -90,58 +111,70 @@ impl TensorKeyCompressed> { 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>> = 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(infos: &A) -> usize + fn tensor_key_compressed_bytes_of_from_infos(&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(), + ) + } +} + +impl TensorKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc_from_infos(infos) } - 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) + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_alloc(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: TensorKeyCompressedAlloc, + { + module.tensor_key_compressed_bytes_of(base2k, k, rank, dnum, dsize) } } @@ -181,28 +214,41 @@ impl TensorKeyCompressed { } } -impl Decompress> for TensorKey +pub trait TensorKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &TensorKeyCompressed) { - #[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(&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(); - self.keys - .iter_mut() - .zip(other.keys.iter()) - .for_each(|(a, b)| { - a.decompress(module, b); - }); + 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); + } + } +} + +impl TensorKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl TensorKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: TensorKeyCompressedToRef, + Module: GLWESwitchingKeyDecompress, + { + module.decompress_tensor_key(self, other); } } diff --git a/poulpy-core/src/layouts/compressed/ggsw_ct.rs b/poulpy-core/src/layouts/compressed/ggsw_ct.rs index 3d15e3e..d417385 100644 --- a/poulpy-core/src/layouts/compressed/ggsw_ct.rs +++ b/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 @@ use poulpy_hal::{ }; 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 { +pub struct GGSWCompressed { pub(crate) data: MatZnx, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, @@ -23,7 +22,7 @@ pub struct GGSWCiphertextCompressed { pub(crate) seed: Vec<[u8; 32]>, } -impl LWEInfos for GGSWCiphertextCompressed { +impl LWEInfos for GGSWCompressed { fn n(&self) -> Degree { Degree(self.data.n() as u32) } @@ -39,13 +38,13 @@ impl LWEInfos for GGSWCiphertextCompressed { self.data.size() } } -impl GLWEInfos for GGSWCiphertextCompressed { +impl GLWEInfos for GGSWCompressed { fn rank(&self) -> Rank { self.rank } } -impl GGSWInfos for GGSWCiphertextCompressed { +impl GGSWInfos for GGSWCompressed { fn dsize(&self) -> Dsize { self.dsize } @@ -55,46 +54,42 @@ impl GGSWInfos for GGSWCiphertextCompressed { } } -impl fmt::Debug for GGSWCiphertextCompressed { +impl fmt::Debug for GGSWCompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.data) } } -impl fmt::Display for GGSWCiphertextCompressed { +impl fmt::Display for GGSWCompressed { 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 FillUniform for GGSWCiphertextCompressed { +impl FillUniform for GGSWCompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GGSWCiphertextCompressed> { - pub fn alloc(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> { 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 @@ impl GGSWCiphertextCompressed> { dsize.0, ); - Self { + GGSWCompressed { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), 1, @@ -123,12 +118,11 @@ impl GGSWCiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_compressed_from_infos(&self, infos: &A) -> GGSWCompressed> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw_compressed( infos.base2k(), infos.k(), infos.rank(), @@ -137,9 +131,9 @@ impl GGSWCiphertextCompressed> { ) } - 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 @@ impl GGSWCiphertextCompressed> { 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(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw_compressed( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } } -impl GGSWCiphertextCompressed { - pub fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> { +impl GGSWCompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGSWInfos, + Module: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: GGSWCompressedAlloc, + { + module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGSWInfos, + Module: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed_key_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGSWCompressedAlloc, + { + module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize) + } +} + +impl GGSWCompressed { + 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 GGSWCiphertextCompressed { } } -impl GGSWCiphertextCompressed { - pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> { +impl GGSWCompressed { + 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 GGSWCiphertextCompressed { } } -impl ReaderFrom for GGSWCiphertextCompressed { +impl ReaderFrom for GGSWCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -203,7 +249,7 @@ impl ReaderFrom for GGSWCiphertextCompressed { } } -impl WriterTo for GGSWCiphertextCompressed { +impl WriterTo for GGSWCompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -217,34 +263,49 @@ impl WriterTo for GGSWCiphertextCompressed { } } -impl Decompress> for GGSW +pub trait GGSWDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWEDecompress, { - fn decompress(&mut self, module: &Module, other: &GGSWCiphertextCompressed) { - #[cfg(debug_assertions)] - { - assert_eq!(self.rank(), other.rank()) - } + fn decompress_ggsw(&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(); - 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)); - }); - }); + 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)); + } + } } } -pub trait GGSWCiphertextCompressedToMut { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]>; +impl GGSWDecompress for Module where Self: GGSWDecompress {} + +impl GGSW { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GGSWCompressedToRef, + Module: GGSWDecompress, + { + module.decompress_ggsw(self, other); + } } -impl GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed { - fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> { - GGSWCiphertextCompressed { +pub trait GGSWCompressedToMut { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]>; +} + +impl GGSWCompressedToMut for GGSWCompressed { + fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), @@ -255,13 +316,13 @@ impl GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed { } } -pub trait GGSWCiphertextCompressedToRef { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>; +pub trait GGSWCompressedToRef { + fn to_ref(&self) -> GGSWCompressed<&[u8]>; } -impl GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed { - fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> { - GGSWCiphertextCompressed { +impl GGSWCompressedToRef for GGSWCompressed { + fn to_ref(&self) -> GGSWCompressed<&[u8]> { + GGSWCompressed { k: self.k(), base2k: self.base2k(), dsize: self.dsize(), diff --git a/poulpy-core/src/layouts/compressed/glwe_ct.rs b/poulpy-core/src/layouts/compressed/glwe_ct.rs index 93cb829..1ee976a 100644 --- a/poulpy-core/src/layouts/compressed/glwe_ct.rs +++ b/poulpy-core/src/layouts/compressed/glwe_ct.rs @@ -6,12 +6,12 @@ use poulpy_hal::{ 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 { +pub struct GLWECompressed { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, @@ -19,7 +19,7 @@ pub struct GLWECiphertextCompressed { pub(crate) seed: [u8; 32], } -impl LWEInfos for GLWECiphertextCompressed { +impl LWEInfos for GLWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -36,23 +36,23 @@ impl LWEInfos for GLWECiphertextCompressed { Degree(self.data.n() as u32) } } -impl GLWEInfos for GLWECiphertextCompressed { +impl GLWEInfos for GLWECompressed { fn rank(&self) -> Rank { self.rank } } -impl fmt::Debug for GLWECiphertextCompressed { +impl fmt::Debug for GLWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertextCompressed { +impl fmt::Display for GLWECompressed { 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 fmt::Display for GLWECiphertextCompressed { } } -impl FillUniform for GLWECiphertextCompressed { +impl FillUniform for GLWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertextCompressed> { - pub fn alloc(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> { + GLWECompressed { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, rank, @@ -86,19 +82,62 @@ impl GLWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_compressed_from_infos(&self, infos: &A) -> GLWECompressed> 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(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + assert_eq!(self.n(), infos.n()); + self.bytes_of_glwe_compressed(infos.base2k(), infos.k()) } } -impl ReaderFrom for GLWECiphertextCompressed { +impl GLWECompressedAlloc for Module where Self: GetDegree {} + +impl GLWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWECompressedAlloc, + { + module.alloc_glwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWECompressedAlloc, + { + module.alloc_glwe_compressed(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: GLWECompressedAlloc, + { + module.bytes_of_glwe_compressed(base2k, k) + } +} + +impl ReaderFrom for GLWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -108,7 +147,7 @@ impl ReaderFrom for GLWECiphertextCompressed { } } -impl WriterTo for GLWECiphertextCompressed { +impl WriterTo for GLWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -118,74 +157,61 @@ impl WriterTo for GLWECiphertextCompressed { } } -impl Decompress> for GLWECiphertext +pub trait GLWEDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GetDegree + VecZnxFillUniform + VecZnxCopy, { - fn decompress(&mut self, module: &Module, other: &GLWECiphertextCompressed) { - #[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(), - other.rank(), - "invalid receiver: self.rank()={} != other.rank()={}", - self.rank(), - other.rank() - ); - } - - let mut source: Source = Source::new(other.seed); - self.decompress_internal(module, other, &mut source); - } -} - -impl GLWECiphertext { - pub(crate) fn decompress_internal( - &mut self, - module: &Module, - other: &GLWECiphertextCompressed, - source: &mut Source, - ) where - DataOther: DataRef, - Module: VecZnxCopy + VecZnxFillUniform, + fn decompress_glwe(&self, res: &mut R, other: &O) + where + R: GLWEToMut + SetGLWEInfos, + O: GLWECompressedToRef + GLWEInfos, { - #[cfg(debug_assertions)] { - assert_eq!(self.rank(), other.rank()); - debug_assert_eq!(self.size(), other.size()); + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + let other: &GLWECompressed<&[u8]> = &other.to_ref(); + assert_eq!( + res.n(), + self.n(), + "invalid receiver: res.n()={} != other.n()={}", + res.n(), + self.n() + ); + + 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); + }); } - 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; + res.set_base2k(other.base2k()); + res.set_k(other.k()); } } -pub trait GLWECiphertextCompressedToRef { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]>; +impl GLWEDecompress for Module where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {} + +impl GLWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GLWECompressedToRef + GLWEInfos, + Module: GLWEDecompress, + { + module.decompress_glwe(self, other); + } } -impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { - fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> { - GLWECiphertextCompressed { +pub trait GLWECompressedToRef { + fn to_ref(&self) -> GLWECompressed<&[u8]>; +} + +impl GLWECompressedToRef for GLWECompressed { + fn to_ref(&self) -> GLWECompressed<&[u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k, @@ -195,13 +221,13 @@ impl GLWECiphertextCompressedToRef for GLWECiphertextCompressed { } } -pub trait GLWECiphertextCompressedToMut { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>; +pub trait GLWECompressedToMut { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]>; } -impl GLWECiphertextCompressedToMut for GLWECiphertextCompressed { - fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> { - GLWECiphertextCompressed { +impl GLWECompressedToMut for GLWECompressed { + fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> { + GLWECompressed { seed: self.seed.clone(), base2k: self.base2k, k: self.k, diff --git a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs index c3969b2..9f038a3 100644 --- a/poulpy-core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/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 @@ impl WriterTo for GLWEToLWESwitchingKeyCompressed { } } -impl GLWEToLWESwitchingKeyCompressed> { - pub fn alloc(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> { + 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(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed> 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(infos: &A) -> usize + fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(&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) - } - - 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)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) + } +} + +impl GLWEToLWESwitchingKeyCompressedAlloc for Module where Self: GLWESwitchingKeyCompressedAlloc {} + +impl GLWEToLWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize + where + Module: 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(&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 GLWEToLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl GLWEToLWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: GLWEToLWESwitchingKeyCompressedToRef, + Module: GLWEToLWESwitchingKeyDecompress, + { + module.decompress_glwe_to_lwe_switching_key(self, other); + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait GLWEToLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> { + GLWEToLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ct.rs b/poulpy-core/src/layouts/compressed/lwe_ct.rs index e11b3f3..2696784 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ct.rs +++ b/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 { +pub struct LWECompressed { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, pub(crate) seed: [u8; 32], } -impl LWEInfos for LWECiphertextCompressed { +impl LWEInfos for LWECompressed { fn base2k(&self) -> Base2K { self.base2k } @@ -34,17 +37,17 @@ impl LWEInfos for LWECiphertextCompressed { } } -impl fmt::Debug for LWECiphertextCompressed { +impl fmt::Debug for LWECompressed { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertextCompressed { +impl fmt::Display for LWECompressed { 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,22 +56,15 @@ impl fmt::Display for LWECiphertextCompressed { } } -impl FillUniform for LWECiphertextCompressed { +impl FillUniform for LWECompressed { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl LWECiphertextCompressed> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWECompressedAlloc { + fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed> { + LWECompressed { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, @@ -76,21 +72,60 @@ impl LWECiphertextCompressed> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_compressed_from_infos(&self, infos: &A) -> LWECompressed> where A: LWEInfos, { - Self::alloc_bytes_with(infos.base2k(), infos.k()) + self.alloc_lwe_compressed(infos.base2k(), infos.k()) } - pub fn alloc_bytes_with(base2k: Base2K, k: TorusPrecision) -> usize { - Zn::alloc_bytes(1, 1, k.0.div_ceil(base2k.0) as usize) + 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(&self, infos: &A) -> usize + where + A: LWEInfos, + { + self.bytes_of_lwe_compressed(infos.base2k(), infos.k()) + } +} + +impl LWECompressed> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: LWEInfos, + Module: LWECompressedAlloc, + { + module.alloc_lwe_compressed_from_infos(infos) + } + + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: LWECompressedAlloc, + { + module.alloc_lwe_compressed(base2k, k) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: LWEInfos, + Module: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: LWECompressedAlloc, + { + module.bytes_of_lwe_compressed(base2k, k) } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -impl ReaderFrom for LWECiphertextCompressed { +impl ReaderFrom for LWECompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -99,7 +134,7 @@ impl ReaderFrom for LWECiphertextCompressed { } } -impl WriterTo for LWECiphertextCompressed { +impl WriterTo for LWECompressed { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; @@ -108,22 +143,72 @@ impl WriterTo for LWECiphertextCompressed { } } -impl Decompress> for LWECiphertext +pub trait LWEDecompress where - Module: ZnFillUniform, + Self: ZnFillUniform, { - fn decompress(&mut self, module: &Module, other: &LWECiphertextCompressed) { - debug_assert_eq!(self.size(), other.size()); + fn decompress_lwe(&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 LWEDecompress for Module where Self: ZnFillUniform {} + +impl LWE { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWECompressedToRef, + Module: LWEDecompress, + { + module.decompress_lwe(self, other); + } +} + +pub trait LWECompressedToRef { + fn to_ref(&self) -> LWECompressed<&[u8]>; +} + +impl LWECompressedToRef for LWECompressed { + 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 LWECompressedToMut for LWECompressed { + fn to_mut(&mut self) -> LWECompressed<&mut [u8]> { + LWECompressed { + k: self.k, + base2k: self.base2k, + seed: self.seed, + data: self.data.to_mut(), + } } } diff --git a/poulpy-core/src/layouts/compressed/lwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_ksk.rs index d7ea93b..ba987ab 100644 --- a/poulpy-core/src/layouts/compressed/lwe_ksk.rs +++ b/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 @@ impl WriterTo for LWESwitchingKeyCompressed { } } -impl LWESwitchingKeyCompressed> { - pub fn alloc(infos: &A) -> Self +pub trait LWESwitchingKeyCompressedAlloc +where + Self: GLWESwitchingKeyCompressedAlloc, +{ + fn alloc_lwe_switching_key_compressed( + &self, + base2k: Base2K, + k: TorusPrecision, + dnum: Dnum, + ) -> LWESwitchingKeyCompressed> { + LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_compressed_from_infos(&self, infos: &A) -> LWESwitchingKeyCompressed> 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(infos: &A) -> usize + fn bytes_of_lwe_switching_key_compressed_from_infos(&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) - } - - 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)) + self.bytes_of_glwe_switching_key_compressed_from_infos(infos) } } -impl Decompress> for LWESwitchingKey +impl LWESwitchingKeyCompressed> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module: LWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_switching_key_compressed(base2k, k, dnum) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_switching_key(&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 LWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWESwitchingKeyCompressedToRef, + Module: LWESwitchingKeyDecompress, + { + module.decompress_lwe_switching_key(self, other); + } +} + +pub trait LWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>; +} + +impl LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> { + LWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> { + LWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs index c1000ff..ce4e889 100644 --- a/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/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 @@ impl WriterTo for LWEToGLWESwitchingKeyCompressed { } } -impl LWEToGLWESwitchingKeyCompressed> { - pub fn alloc(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> { + 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(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed> 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(infos: &A) -> usize + fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { - debug_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" + "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed" ); - GLWESwitchingKeyCompressed::alloc_bytes(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)) + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed" + ); + self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum()) } } -impl Decompress> for LWEToGLWESwitchingKey +impl LWEToGLWESwitchingKeyCompressed> { + pub fn alloc(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos) + } + + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWEToGLWESwitchingKeyCompressedAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum) + } +} + +pub trait LWEToGLWESwitchingKeyDecompress where - Module: VecZnxFillUniform + VecZnxCopy, + Self: GLWESwitchingKeyDecompress, { - fn decompress(&mut self, module: &Module, other: &LWEToGLWESwitchingKeyCompressed) { - self.0.decompress(module, &other.0); + fn decompress_lwe_to_glwe_switching_key(&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 LWEToGLWESwitchingKeyDecompress for Module where Self: GLWESwitchingKeyDecompress {} + +impl LWEToGLWESwitchingKey { + pub fn decompress(&mut self, module: &Module, other: &O) + where + O: LWEToGLWESwitchingKeyCompressedToRef, + Module: LWEToGLWESwitchingKeyDecompress, + { + module.decompress_lwe_to_glwe_switching_key(self, other); + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToRef { + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToRef, +{ + fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_ref()) + } +} + +pub trait LWEToGLWESwitchingKeyCompressedToMut { + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>; +} + +impl LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed +where + GLWESwitchingKeyCompressed: GLWESwitchingKeyCompressedToMut, +{ + fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> { + LWEToGLWESwitchingKeyCompressed(self.0.to_mut()) } } diff --git a/poulpy-core/src/layouts/compressed/mod.rs b/poulpy-core/src/layouts/compressed/mod.rs index c1fcacf..cd7c459 100644 --- a/poulpy-core/src/layouts/compressed/mod.rs +++ b/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 { - fn decompress(&mut self, module: &Module, other: &C); -} diff --git a/poulpy-core/src/layouts/gglwe_atk.rs b/poulpy-core/src/layouts/gglwe_atk.rs index f9244e1..c197828 100644 --- a/poulpy-core/src/layouts/gglwe_atk.rs +++ b/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 @@ impl fmt::Display for AutomorphismKey { } } +impl AutomorphismKeyAlloc for Module 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> { + AutomorphismKey { + key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize), + p: 0, + } + } + + fn alloc_automorphism_key_from_infos(&self, infos: &A) -> AutomorphismKey> + 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) + } + + fn bytes_of_automorphism_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in(), + infos.rank_out(), + "rank_in != rank_out is not supported for AutomorphismKey" + ); + self.bytes_of_automorphism_key( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + impl AutomorphismKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: AutomorphismKeyAlloc, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - AutomorphismKey { - key: GLWESwitchingKey::alloc(infos), - p: 0, - } + module.alloc_automorphism_key_from_infos(infos) } - 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, - } + pub fn alloc_with( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: AutomorphismKeyAlloc, + { + module.alloc_automorphism_key(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: AutomorphismKeyAlloc, { - assert_eq!( - infos.rank_in(), - infos.rank_out(), - "rank_in != rank_out is not supported for GGLWEAutomorphismKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_automorphism_key_from_infos(infos) } - 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) + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: AutomorphismKeyAlloc, + { + module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize) } } @@ -204,13 +266,13 @@ where } impl AutomorphismKey { - 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 AutomorphismKey { - 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) } } diff --git a/poulpy-core/src/layouts/gglwe_ct.rs b/poulpy-core/src/layouts/gglwe_ct.rs index 509e46e..97508c2 100644 --- a/poulpy-core/src/layouts/gglwe_ct.rs +++ b/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 @@ where 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 fmt::Display for GGLWE { } impl GGLWE { - 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 GGLWE { } impl GGLWE { - 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 { } } -impl GGLWE> { - pub fn alloc(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> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -228,9 +218,9 @@ impl GGLWE> { 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 @@ impl GGLWE> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GGLWE> where A: GGLWEInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_gglwe( infos.base2k(), infos.k(), infos.rank_in(), @@ -257,8 +246,8 @@ impl GGLWE> { ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_gglwe( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -280,21 +269,85 @@ impl GGLWE> { 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(&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 GGLWEAlloc for Module where Self: GetDegree {} + +impl GGLWE> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GGLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GGLWEAlloc, + { + module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GGLWEAlloc, + { + module.bytes_of_gglwe_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: 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 GGLWECiphertextToMut for GGLWE { +impl GGLWEToMut for GGLWE { fn to_mut(&mut self) -> GGLWE<&mut [u8]> { GGLWE { k: self.k(), diff --git a/poulpy-core/src/layouts/gglwe_ksk.rs b/poulpy-core/src/layouts/gglwe_ksk.rs index 7b74193..c818d84 100644 --- a/poulpy-core/src/layouts/gglwe_ksk.rs +++ b/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,43 +163,42 @@ impl FillUniform for GLWESwitchingKey { } } -impl GLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self - where - A: GGLWEInfos, - { - GLWESwitchingKey { - key: GGLWE::alloc(infos), - sk_in_n: 0, - sk_out_n: 0, - } - } - - pub fn alloc_with( - n: Degree, +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, - ) -> Self { + ) -> GLWESwitchingKey> { GLWESwitchingKey { - key: GGLWE::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize), + key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize), sk_in_n: 0, sk_out_n: 0, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_switching_key_from_infos(&self, infos: &A) -> GLWESwitchingKey> where A: GGLWEInfos, { - GGLWE::alloc_bytes(infos) + self.alloc_glwe_switching_key( + infos.base2k(), + infos.k(), + infos.rank_in(), + infos.rank_out(), + infos.dnum(), + infos.dsize(), + ) } - pub fn alloc_bytes_with( - n: Degree, + fn bytes_of_glwe_switching_key( + &self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, @@ -207,7 +206,69 @@ impl GLWESwitchingKey> { dnum: Dnum, dsize: Dsize, ) -> usize { - GGLWE::alloc_bytes_with(n, base2k, k, rank_in, rank_out, dnum, dsize) + self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize) + } + + fn bytes_of_glwe_switching_key_from_infos(&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> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key_from_infos(infos) + } + + pub fn alloc( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> Self + where + Module: GLWESwitchingKeyAlloc, + { + module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key_from_infos(infos) + } + + pub fn bytes_of( + module: &Module, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + rank_out: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize) } } @@ -217,7 +278,7 @@ pub trait GLWESwitchingKeyToMut { impl GLWESwitchingKeyToMut for GLWESwitchingKey where - GGLWE: GGLWECiphertextToMut, + GGLWE: GGLWEToMut, { fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> { GLWESwitchingKey { @@ -246,13 +307,13 @@ where } impl GLWESwitchingKey { - 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 GLWESwitchingKey { - 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) } } diff --git a/poulpy-core/src/layouts/gglwe_tsk.rs b/poulpy-core/src/layouts/gglwe_tsk.rs index e6eae2d..2483868 100644 --- a/poulpy-core/src/layouts/gglwe_tsk.rs +++ b/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 @@ impl fmt::Display for TensorKey { } } -impl TensorKey> { - pub fn alloc(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> { + 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(&self, infos: &A) -> TensorKey> where A: GGLWEInfos, { @@ -140,34 +152,21 @@ impl TensorKey> { 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>> = 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(infos: &A) -> usize + fn bytes_of_tensor_key_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -176,23 +175,54 @@ impl TensorKey> { 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(), + ) + } +} + +impl TensorKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + +impl TensorKey> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGLWEInfos, + Module: TensorKeyAlloc, + { + module.alloc_tensor_key_from_infos(infos) } - 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) + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: TensorKeyAlloc, + { + module.alloc_tensor_key(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGLWEInfos, + Module: TensorKeyAlloc, + { + module.bytes_of_tensor_key_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: TensorKeyAlloc, + { + module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize) } } @@ -260,14 +290,14 @@ where } pub trait TensorKeyToMut { - fn to_ref(&mut self) -> TensorKey<&mut [u8]>; + fn to_mut(&mut self) -> TensorKey<&mut [u8]>; } impl TensorKeyToMut for TensorKey where GLWESwitchingKey: 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(), } diff --git a/poulpy-core/src/layouts/ggsw_ct.rs b/poulpy-core/src/layouts/ggsw_ct.rs index b1fa240..b427ecd 100644 --- a/poulpy-core/src/layouts/ggsw_ct.rs +++ b/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 FillUniform for GGSW { } impl GGSW { - 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 GGSW { } impl GGSW { - 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 { } } -impl GGSW> { - pub fn alloc(infos: &A) -> Self - where - A: GGSWInfos, - { - Self::alloc_with( - infos.n(), - infos.base2k(), - infos.k(), - infos.rank(), - infos.dnum(), - infos.dsize(), - ) - } +impl GGSWAlloc for Module 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> { let size: usize = k.0.div_ceil(base2k.0) as usize; debug_assert!( size as u32 > dsize.0, @@ -180,9 +173,9 @@ impl GGSW> { dsize.0, ); - Self { + GGSW { data: MatZnx::alloc( - n.into(), + self.n().into(), dnum.into(), (rank + 1).into(), (rank + 1).into(), @@ -194,12 +187,11 @@ impl GGSW> { } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_ggsw_from_infos(&self, infos: &A) -> GGSW> where A: GGSWInfos, { - Self::alloc_bytes_with( - infos.n(), + self.alloc_ggsw( infos.base2k(), infos.k(), infos.rank(), @@ -208,7 +200,7 @@ impl GGSW> { ) } - 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,14 +215,66 @@ impl GGSW> { 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(&self, infos: &A) -> usize + where + A: GGSWInfos, + { + self.bytes_of_ggsw( + infos.base2k(), + infos.k(), + infos.rank(), + infos.dnum(), + infos.dsize(), + ) + } +} + +impl GGSW> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GGSWInfos, + Module: GGSWAlloc, + { + module.alloc_ggsw_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + where + Module: GGSWAlloc, + { + module.alloc_ggsw(base2k, k, rank, dnum, dsize) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GGSWInfos, + Module: GGSWAlloc, + { + module.bytes_of_ggsw_from_infos(infos) + } + + pub fn bytes_of( + module: Module, + base2k: Base2K, + k: TorusPrecision, + rank: Rank, + dnum: Dnum, + dsize: Dsize, + ) -> usize + where + Module: GGSWAlloc, + { + module.bytes_of_ggsw(base2k, k, rank, dnum, dsize) + } } use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/poulpy-core/src/layouts/glwe_ct.rs b/poulpy-core/src/layouts/glwe_ct.rs index 7776662..5bdb2cb 100644 --- a/poulpy-core/src/layouts/glwe_ct.rs +++ b/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 @@ where } } -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 @@ impl LWEInfos for GLWECiphertextLayout { } } -impl GLWEInfos for GLWECiphertextLayout { +impl GLWEInfos for GLWELayout { fn rank(&self) -> Rank { self.rank } } #[derive(PartialEq, Eq, Clone)] -pub struct GLWECiphertext { +pub struct GLWE { pub(crate) data: VecZnx, pub(crate) base2k: Base2K, pub(crate) k: TorusPrecision, } -impl GLWELayoutSet for GLWECiphertext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWE { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -74,19 +75,19 @@ impl GLWELayoutSet for GLWECiphertext { } } -impl GLWECiphertext { +impl GLWE { pub fn data(&self) -> &VecZnx { &self.data } } -impl GLWECiphertext { +impl GLWE { pub fn data_mut(&mut self) -> &mut VecZnx { &mut self.data } } -impl LWEInfos for GLWECiphertext { +impl LWEInfos for GLWE { fn base2k(&self) -> Base2K { self.base2k } @@ -104,16 +105,16 @@ impl LWEInfos for GLWECiphertext { } } -impl GLWEInfos for GLWECiphertext { +impl GLWEInfos for GLWE { fn rank(&self) -> Rank { Rank(self.data.cols() as u32 - 1) } } -impl ToOwnedDeep for GLWECiphertext { - type Owned = GLWECiphertext>; +impl ToOwnedDeep for GLWE { + type Owned = GLWE>; 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 ToOwnedDeep for GLWECiphertext { } } -impl fmt::Debug for GLWECiphertext { +impl fmt::Debug for GLWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for GLWECiphertext { +impl fmt::Display for GLWE { 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 fmt::Display for GLWECiphertext { } } -impl FillUniform for GLWECiphertext { +impl FillUniform for GLWE { fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) { self.data.fill_uniform(log_bound, source); } } -impl GLWECiphertext> { - pub fn alloc(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(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEAlloc +where + Self: GetDegree, +{ + fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE> { + GLWE { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_from_infos(&self, infos: &A) -> GLWE> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe(infos.base2k(), infos.k(), infos.rank()) } - 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) + 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, + ) + } + + fn bytes_of_glwe_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank()) } } -impl ReaderFrom for GLWECiphertext { +impl GLWEAlloc for Module where Self: GetDegree {} + +impl GLWE> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEAlloc, + { + module.alloc_glwe_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWEAlloc, + { + module.alloc_glwe(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEAlloc, + { + module.bytes_of_glwe_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module: GLWEAlloc, + { + module.bytes_of_glwe(base2k, k, rank) + } +} + +impl ReaderFrom for GLWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -181,7 +227,7 @@ impl ReaderFrom for GLWECiphertext { } } -impl WriterTo for GLWECiphertext { +impl WriterTo for GLWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.0)?; writer.write_u32::(self.base2k.0)?; @@ -189,13 +235,13 @@ impl WriterTo for GLWECiphertext { } } -pub trait GLWECiphertextToRef { - fn to_ref(&self) -> GLWECiphertext<&[u8]>; +pub trait GLWEToRef { + fn to_ref(&self) -> GLWE<&[u8]>; } -impl GLWECiphertextToRef for GLWECiphertext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl GLWEToRef for GLWE { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -203,13 +249,13 @@ impl GLWECiphertextToRef for GLWECiphertext { } } -pub trait GLWECiphertextToMut { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>; +pub trait GLWEToMut { + fn to_mut(&mut self) -> GLWE<&mut [u8]>; } -impl GLWECiphertextToMut for GLWECiphertext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl GLWEToMut for GLWE { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/glwe_pk.rs b/poulpy-core/src/layouts/glwe_pk.rs index b270b33..a370656 100644 --- a/poulpy-core/src/layouts/glwe_pk.rs +++ b/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 @@ impl GLWEInfos for GLWEPublicKeyLayout { } } -impl GLWEPublicKey> { - pub fn alloc(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(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize), +pub trait GLWEPublicKeyAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey> { + GLWEPublicKey { + data: VecZnx::alloc( + self.n().into(), + (rank + 1).into(), + k.0.div_ceil(base2k.0) as usize, + ), base2k, k, dist: Distribution::NONE, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_public_key_from_infos(&self, infos: &A) -> GLWEPublicKey> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), infos.rank()) + self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) } - 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) + 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(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank()) + } +} + +impl GLWEPublicKey> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self + where + Module: GLWEPublicKeyAlloc, + { + module.alloc_glwe_public_key(base2k, k, rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize + where + Module: GLWEPublicKeyAlloc, + { + module.bytes_of_glwe_public_key(base2k, k, rank) } } diff --git a/poulpy-core/src/layouts/glwe_pt.rs b/poulpy-core/src/layouts/glwe_pt.rs index dfd4b8d..5f4e2df 100644 --- a/poulpy-core/src/layouts/glwe_pt.rs +++ b/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 struct GLWEPlaintext { pub k: TorusPrecision, } -impl GLWELayoutSet for GLWEPlaintext { - fn set_basek(&mut self, base2k: Base2K) { +impl SetGLWEInfos for GLWEPlaintext { + fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -86,39 +85,72 @@ impl fmt::Display for GLWEPlaintext { } } -impl GLWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) - } - - 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), +pub trait GLWEPlaintextAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext> { + GLWEPlaintext { + data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize), base2k, k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_plaintext_from_infos(&self, infos: &A) -> GLWEPlaintext> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k(), Rank(0)) + self.alloc_glwe_plaintext(infos.base2k(), infos.k()) } - 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) + 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(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_plaintext(infos.base2k(), infos.k()) } } -impl GLWECiphertextToRef for GLWEPlaintext { - fn to_ref(&self) -> GLWECiphertext<&[u8]> { - GLWECiphertext { +impl GLWEPlaintext> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: GLWEPlaintextAlloc, + { + module.alloc_glwe_plaintext(base2k, k) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext_from_infos(infos) + } + + pub fn bytes_of(module: Module, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: GLWEPlaintextAlloc, + { + module.bytes_of_glwe_plaintext(base2k, k) + } +} + +impl GLWEToRef for GLWEPlaintext { + fn to_ref(&self) -> GLWE<&[u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -126,9 +158,9 @@ impl GLWECiphertextToRef for GLWEPlaintext { } } -impl GLWECiphertextToMut for GLWEPlaintext { - fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> { - GLWECiphertext { +impl GLWEToMut for GLWEPlaintext { + fn to_mut(&mut self) -> GLWE<&mut [u8]> { + GLWE { k: self.k, base2k: self.base2k, data: self.data.to_mut(), diff --git a/poulpy-core/src/layouts/glwe_sk.rs b/poulpy-core/src/layouts/glwe_sk.rs index e954c3b..c5be370 100644 --- a/poulpy-core/src/layouts/glwe_sk.rs +++ b/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 @@ impl GLWEInfos for GLWESecret { } } -impl GLWESecret> { - pub fn alloc(infos: &A) -> Self - where - A: GLWEInfos, - { - Self::alloc_with(infos.n(), infos.rank()) - } - - pub fn alloc_with(n: Degree, rank: Rank) -> Self { - Self { - data: ScalarZnx::alloc(n.into(), rank.into()), +pub trait GLWESecretAlloc +where + Self: GetDegree, +{ + fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret> { + GLWESecret { + data: ScalarZnx::alloc(self.n().into(), rank.into()), dist: Distribution::NONE, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_glwe_secret_from_infos(&self, infos: &A) -> GLWESecret> where A: GLWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.rank()) + self.alloc_glwe_secret(infos.rank()) } - pub fn alloc_bytes_with(n: Degree, rank: Rank) -> usize { - ScalarZnx::alloc_bytes(n.into(), rank.into()) + fn bytes_of_glwe_secret(&self, rank: Rank) -> usize { + ScalarZnx::bytes_of(self.n().into(), rank.into()) + } + + fn bytes_of_glwe_secret_from_infos(&self, infos: &A) -> usize + where + A: GLWEInfos, + { + self.bytes_of_glwe_secret(infos.rank()) + } +} + +impl GLWESecret> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: GLWEInfos, + Module: GLWESecretAlloc, + { + module.alloc_glwe_secret_from_infos(infos) + } + + pub fn alloc(module: Module, rank: Rank) -> Self + where + Module: GLWESecretAlloc, + { + module.alloc_glwe_secret(rank) + } + + pub fn bytes_of_from_infos(module: Module, infos: &A) -> usize + where + A: GLWEInfos, + Module: GLWESecretAlloc, + { + module.bytes_of_glwe_secret_from_infos(infos) + } + + pub fn bytes_of(module: Module, rank: Rank) -> usize + where + Module: GLWESecretAlloc, + { + module.bytes_of_glwe_secret(rank) } } diff --git a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs index ec5a4a3..4020721 100644 --- a/poulpy-core/src/layouts/glwe_to_lwe_ksk.rs +++ b/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 @@ impl WriterTo for GLWEToLWESwitchingKey { } } +pub trait GLWEToLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_glwe_to_lwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_in: Rank, + dnum: Dnum, + ) -> GLWEToLWESwitchingKey> { + GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))) + } + + fn alloc_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> GLWEToLWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } + + 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)) + } + + fn bytes_of_glwe_to_lwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for GLWEToLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for GLWEToLWESwitchingKey" + ); + self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum()) + } +} + impl GLWEToLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_glwe_to_lwe_switching_key_from_infos(infos) } - 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), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self + where + Module: GLWEToLWESwitchingKeyAlloc, + { + module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: GLWEToLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for GLWEToLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for GLWEToLWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos) } - 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(module: &Module, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize + where + Module: GLWEToLWESwitchingKeyAlloc, + { + module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum) } } diff --git a/poulpy-core/src/layouts/lwe_ct.rs b/poulpy-core/src/layouts/lwe_ct.rs index 8c73a4d..26ee40b 100644 --- a/poulpy-core/src/layouts/lwe_ct.rs +++ b/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 @@ impl LWEInfos for LWECiphertextLayout { } } #[derive(PartialEq, Eq, Clone)] -pub struct LWECiphertext { +pub struct LWE { pub(crate) data: Zn, pub(crate) k: TorusPrecision, pub(crate) base2k: Base2K, } -impl LWEInfos for LWECiphertext { +impl LWEInfos for LWE { fn base2k(&self) -> Base2K { self.base2k } @@ -76,7 +76,7 @@ impl LWEInfos for LWECiphertext { } } -impl SetLWEInfos for LWECiphertext { +impl SetLWEInfos for LWE { fn set_base2k(&mut self, base2k: Base2K) { self.base2k = base2k } @@ -86,25 +86,25 @@ impl SetLWEInfos for LWECiphertext { } } -impl LWECiphertext { +impl LWE { pub fn data(&self) -> &Zn { &self.data } } -impl LWECiphertext { +impl LWE { pub fn data_mut(&mut self) -> &Zn { &mut self.data } } -impl fmt::Debug for LWECiphertext { +impl fmt::Debug for LWE { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{self}") } } -impl fmt::Display for LWECiphertext { +impl fmt::Display for LWE { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -116,7 +116,7 @@ impl fmt::Display for LWECiphertext { } } -impl FillUniform for LWECiphertext +impl FillUniform for LWE where Zn: FillUniform, { @@ -125,41 +125,73 @@ where } } -impl LWECiphertext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.n(), infos.base2k(), infos.k()) - } - - pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEAlloc { + fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE> { + LWE { data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } - pub fn alloc_bytes(infos: &A) -> usize + fn alloc_lwe_from_infos(&self, infos: &A) -> LWE> where A: LWEInfos, { - Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k()) + self.alloc_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) + 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) + } + + fn bytes_of_lwe_from_infos(&self, infos: &A) -> usize + where + A: LWEInfos, + { + self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k()) + } +} + +impl LWE> { + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self + where + A: LWEInfos, + Module: LWEAlloc, + { + module.alloc_lwe_from_infos(infos) + } + + pub fn alloc(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self + where + Module: LWEAlloc, + { + module.alloc_lwe(n, base2k, k) + } + + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize + where + A: LWEInfos, + Module: LWEAlloc, + { + module.bytes_of_lwe_from_infos(infos) + } + + pub fn bytes_of(module: &Module, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize + where + Module: LWEAlloc, + { + module.bytes_of_lwe(n, base2k, k) } } pub trait LWECiphertextToRef { - fn to_ref(&self) -> LWECiphertext<&[u8]>; + fn to_ref(&self) -> LWE<&[u8]>; } -impl LWECiphertextToRef for LWECiphertext { - fn to_ref(&self) -> LWECiphertext<&[u8]> { - LWECiphertext { +impl LWECiphertextToRef for LWE { + fn to_ref(&self) -> LWE<&[u8]> { + LWE { k: self.k, base2k: self.base2k, data: self.data.to_ref(), @@ -167,14 +199,14 @@ impl LWECiphertextToRef for LWECiphertext { } } -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 LWECiphertextToMut for LWECiphertext { - fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> { - LWECiphertext { +impl LWEToMut for LWE { + 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 LWECiphertextToMut for LWECiphertext { } } -impl ReaderFrom for LWECiphertext { +impl ReaderFrom for LWE { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = TorusPrecision(reader.read_u32::()?); self.base2k = Base2K(reader.read_u32::()?); @@ -190,7 +222,7 @@ impl ReaderFrom for LWECiphertext { } } -impl WriterTo for LWECiphertext { +impl WriterTo for LWE { fn write_to(&self, writer: &mut W) -> std::io::Result<()> { writer.write_u32::(self.k.into())?; writer.write_u32::(self.base2k.into())?; diff --git a/poulpy-core/src/layouts/lwe_ksk.rs b/poulpy-core/src/layouts/lwe_ksk.rs index 2d4ba31..77a027a 100644 --- a/poulpy-core/src/layouts/lwe_ksk.rs +++ b/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 @@ impl GGLWEInfos for LWESwitchingKey { } } +pub trait LWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey> { + LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))) + } + + fn alloc_lwe_switching_key_from_infos(&self, infos: &A) -> LWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } + + 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)) + } + + fn bytes_of_lwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWESwitchingKey" + ); + assert_eq!( + infos.rank_out().0, + 1, + "rank_out > 1 is not supported for LWESwitchingKey" + ); + self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum()) + } +} + +impl LWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl LWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_lwe_switching_key_from_infos(infos) } - 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), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self + where + Module: LWESwitchingKeyAlloc, + { + module.alloc_lwe_switching_key(base2k, k, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: LWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWESwitchingKey" - ); - debug_assert_eq!( - infos.rank_out().0, - 1, - "rank_out > 1 is not supported for LWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + module.bytes_of_glwe_switching_key_from_infos(infos) } - 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)) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize + where + Module: LWESwitchingKeyAlloc, + { + module.bytes_of_lwe_switching_key(base2k, k, dnum) } } diff --git a/poulpy-core/src/layouts/lwe_pt.rs b/poulpy-core/src/layouts/lwe_pt.rs index e739722..fb23b18 100644 --- a/poulpy-core/src/layouts/lwe_pt.rs +++ b/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,21 +52,36 @@ impl LWEInfos for LWEPlaintext { } } -impl LWEPlaintext> { - pub fn alloc(infos: &A) -> Self - where - A: LWEInfos, - { - Self::alloc_with(infos.base2k(), infos.k()) - } - - pub fn alloc_with(base2k: Base2K, k: TorusPrecision) -> Self { - Self { +pub trait LWEPlaintextAlloc { + fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext> { + LWEPlaintext { data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize), k, base2k, } } + + fn alloc_lwe_plaintext_from_infos(&self, infos: &A) -> LWEPlaintext> + where + A: LWEInfos, + { + self.alloc_lwe_plaintext(infos.base2k(), infos.k()) + } +} + +impl LWEPlaintextAlloc for Module {} + +impl LWEPlaintext> { + pub fn alloc_from_infos(module: Module, infos: &A) -> Self + where + A: LWEInfos, + { + module.alloc_lwe_plaintext_from_infos(infos) + } + + pub fn alloc(module: Module, base2k: Base2K, k: TorusPrecision) -> Self { + module.alloc_lwe_plaintext(base2k, k) + } } impl fmt::Display for LWEPlaintext { diff --git a/poulpy-core/src/layouts/lwe_sk.rs b/poulpy-core/src/layouts/lwe_sk.rs index f2ba869..4190a4b 100644 --- a/poulpy-core/src/layouts/lwe_sk.rs +++ b/poulpy-core/src/layouts/lwe_sk.rs @@ -1,5 +1,5 @@ use poulpy_hal::{ - layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, + layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero}, source::Source, }; @@ -13,15 +13,26 @@ pub struct LWESecret { pub(crate) dist: Distribution, } -impl LWESecret> { - pub fn alloc(n: Degree) -> Self { - Self { +pub trait LWESecretAlloc { + fn alloc_lwe_secret(&self, n: Degree) -> LWESecret> { + LWESecret { data: ScalarZnx::alloc(n.into(), 1), dist: Distribution::NONE, } } } +impl LWESecretAlloc for Module {} + +impl LWESecret> { + pub fn alloc(module: &Module, n: Degree) -> Self + where + Module: LWESecretAlloc, + { + module.alloc_lwe_secret(n) + } +} + impl LWESecret { pub fn raw(&self) -> &[i64] { self.data.at(0, 0) diff --git a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs index 947e79d..e9905ae 100644 --- a/poulpy-core/src/layouts/lwe_to_glwe_ksk.rs +++ b/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 @@ impl WriterTo for LWEToGLWESwitchingKey { } } +pub trait LWEToGLWESwitchingKeyAlloc +where + Self: GLWESwitchingKeyAlloc, +{ + fn alloc_lwe_to_glwe_switching_key( + &self, + base2k: Base2K, + k: TorusPrecision, + rank_out: Rank, + dnum: Dnum, + ) -> LWEToGLWESwitchingKey> { + LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))) + } + + fn alloc_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> LWEToGLWESwitchingKey> + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + + self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } + + 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)) + } + + fn bytes_of_lwe_to_glwe_switching_key_from_infos(&self, infos: &A) -> usize + where + A: GGLWEInfos, + { + assert_eq!( + infos.rank_in().0, + 1, + "rank_in > 1 is not supported for LWEToGLWESwitchingKey" + ); + assert_eq!( + infos.dsize().0, + 1, + "dsize > 1 is not supported for LWEToGLWESwitchingKey" + ); + self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum()) + } +} + +impl LWEToGLWESwitchingKeyAlloc for Module where Self: GLWESwitchingKeyAlloc {} + impl LWEToGLWESwitchingKey> { - pub fn alloc(infos: &A) -> Self + pub fn alloc_from_infos(module: &Module, infos: &A) -> Self where A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - Self(GLWESwitchingKey::alloc(infos)) + module.alloc_lwe_to_glwe_switching_key_from_infos(infos) } - 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), - )) + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self + where + Module: LWEToGLWESwitchingKeyAlloc, + { + module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } - pub fn alloc_bytes(infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, + Module: LWEToGLWESwitchingKeyAlloc, { - debug_assert_eq!( - infos.rank_in().0, - 1, - "rank_in > 1 is not supported for LWEToGLWESwitchingKey" - ); - debug_assert_eq!( - infos.dsize().0, - 1, - "dsize > 1 is not supported for LWEToGLWESwitchingKey" - ); - GLWESwitchingKey::alloc_bytes(infos) + 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(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize + where + Module: LWEToGLWESwitchingKeyAlloc, + { + module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_atk.rs b/poulpy-core/src/layouts/prepared/gglwe_atk.rs index e590c21..853419b 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_atk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_atk.rs @@ -80,7 +80,7 @@ pub trait AutomorphismKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn automorphism_key_prepared_alloc( + fn alloc_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -89,12 +89,12 @@ where dsize: Dsize, ) -> AutomorphismKeyPrepared, B> { AutomorphismKeyPrepared::, 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(&self, infos: &A) -> AutomorphismKeyPrepared, B> + fn alloc_automorphism_key_prepared_from_infos(&self, infos: &A) -> AutomorphismKeyPrepared, B> where A: GGLWEInfos, { @@ -103,7 +103,7 @@ where 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 @@ where ) } - fn automorphism_key_prepared_alloc_bytes( + fn bytes_of_automorphism_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -120,10 +120,10 @@ where 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(&self, infos: &A) -> usize + fn bytes_of_automorphism_key_prepared_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -132,7 +132,7 @@ where 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 @@ impl AutomorphismKeyPrepared, B> { A: GGLWEInfos, Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc_from_infos(infos) + module.alloc_automorphism_key_prepared_from_infos(infos) } - pub fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self + pub fn alloc(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self where Module: AutomorphismKeyPreparedAlloc, { - module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize) + module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize) } - pub fn alloc_bytes(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, infos: &A) -> usize where A: GGLWEInfos, Module: AutomorphismKeyPreparedAlloc, { - 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, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize where Module: AutomorphismKeyPreparedAlloc, { - 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 where Self: GLWESwitchingKeyPrepare, { - fn automorphism_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_automorphism_key_tmp_bytes(&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(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_automorphism_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: AutomorphismKeyPreparedToMut + 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 @@ impl AutomorphismKeyPrepared, B> { where Module: GLWESwitchingKeyPrepare, { - module.automorphism_key_prepare_tmp_bytes(self) + module.prepare_automorphism_key_tmp_bytes(self) } } @@ -214,7 +214,7 @@ impl AutomorphismKeyPrepared { O: AutomorphismKeyToRef + GetAutomorphismGaloisElement, Module: AutomorphismKeyPrepare, { - module.automorphism_key_prepare(self, other, scratch); + module.prepare_automorphism_key(self, other, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ct.rs b/poulpy-core/src/layouts/prepared/gglwe_ct.rs index 6060e80..2f7f6d2 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ct.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ct.rs @@ -61,7 +61,7 @@ pub trait GGLWEPreparedAlloc where Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { - fn gglwe_prepared_alloc( + fn alloc_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -92,12 +92,12 @@ where } } - fn gglwe_prepared_alloc_from_infos(&self, infos: &A) -> GGLWEPrepared, B> + fn alloc_gglwe_prepared_from_infos(&self, infos: &A) -> GGLWEPrepared, 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 @@ where ) } - fn gglwe_prepared_alloc_bytes( + fn bytes_of_gglwe_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -130,15 +130,15 @@ where 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(&self, infos: &A) -> usize + fn bytes_of_gglwe_prepared_from_infos(&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 @@ where where A: GGLWEInfos, { - module.gglwe_prepared_alloc_from_infos(infos) + module.alloc_gglwe_prepared_from_infos(infos) } pub fn alloc( @@ -171,17 +171,17 @@ where 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, base2k: Base2K, k: TorusPrecision, @@ -190,7 +190,7 @@ where 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 where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare, { - fn gglwe_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_gglwe_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -210,7 +210,7 @@ where ) } - fn gglwe_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_gglwe(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GGLWEPreparedToMut, O: GGLWEToRef, @@ -238,7 +238,7 @@ where where O: GGLWEToRef, { - module.gglwe_prepare(self, other, scratch); + module.prepare_gglwe(self, other, scratch); } } @@ -247,7 +247,7 @@ impl GGLWEPrepared, B> { where Module: GGLWEPrepare, { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs index 6d1e443..43eccc8 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_ksk.rs @@ -79,7 +79,7 @@ pub trait GLWESwitchingKeyPreparedAlloc where Self: GGLWEPreparedAlloc, { - fn glwe_switching_key_prepared_alloc( + fn alloc_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -89,7 +89,7 @@ where dsize: Dsize, ) -> GLWESwitchingKeyPrepared, B> { GLWESwitchingKeyPrepared::, 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 @@ where 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 @@ where ) } - fn glwe_switching_key_prepared_alloc_bytes( + fn bytes_of_glwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -118,14 +118,14 @@ where 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(&self, infos: &A) -> usize + fn glwe_switching_key_prepared_bytes_of_from_infos(&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 @@ where 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, base2k: Base2K, k: TorusPrecision, @@ -177,7 +177,7 @@ where 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 where Self: GGLWEPrepare, { - fn glwe_switching_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_glwe_switching_key_tmp_bytes(&self, infos: &A) -> usize where A: GGLWEInfos, { - self.gglwe_prepare_tmp_bytes(infos) + self.prepare_gglwe_tmp_bytes(infos) } - fn glwe_switching_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_glwe_switching(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: GLWESwitchingKeyPreparedToMut + 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 @@ impl GLWESwitchingKeyPrepared { O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData, Module: GLWESwitchingKeyPrepare, { - module.glwe_switching_prepare(self, other, scratch); + module.prepare_glwe_switching(self, other, scratch); } } @@ -220,7 +220,7 @@ where Module: GLWESwitchingKeyPrepare, { pub fn prepare_tmp_bytes(&self, module: &Module) -> usize { - module.gglwe_prepare_tmp_bytes(self) + module.prepare_gglwe_tmp_bytes(self) } } diff --git a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs index b4cd56d..27b1699 100644 --- a/poulpy-core/src/layouts/prepared/gglwe_tsk.rs +++ b/poulpy-core/src/layouts/prepared/gglwe_tsk.rs @@ -59,7 +59,7 @@ pub trait TensorKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn tensor_key_prepared_alloc( + fn alloc_tensor_key_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -70,12 +70,12 @@ where 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(&self, infos: &A) -> TensorKeyPrepared, B> + fn alloc_tensor_key_prepared_from_infos(&self, infos: &A) -> TensorKeyPrepared, B> where A: GGLWEInfos, { @@ -84,7 +84,7 @@ where 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 @@ where ) } - 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(&self, infos: &A) -> usize + fn bytes_of_tensor_key_prepared_from_infos(&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 @@ where 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, 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, - 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, 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 where Self: GLWESwitchingKeyPrepare, { - fn tensor_key_prepare_tmp_bytes(&self, infos: &A) -> usize + fn prepare_tensor_key_tmp_bytes(&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(&self, res: &mut R, other: &O, scratch: &mut Scratch) + fn prepare_tensor_key(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: TensorKeyPreparedToMut, O: TensorKeyToRef, @@ -192,7 +185,7 @@ where 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 @@ where where A: GGLWEInfos, { - module.tensor_key_prepare_tmp_bytes(infos) + module.prepare_tensor_key_tmp_bytes(infos) } } @@ -219,7 +212,7 @@ where where O: TensorKeyToRef, { - module.tensor_key_prepare(self, other, scratch); + module.prepare_tensor_key(self, other, scratch); } } diff --git a/poulpy-core/src/layouts/prepared/ggsw_ct.rs b/poulpy-core/src/layouts/prepared/ggsw_ct.rs index da9c5db..8f3b6c1 100644 --- a/poulpy-core/src/layouts/prepared/ggsw_ct.rs +++ b/poulpy-core/src/layouts/prepared/ggsw_ct.rs @@ -53,7 +53,7 @@ pub trait GGSWPreparedAlloc where Self: GetDegree + VmpPMatAlloc + VmpPMatAllocBytes, { - fn ggsw_prepared_alloc( + fn alloc_ggsw_prepared( &self, base2k: Base2K, k: TorusPrecision, @@ -88,12 +88,12 @@ where } } - fn ggsw_prepared_alloc_from_infos(&self, infos: &A) -> GGSWPrepared, B> + fn alloc_ggsw_prepared_from_infos(&self, infos: &A) -> GGSWPrepared, 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 @@ where ) } - 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 @@ where 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(&self, infos: &A) -> usize + fn bytes_of_ggsw_prepared_from_infos(&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 @@ where where A: GGSWInfos, { - module.ggsw_prepared_alloc_from_infos(infos) + module.alloc_ggsw_prepared_from_infos(infos) } pub fn alloc(module: &Module, 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, - 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, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize { + module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank) } } diff --git a/poulpy-core/src/layouts/prepared/glwe_pk.rs b/poulpy-core/src/layouts/prepared/glwe_pk.rs index 4ad2be6..36d1e81 100644 --- a/poulpy-core/src/layouts/prepared/glwe_pk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_pk.rs @@ -54,7 +54,7 @@ pub trait GLWEPublicKeyPreparedAlloc where Self: GetDegree + VecZnxDftAlloc + VecZnxDftAllocBytes, { - fn glwe_public_key_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { + fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared, B> { GLWEPublicKeyPrepared { data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize), base2k, @@ -63,22 +63,22 @@ where } } - fn glwe_public_key_alloc_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, B> + fn alloc_glwe_public_key_prepared_from_infos(&self, infos: &A) -> GLWEPublicKeyPrepared, 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(&self, infos: &A) -> usize + fn bytes_of_glwe_public_key_prepared_from_infos(&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 @@ where 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, 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { - module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize { + module.bytes_of_glwe_public_key_prepared(base2k, k, rank) } } @@ -115,7 +115,7 @@ pub trait GLWEPublicKeyPrepare where Self: GetDegree + VecZnxDftApply, { - fn glwe_public_key_prepare(&self, res: &mut R, other: &O) + fn prepare_glwe_public_key(&self, res: &mut R, other: &O) where R: GLWEPublicKeyPreparedToMut + SetDist, O: GLWEPublicKeyToRef + GetDist, @@ -149,7 +149,7 @@ where where O: GLWEPublicKeyToRef + GetDist, { - module.glwe_public_key_prepare(self, other); + module.prepare_glwe_public_key(self, other); } } diff --git a/poulpy-core/src/layouts/prepared/glwe_sk.rs b/poulpy-core/src/layouts/prepared/glwe_sk.rs index 4bac6c0..4fc9ba3 100644 --- a/poulpy-core/src/layouts/prepared/glwe_sk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_sk.rs @@ -49,29 +49,29 @@ pub trait GLWESecretPreparedAlloc where Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc, { - fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared, B> { + fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared, B> { GLWESecretPrepared { data: self.svp_ppol_alloc(rank.into()), dist: Distribution::NONE, } } - fn glwe_secret_prepared_alloc_from_infos(&self, infos: &A) -> GLWESecretPrepared, B> + fn alloc_glwe_secret_prepared_from_infos(&self, infos: &A) -> GLWESecretPrepared, 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(&self, infos: &A) -> usize + fn bytes_of_glwe_secret_from_infos(&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 @@ where where A: GLWEInfos, { - module.glwe_secret_prepared_alloc_from_infos(infos) + module.alloc_glwe_secret_prepared_from_infos(infos) } pub fn alloc(module: &Module, rank: Rank) -> Self { - module.glwe_secret_prepared_alloc(rank) + module.alloc_glwe_secret_prepared(rank) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, rank: Rank) -> usize { - module.glwe_secret_alloc_bytes(rank) + pub fn bytes_of(module: &Module, rank: Rank) -> usize { + module.bytes_of_glwe_secret(rank) } } @@ -118,7 +118,7 @@ pub trait GLWESecretPrepare where Self: SvpPrepare, { - fn glwe_secret_prepare(&self, res: &mut R, other: &O) + fn prepare_glwe_secret(&self, res: &mut R, other: &O) where R: GLWESecretPreparedToMut + SetDist, O: GLWESecretToRef + GetDist, diff --git a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 5f10f66..620bf85 100644 --- a/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -64,7 +64,7 @@ where rank_in: Rank, dnum: Dnum, ) -> GLWEToLWESwitchingKeyPrepared, 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(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared, B> where @@ -83,17 +83,11 @@ where 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(&self, infos: &A) -> usize + fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -107,7 +101,7 @@ where 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 @@ where module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, 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, 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 @@ where 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(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -156,7 +150,7 @@ where R: GLWEToLWESwitchingKeyPreparedToMut, 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); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_ksk.rs index 6017f2f..ffb5bf9 100644 --- a/poulpy-core/src/layouts/prepared/lwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_ksk.rs @@ -56,13 +56,13 @@ pub trait LWESwitchingKeyPreparedAlloc where Self: GLWESwitchingKeyPreparedAlloc, { - fn lwe_switching_key_prepared_alloc( + fn alloc_lwe_switching_key_prepared( &self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, ) -> LWESwitchingKeyPrepared, 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(&self, infos: &A) -> LWESwitchingKeyPrepared, B> @@ -84,14 +84,14 @@ where 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(&self, infos: &A) -> usize + fn lwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -110,7 +110,7 @@ where 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 @@ where } pub fn alloc(module: &Module, 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(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { - module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum) + pub fn bytes_of(module: &Module, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize { + module.lwe_switching_key_prepared_bytes_of(base2k, k, dnum) } } @@ -151,14 +151,14 @@ where where A: GGLWEInfos, { - self.glwe_switching_key_prepare_tmp_bytes(infos); + self.prepare_glwe_switching_key_tmp_bytes(infos); } fn lwe_switching_key_prepare(&self, res: &mut R, other: &O, scratch: &mut Scratch) where R: LWESwitchingKeyPreparedToMut, 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); } } diff --git a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs index ed99a51..b2e7383 100644 --- a/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/poulpy-core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -65,7 +65,7 @@ where rank_out: Rank, dnum: Dnum, ) -> LWEToGLWESwitchingKeyPrepared, 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(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared, B> where @@ -84,17 +84,17 @@ where 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(&self, infos: &A) -> usize + fn lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(&self, infos: &A) -> usize where A: GGLWEInfos, { @@ -108,7 +108,7 @@ where 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 @@ where module.lwe_to_glwe_switching_key_prepared_alloc(base2k, k, rank_out, dnum) } - pub fn alloc_bytes_from_infos(module: &Module, infos: &A) -> usize + pub fn bytes_of_from_infos(module: &Module, 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, 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, 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 @@ where 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(&self, res: &mut R, other: &O, scratch: &mut Scratch) @@ -157,7 +157,7 @@ where R: LWEToGLWESwitchingKeyPreparedToMut, 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); } } diff --git a/poulpy-core/src/noise/gglwe_ct.rs b/poulpy-core/src/noise/gglwe_ct.rs index 70a8ef3..4582ad2 100644 --- a/poulpy-core/src/noise/gglwe_ct.rs +++ b/poulpy-core/src/noise/gglwe_ct.rs @@ -8,7 +8,7 @@ use poulpy_hal::{ 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 GGLWE { pub fn assert_noise( @@ -35,8 +35,8 @@ impl GGLWE { let dsize: usize = self.dsize().into(); let base2k: usize = self.base2k().into(); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self)); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); (0..self.rank_in().into()).for_each(|col_i| { (0..self.dnum().into()).for_each(|row_i| { diff --git a/poulpy-core/src/noise/ggsw_ct.rs b/poulpy-core/src/noise/ggsw_ct.rs index 84a2f87..0527b14 100644 --- a/poulpy-core/src/noise/ggsw_ct.rs +++ b/poulpy-core/src/noise/ggsw_ct.rs @@ -9,7 +9,7 @@ use poulpy_hal::{ 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 GGSW { pub fn assert_noise( @@ -42,13 +42,13 @@ impl GGSW { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - 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 @@ impl GGSW { let base2k: usize = self.base2k().into(); let dsize: usize = self.dsize().into(); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(self); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(self); let mut pt_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(1, self.size()); let mut pt_big: VecZnxBig, B> = module.vec_znx_big_alloc(1, self.size()); let mut scratch: ScratchOwned = - 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| { diff --git a/poulpy-core/src/noise/glwe_ct.rs b/poulpy-core/src/noise/glwe_ct.rs index f7af2a1..c1fcd2f 100644 --- a/poulpy-core/src/noise/glwe_ct.rs +++ b/poulpy-core/src/noise/glwe_ct.rs @@ -8,9 +8,9 @@ use poulpy_hal::{ oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl}, }; -use crate::layouts::{GLWECiphertext, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; +use crate::layouts::{GLWE, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; -impl GLWECiphertext { +impl GLWE { pub fn noise( &self, module: &Module, @@ -32,7 +32,7 @@ impl GLWECiphertext { + VecZnxBigNormalize, Scratch: TakeVecZnxDft + TakeVecZnxBig, { - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self); + let mut pt_have: GLWEPlaintext> = 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 @@ impl GLWECiphertext { + VecZnxNormalizeInplace, B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { - let mut scratch: ScratchOwned = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self)); + let mut scratch: ScratchOwned = 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}"); } diff --git a/poulpy-core/src/operations/glwe.rs b/poulpy-core/src/operations/glwe.rs index b0ee4f6..ea4cf9d 100644 --- a/poulpy-core/src/operations/glwe.rs +++ b/poulpy-core/src/operations/glwe.rs @@ -7,24 +7,22 @@ use poulpy_hal::{ 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 GLWEOperations for GLWEPlaintext where D: DataMut, - GLWEPlaintext: GLWECiphertextToMut + GLWEInfos, + GLWEPlaintext: GLWEToMut + GLWEInfos, { } -impl GLWEOperations for GLWECiphertext where GLWECiphertext: GLWECiphertextToMut + GLWEInfos {} +impl GLWEOperations for GLWE where GLWE: GLWEToMut + GLWEInfos {} -pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Sized { +pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized { fn add(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxAdd + VecZnxCopy, { #[cfg(debug_assertions)] @@ -39,9 +37,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn add_inplace(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxAddInplace, { #[cfg(debug_assertions)] @@ -80,8 +78,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub(&mut self, module: &Module, a: &A, b: &B) where - A: GLWECiphertextToRef + GLWEInfos, - B: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, + B: GLWEToRef + GLWEInfos, Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace, { #[cfg(debug_assertions)] @@ -108,9 +106,9 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size }); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(set_k_binary(self, a, b)); } fn sub_inplace_ab(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubInplace, { #[cfg(debug_assertions)] @@ -150,8 +148,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn sub_inplace_ba(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxSubNegateInplace, { #[cfg(debug_assertions)] @@ -172,8 +170,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn rotate(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxRotate, { #[cfg(debug_assertions)] @@ -193,14 +191,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxRotateInplace, { - 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn mul_xp_minus_one(&mut self, module: &Module, k: i64, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxMulXpMinusOne, { #[cfg(debug_assertions)] @@ -226,14 +224,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxMulXpMinusOneInplace, { - 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn copy(&mut self, module: &Module, a: &A) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxCopy, { #[cfg(debug_assertions)] @@ -259,15 +257,15 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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(&mut self, module: &Module, k: usize, scratch: &mut Scratch) @@ -282,7 +280,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size fn normalize(&mut self, module: &Module, a: &A, scratch: &mut Scratch) where - A: GLWECiphertextToRef + GLWEInfos, + A: GLWEToRef + GLWEInfos, Module: VecZnxNormalize, { #[cfg(debug_assertions)] @@ -291,8 +289,8 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size 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 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size scratch, ); }); - self.set_basek(a.base2k()); + self.set_base2k(a.base2k()); self.set_k(a.k().min(self.k())); } @@ -313,14 +311,14 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size where Module: VecZnxNormalizeInplace, { - 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> { +impl GLWE> { pub fn rsh_scratch_space(n: usize) -> usize { VecZnx::rsh_scratch_space(n) } diff --git a/poulpy-core/src/scratch.rs b/poulpy-core/src/scratch.rs index 01b260e..a125d70 100644 --- a/poulpy-core/src/scratch.rs +++ b/poulpy-core/src/scratch.rs @@ -6,8 +6,8 @@ use poulpy_hal::{ 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 @@ use crate::{ }; pub trait TakeGLWECt { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self) where A: GLWEInfos; } pub trait TakeGLWECtSlice { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos; } @@ -123,13 +123,13 @@ impl TakeGLWECt for Scratch where Scratch: TakeVecZnx, { - fn take_glwe_ct(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self) + fn take_glwe_ct(&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 TakeGLWECtSlice for Scratch where Scratch: TakeVecZnx, { - fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) + fn take_glwe_ct_slice(&mut self, size: usize, infos: &A) -> (Vec>, &mut Self) where A: GLWEInfos, { let mut scratch: &mut Scratch = self; - let mut cts: Vec> = Vec::with_capacity(size); + let mut cts: Vec> = Vec::with_capacity(size); for _ in 0..size { let (ct, new_scratch) = scratch.take_glwe_ct(infos); scratch = new_scratch; @@ -463,7 +463,7 @@ where let mut scratch: &mut Scratch = 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 @@ where let mut scratch: &mut Scratch = 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 { diff --git a/poulpy-core/src/tests/serialization.rs b/poulpy-core/src/tests/serialization.rs index 50dc0d6..3c4e564 100644 --- a/poulpy-core/src/tests/serialization.rs +++ b/poulpy-core/src/tests/serialization.rs @@ -1,12 +1,12 @@ use poulpy_hal::test_suite::serialization::test_reader_writer_interface; use crate::layouts::{ - 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> = GLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWE> = 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> = GLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK); + let original: GLWECompressed> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK); test_reader_writer_interface(original); } #[test] fn lwe_serialization() { - let original: LWECiphertext> = LWECiphertext::alloc_with(N_LWE, BASE2K, K); + let original: LWE> = LWE::alloc(N_LWE, BASE2K, K); test_reader_writer_interface(original); } #[test] fn lwe_compressed_serialization() { - let original: LWECiphertextCompressed> = LWECiphertextCompressed::alloc_with(BASE2K, K); + let original: LWECompressed> = LWECompressed::alloc(BASE2K, K); test_reader_writer_interface(original); } #[test] fn test_gglwe_serialization() { - let original: GGLWE> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWE> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_gglwe_compressed_serialization() { - let original: GGLWECiphertextCompressed> = - GGLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GGLWECompressed> = 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> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE); + let original: GLWESwitchingKey> = 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> = - 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 @@ fn test_automorphism_key_serialization() { #[test] fn test_automorphism_key_compressed_serialization() { - let original: AutomorphismKeyCompressed> = - AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_serialization() { - let original: TensorKey> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKey> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn test_tensor_key_compressed_serialization() { - let original: TensorKeyCompressed> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: TensorKeyCompressed> = 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> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: GLWEToLWESwitchingKey> = 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> = - 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> = LWEToGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM); + let original: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM); test_reader_writer_interface(original); } @@ -121,24 +119,24 @@ fn lwe_to_glwe_switching_key_compressed_serialization() { #[test] fn lwe_switching_key_serialization() { - let original: LWESwitchingKey> = LWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKey> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn lwe_switching_key_compressed_serialization() { - let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, DNUM); + let original: LWESwitchingKeyCompressed> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM); test_reader_writer_interface(original); } #[test] fn ggsw_serialization() { - let original: GGSW> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSW> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } #[test] fn ggsw_compressed_serialization() { - let original: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); + let original: GGSWCompressed> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE); test_reader_writer_interface(original); } diff --git a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs index b388e1c..b1acd01 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/gglwe_atk.rs @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_in_infos); - let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_out_infos); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_infos); + let mut auto_key_in: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos); + let mut auto_key_out: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos); + let mut auto_key_apply: AutomorphismKey> = 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 @@ where ), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key_in); + let mut sk: GLWESecret> = 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 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&auto_key_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key_out_infos); + let mut sk_auto: GLWESecret> = 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 @@ where rank: rank.into(), }; - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); - let mut auto_key_apply: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_apply_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); + let mut auto_key_apply: AutomorphismKey> = 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 @@ where | AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply), ); - let mut sk: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk.fill_ternary_prob(0.5, &mut source_xs); // gglwe_{s1}(s0) = s0 -> s1 @@ -355,9 +355,9 @@ where // 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> = GLWEPlaintext::alloc(&auto_key); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&auto_key); - let mut sk_auto: GLWESecret> = GLWESecret::alloc(&auto_key); + let mut sk_auto: GLWESecret> = GLWESecret::alloc_from_infos(&auto_key); sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk for i in 0..rank { diff --git a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs index b63d616..e49fb0f 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/ggsw_ct.rs @@ -128,10 +128,10 @@ where rank: rank.into(), }; - let mut ct_in: GGSW> = GGSW::alloc(&ggsw_in_layout); - let mut ct_out: GGSW> = GGSW::alloc(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut ct_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_layout); + let mut ct_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -147,7 +147,7 @@ where let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -309,9 +309,9 @@ where rank: rank.into(), }; - let mut ct: GGSW> = GGSW::alloc(&ggsw_out_layout); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_layout); - let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc(&auto_key_layout); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_out_layout); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_layout); + let mut auto_key: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&auto_key_layout); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -327,7 +327,7 @@ where let var_xs: f64 = 0.5; - let mut sk: GLWESecret> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(var_xs, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs index 5affb70..4d6218a 100644 --- a/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/automorphism/glwe_ct.rs @@ -18,7 +18,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); - let mut ct_in: GLWECiphertext> = GLWECiphertext::alloc(&ct_in_infos); - let mut ct_out: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct_in: GLWE> = GLWE::alloc_from_infos(&ct_in_infos); + let mut ct_out: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&ct_out); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct_out); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -219,7 +219,7 @@ where 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 @@ where dsize: di.into(), }; - let mut autokey: AutomorphismKey> = AutomorphismKey::alloc(&autokey_infos); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&ct_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&ct_out_infos); + let mut autokey: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&autokey_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&ct_out_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&ct); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ct); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/conversion.rs b/poulpy-core/src/tests/test_suite/conversion.rs index 7e3beba..d1384ee 100644 --- a/poulpy-core/src/tests/test_suite/conversion.rs +++ b/poulpy-core/src/tests/test_suite/conversion.rs @@ -16,9 +16,8 @@ use poulpy_hal::{ }; 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 @@ where 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -112,13 +111,13 @@ where let data: i64 = 17; - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); lwe_pt.encode_i64(data, k_lwe_pt); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = 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> = LWEToGLWESwitchingKey::alloc(&lwe_to_glwe_infos); + let mut ksk: LWEToGLWESwitchingKey> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos); ksk.encrypt_sk( module, @@ -129,13 +128,13 @@ where scratch.borrow(), ); - let mut glwe_ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut glwe_ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let ksk_prepared: LWEToGLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow()); - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = 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 @@ where 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -228,10 +227,10 @@ where sk_lwe.fill_ternary_prob(0.5, &mut source_xs); let data: i64 = 17; - let mut glwe_pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut glwe_pt: GLWEPlaintext> = 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 @@ where scratch.borrow(), ); - let mut lwe_ct: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe_ct: LWE> = LWE::alloc_from_infos(&lwe_infos); let ksk_prepared: GLWEToLWESwitchingKeyPrepared, B> = ksk.prepare_alloc(module, scratch.borrow()); lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow()); - let mut lwe_pt: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut lwe_pt: LWEPlaintext> = 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]); diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs index 476abd1..b08de6b 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_atk.rs @@ -84,7 +84,7 @@ where rank: rank.into(), }; - let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = 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 @@ where module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -187,7 +187,7 @@ where rank: rank.into(), }; - let mut atk_compressed: AutomorphismKeyCompressed> = AutomorphismKeyCompressed::alloc(&atk_infos); + let mut atk_compressed: AutomorphismKeyCompressed> = 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 @@ where module, &atk_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&atk_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&atk_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let p = -5; @@ -217,7 +217,7 @@ where }); let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); - let mut atk: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut atk: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&atk_infos); atk.decompress(module, &atk_compressed); atk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs index b0a8a8b..95a0f7e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/gglwe_ct.rs @@ -81,7 +81,7 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = 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 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -173,7 +173,8 @@ where rank_out: rank_out.into(), }; - let mut ksk_compressed: GLWESwitchingKeyCompressed> = GLWESwitchingKeyCompressed::alloc(&gglwe_infos); + let mut ksk_compressed: GLWESwitchingKeyCompressed> = + 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 @@ where &gglwe_infos, )); - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -201,7 +202,7 @@ where scratch.borrow(), ); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_infos); ksk.decompress(module, &ksk_compressed); ksk.key diff --git a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs index 13ed1de..2aa8f1a 100644 --- a/poulpy-core/src/tests/test_suite/encryption/ggsw_ct.rs +++ b/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 @@ where rank: rank.into(), }; - let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -57,7 +57,7 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSW::encrypt_sk_scratch_space(module, &ggsw_infos)); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -135,7 +135,7 @@ where rank: rank.into(), }; - let mut ct_compressed: GGSWCiphertextCompressed> = GGSWCiphertextCompressed::alloc(&ggsw_infos); + let mut ct_compressed: GGSWCompressed> = GGSWCompressed::alloc_from_infos(&ggsw_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -144,12 +144,12 @@ where pt_scalar.fill_ternary_hw(0, n, &mut source_xs); - let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCiphertextCompressed::encrypt_sk_scratch_space( + let mut scratch: ScratchOwned = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_scratch_space( module, &ggsw_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&ggsw_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&ggsw_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -166,7 +166,7 @@ where let noise_f = |_col_i: usize| -(k as f64) + SIGMA.log2() + 0.5; - let mut ct: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); ct.decompress(module, &ct_compressed); ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs index 35938c3..841ed6e 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_ct.rs @@ -17,8 +17,8 @@ use poulpy_hal::{ 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 @@ where 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 @@ where k: k_pt.into(), }; - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = 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 = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -157,7 +156,7 @@ where 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 @@ where k: k_pt.into(), }; - let mut ct_compressed: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc(&glwe_infos); + let mut ct_compressed: GLWECompressed> = GLWECompressed::alloc_from_infos(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&pt_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&pt_infos); + let mut pt_have: GLWEPlaintext> = 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 = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -201,7 +199,7 @@ where scratch.borrow(), ); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = 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 @@ where 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> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt: GLWEPlaintext> = 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 = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); ct.encrypt_zero_sk( module, @@ -336,16 +333,16 @@ where 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> = GLWECiphertext::alloc(&glwe_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut source_xu: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc(&glwe_infos); + let mut pk: GLWEPublicKey> = 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); diff --git a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs index 2944d59..7b34e63 100644 --- a/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs +++ b/poulpy-core/src/tests/test_suite/encryption/glwe_tsk.rs @@ -80,7 +80,7 @@ where rank: rank.into(), }; - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = 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 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -103,11 +103,11 @@ where scratch.borrow(), ); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = 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> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { @@ -199,7 +199,7 @@ where rank: rank.into(), }; - let mut tensor_key_compressed: TensorKeyCompressed> = TensorKeyCompressed::alloc(&tensor_key_infos); + let mut tensor_key_compressed: TensorKeyCompressed> = 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 @@ where &tensor_key_infos, )); - let mut sk: GLWESecret> = GLWESecret::alloc(&tensor_key_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&tensor_key_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -217,14 +217,14 @@ where tensor_key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow()); - let mut tensor_key: TensorKey> = TensorKey::alloc(&tensor_key_infos); + let mut tensor_key: TensorKey> = TensorKey::alloc_from_infos(&tensor_key_infos); tensor_key.decompress(module, &tensor_key_compressed); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&tensor_key_infos); + let mut pt: GLWEPlaintext> = 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> = GLWESecret::alloc_with(n.into(), 1_u32.into()); + let mut sk_ij: GLWESecret> = GLWESecret::alloc(n.into(), 1_u32.into()); let mut sk_dft: VecZnxDft, B> = module.vec_znx_dft_alloc(rank, 1); for i in 0..rank { diff --git a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs index 15ac048..c4cb4aa 100644 --- a/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs +++ b/poulpy-core/src/tests/test_suite/external_product/gglwe_ksk.rs @@ -110,9 +110,9 @@ where rank: rank_out.into(), }; - let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_in_infos); - let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe_in: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_in_infos); + let mut ct_gglwe_out: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -137,10 +137,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -282,8 +282,8 @@ where rank: rank_out.into(), }; - let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_out_infos); - let mut ct_rgsw: GGSW> = GGSW::alloc(&ggsw_infos); + let mut ct_gglwe: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_out_infos); + let mut ct_rgsw: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let mut pt_rgsw: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -303,10 +303,10 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs index 32f15e9..e7f8d85 100644 --- a/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/ggsw_ct.rs @@ -111,9 +111,9 @@ where rank: rank.into(), }; - let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -133,7 +133,7 @@ where | GGSW::external_product_scratch_space(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -265,8 +265,8 @@ where rank: rank.into(), }; - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); let mut pt_in: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut pt_apply: ScalarZnx> = ScalarZnx::alloc(n, 1); @@ -287,7 +287,7 @@ where | GGSW::external_product_inplace_scratch_space(module, &ggsw_out_infos, &ggsw_apply_infos), ); - let mut sk: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs index efd5af0..cf1880a 100644 --- a/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/external_product/glwe_ct.rs @@ -17,7 +17,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -224,7 +224,7 @@ where 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 @@ where rank: rank.into(), }; - let mut ggsw_apply: GGSW> = GGSW::alloc(&ggsw_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ggsw_apply: GGSW> = GGSW::alloc_from_infos(&ggsw_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); let mut pt_ggsw: ScalarZnx> = ScalarZnx::alloc(n, 1); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_prepared: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs index dfde51e..41fb701 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/gglwe_ct.rs @@ -110,9 +110,9 @@ where rank_out: rank_out_s1s2.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); - let mut gglwe_s0s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s1s2_infos); + let mut gglwe_s0s2: GLWESwitchingKey> = 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 @@ where &gglwe_s1s2_infos, )); - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in_s0s1.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in_s0s1.into()); sk0.fill_ternary_prob(0.5, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s0s1.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s0s1.into()); sk1.fill_ternary_prob(0.5, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out_s1s2.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out_s1s2.into()); sk2.fill_ternary_prob(0.5, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); @@ -266,8 +266,8 @@ where rank_out: rank_out.into(), }; - let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s0s1_infos); - let mut gglwe_s1s2: GLWESwitchingKey> = GLWESwitchingKey::alloc(&gglwe_s1s2_infos); + let mut gglwe_s0s1: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&gglwe_s0s1_infos); + let mut gglwe_s1s2: GLWESwitchingKey> = 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 @@ where let var_xs: f64 = 0.5; - let mut sk0: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk0: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk0.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk1: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk1: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk1.fill_ternary_prob(var_xs, &mut source_xs); - let mut sk2: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk2: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk2.fill_ternary_prob(var_xs, &mut source_xs); let sk2_prepared: GLWESecretPrepared, B> = sk2.prepare_alloc(module, scratch_apply.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs index 91114a7..55f1b7f 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/ggsw_ct.rs @@ -121,10 +121,10 @@ where rank_out: rank.into(), }; - let mut ggsw_in: GGSW> = GGSW::alloc(&ggsw_in_infos); - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_in: GGSW> = GGSW::alloc_from_infos(&ggsw_in_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -146,11 +146,11 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -300,9 +300,9 @@ where rank_out: rank.into(), }; - let mut ggsw_out: GGSW> = GGSW::alloc(&ggsw_out_infos); - let mut tsk: TensorKey> = TensorKey::alloc(&tsk_infos); - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&ksk_apply_infos); + let mut ggsw_out: GGSW> = GGSW::alloc_from_infos(&ggsw_out_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&tsk_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos); let mut pt_scalar: ScalarZnx> = ScalarZnx::alloc(n, 1); let mut source_xs: Source = Source::new([0u8; 32]); @@ -318,11 +318,11 @@ where let var_xs: f64 = 0.5; - let mut sk_in: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(var_xs, &mut source_xs); let sk_in_dft: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(var_xs, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs index 7c8c1ec..6654fe9 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/glwe_ct.rs @@ -18,7 +18,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where rank_out: rank_out.into(), }; - let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply); - let mut glwe_in: GLWECiphertext> = GLWECiphertext::alloc(&glwe_in_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_in_infos); + let mut ksk: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply); + let mut glwe_in: GLWE> = GLWE::alloc_from_infos(&glwe_in_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc_with(n.into(), rank_in.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank_in.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank_out.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank_out.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); @@ -217,7 +217,7 @@ where 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 @@ where rank_out: rank.into(), }; - let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc(&key_apply_infos); - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut key_apply: GLWESwitchingKey> = GLWESwitchingKey::alloc_from_infos(&key_apply_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = 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 @@ where let mut scratch: ScratchOwned = 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> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_in: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_in.fill_ternary_prob(0.5, &mut source_xs); let sk_in_prepared: GLWESecretPrepared, B> = sk_in.prepare_alloc(module, scratch.borrow()); - let mut sk_out: GLWESecret> = GLWESecret::alloc_with(n.into(), rank.into()); + let mut sk_out: GLWESecret> = GLWESecret::alloc(n.into(), rank.into()); sk_out.fill_ternary_prob(0.5, &mut source_xs); let sk_out_prepared: GLWESecretPrepared, B> = sk_out.prepare_alloc(module, scratch.borrow()); diff --git a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs index 1c29c70..b20361a 100644 --- a/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs +++ b/poulpy-core/src/tests/test_suite/keyswitch/lwe_ct.rs @@ -16,7 +16,7 @@ use poulpy_hal::{ }; use crate::layouts::{ - LWECiphertext, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, + LWE, LWECiphertextLayout, LWEPlaintext, LWESecret, LWESwitchingKey, LWESwitchingKeyLayout, prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, }; @@ -100,7 +100,7 @@ where let mut scratch: ScratchOwned = 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> = LWESecret::alloc(n_lwe_in.into()); @@ -111,10 +111,10 @@ where let data: i64 = 17; - let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut lwe_pt_in: LWEPlaintext> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into()); lwe_pt_in.encode_i64(data, k_lwe_pt.into()); - let mut lwe_ct_in: LWECiphertext> = LWECiphertext::alloc(&lwe_in_infos); + let mut lwe_ct_in: LWE> = LWE::alloc_from_infos(&lwe_in_infos); lwe_ct_in.encrypt_sk( module, &lwe_pt_in, @@ -123,7 +123,7 @@ where &mut source_xe, ); - let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc(&key_apply_infos); + let mut ksk: LWESwitchingKey> = LWESwitchingKey::alloc_from_infos(&key_apply_infos); ksk.encrypt_sk( module, @@ -134,13 +134,13 @@ where scratch.borrow(), ); - let mut lwe_ct_out: LWECiphertext> = LWECiphertext::alloc(&lwe_out_infos); + let mut lwe_ct_out: LWE> = LWE::alloc_from_infos(&lwe_out_infos); let ksk_prepared: LWESwitchingKeyPrepared, 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> = LWEPlaintext::alloc(&lwe_out_infos); + let mut lwe_pt_out: LWEPlaintext> = 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]); diff --git a/poulpy-core/src/tests/test_suite/packing.rs b/poulpy-core/src/tests/test_suite/packing.rs index 354738b..005a556 100644 --- a/poulpy-core/src/tests/test_suite/packing.rs +++ b/poulpy-core/src/tests/test_suite/packing.rs @@ -21,7 +21,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where }; let mut scratch: ScratchOwned = 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> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); - let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { *x = i as i64; @@ -125,7 +125,7 @@ where let gal_els: Vec = GLWEPacker::galois_elements(module); let mut auto_keys: HashMap, B>> = HashMap::new(); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, @@ -143,7 +143,7 @@ where let mut packer: GLWEPacker = GLWEPacker::new(&glwe_out_infos, log_batch); - let mut ct: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut ct: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); ct.encrypt_sk( module, @@ -171,19 +171,14 @@ where 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>>, - &auto_keys, - scratch.borrow(), - ) + packer.add(module, None::<&GLWE>>, &auto_keys, scratch.borrow()) } }); - let mut res: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); packer.flush(module, &mut res); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { if i.is_multiple_of(5) { diff --git a/poulpy-core/src/tests/test_suite/trace.rs b/poulpy-core/src/tests/test_suite/trace.rs index d3a8976..73ce3cb 100644 --- a/poulpy-core/src/tests/test_suite/trace.rs +++ b/poulpy-core/src/tests/test_suite/trace.rs @@ -21,7 +21,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where dnum: dnum.into(), }; - let mut glwe_out: GLWECiphertext> = GLWECiphertext::alloc(&glwe_out_infos); - let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_out_infos); + let mut glwe_out: GLWE> = GLWE::alloc_from_infos(&glwe_out_infos); + let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_out_infos); + let mut pt_have: GLWEPlaintext> = 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 = 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> = GLWESecret::alloc(&glwe_out_infos); + let mut sk: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_out_infos); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_dft: GLWESecretPrepared, B> = sk.prepare_alloc(module, scratch.borrow()); @@ -135,8 +135,8 @@ where ); let mut auto_keys: HashMap, B>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); - let mut tmp: AutomorphismKey> = AutomorphismKey::alloc(&key_infos); + let gal_els: Vec = GLWE::trace_galois_elements(module); + let mut tmp: AutomorphismKey> = AutomorphismKey::alloc_from_infos(&key_infos); gal_els.iter().for_each(|gal_el| { tmp.encrypt_sk( module, diff --git a/poulpy-hal/src/api/svp_ppol.rs b/poulpy-hal/src/api/svp_ppol.rs index 5a72367..b1733cc 100644 --- a/poulpy-hal/src/api/svp_ppol.rs +++ b/poulpy-hal/src/api/svp_ppol.rs @@ -9,7 +9,7 @@ pub trait SvpPPolAlloc { /// 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]. diff --git a/poulpy-hal/src/api/vec_znx_big.rs b/poulpy-hal/src/api/vec_znx_big.rs index 08159bb..52048a5 100644 --- a/poulpy-hal/src/api/vec_znx_big.rs +++ b/poulpy-hal/src/api/vec_znx_big.rs @@ -17,7 +17,7 @@ pub trait VecZnxBigAlloc { /// 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]. diff --git a/poulpy-hal/src/api/vec_znx_dft.rs b/poulpy-hal/src/api/vec_znx_dft.rs index 58589c3..621860c 100644 --- a/poulpy-hal/src/api/vec_znx_dft.rs +++ b/poulpy-hal/src/api/vec_znx_dft.rs @@ -11,7 +11,7 @@ pub trait VecZnxDftFromBytes { } 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 { diff --git a/poulpy-hal/src/api/vmp_pmat.rs b/poulpy-hal/src/api/vmp_pmat.rs index 3d0e248..be749de 100644 --- a/poulpy-hal/src/api/vmp_pmat.rs +++ b/poulpy-hal/src/api/vmp_pmat.rs @@ -7,7 +7,7 @@ pub trait VmpPMatAlloc { } 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 { diff --git a/poulpy-hal/src/delegates/svp_ppol.rs b/poulpy-hal/src/delegates/svp_ppol.rs index 54a99b2..aaa3954 100644 --- a/poulpy-hal/src/delegates/svp_ppol.rs +++ b/poulpy-hal/src/delegates/svp_ppol.rs @@ -34,8 +34,8 @@ impl SvpPPolAllocBytes for Module where B: Backend + SvpPPolAllocBytesImpl, { - 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) } } diff --git a/poulpy-hal/src/delegates/vec_znx_big.rs b/poulpy-hal/src/delegates/vec_znx_big.rs index 1556a87..d5c06f5 100644 --- a/poulpy-hal/src/delegates/vec_znx_big.rs +++ b/poulpy-hal/src/delegates/vec_znx_big.rs @@ -53,8 +53,8 @@ impl VecZnxBigAllocBytes for Module where B: Backend + VecZnxBigAllocBytesImpl, { - 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) } } diff --git a/poulpy-hal/src/delegates/vec_znx_dft.rs b/poulpy-hal/src/delegates/vec_znx_dft.rs index 3736e34..650e657 100644 --- a/poulpy-hal/src/delegates/vec_znx_dft.rs +++ b/poulpy-hal/src/delegates/vec_znx_dft.rs @@ -28,8 +28,8 @@ impl VecZnxDftAllocBytes for Module where B: Backend + VecZnxDftAllocBytesImpl, { - 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) } } diff --git a/poulpy-hal/src/delegates/vmp_pmat.rs b/poulpy-hal/src/delegates/vmp_pmat.rs index a875a40..2df0d4a 100644 --- a/poulpy-hal/src/delegates/vmp_pmat.rs +++ b/poulpy-hal/src/delegates/vmp_pmat.rs @@ -27,8 +27,8 @@ impl VmpPMatAllocBytes for Module where B: Backend + VmpPMatAllocBytesImpl, { - 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) } } diff --git a/poulpy-hal/src/layouts/mat_znx.rs b/poulpy-hal/src/layouts/mat_znx.rs index 01be1a1..5ccb860 100644 --- a/poulpy-hal/src/layouts/mat_znx.rs +++ b/poulpy-hal/src/layouts/mat_znx.rs @@ -114,12 +114,12 @@ impl MatZnx { } impl MatZnx> { - pub fn alloc_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize { - rows * cols_in * VecZnx::>::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::>::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 = alloc_aligned(Self::alloc_bytes(n, rows, cols_in, cols_out, size)); + let data: Vec = alloc_aligned(Self::bytes_of(n, rows, cols_in, cols_out, size)); Self { data, n, @@ -132,7 +132,7 @@ impl MatZnx> { pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = 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 @@ impl MatZnx { } let self_ref: MatZnx<&[u8]> = self.to_ref(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(self.n, self.cols_out, self.size); + let nb_bytes: usize = VecZnx::>::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 @@ impl MatZnx { let size: usize = self.size(); let self_ref: MatZnx<&mut [u8]> = self.to_mut(); - let nb_bytes: usize = VecZnx::>::alloc_bytes(n, cols_out, size); + let nb_bytes: usize = VecZnx::>::bytes_of(n, cols_out, size); let start: usize = nb_bytes * cols_in * row + col * nb_bytes; let end: usize = start + nb_bytes; diff --git a/poulpy-hal/src/layouts/scalar_znx.rs b/poulpy-hal/src/layouts/scalar_znx.rs index 296071b..bdf2159 100644 --- a/poulpy-hal/src/layouts/scalar_znx.rs +++ b/poulpy-hal/src/layouts/scalar_znx.rs @@ -132,18 +132,18 @@ impl ScalarZnx { } impl ScalarZnx> { - pub fn alloc_bytes(n: usize, cols: usize) -> usize { + pub fn bytes_of(n: usize, cols: usize) -> usize { n * cols * size_of::() } pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols)); Self { data, n, cols } } pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data, n, cols } } } diff --git a/poulpy-hal/src/layouts/svp_ppol.rs b/poulpy-hal/src/layouts/svp_ppol.rs index 50523fc..234ebca 100644 --- a/poulpy-hal/src/layouts/svp_ppol.rs +++ b/poulpy-hal/src/layouts/svp_ppol.rs @@ -77,7 +77,7 @@ where B: SvpPPolAllocBytesImpl, { pub fn alloc(n: usize, cols: usize) -> Self { - let data: Vec = alloc_aligned::(B::svp_ppol_alloc_bytes_impl(n, cols)); + let data: Vec = alloc_aligned::(B::svp_ppol_bytes_of_impl(n, cols)); Self { data: data.into(), n, @@ -88,7 +88,7 @@ where pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = 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, diff --git a/poulpy-hal/src/layouts/vec_znx.rs b/poulpy-hal/src/layouts/vec_znx.rs index d40ef4b..c96de28 100644 --- a/poulpy-hal/src/layouts/vec_znx.rs +++ b/poulpy-hal/src/layouts/vec_znx.rs @@ -125,12 +125,12 @@ impl ZnxZero for VecZnx { } impl VecZnx> { - 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::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -142,7 +142,7 @@ impl VecZnx> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/layouts/vec_znx_big.rs b/poulpy-hal/src/layouts/vec_znx_big.rs index c50cf66..73a3e0f 100644 --- a/poulpy-hal/src/layouts/vec_znx_big.rs +++ b/poulpy-hal/src/layouts/vec_znx_big.rs @@ -96,7 +96,7 @@ where B: VecZnxBigAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data = alloc_aligned::(B::vec_znx_big_alloc_bytes_impl(n, cols, size)); + let data = alloc_aligned::(B::vec_znx_big_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -109,7 +109,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = 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, diff --git a/poulpy-hal/src/layouts/vec_znx_dft.rs b/poulpy-hal/src/layouts/vec_znx_dft.rs index 3dc92d5..19d28e1 100644 --- a/poulpy-hal/src/layouts/vec_znx_dft.rs +++ b/poulpy-hal/src/layouts/vec_znx_dft.rs @@ -116,7 +116,7 @@ where B: VecZnxDftAllocBytesImpl, { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(B::vec_znx_dft_alloc_bytes_impl(n, cols, size)); + let data: Vec = alloc_aligned::(B::vec_znx_dft_bytes_of_impl(n, cols, size)); Self { data: data.into(), n, @@ -129,7 +129,7 @@ where pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = 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, diff --git a/poulpy-hal/src/layouts/vmp_pmat.rs b/poulpy-hal/src/layouts/vmp_pmat.rs index ce83458..bd469ec 100644 --- a/poulpy-hal/src/layouts/vmp_pmat.rs +++ b/poulpy-hal/src/layouts/vmp_pmat.rs @@ -88,9 +88,7 @@ where B: VmpPMatAllocBytesImpl, { pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { - let data: Vec = alloc_aligned(B::vmp_pmat_alloc_bytes_impl( - n, rows, cols_in, cols_out, size, - )); + let data: Vec = alloc_aligned(B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size)); Self { data: data.into(), n, @@ -104,7 +102,7 @@ where pub fn from_bytes(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = 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, diff --git a/poulpy-hal/src/layouts/zn.rs b/poulpy-hal/src/layouts/zn.rs index 40f5622..eaba4b4 100644 --- a/poulpy-hal/src/layouts/zn.rs +++ b/poulpy-hal/src/layouts/zn.rs @@ -113,12 +113,12 @@ impl ZnxZero for Zn { } impl Zn> { - 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::() } pub fn alloc(n: usize, cols: usize, size: usize) -> Self { - let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); + let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data, n, @@ -130,7 +130,7 @@ impl Zn> { pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::alloc_bytes(n, cols, size)); + assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data, n, diff --git a/poulpy-hal/src/oep/svp_ppol.rs b/poulpy-hal/src/oep/svp_ppol.rs index 6550b6f..42c50ea 100644 --- a/poulpy-hal/src/oep/svp_ppol.rs +++ b/poulpy-hal/src/oep/svp_ppol.rs @@ -23,7 +23,7 @@ pub unsafe trait SvpPPolAllocImpl { /// * See [crate::api::SvpPPolAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocBytesImpl { - 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) diff --git a/poulpy-hal/src/oep/vec_znx_big.rs b/poulpy-hal/src/oep/vec_znx_big.rs index b2bd4c8..4c12e6a 100644 --- a/poulpy-hal/src/oep/vec_znx_big.rs +++ b/poulpy-hal/src/oep/vec_znx_big.rs @@ -35,7 +35,7 @@ pub unsafe trait VecZnxBigFromBytesImpl { /// * See [crate::api::VecZnxBigAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocBytesImpl { - 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)] diff --git a/poulpy-hal/src/oep/vec_znx_dft.rs b/poulpy-hal/src/oep/vec_znx_dft.rs index e5a2bcb..0f9288b 100644 --- a/poulpy-hal/src/oep/vec_znx_dft.rs +++ b/poulpy-hal/src/oep/vec_znx_dft.rs @@ -42,7 +42,7 @@ pub unsafe trait VecZnxDftApplyImpl { /// * See [crate::api::VecZnxDftAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocBytesImpl { - 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) diff --git a/poulpy-hal/src/oep/vmp_pmat.rs b/poulpy-hal/src/oep/vmp_pmat.rs index e399f00..bdca416 100644 --- a/poulpy-hal/src/oep/vmp_pmat.rs +++ b/poulpy-hal/src/oep/vmp_pmat.rs @@ -15,7 +15,7 @@ pub unsafe trait VmpPMatAllocImpl { /// * See [crate::api::VmpPMatAllocBytes] for corresponding public API. /// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocBytesImpl { - 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) diff --git a/poulpy-hal/src/reference/fft64/vmp.rs b/poulpy-hal/src/reference/fft64/vmp.rs index f6fb73c..07e1a8d 100644 --- a/poulpy-hal/src/reference/fft64/vmp.rs +++ b/poulpy-hal/src/reference/fft64/vmp.rs @@ -140,7 +140,7 @@ where 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); diff --git a/poulpy-schemes/benches/circuit_bootstrapping.rs b/poulpy-schemes/benches/circuit_bootstrapping.rs index 12a44fd..cd86fa3 100644 --- a/poulpy-schemes/benches/circuit_bootstrapping.rs +++ b/poulpy-schemes/benches/circuit_bootstrapping.rs @@ -3,8 +3,8 @@ use std::hint::black_box; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios}; use poulpy_core::layouts::{ - 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 @@ where sk_lwe.fill_binary_block(params.block_size, &mut source_xs); sk_lwe.fill_zero(); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe, rank); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe, rank); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); - let ct_lwe: LWECiphertext> = LWECiphertext::alloc(¶ms.lwe_infos); + let ct_lwe: LWE> = LWE::alloc_from_infos(¶ms.lwe_infos); // Circuit bootstrapping evaluation key let cbt_key: CircuitBootstrappingKey, BRA> = CircuitBootstrappingKey::encrypt_sk( @@ -218,7 +218,7 @@ where scratch.borrow(), ); - let mut res: GGSW> = GGSW::alloc(¶ms.ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(¶ms.ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow()); move || { diff --git a/poulpy-schemes/examples/circuit_bootstrapping.rs b/poulpy-schemes/examples/circuit_bootstrapping.rs index 2ac8eb2..73435a9 100644 --- a/poulpy-schemes/examples/circuit_bootstrapping.rs +++ b/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 @@ fn main() { sk_lwe.fill_zero(); // GLWE secret - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = 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 @@ fn main() { let data: i64 = 1 % (1 << k_lwe_pt); // LWE plaintext - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = 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 @@ fn main() { println!("pt_lwe: {pt_lwe}"); // LWE ciphertext - let mut ct_lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = 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 @@ fn main() { println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); // Output GGSW - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); // Circuit bootstrapping key prepared (opaque backend dependant write only struct) let cbt_prepared: CircuitBootstrappingKeyPrepared, CGGI, BackendImpl> = @@ -214,7 +214,7 @@ fn main() { // 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 @@ fn main() { }; // GLWE ciphertext modulus - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&glwe_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&glwe_infos); // Some GLWE plaintext with signed data let k_glwe_pt: usize = 3; - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); let mut data_vec: Vec = vec![0i64; n_glwe]; data_vec .iter_mut() @@ -255,7 +255,7 @@ fn main() { ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow()); // Decrypt - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_res: GLWEPlaintext> = 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]); diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs index 145ce6b..d579db2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs +++ b/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 { - pub(crate) blocks: Vec>, + pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } @@ -62,7 +62,7 @@ impl FheUintBlocks, T> { pub(crate) fn alloc_with(module: &Module, 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, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs index 1af36d1..2571896 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs @@ -60,7 +60,7 @@ impl FheUintBlocksPrepDebug, T> { ) -> 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, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs index b09b8a5..5216611 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs +++ b/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 std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// A FHE ciphertext encrypting a [UnsignedInteger]. -pub struct FheUintWord(pub(crate) GLWECiphertext, pub(crate) PhantomData); +pub struct FheUintWord(pub(crate) GLWE, pub(crate) PhantomData); impl FheUintWord { #[allow(dead_code)] fn post_process( &mut self, module: &Module, - mut tmp_res: Vec>, + mut tmp_res: Vec>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where @@ -67,7 +67,7 @@ impl FheUintWord { // 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> = HashMap::new(); + let mut cts: HashMap> = HashMap::new(); for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); } diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs index 13f34e1..6eb72e7 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs +++ b/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 @@ where fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], + out: &mut [GLWE], inputs: &[&dyn GGSWCiphertextPreparedToRef], scratch: &mut Scratch, ) where @@ -54,7 +54,7 @@ where fn execute( &self, module: &Module, - out: &mut [GLWECiphertext], + out: &mut [GLWE], inputs: &[&dyn GGSWCiphertextPreparedToRef], scratch: &mut Scratch, ) where @@ -159,14 +159,8 @@ impl Node { } pub trait Cmux { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, @@ -177,14 +171,8 @@ impl Cmux for Module where Module: GLWEExternalProductInplace + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace, { - fn cmux( - &self, - out: &mut GLWECiphertext, - t: &GLWECiphertext, - f: &GLWECiphertext, - s: &GGSWPrepared, - scratch: &mut Scratch, - ) where + fn cmux(&self, out: &mut GLWE, t: &GLWE, f: &GLWE, s: &GGSWPrepared, scratch: &mut Scratch) + where O: DataMut, T: DataRef, F: DataRef, diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs index 322a2e2..260e8ba 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs +++ b/poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs @@ -11,7 +11,7 @@ use crate::tfhe::{ use poulpy_core::{ TakeGGSW, TakeGLWECt, layouts::{ - GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret, + GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret, prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc}, }, }; @@ -182,7 +182,7 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = 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 @@ where { assert_eq!(out.blocks.len(), bits.blocks.len()); } - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE + let mut lwe: LWE> = 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 diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs index 5a7ae97..40d91c2 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs +++ b/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 @@ pub(crate) const TEST_BLOCK_SIZE: u32 = 7; 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), diff --git a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs b/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs index 1889e02..7b469c8 100644 --- a/poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs +++ b/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 @@ where BlindRotationKeyPrepared, BRA, BE>: BlincRotationExecute, BlindRotationKey, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk, { - 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 @@ where let mut scratch: ScratchOwned = ScratchOwned::alloc(1 << 22); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prep: GLWESecretPrepared, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs index 03f36e9..ca9415b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -13,7 +13,7 @@ use poulpy_hal::{ 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 @@ where 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 @@ where + 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 @@ where fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ) { @@ -121,8 +120,8 @@ where fn execute_block_binary_extended( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -179,7 +178,7 @@ fn execute_block_binary_extended( } let mut lwe_2n: Vec = 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_extended( fn execute_block_binary( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -324,8 +323,8 @@ fn execute_block_binary( { let n_glwe: usize = brk.n_glwe().into(); let mut lwe_2n: Vec = 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_block_binary( fn execute_standard( module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, brk: &BlindRotationKeyPrepared, scratch: &mut Scratch, @@ -480,8 +479,8 @@ fn execute_standard( } let mut lwe_2n: Vec = 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 @@ fn execute_standard( 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; diff --git a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs b/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs index 22e9bd4..f0a0962 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/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 @@ impl BlindRotationKeyAlloc for BlindRotationKey, CGGI> { { let mut data: Vec>> = 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 @@ impl BlindRotationKeyCompressed, CGGI> { where A: BlindRotationKeyInfos, { - let mut data: Vec>> = Vec::with_capacity(infos.n_lwe().into()); - (0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos))); + let mut data: Vec>> = 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 @@ impl BlindRotationKeyCompressed, CGGI> { A: GGSWInfos, Module: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes, { - GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos) + GGSWCompressed::encrypt_sk_scratch_space(module, infos) } } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs index 51ff139..784d332 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/key_compressed.rs @@ -8,14 +8,14 @@ use std::{fmt, marker::PhantomData}; 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 { - pub(crate) keys: Vec>, + pub(crate) keys: Vec>, pub(crate) dist: Distribution, pub(crate) _phantom: PhantomData, } diff --git a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs b/poulpy-schemes/src/tfhe/blind_rotation/mod.rs index bd83a08..8cc262b 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/mod.rs +++ b/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 { fn execute( &self, module: &Module, - res: &mut GLWECiphertext, - lwe: &LWECiphertext, + res: &mut GLWE, + lwe: &LWE, lut: &LookUpTable, scratch: &mut Scratch, ); diff --git a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 190adff..b09f975 100644 --- a/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/poulpy-schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -23,8 +23,7 @@ use crate::tfhe::blind_rotation::{ }; 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 @@ where 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 @@ where module, &brk_infos, )); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc(&glwe_infos); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc_from_infos(&glwe_infos); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_dft: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); @@ -154,9 +153,9 @@ where scratch.borrow(), ); - let mut lwe: LWECiphertext> = LWECiphertext::alloc(&lwe_infos); + let mut lwe: LWE> = LWE::alloc_from_infos(&lwe_infos); - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(&lwe_infos); + let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_from_infos(&lwe_infos); let x: i64 = 15 % (message_modulus as i64); @@ -175,13 +174,13 @@ where 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> = GLWECiphertext::alloc(&glwe_infos); + let mut res: GLWE> = GLWE::alloc_from_infos(&glwe_infos); let brk_prepared: BlindRotationKeyPrepared, CGGI, B> = brk.prepare_alloc(module, scratch.borrow()); brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow()); - let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&glwe_infos); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc_from_infos(&glwe_infos); res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow()); diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs index 22e7a7a..9e419fe 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -20,7 +20,7 @@ use poulpy_core::{ }; use poulpy_core::glwe_packing; -use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared}; +use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared}; use crate::tfhe::{ blind_rotation::{ @@ -75,7 +75,7 @@ where &self, module: &Module, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -98,7 +98,7 @@ where module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -123,7 +123,7 @@ pub fn circuit_bootstrap_core( module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, key: &CircuitBootstrappingKeyPrepared, @@ -233,7 +233,7 @@ pub fn circuit_bootstrap_core( 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 @@ pub fn circuit_bootstrap_core( #[allow(clippy::too_many_arguments)] fn post_process( module: &Module, - res: &mut GLWECiphertext, - a: &GLWECiphertext, + res: &mut GLWE, + a: &GLWE, log_gap_in: usize, log_gap_out: usize, log_domain: usize, @@ -303,7 +303,7 @@ fn post_process( { let log_n: usize = module.log_n(); - let mut cts: HashMap>> = HashMap::new(); + let mut cts: HashMap>> = 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 @@ fn post_process( let steps: usize = 1 << log_domain; // TODO: from Scratch - let mut cts_vec: Vec>> = Vec::new(); + let mut cts_vec: Vec>> = Vec::new(); for i in 0..steps { if i != 0 { @@ -336,7 +336,7 @@ fn post_process( } glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch); - let packed: &mut GLWECiphertext> = cts.remove(&0).unwrap(); + let packed: &mut GLWE> = cts.remove(&0).unwrap(); res.trace( module, log_n - log_gap_out, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs index 3d91943..0f1e468 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/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 @@ where let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos(); let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); + let gal_els: Vec = GLWE::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { - let mut key: AutomorphismKey> = AutomorphismKey::alloc(&atk_infos); + let mut key: AutomorphismKey> = 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 @@ where scratch, ); - let mut tsk: TensorKey> = TensorKey::alloc(&trk_infos); + let mut tsk: TensorKey> = TensorKey::alloc_from_infos(&trk_infos); tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch); Self { diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs index e316d34..5835765 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -5,7 +5,7 @@ pub mod tests; pub use circuit::*; pub use key::*; -use poulpy_core::layouts::{GGSW, LWECiphertext}; +use poulpy_core::layouts::{GGSW, LWE}; use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch}; @@ -14,7 +14,7 @@ pub trait CirtuitBootstrappingExecute { &self, module: &Module, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, @@ -26,7 +26,7 @@ pub trait CirtuitBootstrappingExecute { module: &Module, log_gap_out: usize, res: &mut GGSW, - lwe: &LWECiphertext, + lwe: &LWE, log_domain: usize, extension_factor: usize, scratch: &mut Scratch, diff --git a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index a0dbb42..d94ac0c 100644 --- a/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/poulpy-schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -36,7 +36,7 @@ use poulpy_core::layouts::{ }; 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 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = 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> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = 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 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let log_gap_out = 1; @@ -236,8 +236,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = 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 @@ where ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = 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 @@ where let mut sk_lwe: LWESecret> = LWESecret::alloc(n_lwe.into()); sk_lwe.fill_binary_block(block_size, &mut source_xs); - let mut sk_glwe: GLWESecret> = GLWESecret::alloc_with(n_glwe.into(), rank.into()); + let mut sk_glwe: GLWESecret> = GLWESecret::alloc(n_glwe.into(), rank.into()); sk_glwe.fill_ternary_prob(0.5, &mut source_xs); let sk_glwe_prepared: GLWESecretPrepared, B> = sk_glwe.prepare_alloc(module, scratch.borrow()); let data: i64 = 1; - let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into()); + let mut pt_lwe: LWEPlaintext> = 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> = LWECiphertext::alloc(&lwe_infos); + let mut ct_lwe: LWE> = 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 @@ where ); println!("CBT-KGEN: {} ms", now.elapsed().as_millis()); - let mut res: GGSW> = GGSW::alloc(&ggsw_infos); + let mut res: GGSW> = GGSW::alloc_from_infos(&ggsw_infos); let cbt_prepared: CircuitBootstrappingKeyPrepared, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow()); @@ -449,8 +449,8 @@ where res.print_noise(module, &sk_glwe_prepared, &pt_ggsw); - let mut ct_glwe: GLWECiphertext> = GLWECiphertext::alloc(&ggsw_infos); - let mut pt_glwe: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut ct_glwe: GLWE> = GLWE::alloc_from_infos(&ggsw_infos); + let mut pt_glwe: GLWEPlaintext> = 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 @@ where ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow()); - let mut pt_res: GLWEPlaintext> = GLWEPlaintext::alloc(&ggsw_infos); + let mut pt_res: GLWEPlaintext> = 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.