This commit is contained in:
Pro7ech
2025-10-14 18:46:25 +02:00
parent 0533cdff8a
commit 72dca47cbe
153 changed files with 3099 additions and 1956 deletions

View File

@@ -69,7 +69,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols));
(
ScalarZnx::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -82,7 +82,7 @@ where
B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>,
{
fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols));
(
SvpPPol::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -95,7 +95,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size));
(
VecZnx::from_data(take_slice, n, cols, size),
Scratch::from_bytes(rem_slice),
@@ -115,7 +115,7 @@ where
) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_big_alloc_bytes_impl(n, cols, size),
B::vec_znx_big_bytes_of_impl(n, cols, size),
);
(
VecZnxBig::from_data(take_slice, n, cols, size),
@@ -136,7 +136,7 @@ where
) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_dft_alloc_bytes_impl(n, cols, size),
B::vec_znx_dft_bytes_of_impl(n, cols, size),
);
(
@@ -204,7 +204,7 @@ where
) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size),
B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size),
);
(
VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),
@@ -227,7 +227,7 @@ where
) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size),
MatZnx::bytes_of(n, rows, cols_in, cols_out, size),
);
(
MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),

View File

@@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl<Self> for FFT64Avx {
}
unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Avx {
fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize {
fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize {
Self::layout_prep_word_count() * n * cols * size_of::<f64>()
}
}

View File

@@ -27,7 +27,7 @@ use poulpy_hal::{
};
unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Avx {
fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_big_word_count() * n * cols * size * size_of::<f64>()
}
}

View File

@@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl<Self> for FFT64Avx {
}
unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Avx {
fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Avx as Backend>::ScalarPrep>()
}
}

View File

@@ -16,7 +16,7 @@ use poulpy_hal::{
use crate::cpu_fft64_avx::{FFT64Avx, module::FFT64ModuleHandle};
unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Avx {
fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>()
}
}

View File

@@ -69,7 +69,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols));
(
ScalarZnx::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -82,7 +82,7 @@ where
B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>,
{
fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols));
(
SvpPPol::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -95,7 +95,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size));
(
VecZnx::from_data(take_slice, n, cols, size),
Scratch::from_bytes(rem_slice),
@@ -115,7 +115,7 @@ where
) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_big_alloc_bytes_impl(n, cols, size),
B::vec_znx_big_bytes_of_impl(n, cols, size),
);
(
VecZnxBig::from_data(take_slice, n, cols, size),
@@ -136,7 +136,7 @@ where
) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_dft_alloc_bytes_impl(n, cols, size),
B::vec_znx_dft_bytes_of_impl(n, cols, size),
);
(
@@ -204,7 +204,7 @@ where
) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size),
B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size),
);
(
VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),
@@ -227,7 +227,7 @@ where
) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size),
MatZnx::bytes_of(n, rows, cols_in, cols_out, size),
);
(
MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),

View File

@@ -22,7 +22,7 @@ unsafe impl SvpPPolAllocImpl<Self> for FFT64Ref {
}
unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Ref {
fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize {
fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize {
Self::layout_prep_word_count() * n * cols * size_of::<f64>()
}
}

View File

@@ -27,7 +27,7 @@ use poulpy_hal::{
};
unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Ref {
fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_big_word_count() * n * cols * size * size_of::<f64>()
}
}

View File

@@ -24,7 +24,7 @@ unsafe impl VecZnxDftFromBytesImpl<Self> for FFT64Ref {
}
unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Ref {
fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Ref as Backend>::ScalarPrep>()
}
}

View File

@@ -16,7 +16,7 @@ use poulpy_hal::{
use crate::cpu_fft64_ref::{FFT64Ref, module::FFT64ModuleHandle};
unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Ref {
fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>()
}
}

View File

@@ -69,7 +69,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_scalar_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::alloc_bytes(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, ScalarZnx::bytes_of(n, cols));
(
ScalarZnx::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -82,7 +82,7 @@ where
B: SvpPPolAllocBytesImpl<B> + ScratchFromBytesImpl<B>,
{
fn take_svp_ppol_impl(scratch: &mut Scratch<B>, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_alloc_bytes_impl(n, cols));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, B::svp_ppol_bytes_of_impl(n, cols));
(
SvpPPol::from_data(take_slice, n, cols),
Scratch::from_bytes(rem_slice),
@@ -95,7 +95,7 @@ where
B: ScratchFromBytesImpl<B>,
{
fn take_vec_znx_impl(scratch: &mut Scratch<B>, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::alloc_bytes(n, cols, size));
let (take_slice, rem_slice) = take_slice_aligned(&mut scratch.data, VecZnx::bytes_of(n, cols, size));
(
VecZnx::from_data(take_slice, n, cols, size),
Scratch::from_bytes(rem_slice),
@@ -115,7 +115,7 @@ where
) -> (VecZnxBig<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_big_alloc_bytes_impl(n, cols, size),
B::vec_znx_big_bytes_of_impl(n, cols, size),
);
(
VecZnxBig::from_data(take_slice, n, cols, size),
@@ -136,7 +136,7 @@ where
) -> (VecZnxDft<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vec_znx_dft_alloc_bytes_impl(n, cols, size),
B::vec_znx_dft_bytes_of_impl(n, cols, size),
);
(
@@ -204,7 +204,7 @@ where
) -> (VmpPMat<&mut [u8], B>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
B::vmp_pmat_alloc_bytes_impl(n, rows, cols_in, cols_out, size),
B::vmp_pmat_bytes_of_impl(n, rows, cols_in, cols_out, size),
);
(
VmpPMat::from_data(take_slice, n, rows, cols_in, cols_out, size),
@@ -227,7 +227,7 @@ where
) -> (MatZnx<&mut [u8]>, &mut Scratch<B>) {
let (take_slice, rem_slice) = take_slice_aligned(
&mut scratch.data,
MatZnx::alloc_bytes(n, rows, cols_in, cols_out, size),
MatZnx::bytes_of(n, rows, cols_in, cols_out, size),
);
(
MatZnx::from_data(take_slice, n, rows, cols_in, cols_out, size),

View File

@@ -27,7 +27,7 @@ unsafe impl SvpPPolAllocImpl<Self> for FFT64Spqlios {
}
unsafe impl SvpPPolAllocBytesImpl<Self> for FFT64Spqlios {
fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize {
fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize {
FFT64Spqlios::layout_prep_word_count() * n * cols * size_of::<f64>()
}
}

View File

@@ -22,7 +22,7 @@ use poulpy_hal::{
};
unsafe impl VecZnxBigAllocBytesImpl<Self> for FFT64Spqlios {
fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_big_word_count() * n * cols * size * size_of::<f64>()
}
}

View File

@@ -30,7 +30,7 @@ unsafe impl VecZnxDftFromBytesImpl<Self> for FFT64Spqlios {
}
unsafe impl VecZnxDftAllocBytesImpl<Self> for FFT64Spqlios {
fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * cols * size * size_of::<<FFT64Spqlios as Backend>::ScalarPrep>()
}
}

View File

@@ -16,7 +16,7 @@ use crate::cpu_spqlios::{
};
unsafe impl VmpPMatAllocBytesImpl<Self> for FFT64Spqlios {
fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
fn vmp_pmat_bytes_of_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
Self::layout_prep_word_count() * n * rows * cols_in * cols_out * size * size_of::<f64>()
}
}

View File

@@ -18,7 +18,7 @@ unsafe impl SvpPPolAllocImpl<Self> for NTT120 {
}
unsafe impl SvpPPolAllocBytesImpl<Self> for NTT120 {
fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize {
fn svp_ppol_bytes_of_impl(n: usize, cols: usize) -> usize {
NTT120::layout_prep_word_count() * n * cols * size_of::<i64>()
}
}

View File

@@ -3,7 +3,7 @@ use poulpy_hal::{layouts::Backend, oep::VecZnxBigAllocBytesImpl};
use crate::cpu_spqlios::NTT120;
unsafe impl VecZnxBigAllocBytesImpl<NTT120> for NTT120 {
fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_big_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
NTT120::layout_big_word_count() * n * cols * size * size_of::<i128>()
}
}

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
use crate::cpu_spqlios::NTT120;
unsafe impl VecZnxDftAllocBytesImpl<NTT120> for NTT120 {
fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize {
fn vec_znx_dft_bytes_of_impl(n: usize, cols: usize, size: usize) -> usize {
NTT120::layout_prep_word_count() * n * cols * size * size_of::<i64>()
}
}

View File

@@ -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<Vec<u8>> = GGSW::alloc(&ggsw_layout);
let mut ct_glwe_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout);
let mut ct_glwe_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout);
let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout);
let mut ct_glwe_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout);
let mut ct_glwe_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout);
let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWECiphertext::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout),
| GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWE::external_product_scratch_space(&module, &glwe_out_layout, &glwe_in_layout, &ggsw_layout),
);
let mut source_xs = Source::new([0u8; 32]);
let mut source_xe = Source::new([0u8; 32]);
let mut source_xa = Source::new([0u8; 32]);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_in_layout);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow());
@@ -156,28 +155,28 @@ 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<Vec<u8>> = GGSW::alloc(&ggsw_layout);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout);
let mut ct_ggsw: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_layout);
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout);
let pt_rgsw: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n.into(), 1);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GGSW::encrypt_sk_scratch_space(&module, &ggsw_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWECiphertext::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout),
| GLWE::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWE::external_product_inplace_scratch_space(&module, &glwe_layout, &ggsw_layout),
);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk.prepare_alloc(&module, scratch.borrow());

View File

@@ -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<Vec<u8>> = AutomorphismKey::alloc(&gglwe_atk_layout);
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_in_layout);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_out_layout);
let mut ksk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&gglwe_atk_layout);
let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_in_layout);
let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_out_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_atk_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWECiphertext::keyswitch_scratch_space(
| GLWE::encrypt_sk_scratch_space(&module, &glwe_in_layout)
| GLWE::keyswitch_scratch_space(
&module,
&glwe_out_layout,
&glwe_in_layout,
@@ -81,7 +81,7 @@ 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<Vec<u8>> = GLWESecret::alloc(&glwe_in_layout);
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_in_layout);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow());
@@ -167,31 +167,31 @@ 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<Vec<u8>> = GLWESwitchingKey::alloc(&gglwe_layout);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_layout);
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&gglwe_layout);
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_layout);
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, &gglwe_layout)
| GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout),
| GLWE::encrypt_sk_scratch_space(&module, &glwe_layout)
| GLWE::keyswitch_inplace_scratch_space(&module, &glwe_layout, &gglwe_layout),
);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout);
let mut sk_in: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let sk_in_dft: GLWESecretPrepared<Vec<u8>, FFT64Spqlios> = sk_in.prepare_alloc(&module, scratch.borrow());
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_layout);
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_layout);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
ksk.encrypt_sk(

View File

@@ -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<FFT64Spqlios> = Module::<FFT64Spqlios>::new(n.0 as u64);
let glwe_ct_infos: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_ct_infos: GLWELayout = GLWELayout {
n,
base2k,
k: k_ct,
@@ -44,9 +43,9 @@ fn main() {
let glwe_pt_infos: GLWEPlaintextLayout = GLWEPlaintextLayout { n, base2k, k: k_pt };
// Allocates ciphertext & plaintexts
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_ct_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_pt_infos);
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_ct_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_pt_infos);
// CPRNG
let mut source_xs: Source = Source::new([0u8; 32]);
@@ -55,12 +54,11 @@ fn main() {
// Scratch space
let mut scratch: ScratchOwned<FFT64Spqlios> = ScratchOwned::alloc(
GLWECiphertext::encrypt_sk_scratch_space(&module, &glwe_ct_infos)
| GLWECiphertext::decrypt_scratch_space(&module, &glwe_ct_infos),
GLWE::encrypt_sk_scratch_space(&module, &glwe_ct_infos) | GLWE::decrypt_scratch_space(&module, &glwe_ct_infos),
);
// Generate secret-key
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_ct_infos);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_ct_infos);
sk.fill_ternary_prob(0.5, &mut source_xs);
// Backend-prepared secret

View File

@@ -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<Vec<u8>> {
pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>(
@@ -22,7 +22,7 @@ impl AutomorphismKey<Vec<u8>> {
KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::keyswitch_scratch_space(
GLWE::keyswitch_scratch_space(
module,
&out_infos.glwe_layout(),
&in_infos.glwe_layout(),
@@ -103,8 +103,8 @@ impl<DataSelf: DataMut> AutomorphismKey<DataSelf> {
(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<DataSelf: DataMut> AutomorphismKey<DataSelf> {
(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| {

View File

@@ -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<Vec<u8>> {
+ 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(),

View File

@@ -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<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn automorphism_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>,
out_infos: &OUT,
@@ -36,11 +36,11 @@ impl GLWECiphertext<Vec<u8>> {
}
}
impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn automorphism<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
@@ -93,7 +93,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_add<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
@@ -176,7 +176,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_ab<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
@@ -261,7 +261,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
pub fn automorphism_sub_negate<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &AutomorphismKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where

View File

@@ -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<Vec<u8>> {
impl LWE<Vec<u8>> {
pub fn from_glwe_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>,
lwe_infos: &OUT,
@@ -28,24 +25,24 @@ impl LWECiphertext<Vec<u8>> {
KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{
let glwe_layout: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_layout: GLWELayout = GLWELayout {
n: module.n().into(),
base2k: lwe_infos.base2k(),
k: lwe_infos.k(),
rank: Rank(1),
};
GLWECiphertext::alloc_bytes_with(
GLWE::bytes_of(
module.n().into(),
lwe_infos.base2k(),
lwe_infos.k(),
1u32.into(),
) + GLWECiphertext::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos)
) + GLWE::keyswitch_scratch_space(module, &glwe_layout, glwe_infos, key_infos)
}
}
impl<DLwe: DataMut> LWECiphertext<DLwe> {
pub fn sample_extract<DGlwe: DataRef>(&mut self, a: &GLWECiphertext<DGlwe>) {
impl<DLwe: DataMut> LWE<DLwe> {
pub fn sample_extract<DGlwe: DataRef>(&mut self, a: &GLWE<DGlwe>) {
#[cfg(debug_assertions)]
{
assert!(self.n() <= a.n());
@@ -66,7 +63,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
pub fn from_glwe<DGlwe, DKs, B: Backend>(
&mut self,
module: &Module<B>,
a: &GLWECiphertext<DGlwe>,
a: &GLWE<DGlwe>,
ks: &GLWEToLWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>,
) where
@@ -92,7 +89,7 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
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(),

View File

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

View File

@@ -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<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn decrypt_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{
let size: usize = infos.size();
(module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size)
(module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_bytes_of(1, size)) + module.vec_znx_dft_bytes_of(1, size)
}
}
impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
impl<DataSelf: DataRef> GLWE<DataSelf> {
pub fn decrypt<DataPt: DataMut, DataSk: DataRef, B: Backend>(
&self,
module: &Module<B>,

View File

@@ -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<DataSelf> LWECiphertext<DataSelf>
impl<DataSelf> LWE<DataSelf>
where
DataSelf: DataRef,
{

View File

@@ -20,8 +20,7 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{
assert_eq!(module.n() as u32, infos.n());
GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos)
+ GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out())
GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(infos.n(), infos.rank_out())
}
}

View File

@@ -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<D: DataMut> GGLWECiphertextCompressed<D> {
impl<D: DataMut> GGLWECompressed<D> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self,
@@ -34,7 +34,7 @@ impl<D: DataMut> GGLWECiphertextCompressed<D> {
}
}
impl GGLWECiphertextCompressed<Vec<u8>> {
impl GGLWECompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
@@ -54,7 +54,7 @@ pub trait GGLWECompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWECiphertextCompressedToMut,
R: GGLWECompressedToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>;
}
@@ -78,11 +78,11 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWECiphertextCompressedToMut,
R: GGLWECompressedToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>,
{
let res: &mut GGLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
#[cfg(debug_assertions)]
@@ -107,10 +107,10 @@ 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,

View File

@@ -23,9 +23,9 @@ impl GLWESwitchingKeyCompressed<Vec<u8>> {
A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{
(GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
+ ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::alloc_bytes(module, infos.rank_out())
(GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
+ ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::bytes_of(module, infos.rank_out())
}
}

View File

@@ -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<Vec<u8>> {
impl GGSWCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGSWInfos,
@@ -34,7 +34,7 @@ pub trait GGSWCompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGSWCiphertextCompressedToMut,
R: GGSWCompressedToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>;
}
@@ -53,11 +53,11 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGSWCiphertextCompressedToMut,
R: GGSWCompressedToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>,
{
let res: &mut GGSWCiphertextCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], B> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
@@ -113,7 +113,7 @@ where
}
}
impl<DataSelf: DataMut> GGSWCiphertextCompressed<DataSelf> {
impl<DataSelf: DataMut> GGSWCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self,

View File

@@ -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<Vec<u8>> {
impl GLWECompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{
GLWECiphertext::encrypt_sk_scratch_space(module, infos)
GLWE::encrypt_sk_scratch_space(module, infos)
}
}
@@ -33,7 +33,7 @@ pub trait GLWECompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
R: GLWECompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>;
}
@@ -51,11 +51,11 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
R: GLWECompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>,
{
let res: &mut GLWECiphertextCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut();
let mut source_xa: Source = Source::new(seed_xa);
let cols: usize = (res.rank() + 1).into();
@@ -77,7 +77,7 @@ where
}
}
impl<D: DataMut> GLWECiphertextCompressed<D> {
impl<D: DataMut> GLWECompressed<D> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self,

View File

@@ -27,7 +27,7 @@ impl AutomorphismKey<Vec<u8>> {
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKey"
);
GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes(&infos.glwe_layout())
GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) + GLWESecret::bytes_of(&infos.glwe_layout())
}
pub fn encrypt_pk_scratch_space<BE: Backend, A>(module: &Module<BE>, _infos: &A) -> usize

View File

@@ -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<Vec<u8>> {
A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout())
+ (GLWEPlaintext::alloc_bytes(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes())
GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout())
+ (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes())
}
pub fn encrypt_pk_scratch_space<B: Backend, A>(_module: &Module<B>, _infos: &A) -> usize
@@ -44,7 +44,7 @@ pub trait GGLWEEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWECiphertextToMut,
R: GGLWEToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>;
}
@@ -64,7 +64,7 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWECiphertextToMut,
R: GGLWEToMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<B>,
{

View File

@@ -20,9 +20,9 @@ impl GLWESwitchingKey<Vec<u8>> {
A: GGLWEInfos,
Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{
(GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
+ ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::alloc_bytes_from_infos(module, &infos.glwe_layout())
(GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
+ ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout())
}
pub fn encrypt_pk_scratch_space<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize

View File

@@ -23,11 +23,11 @@ impl TensorKey<Vec<u8>> {
Module<B>:
SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes,
{
GLWESecretPrepared::alloc_bytes(module, infos.rank_out())
+ module.vec_znx_dft_alloc_bytes(infos.rank_out().into(), 1)
+ module.vec_znx_big_alloc_bytes(1, 1)
+ module.vec_znx_dft_alloc_bytes(1, 1)
+ GLWESecret::alloc_bytes_with(Degree(module.n() as u32), Rank(1))
GLWESecretPrepared::bytes_of(module, infos.rank_out())
+ module.vec_znx_dft_bytes_of(infos.rank_out().into(), 1)
+ module.vec_znx_big_bytes_of(1, 1)
+ module.vec_znx_dft_bytes_of(1, 1)
+ GLWESecret::bytes_of(Degree(module.n() as u32), Rank(1))
+ GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
}
}

View File

@@ -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<Vec<u8>> {
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{
let size = infos.size();
GLWECiphertext::encrypt_sk_scratch_space(module, &infos.glwe_layout())
+ VecZnx::alloc_bytes(module.n(), (infos.rank() + 1).into(), size)
+ VecZnx::alloc_bytes(module.n(), 1, size)
+ module.vec_znx_dft_alloc_bytes((infos.rank() + 1).into(), size)
GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout())
+ VecZnx::bytes_of(module.n(), (infos.rank() + 1).into(), size)
+ VecZnx::bytes_of(module.n(), 1, size)
+ module.vec_znx_dft_bytes_of((infos.rank() + 1).into(), size)
}
}

View File

@@ -13,12 +13,12 @@ use crate::{
dist::Distribution,
encryption::{SIGMA, SIGMA_BOUND},
layouts::{
GLWECiphertext, GLWECiphertextToMut, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos,
GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, GLWEToMut, LWEInfos,
prepared::{GLWEPublicKeyPrepared, GLWEPublicKeyPreparedToRef, GLWESecretPrepared, GLWESecretPreparedToRef},
},
};
impl GLWECiphertext<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
@@ -26,9 +26,7 @@ impl GLWECiphertext<Vec<u8>> {
{
let size: usize = infos.size();
assert_eq!(module.n() as u32, infos.n());
module.vec_znx_normalize_tmp_bytes()
+ 2 * VecZnx::alloc_bytes(module.n(), 1, size)
+ module.vec_znx_dft_alloc_bytes(1, size)
module.vec_znx_normalize_tmp_bytes() + 2 * VecZnx::bytes_of(module.n(), 1, size) + module.vec_znx_dft_bytes_of(1, size)
}
pub fn encrypt_pk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
@@ -37,14 +35,13 @@ impl GLWECiphertext<Vec<u8>> {
{
let size: usize = infos.size();
assert_eq!(module.n() as u32, infos.n());
((module.vec_znx_dft_alloc_bytes(1, size) + module.vec_znx_big_alloc_bytes(1, size))
| ScalarZnx::alloc_bytes(module.n(), 1))
+ module.svp_ppol_alloc_bytes(1)
((module.vec_znx_dft_bytes_of(1, size) + module.vec_znx_big_bytes_of(1, size)) | ScalarZnx::bytes_of(module.n(), 1))
+ module.svp_ppol_bytes_of(1)
+ module.vec_znx_normalize_tmp_bytes()
}
}
impl<D: DataMut> GLWECiphertext<D> {
impl<D: DataMut> GLWE<D> {
pub fn encrypt_sk<R, P, S, B: Backend>(
&mut self,
module: &Module<B>,
@@ -116,7 +113,7 @@ pub trait GLWEEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>;
}
@@ -135,11 +132,11 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>,
{
let mut res: GLWECiphertext<&mut [u8]> = res.to_mut();
let mut res: GLWE<&mut [u8]> = res.to_mut();
let pt: GLWEPlaintext<&[u8]> = pt.to_ref();
#[cfg(debug_assertions)]
@@ -150,10 +147,10 @@ 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<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
S: GLWESecretPreparedToRef<B>;
}
@@ -200,10 +197,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
S: GLWESecretPreparedToRef<B>,
{
let mut res: GLWECiphertext<&mut [u8]> = res.to_mut();
let mut res: GLWE<&mut [u8]> = res.to_mut();
#[cfg(debug_assertions)]
{
@@ -212,10 +209,10 @@ 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<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
K: GLWEPublicKeyPreparedToRef<B>;
}
@@ -264,7 +261,7 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
K: GLWEPublicKeyPreparedToRef<B>,
{
@@ -281,7 +278,7 @@ pub trait GLWEEncryptZeroPk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
K: GLWEPublicKeyPreparedToRef<B>;
}
@@ -297,7 +294,7 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
K: GLWEPublicKeyPreparedToRef<B>,
{
self.glwe_encrypt_pk_internal(
@@ -321,7 +318,7 @@ pub(crate) trait GLWEEncryptPkInternal<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
K: GLWEPublicKeyPreparedToRef<B>;
}
@@ -345,11 +342,11 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextToMut,
R: GLWEToMut,
P: GLWEPlaintextToRef,
K: GLWEPublicKeyPreparedToRef<B>,
{
let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut();
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let pk: &GLWEPublicKeyPrepared<&[u8], B> = &pk.to_ref();
#[cfg(debug_assertions)]

View File

@@ -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<B> = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(self, res));
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::encrypt_sk_scratch_space(self, res));
let mut tmp: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(res);
let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(res);
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
res.dist = sk.dist;

View File

@@ -22,9 +22,8 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
A: GGLWEInfos,
Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{
GLWESecretPrepared::alloc_bytes(module, infos.rank_in())
+ (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos)
| GLWESecret::alloc_bytes_with(infos.n(), infos.rank_in()))
GLWESecretPrepared::bytes_of(module, infos.rank_in())
+ (GLWESwitchingKey::encrypt_sk_scratch_space(module, infos) | GLWESecret::bytes_of(infos.n(), infos.rank_in()))
}
}

View File

@@ -7,10 +7,10 @@ use poulpy_hal::{
use crate::{
encryption::{SIGMA, SIGMA_BOUND},
layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret},
layouts::{LWE, LWEInfos, LWEPlaintext, LWESecret},
};
impl<DataSelf: DataMut> LWECiphertext<DataSelf> {
impl<DataSelf: DataMut> LWE<DataSelf> {
pub fn encrypt_sk<DataPt, DataSk, B>(
&mut self,
module: &Module<B>,

View File

@@ -38,8 +38,8 @@ impl LWESwitchingKey<Vec<u8>> {
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)
}
}

View File

@@ -26,7 +26,7 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
"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())
}
}

View File

@@ -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<Vec<u8>> {
pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(
@@ -22,7 +22,7 @@ impl GLWESwitchingKey<Vec<u8>> {
GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::external_product_scratch_space(
GLWE::external_product_scratch_space(
module,
&out_infos.glwe_layout(),
&in_infos.glwe_layout(),
@@ -40,7 +40,7 @@ impl GLWESwitchingKey<Vec<u8>> {
GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos)
GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), ggsw_infos)
}
}

View File

@@ -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<Vec<u8>> {
#[allow(clippy::too_many_arguments)]
@@ -23,7 +23,7 @@ impl GGSW<Vec<u8>> {
GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::external_product_scratch_space(
GLWE::external_product_scratch_space(
module,
&out_infos.glwe_layout(),
&in_infos.glwe_layout(),
@@ -41,7 +41,7 @@ impl GGSW<Vec<u8>> {
GGSW: GGSWInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos)
GLWE::external_product_inplace_scratch_space(module, &out_infos.glwe_layout(), apply_infos)
}
}

View File

@@ -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<Vec<u8>> {
impl GLWE<Vec<u8>> {
#[allow(clippy::too_many_arguments)]
pub fn external_product_scratch_space<B: Backend, OUT, IN, GGSW>(
module: &Module<B>,
@@ -35,8 +32,8 @@ impl GLWECiphertext<Vec<u8>> {
.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<Vec<u8>> {
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<Vec<u8>> {
}
}
impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn external_product<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
@@ -88,15 +85,15 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
rhs: &GGSWPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
Module<B>: GLWEExternalProductInplace<B>,
Module<B>: GLWEExternalProduct<B>,
{
module.external_product_inplace(self, rhs, scratch);
}
}
impl<BE: Backend> GLWEExternalProductInplace<BE> for Module<BE>
pub trait GLWEExternalProduct<BE: Backend>
where
Module<BE>: VecZnxDftAllocBytes
Self: VecZnxDftAllocBytes
+ VmpApplyDftToDftTmpBytes
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE>
@@ -107,12 +104,12 @@ where
+ VecZnxNormalize<BE>,
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx,
{
fn external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>)
fn glwe_external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>)
where
R: GLWECiphertextToMut,
R: GLWEToMut,
D: GGSWCiphertextPreparedToRef<BE>,
{
let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut();
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let rhs: &GGSWPrepared<&[u8], BE> = &ggsw.to_ref();
let basek_in: usize = res.base2k().into();
@@ -124,7 +121,7 @@ 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<BE: Backend> GLWEExternalProduct<BE> for Module<BE>
where
Module<BE>: VecZnxDftAllocBytes
+ VmpApplyDftToDftTmpBytes
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE>
+ VmpApplyDftToDft<BE>
+ VmpApplyDftToDftAdd<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalize<BE>,
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx,
{
fn external_product<R, A, D>(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch<BE>)
fn glwe_external_product<R, A, D>(&self, res: &mut R, lhs: &A, rhs: &D, scratch: &mut Scratch<BE>)
where
R: GLWECiphertextToMut,
A: GLWECiphertextToRef,
R: GLWEToMut,
A: GLWEToRef,
D: GGSWCiphertextPreparedToRef<BE>,
{
let res: &mut GLWECiphertext<&mut [u8]> = &mut res.to_mut();
let lhs: &GLWECiphertext<&[u8]> = &lhs.to_ref();
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
let lhs: &GLWE<&[u8]> = &lhs.to_ref();
let rhs: &GGSWPrepared<&[u8], BE> = &rhs.to_ref();
@@ -251,7 +234,7 @@ 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<BE: Backend> GLWEExternalProduct<BE> for Module<BE>
where
Self: VecZnxDftAllocBytes
+ VmpApplyDftToDftTmpBytes
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE>
+ VmpApplyDftToDft<BE>
+ VmpApplyDftToDftAdd<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalize<BE>,
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx,
{
}

View File

@@ -1,23 +1,8 @@
use poulpy_hal::layouts::{Backend, Scratch};
use crate::layouts::{GLWECiphertextToMut, GLWECiphertextToRef, prepared::GGSWCiphertextPreparedToRef};
use crate::layouts::{GLWEToMut, GLWEToRef, prepared::GGSWCiphertextPreparedToRef};
mod gglwe_atk;
mod gglwe_ksk;
mod ggsw_ct;
mod glwe_ct;
pub trait GLWEExternalProduct<BE: Backend> {
fn external_product<R, A, D>(&self, res: &mut R, a: &A, ggsw: &D, scratch: &mut Scratch<BE>)
where
R: GLWECiphertextToMut,
A: GLWECiphertextToRef,
D: GGSWCiphertextPreparedToRef<BE>;
}
pub trait GLWEExternalProductInplace<BE: Backend> {
fn external_product_inplace<R, D>(&self, res: &mut R, ggsw: &D, scratch: &mut Scratch<BE>)
where
R: GLWECiphertextToMut,
D: GGSWCiphertextPreparedToRef<BE>;
}

View File

@@ -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<Vec<u8>>,
data: GLWE<Vec<u8>>,
value: bool, // Implicit flag for zero ciphertext
control: bool, // Can be combined with incoming value
}
@@ -48,7 +48,7 @@ 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<B: Backend>(module: &Module<B>) -> Vec<i64> {
GLWECiphertext::trace_galois_elements(module)
GLWE::trace_galois_elements(module)
}
/// Adds a GLWE ciphertext to the [GLWEPacker].
@@ -115,7 +115,7 @@ impl GLWEPacker {
pub fn add<DataA: DataRef, DataAK: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
a: Option<&GLWECiphertext<DataA>>,
a: Option<&GLWE<DataA>>,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
@@ -162,7 +162,7 @@ impl GLWEPacker {
}
/// Flush result to`res`.
pub fn flush<Data: DataMut, B: Backend>(&mut self, module: &Module<B>, res: &mut GLWECiphertext<Data>)
pub fn flush<Data: DataMut, B: Backend>(&mut self, module: &Module<B>, res: &mut GLWE<Data>)
where
Module<B>: VecZnxCopy,
{
@@ -188,7 +188,7 @@ where
fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(
module: &Module<B>,
a: Option<&GLWECiphertext<D>>,
a: Option<&GLWE<D>>,
accumulators: &mut [Accumulator],
i: usize,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
@@ -258,7 +258,7 @@ fn pack_core<D: DataRef, DataAK: DataRef, B: Backend>(
} else {
pack_core(
module,
None::<&GLWECiphertext<Vec<u8>>>,
None::<&GLWE<Vec<u8>>>,
acc_next,
i + 1,
auto_keys,
@@ -274,16 +274,15 @@ where
KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::alloc_bytes(out_infos)
+ (GLWECiphertext::rsh_scratch_space(module.n())
| GLWECiphertext::automorphism_inplace_scratch_space(module, out_infos, key_infos))
GLWE::bytes_of(out_infos)
+ (GLWE::rsh_scratch_space(module.n()) | GLWE::automorphism_inplace_scratch_space(module, out_infos, key_infos))
}
/// [combine] merges two ciphertexts together.
fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
module: &Module<B>,
acc: &mut Accumulator,
b: Option<&GLWECiphertext<D>>,
b: Option<&GLWE<D>>,
i: usize,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
@@ -314,7 +313,7 @@ fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeGLWECt,
{
let log_n: usize = acc.data.n().log2();
let a: &mut GLWECiphertext<Vec<u8>> = &mut acc.data;
let a: &mut GLWE<Vec<u8>> = &mut acc.data;
let gal_el: i64 = if i == 0 {
-1
@@ -395,7 +394,7 @@ fn combine<D: DataRef, DataAK: DataRef, B: Backend>(
/// to [0: GLWE(m_0 * X^x_0 + m_1 * X^x_1 + ... + m_i * X^x_i)]
pub fn glwe_packing<D: DataMut, ATK, B: Backend>(
module: &Module<B>,
cts: &mut HashMap<usize, &mut GLWECiphertext<D>>,
cts: &mut HashMap<usize, &mut GLWE<D>>,
log_gap_out: usize,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, B>>,
scratch: &mut Scratch<B>,
@@ -446,8 +445,8 @@ pub fn glwe_packing<D: DataMut, ATK, B: Backend>(
};
(0..t).for_each(|j| {
let mut a: Option<&mut GLWECiphertext<D>> = cts.remove(&j);
let mut b: Option<&mut GLWECiphertext<D>> = cts.remove(&(j + t));
let mut a: Option<&mut GLWE<D>> = cts.remove(&j);
let mut b: Option<&mut GLWE<D>> = cts.remove(&(j + t));
pack_internal(module, &mut a, &mut b, i, auto_key, scratch);
@@ -463,8 +462,8 @@ pub fn glwe_packing<D: DataMut, ATK, B: Backend>(
#[allow(clippy::too_many_arguments)]
fn pack_internal<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>(
module: &Module<B>,
a: &mut Option<&mut GLWECiphertext<A>>,
b: &mut Option<&mut GLWECiphertext<D>>,
a: &mut Option<&mut GLWE<A>>,
b: &mut Option<&mut GLWE<D>>,
i: usize,
auto_key: &AutomorphismKeyPrepared<DataAK, B>,
scratch: &mut Scratch<B>,

View File

@@ -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<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn trace_galois_elements<B: Backend>(module: &Module<B>) -> Vec<i64> {
let mut gal_els: Vec<i64> = Vec::new();
(0..module.log_n()).for_each(|i| {
@@ -42,7 +42,7 @@ impl GLWECiphertext<Vec<u8>> {
{
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<Vec<u8>> {
}
}
impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn trace<DataLhs: DataRef, DataAK: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
start: usize,
end: usize,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<DataAK, B>>,
scratch: &mut Scratch<B>,
) where
@@ -135,7 +135,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
}
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(),

View File

@@ -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<Vec<u8>> {
KEY: GGLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::keyswitch_scratch_space(module, out_infos, in_infos, key_apply)
GLWE::keyswitch_scratch_space(module, out_infos, in_infos, key_apply)
}
pub fn keyswitch_inplace_scratch_space<B: Backend, OUT, KEY>(module: &Module<B>, out_infos: &OUT, key_apply: &KEY) -> usize
@@ -107,7 +107,7 @@ impl GLWESwitchingKey<Vec<u8>> {
KEY: GGLWEInfos + GLWEInfos,
Module<B>: VecZnxDftAllocBytes + VmpApplyDftToDftTmpBytes + VecZnxBigNormalizeTmpBytes + VecZnxNormalizeTmpBytes,
{
GLWECiphertext::keyswitch_inplace_scratch_space(module, out_infos, key_apply)
GLWE::keyswitch_inplace_scratch_space(module, out_infos, key_apply)
}
}

View File

@@ -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<Vec<u8>> {
.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<Vec<u8>> {
(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<Vec<u8>> {
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,

View File

@@ -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<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>,
out_infos: &OUT,
@@ -28,8 +28,8 @@ impl GLWECiphertext<Vec<u8>> {
.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<Vec<u8>> {
(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<Vec<u8>> {
}
}
impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
impl<DataSelf: DataRef> GLWE<DataSelf> {
#[allow(dead_code)]
pub(crate) fn assert_keyswitch<B: Backend, DataLhs, DataRhs>(
&self,
module: &Module<B>,
lhs: &GLWECiphertext<DataLhs>,
lhs: &GLWE<DataLhs>,
rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &Scratch<B>,
) where
@@ -93,7 +93,7 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
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<DataSelf: DataRef> GLWECiphertext<DataSelf> {
assert_eq!(rhs.n(), self.n());
let scrach_needed: usize = GLWECiphertext::keyswitch_inplace_scratch_space(module, self, rhs);
let scrach_needed: usize = GLWE::keyswitch_inplace_scratch_space(module, self, rhs);
assert!(
scratch.available() >= scrach_needed,
@@ -144,11 +144,11 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
}
}
impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
impl<DataSelf: DataMut> GLWE<DataSelf> {
pub fn keyswitch<DataLhs: DataRef, DataRhs: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
glwe_in: &GLWECiphertext<DataLhs>,
glwe_in: &GLWE<DataLhs>,
rhs: &GLWESwitchingKeyPrepared<DataRhs, B>,
scratch: &mut Scratch<B>,
) where
@@ -232,7 +232,7 @@ impl<DataSelf: DataMut> GLWECiphertext<DataSelf> {
}
}
impl<D: DataRef> GLWECiphertext<D> {
impl<D: DataRef> GLWE<D> {
pub(crate) fn keyswitch_internal<B: Backend, DataRes, DataKey>(
&self,
module: &Module<B>,

View File

@@ -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<Vec<u8>> {
impl LWE<Vec<u8>> {
pub fn keyswitch_scratch_space<B: Backend, OUT, IN, KEY>(
module: &Module<B>,
out_infos: &OUT,
@@ -40,33 +37,33 @@ impl LWECiphertext<Vec<u8>> {
{
let max_k: TorusPrecision = in_infos.k().max(out_infos.k());
let glwe_in_infos: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_in_infos: GLWELayout = GLWELayout {
n: module.n().into(),
base2k: in_infos.base2k(),
k: max_k,
rank: Rank(1),
};
let glwe_out_infos: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_out_infos: GLWELayout = GLWELayout {
n: module.n().into(),
base2k: out_infos.base2k(),
k: max_k,
rank: Rank(1),
};
let glwe_in: usize = GLWECiphertext::alloc_bytes(&glwe_in_infos);
let glwe_out: usize = GLWECiphertext::alloc_bytes(&glwe_out_infos);
let ks: usize = GLWECiphertext::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos);
let glwe_in: usize = GLWE::bytes_of(&glwe_in_infos);
let glwe_out: usize = GLWE::bytes_of(&glwe_out_infos);
let ks: usize = GLWE::keyswitch_scratch_space(module, &glwe_out_infos, &glwe_in_infos, key_infos);
glwe_in + glwe_out + ks
}
}
impl<DLwe: DataMut> LWECiphertext<DLwe> {
impl<DLwe: DataMut> LWE<DLwe> {
pub fn keyswitch<A, DKs, B: Backend>(
&mut self,
module: &Module<B>,
a: &LWECiphertext<A>,
a: &LWE<A>,
ksk: &LWESwitchingKeyPrepared<DKs, B>,
scratch: &mut Scratch<B>,
) where
@@ -90,14 +87,14 @@ impl<DLwe: DataMut> LWECiphertext<DLwe> {
{
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<DLwe: DataMut> LWECiphertext<DLwe> {
});
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,

View File

@@ -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<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
}
}
pub trait AutomorphismKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_automorphism_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKeyCompressed<Vec<u8>> {
AutomorphismKeyCompressed {
key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(infos.rank_in(), infos.rank_out());
self.alloc_automorphism_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
fn bytes_of_automorphism_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize)
}
fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.rank_in(), infos.rank_out());
self.bytes_of_automorphism_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: AutomorphismKeyCompressedAlloc,
{
debug_assert_eq!(infos.rank_in(), infos.rank_out());
Self {
key: GLWESwitchingKeyCompressed::alloc(infos),
p: 0,
}
module.alloc_automorphism_key_compressed_from_infos(infos)
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
Self {
key: GLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: AutomorphismKeyCompressedAlloc,
{
module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: AutomorphismKeyCompressedAlloc,
{
debug_assert_eq!(infos.rank_in(), infos.rank_out());
GLWESwitchingKeyCompressed::alloc_bytes(infos)
module.bytes_of_automorphism_key_compressed_from_infos(infos)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize)
pub fn bytes_of<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: AutomorphismKeyCompressedAlloc,
{
module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
}
}
@@ -122,13 +190,32 @@ impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, AutomorphismKeyCompressed<DR>> for AutomorphismKey<D>
pub trait AutomorphismKeyDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GLWESwitchingKeyDecompress,
{
fn decompress(&mut self, module: &Module<B>, other: &AutomorphismKeyCompressed<DR>) {
self.key.decompress(module, &other.key);
self.p = other.p;
fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
{
self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key);
res.set_p(other.p());
}
}
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: AutomorphismKeyDecompress {}
impl<D: DataMut> AutomorphismKey<D>
where
Self: SetAutomorphismGaloisElement,
{
pub fn decompressed<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
Module<B>: AutomorphismKeyDecompress,
{
module.decompress_automorphism_key(self, other);
}
}

View File

@@ -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<D: Data> {
pub struct GGLWECompressed<D: Data> {
pub(crate) data: MatZnx<D>,
pub(crate) base2k: Base2K,
pub(crate) k: TorusPrecision,
@@ -23,7 +23,7 @@ pub struct GGLWECiphertextCompressed<D: Data> {
pub(crate) seed: Vec<[u8; 32]>,
}
impl<D: Data> LWEInfos for GGLWECiphertextCompressed<D> {
impl<D: Data> LWEInfos for GGLWECompressed<D> {
fn n(&self) -> Degree {
Degree(self.data.n() as u32)
}
@@ -40,13 +40,13 @@ impl<D: Data> LWEInfos for GGLWECiphertextCompressed<D> {
self.data.size()
}
}
impl<D: Data> GLWEInfos for GGLWECiphertextCompressed<D> {
impl<D: Data> GLWEInfos for GGLWECompressed<D> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data> GGLWEInfos for GGLWECiphertextCompressed<D> {
impl<D: Data> GGLWEInfos for GGLWECompressed<D> {
fn rank_in(&self) -> Rank {
Rank(self.data.cols_in() as u32)
}
@@ -64,53 +64,41 @@ impl<D: Data> GGLWEInfos for GGLWECiphertextCompressed<D> {
}
}
impl<D: DataRef> fmt::Debug for GGLWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Debug for GGLWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataMut> FillUniform for GGLWECiphertextCompressed<D> {
impl<D: DataMut> FillUniform for GGLWECompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source);
}
}
impl<D: DataRef> fmt::Display for GGLWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Display for GGLWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"(GGLWECiphertextCompressed: base2k={} k={} dsize={}) {}",
"(GGLWECompressed: base2k={} k={} dsize={}) {}",
self.base2k.0, self.k.0, self.dsize.0, self.data
)
}
}
impl GGLWECiphertextCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc_with(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_with(
n: Degree,
pub trait GGLWECompressedAlloc
where
Self: GetDegree,
{
fn alloc_gglwe_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self {
) -> GGLWECompressed<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -125,9 +113,9 @@ impl GGLWECiphertextCompressed<Vec<u8>> {
dsize.0,
);
Self {
GGLWECompressed {
data: MatZnx::alloc(
n.into(),
self.n().into(),
dnum.into(),
rank_in.into(),
1,
@@ -141,21 +129,22 @@ impl GGLWECiphertextCompressed<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_gglwe_compressed_from_infos<A>(&self, infos: &A) -> GGLWECompressed<Vec<u8>>
where
A: GGLWEInfos,
{
Self::alloc_bytes_with(
infos.n(),
assert_eq!(infos.n(), self.n());
self.alloc_gglwe_compressed(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize {
fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -170,20 +159,83 @@ impl GGLWECiphertextCompressed<Vec<u8>> {
dsize.0,
);
MatZnx::alloc_bytes(
n.into(),
MatZnx::bytes_of(
self.n().into(),
dnum.into(),
rank_in.into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_gglwe_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.n(), self.n());
self.bytes_of_gglwe_compressed(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<D: DataRef> GGLWECiphertextCompressed<D> {
pub(crate) fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> {
impl<B: Backend> GGLWECompressedAlloc for Module<B> where Self: GetDegree {}
impl GGLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: GGLWECompressedAlloc,
{
module.alloc_gglwe_compressed_from_infos(infos)
}
pub fn alloc<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
Module<B>: GGLWECompressedAlloc,
{
module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: GGLWECompressedAlloc,
{
module.bytes_of_gglwe_compressed_from_infos(infos)
}
pub fn byte_of<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GGLWECompressedAlloc,
{
module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize)
}
}
impl<D: DataRef> GGLWECompressed<D> {
pub(crate) fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> {
let rank_in: usize = self.rank_in().into();
GLWECiphertextCompressed {
GLWECompressed {
data: self.data.at(row, col),
k: self.k,
base2k: self.base2k,
@@ -193,10 +245,10 @@ impl<D: DataRef> GGLWECiphertextCompressed<D> {
}
}
impl<D: DataMut> GGLWECiphertextCompressed<D> {
pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> {
impl<D: DataMut> GGLWECompressed<D> {
pub(crate) fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> {
let rank_in: usize = self.rank_in().into();
GLWECiphertextCompressed {
GLWECompressed {
k: self.k,
base2k: self.base2k,
rank: self.rank_out,
@@ -206,7 +258,7 @@ impl<D: DataMut> GGLWECiphertextCompressed<D> {
}
}
impl<D: DataMut> ReaderFrom for GGLWECiphertextCompressed<D> {
impl<D: DataMut> ReaderFrom for GGLWECompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -221,7 +273,7 @@ impl<D: DataMut> ReaderFrom for GGLWECiphertextCompressed<D> {
}
}
impl<D: DataRef> WriterTo for GGLWECiphertextCompressed<D> {
impl<D: DataRef> WriterTo for GGLWECompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?;
@@ -235,70 +287,50 @@ impl<D: DataRef> WriterTo for GGLWECiphertextCompressed<D> {
}
}
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGLWECiphertextCompressed<DR>> for GGLWE<D>
pub trait GGLWEDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GLWEDecompress,
{
fn decompress(&mut self, module: &Module<B>, other: &GGLWECiphertextCompressed<DR>) {
#[cfg(debug_assertions)]
{
assert_eq!(
self.n(),
other.n(),
"invalid receiver: self.n()={} != other.n()={}",
self.n(),
other.n()
);
assert_eq!(
self.size(),
other.size(),
"invalid receiver: self.size()={} != other.size()={}",
self.size(),
other.size()
);
assert_eq!(
self.rank_in(),
other.rank_in(),
"invalid receiver: self.rank_in()={} != other.rank_in()={}",
self.rank_in(),
other.rank_in()
);
assert_eq!(
self.rank_out(),
other.rank_out(),
"invalid receiver: self.rank_out()={} != other.rank_out()={}",
self.rank_out(),
other.rank_out()
);
fn decompress_gglwe<R, O>(&self, res: &mut R, other: &O)
where
R: GGLWEToMut,
O: GGLWECompressedToRef,
{
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let other: &GGLWECompressed<&[u8]> = &other.to_ref();
assert_eq!(
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<B: Backend> GGLWEDecompress for Module<B> where Self: VecZnxFillUniform + VecZnxCopy {}
impl<D: DataMut> GGLWE<D> {
pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: GGLWECompressedToRef,
Module<B>: GGLWEDecompress,
{
module.decompress_gglwe(self, other);
}
}
impl<D: DataMut> GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed<D> {
fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> {
GGLWECiphertextCompressed {
pub trait GGLWECompressedToMut {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]>;
}
impl<D: DataMut> GGLWECompressedToMut for GGLWECompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
GGLWECompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
@@ -309,13 +341,13 @@ impl<D: DataMut> GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed<D>
}
}
pub trait GGLWECiphertextCompressedToRef {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>;
pub trait GGLWECompressedToRef {
fn to_ref(&self) -> GGLWECompressed<&[u8]>;
}
impl<D: DataRef> GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed<D> {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> {
GGLWECiphertextCompressed {
impl<D: DataRef> GGLWECompressedToRef for GGLWECompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
GGLWECompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),

View File

@@ -1,19 +1,19 @@
use poulpy_hal::{
api::{VecZnxCopy, VecZnxFillUniform},
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut,
LWEInfos, Rank, TorusPrecision,
compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct GLWESwitchingKeyCompressed<D: Data> {
pub(crate) key: GGLWECiphertextCompressed<D>,
pub(crate) key: GGLWECompressed<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
}
@@ -81,43 +81,103 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
}
}
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
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<A>(infos: &A) -> usize
fn alloc_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyCompressed<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_gglwe_compressed_from_infos(infos)
}
}
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: GLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_switching_key_compressed_from_infos(infos)
}
pub fn alloc<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
Module<B>: GLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: GLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize)
}
}
@@ -137,14 +197,31 @@ impl<D: DataRef> WriterTo for GLWESwitchingKeyCompressed<D> {
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GLWESwitchingKeyCompressed<DR>> for GLWESwitchingKey<D>
pub trait GLWESwitchingKeyDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GGLWEDecompress,
{
fn decompress(&mut self, module: &Module<B>, other: &GLWESwitchingKeyCompressed<DR>) {
self.key.decompress(module, &other.key);
self.sk_in_n = other.sk_in_n;
self.sk_out_n = other.sk_out_n;
fn decompress_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where
R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData,
O: GLWESwitchingKeyCompressedToRef,
{
let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref();
self.decompress_gglwe(&mut res.to_mut().key, &other.key);
res.set_sk_in_n(other.sk_in_n);
res.set_sk_out_n(other.sk_out_n);
}
}
impl<B: Backend> GLWESwitchingKeyDecompress for Module<B> where Self: GGLWEDecompress {}
impl<D: DataMut> GLWESwitchingKey<D> {
pub fn decompress<O, B: Backend>(&mut self, module: Module<B>, other: &O)
where
O: GLWESwitchingKeyCompressedToRef,
Module<B>: GGLWEDecompress,
{
module.decompress_glwe_switching_key(self, other);
}
}
@@ -154,7 +231,7 @@ pub trait GLWESwitchingKeyCompressedToMut {
impl<D: DataMut> GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed<D>
where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToMut,
GGLWECompressed<D>: GGLWECompressedToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> {
GLWESwitchingKeyCompressed {
@@ -171,7 +248,7 @@ pub trait GLWESwitchingKeyCompressedToRef {
impl<D: DataRef> GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed<D>
where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToRef,
GGLWECompressed<D>: GGLWECompressedToRef,
{
fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> {
GLWESwitchingKeyCompressed {

View File

@@ -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<D: DataRef> fmt::Display for TensorKeyCompressed<D> {
}
}
impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub trait TensorKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn tensor_key_compressed_alloc(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> TensorKeyCompressed<Vec<u8>> {
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyCompressed {
keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
fn tensor_key_compressed_alloc_from_infos<A>(&self, infos: &A) -> TensorKeyCompressed<Vec<u8>>
where
A: GGLWEInfos,
{
@@ -90,58 +111,70 @@ impl TensorKeyCompressed<Vec<u8>> {
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyCompressed"
);
Self::alloc_with(
infos.n(),
self.tensor_key_compressed_alloc(
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let mut keys: Vec<GLWESwitchingKeyCompressed<Vec<u8>>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| {
keys.push(GLWESwitchingKeyCompressed::alloc_with(
n,
base2k,
k,
Rank(1),
rank,
dnum,
dsize,
));
});
Self { keys }
fn tensor_key_compressed_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn tensor_key_compressed_bytes_of_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyCompressed"
);
let rank_out: usize = infos.rank_out().into();
let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1);
pairs
* GLWESwitchingKeyCompressed::alloc_bytes_with(
infos.n(),
infos.base2k(),
infos.k(),
Rank(1),
infos.dnum(),
infos.dsize(),
)
self.tensor_key_compressed_bytes_of(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: TensorKeyCompressedAlloc,
{
module.tensor_key_compressed_alloc_from_infos(infos)
}
pub fn alloc_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<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: TensorKeyCompressedAlloc,
{
module.tensor_key_compressed_alloc(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: TensorKeyCompressedAlloc,
{
module.tensor_key_compressed_bytes_of_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: TensorKeyCompressedAlloc,
{
module.tensor_key_compressed_bytes_of(base2k, k, rank, dnum, dsize)
}
}
@@ -181,28 +214,41 @@ impl<D: DataMut> TensorKeyCompressed<D> {
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, TensorKeyCompressed<DR>> for TensorKey<D>
pub trait TensorKeyDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GLWESwitchingKeyDecompress,
{
fn decompress(&mut self, module: &Module<B>, other: &TensorKeyCompressed<DR>) {
#[cfg(debug_assertions)]
{
assert_eq!(
self.keys.len(),
other.keys.len(),
"invalid receiver: self.keys.len()={} != other.keys.len()={}",
self.keys.len(),
other.keys.len()
);
}
fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O)
where
R: TensorKeyToMut,
O: TensorKeyCompressedToRef,
{
let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut();
let other: &TensorKeyCompressed<&[u8]> = &other.to_ref();
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<B: Backend> TensorKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
impl<D: DataMut> TensorKey<D> {
pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: TensorKeyCompressedToRef,
Module<B>: GLWESwitchingKeyDecompress,
{
module.decompress_tensor_key(self, other);
}
}

View File

@@ -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<D: Data> {
pub struct GGSWCompressed<D: Data> {
pub(crate) data: MatZnx<D>,
pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K,
@@ -23,7 +22,7 @@ pub struct GGSWCiphertextCompressed<D: Data> {
pub(crate) seed: Vec<[u8; 32]>,
}
impl<D: Data> LWEInfos for GGSWCiphertextCompressed<D> {
impl<D: Data> LWEInfos for GGSWCompressed<D> {
fn n(&self) -> Degree {
Degree(self.data.n() as u32)
}
@@ -39,13 +38,13 @@ impl<D: Data> LWEInfos for GGSWCiphertextCompressed<D> {
self.data.size()
}
}
impl<D: Data> GLWEInfos for GGSWCiphertextCompressed<D> {
impl<D: Data> GLWEInfos for GGSWCompressed<D> {
fn rank(&self) -> Rank {
self.rank
}
}
impl<D: Data> GGSWInfos for GGSWCiphertextCompressed<D> {
impl<D: Data> GGSWInfos for GGSWCompressed<D> {
fn dsize(&self) -> Dsize {
self.dsize
}
@@ -55,46 +54,42 @@ impl<D: Data> GGSWInfos for GGSWCiphertextCompressed<D> {
}
}
impl<D: DataRef> fmt::Debug for GGSWCiphertextCompressed<D> {
impl<D: DataRef> fmt::Debug for GGSWCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.data)
}
}
impl<D: DataRef> fmt::Display for GGSWCiphertextCompressed<D> {
impl<D: DataRef> fmt::Display for GGSWCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"(GGSWCiphertextCompressed: base2k={} k={} dsize={}) {}",
"(GGSWCompressed: base2k={} k={} dsize={}) {}",
self.base2k, self.k, self.dsize, self.data
)
}
}
impl<D: DataMut> FillUniform for GGSWCiphertextCompressed<D> {
impl<D: DataMut> FillUniform for GGSWCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source);
}
}
impl GGSWCiphertextCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GGSWInfos,
{
Self::alloc_with(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
pub trait GGSWCompressedAlloc
where
Self: GetDegree,
{
fn alloc_ggsw_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGSWCompressed<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
assert!(
size as u32 > dsize.0,
"invalid ggsw: ceil(k/base2k): {size} <= dsize: {}",
dsize.0
@@ -107,9 +102,9 @@ impl GGSWCiphertextCompressed<Vec<u8>> {
dsize.0,
);
Self {
GGSWCompressed {
data: MatZnx::alloc(
n.into(),
self.n().into(),
dnum.into(),
(rank + 1).into(),
1,
@@ -123,12 +118,11 @@ impl GGSWCiphertextCompressed<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_ggsw_compressed_from_infos<A>(&self, infos: &A) -> GGSWCompressed<Vec<u8>>
where
A: GGSWInfos,
{
Self::alloc_bytes_with(
infos.n(),
self.alloc_ggsw_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
@@ -137,9 +131,9 @@ impl GGSWCiphertextCompressed<Vec<u8>> {
)
}
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<Vec<u8>> {
dsize.0,
);
MatZnx::alloc_bytes(
n.into(),
MatZnx::bytes_of(
self.n().into(),
dnum.into(),
(rank + 1).into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_ggsw_compressed_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGSWInfos,
{
self.bytes_of_ggsw_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<D: DataRef> GGSWCiphertextCompressed<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertextCompressed<&[u8]> {
impl GGSWCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGSWInfos,
Module<B>: GGSWCompressedAlloc,
{
module.alloc_ggsw_compressed_from_infos(infos)
}
pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: GGSWCompressedAlloc,
{
module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGSWInfos,
Module<B>: GGSWCompressedAlloc,
{
module.bytes_of_ggsw_compressed_key_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GGSWCompressedAlloc,
{
module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize)
}
}
impl<D: DataRef> GGSWCompressed<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECompressed<&[u8]> {
let rank: usize = self.rank().into();
GLWECiphertextCompressed {
GLWECompressed {
data: self.data.at(row, col),
k: self.k,
base2k: self.base2k,
@@ -175,10 +221,10 @@ impl<D: DataRef> GGSWCiphertextCompressed<D> {
}
}
impl<D: DataMut> GGSWCiphertextCompressed<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertextCompressed<&mut [u8]> {
impl<D: DataMut> GGSWCompressed<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECompressed<&mut [u8]> {
let rank: usize = self.rank().into();
GLWECiphertextCompressed {
GLWECompressed {
data: self.data.at_mut(row, col),
k: self.k,
base2k: self.base2k,
@@ -188,7 +234,7 @@ impl<D: DataMut> GGSWCiphertextCompressed<D> {
}
}
impl<D: DataMut> ReaderFrom for GGSWCiphertextCompressed<D> {
impl<D: DataMut> ReaderFrom for GGSWCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -203,7 +249,7 @@ impl<D: DataMut> ReaderFrom for GGSWCiphertextCompressed<D> {
}
}
impl<D: DataRef> WriterTo for GGSWCiphertextCompressed<D> {
impl<D: DataRef> WriterTo for GGSWCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?;
@@ -217,34 +263,49 @@ impl<D: DataRef> WriterTo for GGSWCiphertextCompressed<D> {
}
}
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GGSWCiphertextCompressed<DR>> for GGSW<D>
pub trait GGSWDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GLWEDecompress,
{
fn decompress(&mut self, module: &Module<B>, other: &GGSWCiphertextCompressed<DR>) {
#[cfg(debug_assertions)]
{
assert_eq!(self.rank(), other.rank())
}
fn decompress_ggsw<R, O>(&self, res: &mut R, other: &O)
where
R: GGSWToMut,
O: GGSWCompressedToRef,
{
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
let other: &GGSWCompressed<&[u8]> = &other.to_ref();
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<B: Backend> GGSWDecompress for Module<B> where Self: GGSWDecompress {}
impl<D: DataMut> GGSW<D> {
pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: GGSWCompressedToRef,
Module<B>: GGSWDecompress,
{
module.decompress_ggsw(self, other);
}
}
impl<D: DataMut> GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed<D> {
fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> {
GGSWCiphertextCompressed {
pub trait GGSWCompressedToMut {
fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]>;
}
impl<D: DataMut> GGSWCompressedToMut for GGSWCompressed<D> {
fn to_mut(&mut self) -> GGSWCompressed<&mut [u8]> {
GGSWCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
@@ -255,13 +316,13 @@ impl<D: DataMut> GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed<D> {
}
}
pub trait GGSWCiphertextCompressedToRef {
fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>;
pub trait GGSWCompressedToRef {
fn to_ref(&self) -> GGSWCompressed<&[u8]>;
}
impl<D: DataRef> GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed<D> {
fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> {
GGSWCiphertextCompressed {
impl<D: DataRef> GGSWCompressedToRef for GGSWCompressed<D> {
fn to_ref(&self) -> GGSWCompressed<&[u8]> {
GGSWCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),

View File

@@ -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<D: Data> {
pub struct GLWECompressed<D: Data> {
pub(crate) data: VecZnx<D>,
pub(crate) base2k: Base2K,
pub(crate) k: TorusPrecision,
@@ -19,7 +19,7 @@ pub struct GLWECiphertextCompressed<D: Data> {
pub(crate) seed: [u8; 32],
}
impl<D: Data> LWEInfos for GLWECiphertextCompressed<D> {
impl<D: Data> LWEInfos for GLWECompressed<D> {
fn base2k(&self) -> Base2K {
self.base2k
}
@@ -36,23 +36,23 @@ impl<D: Data> LWEInfos for GLWECiphertextCompressed<D> {
Degree(self.data.n() as u32)
}
}
impl<D: Data> GLWEInfos for GLWECiphertextCompressed<D> {
impl<D: Data> GLWEInfos for GLWECompressed<D> {
fn rank(&self) -> Rank {
self.rank
}
}
impl<D: DataRef> fmt::Debug for GLWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Debug for GLWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataRef> fmt::Display for GLWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Display for GLWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"GLWECiphertextCompressed: base2k={} k={} rank={} seed={:?}: {}",
"GLWECompressed: base2k={} k={} rank={} seed={:?}: {}",
self.base2k(),
self.k(),
self.rank(),
@@ -62,23 +62,19 @@ impl<D: DataRef> fmt::Display for GLWECiphertextCompressed<D> {
}
}
impl<D: DataMut> FillUniform for GLWECiphertextCompressed<D> {
impl<D: DataMut> FillUniform for GLWECompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source);
}
}
impl GLWECiphertextCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
pub trait GLWECompressedAlloc
where
Self: GetDegree,
{
fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed<Vec<u8>> {
GLWECompressed {
data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize),
base2k,
k,
rank,
@@ -86,19 +82,62 @@ impl GLWECiphertextCompressed<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_compressed_from_infos<A>(&self, infos: &A) -> GLWECompressed<Vec<u8>>
where
A: GLWEInfos,
{
Self::alloc_bytes_with(infos.n(), infos.base2k(), infos.k())
assert_eq!(self.n(), infos.n());
self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::alloc_bytes(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize)
}
fn bytes_of_glwe_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
assert_eq!(self.n(), infos.n());
self.bytes_of_glwe_compressed(infos.base2k(), infos.k())
}
}
impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> {
impl<B: Backend> GLWECompressedAlloc for Module<B> where Self: GetDegree {}
impl GLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GLWEInfos,
Module<B>: GLWECompressedAlloc,
{
module.alloc_glwe_compressed_from_infos(infos)
}
pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
Module<B>: GLWECompressedAlloc,
{
module.alloc_glwe_compressed(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: GLWECompressedAlloc,
{
module.bytes_of_glwe_compressed_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> usize
where
Module<B>: GLWECompressedAlloc,
{
module.bytes_of_glwe_compressed(base2k, k)
}
}
impl<D: DataMut> ReaderFrom for GLWECompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -108,7 +147,7 @@ impl<D: DataMut> ReaderFrom for GLWECiphertextCompressed<D> {
}
}
impl<D: DataRef> WriterTo for GLWECiphertextCompressed<D> {
impl<D: DataRef> WriterTo for GLWECompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?;
@@ -118,74 +157,61 @@ impl<D: DataRef> WriterTo for GLWECiphertextCompressed<D> {
}
}
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, GLWECiphertextCompressed<DR>> for GLWECiphertext<D>
pub trait GLWEDecompress
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
Self: GetDegree + VecZnxFillUniform + VecZnxCopy,
{
fn decompress(&mut self, module: &Module<B>, other: &GLWECiphertextCompressed<DR>) {
#[cfg(debug_assertions)]
{
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<D: DataMut> GLWECiphertext<D> {
pub(crate) fn decompress_internal<DataOther, B: Backend>(
&mut self,
module: &Module<B>,
other: &GLWECiphertextCompressed<DataOther>,
source: &mut Source,
) where
DataOther: DataRef,
Module<B>: VecZnxCopy + VecZnxFillUniform,
fn decompress_glwe<R, O>(&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<B: Backend> GLWEDecompress for Module<B> where Self: GetDegree + VecZnxFillUniform + VecZnxCopy {}
impl<D: DataMut> GLWE<D> {
pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: GLWECompressedToRef + GLWEInfos,
Module<B>: GLWEDecompress,
{
module.decompress_glwe(self, other);
}
}
impl<D: DataRef> GLWECiphertextCompressedToRef for GLWECiphertextCompressed<D> {
fn to_ref(&self) -> GLWECiphertextCompressed<&[u8]> {
GLWECiphertextCompressed {
pub trait GLWECompressedToRef {
fn to_ref(&self) -> GLWECompressed<&[u8]>;
}
impl<D: DataRef> GLWECompressedToRef for GLWECompressed<D> {
fn to_ref(&self) -> GLWECompressed<&[u8]> {
GLWECompressed {
seed: self.seed.clone(),
base2k: self.base2k,
k: self.k,
@@ -195,13 +221,13 @@ impl<D: DataRef> GLWECiphertextCompressedToRef for GLWECiphertextCompressed<D> {
}
}
pub trait GLWECiphertextCompressedToMut {
fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]>;
pub trait GLWECompressedToMut {
fn to_mut(&mut self) -> GLWECompressed<&mut [u8]>;
}
impl<D: DataMut> GLWECiphertextCompressedToMut for GLWECiphertextCompressed<D> {
fn to_mut(&mut self) -> GLWECiphertextCompressed<&mut [u8]> {
GLWECiphertextCompressed {
impl<D: DataMut> GLWECompressedToMut for GLWECompressed<D> {
fn to_mut(&mut self) -> GLWECompressed<&mut [u8]> {
GLWECompressed {
seed: self.seed.clone(),
base2k: self.base2k,
k: self.k,

View File

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

View File

@@ -2,21 +2,24 @@ use std::fmt;
use poulpy_hal::{
api::ZnFillUniform,
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnxInfos, ZnxView, ZnxViewMut},
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos, ZnxView,
ZnxViewMut,
},
source::Source,
};
use crate::layouts::{Base2K, Degree, LWECiphertext, LWEInfos, TorusPrecision, compressed::Decompress};
use crate::layouts::{Base2K, Degree, LWE, LWEInfos, LWEToMut, TorusPrecision};
#[derive(PartialEq, Eq, Clone)]
pub struct LWECiphertextCompressed<D: Data> {
pub struct LWECompressed<D: Data> {
pub(crate) data: Zn<D>,
pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K,
pub(crate) seed: [u8; 32],
}
impl<D: Data> LWEInfos for LWECiphertextCompressed<D> {
impl<D: Data> LWEInfos for LWECompressed<D> {
fn base2k(&self) -> Base2K {
self.base2k
}
@@ -34,17 +37,17 @@ impl<D: Data> LWEInfos for LWECiphertextCompressed<D> {
}
}
impl<D: DataRef> fmt::Debug for LWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Debug for LWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataRef> fmt::Display for LWECiphertextCompressed<D> {
impl<D: DataRef> fmt::Display for LWECompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LWECiphertextCompressed: base2k={} k={} seed={:?}: {}",
"LWECompressed: base2k={} k={} seed={:?}: {}",
self.base2k(),
self.k(),
self.seed,
@@ -53,22 +56,15 @@ impl<D: DataRef> fmt::Display for LWECiphertextCompressed<D> {
}
}
impl<D: DataMut> FillUniform for LWECiphertextCompressed<D> {
impl<D: DataMut> FillUniform for LWECompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source);
}
}
impl LWECiphertextCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
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<Vec<u8>> {
LWECompressed {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k,
base2k,
@@ -76,21 +72,60 @@ impl LWECiphertextCompressed<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: LWEInfos,
{
self.bytes_of_lwe_compressed(infos.base2k(), infos.k())
}
}
impl LWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: LWEInfos,
Module<B>: LWECompressedAlloc,
{
module.alloc_lwe_compressed_from_infos(infos)
}
pub fn alloc<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> Self
where
Module<B>: LWECompressedAlloc,
{
module.alloc_lwe_compressed(base2k, k)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: LWEInfos,
Module<B>: LWECompressedAlloc,
{
module.bytes_of_lwe_compressed_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision) -> usize
where
Module<B>: LWECompressedAlloc,
{
module.bytes_of_lwe_compressed(base2k, k)
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
impl<D: DataMut> ReaderFrom for LWECiphertextCompressed<D> {
impl<D: DataMut> ReaderFrom for LWECompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -99,7 +134,7 @@ impl<D: DataMut> ReaderFrom for LWECiphertextCompressed<D> {
}
}
impl<D: DataRef> WriterTo for LWECiphertextCompressed<D> {
impl<D: DataRef> WriterTo for LWECompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?;
@@ -108,22 +143,72 @@ impl<D: DataRef> WriterTo for LWECiphertextCompressed<D> {
}
}
impl<D: DataMut, B: Backend, DR: DataRef> Decompress<B, LWECiphertextCompressed<DR>> for LWECiphertext<D>
pub trait LWEDecompress
where
Module<B>: ZnFillUniform,
Self: ZnFillUniform,
{
fn decompress(&mut self, module: &Module<B>, other: &LWECiphertextCompressed<DR>) {
debug_assert_eq!(self.size(), other.size());
fn decompress_lwe<R, O>(&self, res: &mut R, other: &O)
where
R: LWEToMut,
O: LWECompressedToRef,
{
let res: &mut LWE<&mut [u8]> = &mut res.to_mut();
let other: &LWECompressed<&[u8]> = &other.to_ref();
assert_eq!(res.lwe_layout(), other.lwe_layout());
let mut source: Source = Source::new(other.seed);
module.zn_fill_uniform(
self.n().into(),
self.zn_fill_uniform(
res.n().into(),
other.base2k().into(),
&mut self.data,
&mut res.data,
0,
&mut source,
);
(0..self.size()).for_each(|i| {
self.data.at_mut(0, i)[0] = other.data.at(0, i)[0];
});
for i in 0..res.size() {
res.data.at_mut(0, i)[0] = other.data.at(0, i)[0];
}
}
}
impl<B: Backend> LWEDecompress for Module<B> where Self: ZnFillUniform {}
impl<D: DataMut> LWE<D> {
pub fn decompress<O, B: Backend>(&mut self, module: &Module<B>, other: &O)
where
O: LWECompressedToRef,
Module<B>: LWEDecompress,
{
module.decompress_lwe(self, other);
}
}
pub trait LWECompressedToRef {
fn to_ref(&self) -> LWECompressed<&[u8]>;
}
impl<D: DataRef> LWECompressedToRef for LWECompressed<D> {
fn to_ref(&self) -> LWECompressed<&[u8]> {
LWECompressed {
k: self.k,
base2k: self.base2k,
seed: self.seed,
data: self.data.to_ref(),
}
}
}
pub trait LWECompressedToMut {
fn to_mut(&mut self) -> LWECompressed<&mut [u8]>;
}
impl<D: DataMut> LWECompressedToMut for LWECompressed<D> {
fn to_mut(&mut self) -> LWECompressed<&mut [u8]> {
LWECompressed {
k: self.k,
base2k: self.base2k,
seed: self.seed,
data: self.data.to_mut(),
}
}
}

View File

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

View File

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

View File

@@ -19,9 +19,3 @@ pub use glwe_to_lwe_ksk::*;
pub use lwe_ct::*;
pub use lwe_ksk::*;
pub use lwe_to_glwe_ksk::*;
use poulpy_hal::layouts::{Backend, Module};
pub trait Decompress<B: Backend, C> {
fn decompress(&mut self, module: &Module<B>, other: &C);
}

View File

@@ -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<D: DataRef> fmt::Display for AutomorphismKey<D> {
}
}
impl<B: Backend> AutomorphismKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
pub trait AutomorphismKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_automorphism_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKey<Vec<u8>> {
AutomorphismKey {
key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
fn alloc_automorphism_key_from_infos<A>(&self, infos: &A) -> AutomorphismKey<Vec<u8>>
where
A: GGLWEInfos,
{
self.alloc_automorphism_key(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize)
}
fn bytes_of_automorphism_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for AutomorphismKey"
);
self.bytes_of_automorphism_key(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl AutomorphismKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
Module<B>: AutomorphismKeyAlloc,
{
module.alloc_automorphism_key(base2k, k, rank, dnum, dsize)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: AutomorphismKeyAlloc,
{
module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize)
}
}
@@ -204,13 +266,13 @@ where
}
impl<D: DataRef> AutomorphismKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> {
self.key.at(row, col)
}
}
impl<D: DataMut> AutomorphismKey<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> {
self.key.at_mut(row, col)
}
}

View File

@@ -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<D: DataRef> fmt::Display for GGLWE<D> {
}
impl<D: DataRef> GGLWE<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
GLWECiphertext {
pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.at(row, col),
@@ -180,8 +182,8 @@ impl<D: DataRef> GGLWE<D> {
}
impl<D: DataMut> GGLWE<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.at_mut(row, col),
@@ -189,31 +191,19 @@ impl<D: DataMut> GGLWE<D> {
}
}
impl GGLWE<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc_with(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_with(
n: Degree,
pub trait GGLWEAlloc
where
Self: GetDegree,
{
fn alloc_gglwe(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self {
) -> GGLWE<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -228,9 +218,9 @@ impl GGLWE<Vec<u8>> {
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<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GGLWE<Vec<u8>>
where
A: GGLWEInfos,
{
Self::alloc_bytes_with(
infos.n(),
self.alloc_gglwe(
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -257,8 +246,8 @@ impl GGLWE<Vec<u8>> {
)
}
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<Vec<u8>> {
dsize.0,
);
MatZnx::alloc_bytes(
n.into(),
MatZnx::bytes_of(
self.n().into(),
dnum.into(),
rank_in.into(),
(rank_out + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_gglwe_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_gglwe(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
}
pub trait GGLWECiphertextToMut {
impl<B: Backend> GGLWEAlloc for Module<B> where Self: GetDegree {}
impl GGLWE<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: GGLWEAlloc,
{
module.alloc_glwe_from_infos(infos)
}
pub fn alloc<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
Module<B>: GGLWEAlloc,
{
module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: GGLWEAlloc,
{
module.bytes_of_gglwe_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GGLWEAlloc,
{
module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize)
}
}
pub trait GGLWEToMut {
fn to_mut(&mut self) -> GGLWE<&mut [u8]>;
}
impl<D: DataMut> GGLWECiphertextToMut for GGLWE<D> {
impl<D: DataMut> GGLWEToMut for GGLWE<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
GGLWE {
k: self.k(),

View File

@@ -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<D: DataMut> FillUniform for GLWESwitchingKey<D> {
}
}
impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
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<A>(infos: &A) -> usize
fn alloc_glwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWESwitchingKey<Vec<u8>>
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<Vec<u8>> {
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<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_glwe_switching_key(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
}
impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: GLWESwitchingKeyAlloc,
{
module.alloc_glwe_switching_key_from_infos(infos)
}
pub fn alloc<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
Module<B>: GLWESwitchingKeyAlloc,
{
module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: GLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_switching_key_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize)
}
}
@@ -217,7 +278,7 @@ pub trait GLWESwitchingKeyToMut {
impl<D: DataMut> GLWESwitchingKeyToMut for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWECiphertextToMut,
GGLWE<D>: GGLWEToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> {
GLWESwitchingKey {
@@ -246,13 +307,13 @@ where
}
impl<D: DataRef> GLWESwitchingKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> {
self.key.at(row, col)
}
}
impl<D: DataMut> GLWESwitchingKey<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> {
self.key.at_mut(row, col)
}
}

View File

@@ -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<D: DataRef> fmt::Display for TensorKey<D> {
}
}
impl TensorKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub trait TensorKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey<Vec<u8>> {
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
TensorKey {
keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
fn alloc_tensor_key_from_infos<A>(&self, infos: &A) -> TensorKey<Vec<u8>>
where
A: GGLWEInfos,
{
@@ -140,34 +152,21 @@ impl TensorKey<Vec<u8>> {
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey"
);
Self::alloc_with(
infos.n(),
self.alloc_tensor_key(
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let mut keys: Vec<GLWESwitchingKey<Vec<u8>>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| {
keys.push(GLWESwitchingKey::alloc_with(
n,
base2k,
k,
Rank(1),
rank,
dnum,
dsize,
));
});
Self { keys }
fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn bytes_of_tensor_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -176,23 +175,54 @@ impl TensorKey<Vec<u8>> {
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<B: Backend> TensorKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl TensorKey<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: TensorKeyAlloc,
{
module.alloc_tensor_key_from_infos(infos)
}
pub fn alloc_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<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: TensorKeyAlloc,
{
module.alloc_tensor_key(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: TensorKeyAlloc,
{
module.bytes_of_tensor_key_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: TensorKeyAlloc,
{
module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize)
}
}
@@ -260,14 +290,14 @@ where
}
pub trait TensorKeyToMut {
fn to_ref(&mut self) -> TensorKey<&mut [u8]>;
fn to_mut(&mut self) -> TensorKey<&mut [u8]>;
}
impl<D: DataMut> TensorKeyToMut for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_ref(&mut self) -> TensorKey<&mut [u8]> {
fn to_mut(&mut self) -> TensorKey<&mut [u8]> {
TensorKey {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}

View File

@@ -1,10 +1,12 @@
use poulpy_hal::{
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
source::Source,
};
use std::fmt;
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision};
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision};
pub trait GGSWInfos
where
@@ -131,8 +133,8 @@ impl<D: DataMut> FillUniform for GGSW<D> {
}
impl<D: DataRef> GGSW<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
GLWECiphertext {
pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.at(row, col),
@@ -141,8 +143,8 @@ impl<D: DataRef> GGSW<D> {
}
impl<D: DataMut> GGSW<D> {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext {
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.at_mut(row, col),
@@ -150,22 +152,13 @@ impl<D: DataMut> GGSW<D> {
}
}
impl GGSW<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GGSWInfos,
{
Self::alloc_with(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
impl<B: Backend> GGSWAlloc for Module<B> where Self: GetDegree {}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
pub trait GGSWAlloc
where
Self: GetDegree,
{
fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW<Vec<u8>> {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -180,9 +173,9 @@ impl GGSW<Vec<u8>> {
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<Vec<u8>> {
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_ggsw_from_infos<A>(&self, infos: &A) -> GGSW<Vec<u8>>
where
A: GGSWInfos,
{
Self::alloc_bytes_with(
infos.n(),
self.alloc_ggsw(
infos.base2k(),
infos.k(),
infos.rank(),
@@ -208,7 +200,7 @@ impl GGSW<Vec<u8>> {
)
}
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<Vec<u8>> {
dsize.0,
);
MatZnx::alloc_bytes(
n.into(),
MatZnx::bytes_of(
self.n().into(),
dnum.into(),
(rank + 1).into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_ggsw_from_infos<A>(&self, infos: &A) -> usize
where
A: GGSWInfos,
{
self.bytes_of_ggsw(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl GGSW<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GGSWInfos,
Module<B>: GGSWAlloc,
{
module.alloc_ggsw_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: GGSWAlloc,
{
module.alloc_ggsw(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GGSWInfos,
Module<B>: GGSWAlloc,
{
module.bytes_of_ggsw_from_infos(infos)
}
pub fn bytes_of<B: Backend>(
module: Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
Module<B>: GGSWAlloc,
{
module.bytes_of_ggsw(base2k, k, rank, dnum, dsize)
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

View File

@@ -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<D: Data> {
pub struct GLWE<D: Data> {
pub(crate) data: VecZnx<D>,
pub(crate) base2k: Base2K,
pub(crate) k: TorusPrecision,
}
impl<D: DataMut> GLWELayoutSet for GLWECiphertext<D> {
fn set_basek(&mut self, base2k: Base2K) {
impl<D: DataMut> SetGLWEInfos for GLWE<D> {
fn set_base2k(&mut self, base2k: Base2K) {
self.base2k = base2k
}
@@ -74,19 +75,19 @@ impl<D: DataMut> GLWELayoutSet for GLWECiphertext<D> {
}
}
impl<D: DataRef> GLWECiphertext<D> {
impl<D: DataRef> GLWE<D> {
pub fn data(&self) -> &VecZnx<D> {
&self.data
}
}
impl<D: DataMut> GLWECiphertext<D> {
impl<D: DataMut> GLWE<D> {
pub fn data_mut(&mut self) -> &mut VecZnx<D> {
&mut self.data
}
}
impl<D: Data> LWEInfos for GLWECiphertext<D> {
impl<D: Data> LWEInfos for GLWE<D> {
fn base2k(&self) -> Base2K {
self.base2k
}
@@ -104,16 +105,16 @@ impl<D: Data> LWEInfos for GLWECiphertext<D> {
}
}
impl<D: Data> GLWEInfos for GLWECiphertext<D> {
impl<D: Data> GLWEInfos for GLWE<D> {
fn rank(&self) -> Rank {
Rank(self.data.cols() as u32 - 1)
}
}
impl<D: DataRef> ToOwnedDeep for GLWECiphertext<D> {
type Owned = GLWECiphertext<Vec<u8>>;
impl<D: DataRef> ToOwnedDeep for GLWE<D> {
type Owned = GLWE<Vec<u8>>;
fn to_owned_deep(&self) -> Self::Owned {
GLWECiphertext {
GLWE {
data: self.data.to_owned_deep(),
k: self.k,
base2k: self.base2k,
@@ -121,17 +122,17 @@ impl<D: DataRef> ToOwnedDeep for GLWECiphertext<D> {
}
}
impl<D: DataRef> fmt::Debug for GLWECiphertext<D> {
impl<D: DataRef> fmt::Debug for GLWE<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataRef> fmt::Display for GLWECiphertext<D> {
impl<D: DataRef> fmt::Display for GLWE<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"GLWECiphertext: base2k={} k={}: {}",
"GLWE: base2k={} k={}: {}",
self.base2k().0,
self.k().0,
self.data
@@ -139,41 +140,86 @@ impl<D: DataRef> fmt::Display for GLWECiphertext<D> {
}
}
impl<D: DataMut> FillUniform for GLWECiphertext<D> {
impl<D: DataMut> FillUniform for GLWE<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.data.fill_uniform(log_bound, source);
}
}
impl GLWECiphertext<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
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<Vec<u8>> {
GLWE {
data: VecZnx::alloc(
self.n().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
base2k,
k,
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GLWE<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank())
}
}
impl<D: DataMut> ReaderFrom for GLWECiphertext<D> {
impl<B: Backend> GLWEAlloc for Module<B> where Self: GetDegree {}
impl GLWE<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GLWEInfos,
Module<B>: GLWEAlloc,
{
module.alloc_glwe_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
Module<B>: GLWEAlloc,
{
module.alloc_glwe(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: GLWEAlloc,
{
module.bytes_of_glwe_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
where
Module<B>: GLWEAlloc,
{
module.bytes_of_glwe(base2k, k, rank)
}
}
impl<D: DataMut> ReaderFrom for GLWE<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -181,7 +227,7 @@ impl<D: DataMut> ReaderFrom for GLWECiphertext<D> {
}
}
impl<D: DataRef> WriterTo for GLWECiphertext<D> {
impl<D: DataRef> WriterTo for GLWE<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.0)?;
writer.write_u32::<LittleEndian>(self.base2k.0)?;
@@ -189,13 +235,13 @@ impl<D: DataRef> WriterTo for GLWECiphertext<D> {
}
}
pub trait GLWECiphertextToRef {
fn to_ref(&self) -> GLWECiphertext<&[u8]>;
pub trait GLWEToRef {
fn to_ref(&self) -> GLWE<&[u8]>;
}
impl<D: DataRef> GLWECiphertextToRef for GLWECiphertext<D> {
fn to_ref(&self) -> GLWECiphertext<&[u8]> {
GLWECiphertext {
impl<D: DataRef> GLWEToRef for GLWE<D> {
fn to_ref(&self) -> GLWE<&[u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_ref(),
@@ -203,13 +249,13 @@ impl<D: DataRef> GLWECiphertextToRef for GLWECiphertext<D> {
}
}
pub trait GLWECiphertextToMut {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]>;
pub trait GLWEToMut {
fn to_mut(&mut self) -> GLWE<&mut [u8]>;
}
impl<D: DataMut> GLWECiphertextToMut for GLWECiphertext<D> {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext {
impl<D: DataMut> GLWEToMut for GLWE<D> {
fn to_mut(&mut self) -> GLWE<&mut [u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_mut(),

View File

@@ -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<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc_with(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
data: VecZnx::alloc(n.into(), (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<Vec<u8>> {
GLWEPublicKey {
data: VecZnx::alloc(
self.n().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
base2k,
k,
dist: Distribution::NONE,
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_public_key_from_infos<A>(&self, infos: &A) -> GLWEPublicKey<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank())
}
}
impl GLWEPublicKey<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GLWEInfos,
Module<B>: GLWEPublicKeyAlloc,
{
module.alloc_glwe_public_key_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
Module<B>: GLWEPublicKeyAlloc,
{
module.alloc_glwe_public_key(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: GLWEPublicKeyAlloc,
{
module.bytes_of_glwe_public_key_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
where
Module<B>: GLWEPublicKeyAlloc,
{
module.bytes_of_glwe_public_key(base2k, k, rank)
}
}

View File

@@ -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<D: Data> {
pub k: TorusPrecision,
}
impl<D: DataMut> GLWELayoutSet for GLWEPlaintext<D> {
fn set_basek(&mut self, base2k: Base2K) {
impl<D: DataMut> SetGLWEInfos for GLWEPlaintext<D> {
fn set_base2k(&mut self, base2k: Base2K) {
self.base2k = base2k
}
@@ -86,39 +85,72 @@ impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
}
}
impl GLWEPlaintext<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
GLWEPlaintext {
data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize),
base2k,
k,
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_plaintext(infos.base2k(), infos.k())
}
}
impl<D: DataRef> GLWECiphertextToRef for GLWEPlaintext<D> {
fn to_ref(&self) -> GLWECiphertext<&[u8]> {
GLWECiphertext {
impl GLWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GLWEInfos,
Module<B>: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> Self
where
Module<B>: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext(base2k, k)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> usize
where
Module<B>: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext(base2k, k)
}
}
impl<D: DataRef> GLWEToRef for GLWEPlaintext<D> {
fn to_ref(&self) -> GLWE<&[u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_ref(),
@@ -126,9 +158,9 @@ impl<D: DataRef> GLWECiphertextToRef for GLWEPlaintext<D> {
}
}
impl<D: DataMut> GLWECiphertextToMut for GLWEPlaintext<D> {
fn to_mut(&mut self) -> GLWECiphertext<&mut [u8]> {
GLWECiphertext {
impl<D: DataMut> GLWEToMut for GLWEPlaintext<D> {
fn to_mut(&mut self) -> GLWE<&mut [u8]> {
GLWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_mut(),

View File

@@ -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<D: Data> GLWEInfos for GLWESecret<D> {
}
}
impl GLWESecret<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
GLWESecret {
data: ScalarZnx::alloc(self.n().into(), rank.into()),
dist: Distribution::NONE,
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_glwe_secret_from_infos<A>(&self, infos: &A) -> GLWESecret<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_secret(infos.rank())
}
}
impl GLWESecret<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: GLWEInfos,
Module<B>: GLWESecretAlloc,
{
module.alloc_glwe_secret_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, rank: Rank) -> Self
where
Module<B>: GLWESecretAlloc,
{
module.alloc_glwe_secret(rank)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
Module<B>: GLWESecretAlloc,
{
module.bytes_of_glwe_secret_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: Module<B>, rank: Rank) -> usize
where
Module<B>: GLWESecretAlloc,
{
module.bytes_of_glwe_secret(rank)
}
}

View File

@@ -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<D: DataRef> WriterTo for GLWEToLWESwitchingKey<D> {
}
}
pub trait GLWEToLWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_glwe_to_lwe_switching_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> GLWEToLWESwitchingKey<Vec<u8>> {
GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
}
fn alloc_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
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<A>(&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<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
where
Module<B>: GLWEToLWESwitchingKeyAlloc,
{
module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
where
Module<B>: GLWEToLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
}
}

View File

@@ -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<D: Data> {
pub struct LWE<D: Data> {
pub(crate) data: Zn<D>,
pub(crate) k: TorusPrecision,
pub(crate) base2k: Base2K,
}
impl<D: Data> LWEInfos for LWECiphertext<D> {
impl<D: Data> LWEInfos for LWE<D> {
fn base2k(&self) -> Base2K {
self.base2k
}
@@ -76,7 +76,7 @@ impl<D: Data> LWEInfos for LWECiphertext<D> {
}
}
impl<D: Data> SetLWEInfos for LWECiphertext<D> {
impl<D: Data> SetLWEInfos for LWE<D> {
fn set_base2k(&mut self, base2k: Base2K) {
self.base2k = base2k
}
@@ -86,25 +86,25 @@ impl<D: Data> SetLWEInfos for LWECiphertext<D> {
}
}
impl<D: DataRef> LWECiphertext<D> {
impl<D: DataRef> LWE<D> {
pub fn data(&self) -> &Zn<D> {
&self.data
}
}
impl<D: DataMut> LWECiphertext<D> {
impl<D: DataMut> LWE<D> {
pub fn data_mut(&mut self) -> &Zn<D> {
&mut self.data
}
}
impl<D: DataRef> fmt::Debug for LWECiphertext<D> {
impl<D: DataRef> fmt::Debug for LWE<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataRef> fmt::Display for LWECiphertext<D> {
impl<D: DataRef> fmt::Display for LWE<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@@ -116,7 +116,7 @@ impl<D: DataRef> fmt::Display for LWECiphertext<D> {
}
}
impl<D: DataMut> FillUniform for LWECiphertext<D>
impl<D: DataMut> FillUniform for LWE<D>
where
Zn<D>: FillUniform,
{
@@ -125,41 +125,73 @@ where
}
}
impl LWECiphertext<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
LWE {
data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize),
k,
base2k,
}
}
pub fn alloc_bytes<A>(infos: &A) -> usize
fn alloc_lwe_from_infos<A>(&self, infos: &A) -> LWE<Vec<u8>>
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<A>(&self, infos: &A) -> usize
where
A: LWEInfos,
{
self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k())
}
}
impl LWE<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: LWEInfos,
Module<B>: LWEAlloc,
{
module.alloc_lwe_from_infos(infos)
}
pub fn alloc<B: Backend>(module: &Module<B>, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self
where
Module<B>: LWEAlloc,
{
module.alloc_lwe(n, base2k, k)
}
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: LWEInfos,
Module<B>: LWEAlloc,
{
module.bytes_of_lwe_from_infos(infos)
}
pub fn bytes_of<B: Backend>(module: &Module<B>, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize
where
Module<B>: LWEAlloc,
{
module.bytes_of_lwe(n, base2k, k)
}
}
pub trait LWECiphertextToRef {
fn to_ref(&self) -> LWECiphertext<&[u8]>;
fn to_ref(&self) -> LWE<&[u8]>;
}
impl<D: DataRef> LWECiphertextToRef for LWECiphertext<D> {
fn to_ref(&self) -> LWECiphertext<&[u8]> {
LWECiphertext {
impl<D: DataRef> LWECiphertextToRef for LWE<D> {
fn to_ref(&self) -> LWE<&[u8]> {
LWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_ref(),
@@ -167,14 +199,14 @@ impl<D: DataRef> LWECiphertextToRef for LWECiphertext<D> {
}
}
pub trait LWECiphertextToMut {
pub trait LWEToMut {
#[allow(dead_code)]
fn to_mut(&mut self) -> LWECiphertext<&mut [u8]>;
fn to_mut(&mut self) -> LWE<&mut [u8]>;
}
impl<D: DataMut> LWECiphertextToMut for LWECiphertext<D> {
fn to_mut(&mut self) -> LWECiphertext<&mut [u8]> {
LWECiphertext {
impl<D: DataMut> LWEToMut for LWE<D> {
fn to_mut(&mut self) -> LWE<&mut [u8]> {
LWE {
k: self.k,
base2k: self.base2k,
data: self.data.to_mut(),
@@ -182,7 +214,7 @@ impl<D: DataMut> LWECiphertextToMut for LWECiphertext<D> {
}
}
impl<D: DataMut> ReaderFrom for LWECiphertext<D> {
impl<D: DataMut> ReaderFrom for LWE<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
@@ -190,7 +222,7 @@ impl<D: DataMut> ReaderFrom for LWECiphertext<D> {
}
}
impl<D: DataRef> WriterTo for LWECiphertext<D> {
impl<D: DataRef> WriterTo for LWE<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u32::<LittleEndian>(self.k.into())?;
writer.write_u32::<LittleEndian>(self.base2k.into())?;

View File

@@ -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<D: Data> GGLWEInfos for LWESwitchingKey<D> {
}
}
pub trait LWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey<Vec<u8>> {
LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
}
fn alloc_lwe_switching_key_from_infos<A>(&self, infos: &A) -> LWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
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<A>(&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<B: Backend> LWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWESwitchingKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self
where
Module<B>: LWESwitchingKeyAlloc,
{
module.alloc_lwe_switching_key(base2k, k, dnum)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where
Module<B>: LWESwitchingKeyAlloc,
{
module.bytes_of_lwe_switching_key(base2k, k, dnum)
}
}

View File

@@ -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<D: Data> LWEInfos for LWEPlaintext<D> {
}
}
impl LWEPlaintext<Vec<u8>> {
pub fn alloc<A>(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<Vec<u8>> {
LWEPlaintext {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k,
base2k,
}
}
fn alloc_lwe_plaintext_from_infos<A>(&self, infos: &A) -> LWEPlaintext<Vec<u8>>
where
A: LWEInfos,
{
self.alloc_lwe_plaintext(infos.base2k(), infos.k())
}
}
impl<B: Backend> LWEPlaintextAlloc for Module<B> {}
impl LWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
where
A: LWEInfos,
{
module.alloc_lwe_plaintext_from_infos(infos)
}
pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> Self {
module.alloc_lwe_plaintext(base2k, k)
}
}
impl<D: DataRef> fmt::Display for LWEPlaintext<D> {

View File

@@ -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<D: Data> {
pub(crate) dist: Distribution,
}
impl LWESecret<Vec<u8>> {
pub fn alloc(n: Degree) -> Self {
Self {
pub trait LWESecretAlloc {
fn alloc_lwe_secret(&self, n: Degree) -> LWESecret<Vec<u8>> {
LWESecret {
data: ScalarZnx::alloc(n.into(), 1),
dist: Distribution::NONE,
}
}
}
impl<B: Backend> LWESecretAlloc for Module<B> {}
impl LWESecret<Vec<u8>> {
pub fn alloc<B: Backend>(module: &Module<B>, n: Degree) -> Self
where
Module<B>: LWESecretAlloc,
{
module.alloc_lwe_secret(n)
}
}
impl<D: DataRef> LWESecret<D> {
pub fn raw(&self) -> &[i64] {
self.data.at(0, 0)

View File

@@ -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<D: DataRef> WriterTo for LWEToGLWESwitchingKey<D> {
}
}
pub trait LWEToGLWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_lwe_to_glwe_switching_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_out: Rank,
dnum: Dnum,
) -> LWEToGLWESwitchingKey<Vec<u8>> {
LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
}
fn alloc_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
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<A>(&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<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
pub fn alloc_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
where
Module<B>: LWEToGLWESwitchingKeyAlloc,
{
module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
}
pub fn alloc_bytes<A>(infos: &A) -> usize
pub fn bytes_of_from_infos<A, B: Backend>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: 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<B: Backend>(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize
where
Module<B>: LWEToGLWESwitchingKeyAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
}
}

View File

@@ -80,7 +80,7 @@ pub trait AutomorphismKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
{
fn automorphism_key_prepared_alloc(
fn alloc_automorphism_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -89,12 +89,12 @@ where
dsize: Dsize,
) -> AutomorphismKeyPrepared<Vec<u8>, B> {
AutomorphismKeyPrepared::<Vec<u8>, B> {
key: self.glwe_switching_key_prepared_alloc(base2k, k, rank, rank, dnum, dsize),
key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
@@ -103,7 +103,7 @@ 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<A>(&self, infos: &A) -> usize
fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -132,7 +132,7 @@ 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<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
A: GGLWEInfos,
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_from_infos(infos)
module.alloc_automorphism_key_prepared_from_infos(infos)
}
pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc(base2k, k, rank, dnum, dsize)
module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
}
pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_bytes_from_infos(infos)
module.bytes_of_automorphism_key_prepared_from_infos(infos)
}
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize)
module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
}
}
@@ -180,19 +180,19 @@ pub trait AutomorphismKeyPrepare<B: Backend>
where
Self: GLWESwitchingKeyPrepare<B>,
{
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.glwe_switching_key_prepare_tmp_bytes(infos)
self.prepare_glwe_switching_key_tmp_bytes(infos)
}
fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
{
self.glwe_switching_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch);
self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch);
res.set_p(other.p());
}
}
@@ -204,7 +204,7 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
where
Module<B>: GLWESwitchingKeyPrepare<B>,
{
module.automorphism_key_prepare_tmp_bytes(self)
module.prepare_automorphism_key_tmp_bytes(self)
}
}
@@ -214,7 +214,7 @@ impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
Module<B>: AutomorphismKeyPrepare<B>,
{
module.automorphism_key_prepare(self, other, scratch);
module.prepare_automorphism_key(self, other, scratch);
}
}

View File

@@ -61,7 +61,7 @@ pub trait GGLWEPreparedAlloc<B: Backend>
where
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatAllocBytes,
{
fn gglwe_prepared_alloc(
fn alloc_gglwe_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -92,12 +92,12 @@ where
}
}
fn gglwe_prepared_alloc_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B>
fn alloc_gglwe_prepared_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
assert_eq!(self.n(), infos.n());
self.gglwe_prepared_alloc(
self.alloc_gglwe_prepared(
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -107,7 +107,7 @@ 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<A>(&self, infos: &A) -> usize
fn bytes_of_gglwe_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(self.n(), infos.n());
self.gglwe_prepared_alloc_bytes(
self.bytes_of_gglwe_prepared(
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -159,7 +159,7 @@ 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<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.gglwe_prepared_alloc_bytes_from_infos(infos)
module.bytes_of_gglwe_prepared_from_infos(infos)
}
pub fn alloc_bytes(
pub fn bytes_of(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
@@ -190,7 +190,7 @@ 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<B: Backend>
where
Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B>,
{
fn gglwe_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
fn prepare_gglwe_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -210,7 +210,7 @@ where
)
}
fn gglwe_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_gglwe<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGLWEPreparedToMut<B>,
O: GGLWEToRef,
@@ -238,7 +238,7 @@ where
where
O: GGLWEToRef,
{
module.gglwe_prepare(self, other, scratch);
module.prepare_gglwe(self, other, scratch);
}
}
@@ -247,7 +247,7 @@ impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
where
Module<B>: GGLWEPrepare<B>,
{
module.gglwe_prepare_tmp_bytes(self)
module.prepare_gglwe_tmp_bytes(self)
}
}

View File

@@ -79,7 +79,7 @@ pub trait GLWESwitchingKeyPreparedAlloc<B: Backend>
where
Self: GGLWEPreparedAlloc<B>,
{
fn glwe_switching_key_prepared_alloc(
fn alloc_glwe_switching_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -89,7 +89,7 @@ where
dsize: Dsize,
) -> GLWESwitchingKeyPrepared<Vec<u8>, B> {
GLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: self.gglwe_prepared_alloc(base2k, k, rank_in, rank_out, dnum, dsize),
key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
}
@@ -99,7 +99,7 @@ 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<A>(&self, infos: &A) -> usize
fn glwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.glwe_switching_key_prepared_alloc_bytes(
self.bytes_of_glwe_switching_key_prepared(
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -158,17 +158,17 @@ 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<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.glwe_switching_key_prepared_alloc_bytes_from_infos(infos)
module.glwe_switching_key_prepared_bytes_of_from_infos(infos)
}
pub fn alloc_bytes(
pub fn bytes_of(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
@@ -177,7 +177,7 @@ 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<B: Backend>
where
Self: GGLWEPrepare<B>,
{
fn glwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.gglwe_prepare_tmp_bytes(infos)
self.prepare_gglwe_tmp_bytes(infos)
}
fn glwe_switching_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData,
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
{
self.gglwe_prepare(&mut res.to_mut().key, &other.to_ref().key, scratch);
self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch);
res.set_sk_in_n(other.sk_in_n());
res.set_sk_out_n(other.sk_out_n());
}
@@ -211,7 +211,7 @@ impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
Module<B>: GLWESwitchingKeyPrepare<B>,
{
module.glwe_switching_prepare(self, other, scratch);
module.prepare_glwe_switching(self, other, scratch);
}
}
@@ -220,7 +220,7 @@ where
Module<B>: GLWESwitchingKeyPrepare<B>,
{
pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize {
module.gglwe_prepare_tmp_bytes(self)
module.prepare_gglwe_tmp_bytes(self)
}
}

View File

@@ -59,7 +59,7 @@ pub trait TensorKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
{
fn tensor_key_prepared_alloc(
fn alloc_tensor_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -70,12 +70,12 @@ 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<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B>
fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
@@ -84,7 +84,7 @@ 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<A>(&self, infos: &A) -> usize
fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.tensor_key_prepared_alloc_bytes(
self.bytes_of_tensor_key_prepared(
infos.base2k(),
infos.k(),
infos.rank(),
@@ -122,29 +122,22 @@ 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<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self {
module.tensor_key_prepared_alloc(base2k, k, dnum, dsize, rank)
module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank)
}
pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.tensor_key_prepared_alloc_bytes_from_infos(infos)
module.bytes_of_tensor_key_prepared_from_infos(infos)
}
pub fn alloc_bytes_with(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize {
module.tensor_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize)
}
}
@@ -174,14 +167,14 @@ pub trait TensorKeyPrepare<B: Backend>
where
Self: GLWESwitchingKeyPrepare<B>,
{
fn tensor_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.glwe_switching_key_prepare_tmp_bytes(infos)
self.prepare_glwe_switching_key_tmp_bytes(infos)
}
fn tensor_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: TensorKeyPreparedToMut<B>,
O: TensorKeyToRef,
@@ -192,7 +185,7 @@ 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);
}
}

View File

@@ -53,7 +53,7 @@ pub trait GGSWPreparedAlloc<B: Backend>
where
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatAllocBytes,
{
fn ggsw_prepared_alloc(
fn alloc_ggsw_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -88,12 +88,12 @@ where
}
}
fn ggsw_prepared_alloc_from_infos<A>(&self, infos: &A) -> GGSWPrepared<Vec<u8>, B>
fn alloc_ggsw_prepared_from_infos<A>(&self, infos: &A) -> GGSWPrepared<Vec<u8>, B>
where
A: GGSWInfos,
{
assert_eq!(self.n(), infos.n());
self.ggsw_prepared_alloc(
self.alloc_ggsw_prepared(
infos.base2k(),
infos.k(),
infos.dnum(),
@@ -102,7 +102,7 @@ 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<A>(&self, infos: &A) -> usize
fn bytes_of_ggsw_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGSWInfos,
{
assert_eq!(self.n(), infos.n());
self.ggsw_prepared_alloc_bytes(
self.bytes_of_ggsw_prepared(
infos.base2k(),
infos.k(),
infos.dnum(),
@@ -145,29 +145,22 @@ where
where
A: GGSWInfos,
{
module.ggsw_prepared_alloc_from_infos(infos)
module.alloc_ggsw_prepared_from_infos(infos)
}
pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self {
module.ggsw_prepared_alloc(base2k, k, dnum, dsize, rank)
module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank)
}
pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGSWInfos,
{
module.ggsw_prepared_alloc_bytes_from_infos(infos)
module.bytes_of_ggsw_prepared_from_infos(infos)
}
pub fn alloc_bytes_with(
module: &Module<B>,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
dsize: Dsize,
rank: Rank,
) -> usize {
module.ggsw_prepared_alloc_bytes(base2k, k, dnum, dsize, rank)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize {
module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank)
}
}

View File

@@ -54,7 +54,7 @@ pub trait GLWEPublicKeyPreparedAlloc<B: Backend>
where
Self: GetDegree + VecZnxDftAlloc<B> + VecZnxDftAllocBytes,
{
fn glwe_public_key_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared<Vec<u8>, B> {
fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared<Vec<u8>, B> {
GLWEPublicKeyPrepared {
data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
base2k,
@@ -63,22 +63,22 @@ where
}
}
fn glwe_public_key_alloc_from_infos<A>(&self, infos: &A) -> GLWEPublicKeyPrepared<Vec<u8>, B>
fn alloc_glwe_public_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEPublicKeyPrepared<Vec<u8>, B>
where
A: GLWEInfos,
{
self.glwe_public_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank())
self.alloc_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank())
}
fn glwe_public_key_prepared_alloc_bytes(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
self.vec_znx_dft_alloc_bytes((rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
self.vec_znx_dft_bytes_of((rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
}
fn glwe_public_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
fn bytes_of_glwe_public_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.glwe_public_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank())
self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank())
}
}
@@ -92,22 +92,22 @@ 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<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
module.glwe_public_key_prepared_alloc(base2k, k, rank)
module.alloc_glwe_public_key_prepared(base2k, k, rank)
}
pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
{
module.glwe_public_key_prepared_alloc_bytes_from_infos(infos)
module.bytes_of_glwe_public_key_prepared_from_infos(infos)
}
pub fn alloc_bytes(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
module.glwe_public_key_prepared_alloc_bytes(base2k, k, rank)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
module.bytes_of_glwe_public_key_prepared(base2k, k, rank)
}
}
@@ -115,7 +115,7 @@ pub trait GLWEPublicKeyPrepare<B: Backend>
where
Self: GetDegree + VecZnxDftApply<B>,
{
fn glwe_public_key_prepare<R, O>(&self, res: &mut R, other: &O)
fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O)
where
R: GLWEPublicKeyPreparedToMut<B> + SetDist,
O: GLWEPublicKeyToRef + GetDist,
@@ -149,7 +149,7 @@ where
where
O: GLWEPublicKeyToRef + GetDist,
{
module.glwe_public_key_prepare(self, other);
module.prepare_glwe_public_key(self, other);
}
}

View File

@@ -49,29 +49,29 @@ pub trait GLWESecretPreparedAlloc<B: Backend>
where
Self: GetDegree + SvpPPolAllocBytes + SvpPPolAlloc<B>,
{
fn glwe_secret_prepared_alloc(&self, rank: Rank) -> GLWESecretPrepared<Vec<u8>, B> {
fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared<Vec<u8>, B> {
GLWESecretPrepared {
data: self.svp_ppol_alloc(rank.into()),
dist: Distribution::NONE,
}
}
fn glwe_secret_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWESecretPrepared<Vec<u8>, B>
fn alloc_glwe_secret_prepared_from_infos<A>(&self, infos: &A) -> GLWESecretPrepared<Vec<u8>, B>
where
A: GLWEInfos,
{
assert_eq!(self.n(), infos.n());
self.glwe_secret_prepared_alloc(infos.rank())
self.alloc_glwe_secret_prepared(infos.rank())
}
fn glwe_secret_alloc_bytes(&self, rank: Rank) -> usize {
self.svp_ppol_alloc_bytes(rank.into())
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize {
self.svp_ppol_bytes_of(rank.into())
}
fn glwe_secret_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
fn bytes_of_glwe_secret_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
assert_eq!(self.n(), infos.n());
self.glwe_secret_alloc_bytes(infos.rank())
self.bytes_of_glwe_secret(infos.rank())
}
}
@@ -85,22 +85,22 @@ 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<B>, rank: Rank) -> Self {
module.glwe_secret_prepared_alloc(rank)
module.alloc_glwe_secret_prepared(rank)
}
pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GLWEInfos,
{
module.glwe_secret_alloc_bytes_from_infos(infos)
module.bytes_of_glwe_secret_from_infos(infos)
}
pub fn alloc_bytes(module: &Module<B>, rank: Rank) -> usize {
module.glwe_secret_alloc_bytes(rank)
pub fn bytes_of(module: &Module<B>, rank: Rank) -> usize {
module.bytes_of_glwe_secret(rank)
}
}
@@ -118,7 +118,7 @@ pub trait GLWESecretPrepare<B: Backend>
where
Self: SvpPrepare<B>,
{
fn glwe_secret_prepare<R, O>(&self, res: &mut R, other: &O)
fn prepare_glwe_secret<R, O>(&self, res: &mut R, other: &O)
where
R: GLWESecretPreparedToMut<B> + SetDist,
O: GLWESecretToRef + GetDist,

View File

@@ -64,7 +64,7 @@ where
rank_in: Rank,
dnum: Dnum,
) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
GLWEToLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
}
fn glwe_to_lwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
where
@@ -83,17 +83,11 @@ 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<A>(&self, infos: &A) -> usize
fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -107,7 +101,7 @@ 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<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos)
module.glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(infos)
}
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
module.glwe_to_lwe_switching_key_prepared_bytes_of(base2k, k, rank_in, dnum)
}
}
@@ -148,7 +142,7 @@ 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<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
@@ -156,7 +150,7 @@ where
R: GLWEToLWESwitchingKeyPreparedToMut<B>,
O: GLWEToLWESwitchingKeyToRef,
{
self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
}
}

View File

@@ -56,13 +56,13 @@ pub trait LWESwitchingKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
{
fn lwe_switching_key_prepared_alloc(
fn alloc_lwe_switching_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
) -> LWESwitchingKeyPrepared<Vec<u8>, B> {
LWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
}
fn lwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyPrepared<Vec<u8>, B>
@@ -84,14 +84,14 @@ 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<A>(&self, infos: &A) -> usize
fn lwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -110,7 +110,7 @@ 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<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
module.lwe_switching_key_prepared_alloc(base2k, k, dnum)
module.alloc_lwe_switching_key_prepared(base2k, k, dnum)
}
pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.lwe_switching_key_prepared_alloc_bytes_from_infos(infos)
module.lwe_switching_key_prepared_bytes_of_from_infos(infos)
}
pub fn alloc_bytes(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
module.lwe_switching_key_prepared_alloc_bytes(base2k, k, dnum)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
module.lwe_switching_key_prepared_bytes_of(base2k, k, dnum)
}
}
@@ -151,14 +151,14 @@ 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<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: LWESwitchingKeyPreparedToMut<B>,
O: LWESwitchingKeyToRef,
{
self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
}
}

View File

@@ -65,7 +65,7 @@ where
rank_out: Rank,
dnum: Dnum,
) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
LWEToGLWESwitchingKeyPrepared(self.glwe_switching_key_prepared_alloc(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
}
fn lwe_to_glwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B>
where
@@ -84,17 +84,17 @@ 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<A>(&self, infos: &A) -> usize
fn lwe_to_glwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -108,7 +108,7 @@ 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<A>(module: &Module<B>, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
{
module.lwe_to_glwe_switching_key_prepared_alloc_bytes_from_infos(infos)
module.lwe_to_glwe_switching_key_prepared_bytes_of_from_infos(infos)
}
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
module.lwe_to_glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_out, dnum)
pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
module.lwe_to_glwe_switching_key_prepared_bytes_of(base2k, k, rank_out, dnum)
}
}
@@ -149,7 +149,7 @@ 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<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
@@ -157,7 +157,7 @@ where
R: LWEToGLWESwitchingKeyPreparedToMut<B>,
O: LWEToGLWESwitchingKeyToRef,
{
self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
}
}

View File

@@ -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<D: DataRef> GGLWE<D> {
pub fn assert_noise<B, DataSk, DataWant>(
@@ -35,8 +35,8 @@ impl<D: DataRef> GGLWE<D> {
let dsize: usize = self.dsize().into();
let base2k: usize = self.base2k().into();
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self));
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self));
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
(0..self.rank_in().into()).for_each(|col_i| {
(0..self.dnum().into()).for_each(|row_i| {

View File

@@ -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<D: DataRef> GGSW<D> {
pub fn assert_noise<B, DataSk, DataScalar, F>(
@@ -42,13 +42,13 @@ impl<D: DataRef> GGSW<D> {
let base2k: usize = self.base2k().into();
let dsize: usize = self.dsize().into();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size());
let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size());
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes());
ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes());
(0..(self.rank() + 1).into()).for_each(|col_j| {
(0..self.dnum().into()).for_each(|row_i| {
@@ -114,13 +114,13 @@ impl<D: DataRef> GGSW<D> {
let base2k: usize = self.base2k().into();
let dsize: usize = self.dsize().into();
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
let mut pt_dft: VecZnxDft<Vec<u8>, B> = module.vec_znx_dft_alloc(1, self.size());
let mut pt_big: VecZnxBig<Vec<u8>, B> = module.vec_znx_big_alloc(1, self.size());
let mut scratch: ScratchOwned<B> =
ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes());
ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self) | module.vec_znx_normalize_tmp_bytes());
(0..(self.rank() + 1).into()).for_each(|col_j| {
(0..self.dnum().into()).for_each(|row_i| {

View File

@@ -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<D: DataRef> GLWECiphertext<D> {
impl<D: DataRef> GLWE<D> {
pub fn noise<B, DataSk, DataPt>(
&self,
module: &Module<B>,
@@ -32,7 +32,7 @@ impl<D: DataRef> GLWECiphertext<D> {
+ VecZnxBigNormalize<B>,
Scratch<B>: TakeVecZnxDft<B> + TakeVecZnxBig<B>,
{
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(self);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(self);
self.decrypt(module, &mut pt_have, sk_prepared, scratch);
module.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(self.base2k().into(), &mut pt_have.data, 0, scratch);
@@ -61,7 +61,7 @@ impl<D: DataRef> GLWECiphertext<D> {
+ VecZnxNormalizeInplace<B>,
B: Backend + TakeVecZnxDftImpl<B> + TakeVecZnxBigImpl<B> + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::decrypt_scratch_space(module, self));
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWE::decrypt_scratch_space(module, self));
let noise_have: f64 = self.noise(module, sk_prepared, pt_want, scratch.borrow());
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
}

View File

@@ -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<D> GLWEOperations for GLWEPlaintext<D>
where
D: DataMut,
GLWEPlaintext<D>: GLWECiphertextToMut + GLWEInfos,
GLWEPlaintext<D>: GLWEToMut + GLWEInfos,
{
}
impl<D: DataMut> GLWEOperations for GLWECiphertext<D> where GLWECiphertext<D>: GLWECiphertextToMut + GLWEInfos {}
impl<D: DataMut> GLWEOperations for GLWE<D> where GLWE<D>: GLWEToMut + GLWEInfos {}
pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Sized {
pub trait GLWEOperations: GLWEToMut + GLWEInfos + SetGLWEInfos + Sized {
fn add<A, B, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A, b: &B)
where
A: GLWECiphertextToRef + GLWEInfos,
B: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
B: GLWEToRef + GLWEInfos,
Module<BACKEND>: VecZnxAdd + VecZnxCopy,
{
#[cfg(debug_assertions)]
@@ -39,9 +37,9 @@ 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<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<BACKEND>: VecZnxAddInplace,
{
#[cfg(debug_assertions)]
@@ -80,8 +78,8 @@ 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<A, B, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A, b: &B)
where
A: GLWECiphertextToRef + GLWEInfos,
B: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
B: GLWEToRef + GLWEInfos,
Module<BACKEND>: VecZnxSub + VecZnxCopy + VecZnxNegateInplace,
{
#[cfg(debug_assertions)]
@@ -108,9 +106,9 @@ 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<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<BACKEND>: VecZnxSubInplace,
{
#[cfg(debug_assertions)]
@@ -150,8 +148,8 @@ 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<A, BACKEND: Backend>(&mut self, module: &Module<BACKEND>, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<BACKEND>: VecZnxSubNegateInplace,
{
#[cfg(debug_assertions)]
@@ -172,8 +170,8 @@ 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<A, B: Backend>(&mut self, module: &Module<B>, k: i64, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<B>: VecZnxRotate,
{
#[cfg(debug_assertions)]
@@ -193,14 +191,14 @@ 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<B>: VecZnxRotateInplace<B>,
{
let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut();
let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut();
(0..(self_mut.rank() + 1).into()).for_each(|i| {
module.vec_znx_rotate_inplace(k, &mut self_mut.data, i, scratch);
@@ -217,7 +215,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size
fn mul_xp_minus_one<A, B: Backend>(&mut self, module: &Module<B>, k: i64, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<B>: VecZnxMulXpMinusOne,
{
#[cfg(debug_assertions)]
@@ -226,14 +224,14 @@ 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<B>: VecZnxMulXpMinusOneInplace<B>,
{
let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut();
let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut();
(0..(self_mut.rank() + 1).into()).for_each(|i| {
module.vec_znx_mul_xp_minus_one_inplace(k, &mut self_mut.data, i, scratch);
@@ -250,7 +248,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size
fn copy<A, B: Backend>(&mut self, module: &Module<B>, a: &A)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<B>: VecZnxCopy,
{
#[cfg(debug_assertions)]
@@ -259,15 +257,15 @@ 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<B: Backend>(&mut self, module: &Module<B>, k: usize, scratch: &mut Scratch<B>)
@@ -282,7 +280,7 @@ pub trait GLWEOperations: GLWECiphertextToMut + GLWEInfos + GLWELayoutSet + Size
fn normalize<A, B: Backend>(&mut self, module: &Module<B>, a: &A, scratch: &mut Scratch<B>)
where
A: GLWECiphertextToRef + GLWEInfos,
A: GLWEToRef + GLWEInfos,
Module<B>: VecZnxNormalize<B>,
{
#[cfg(debug_assertions)]
@@ -291,8 +289,8 @@ 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<B>: VecZnxNormalizeInplace<B>,
{
let self_mut: &mut GLWECiphertext<&mut [u8]> = &mut self.to_mut();
let self_mut: &mut GLWE<&mut [u8]> = &mut self.to_mut();
(0..(self_mut.rank() + 1).into()).for_each(|i| {
module.vec_znx_normalize_inplace(self_mut.base2k().into(), &mut self_mut.data, i, scratch);
});
}
}
impl GLWECiphertext<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn rsh_scratch_space(n: usize) -> usize {
VecZnx::rsh_scratch_space(n)
}

View File

@@ -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<A>(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self)
fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGLWECtSlice {
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWECiphertext<&mut [u8]>>, &mut Self)
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self)
where
A: GLWEInfos;
}
@@ -123,13 +123,13 @@ impl<B: Backend> TakeGLWECt for Scratch<B>
where
Scratch<B>: TakeVecZnx,
{
fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWECiphertext<&mut [u8]>, &mut Self)
fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self)
where
A: GLWEInfos,
{
let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size());
(
GLWECiphertext {
GLWE {
k: infos.k(),
base2k: infos.base2k(),
data,
@@ -143,12 +143,12 @@ impl<B: Backend> TakeGLWECtSlice for Scratch<B>
where
Scratch<B>: TakeVecZnx,
{
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWECiphertext<&mut [u8]>>, &mut Self)
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self)
where
A: GLWEInfos,
{
let mut scratch: &mut Scratch<B> = self;
let mut cts: Vec<GLWECiphertext<&mut [u8]>> = Vec::with_capacity(size);
let mut cts: Vec<GLWE<&mut [u8]>> = Vec::with_capacity(size);
for _ in 0..size {
let (ct, new_scratch) = scratch.take_glwe_ct(infos);
scratch = new_scratch;
@@ -463,7 +463,7 @@ where
let mut scratch: &mut Scratch<B> = self;
let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout();
let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout();
ksk_infos.rank_in = Rank(1);
if pairs != 0 {
@@ -499,7 +499,7 @@ where
let mut scratch: &mut Scratch<B> = self;
let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.layout();
let mut ksk_infos: crate::layouts::GGLWECiphertextLayout = infos.gglwe_layout();
ksk_infos.rank_in = Rank(1);
if pairs != 0 {

View File

@@ -1,12 +1,12 @@
use poulpy_hal::test_suite::serialization::test_reader_writer_interface;
use crate::layouts::{
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWECiphertext, GLWESwitchingKey, GLWEToLWESwitchingKey,
LWECiphertext, LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision,
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWE, GGSW, GLWE, GLWESwitchingKey, GLWEToLWESwitchingKey, LWE,
LWESwitchingKey, LWEToGLWESwitchingKey, Rank, TensorKey, TorusPrecision,
compressed::{
AutomorphismKeyCompressed, GGLWECiphertextCompressed, GGSWCiphertextCompressed, GLWECiphertextCompressed,
GLWESwitchingKeyCompressed, GLWEToLWESwitchingKeyCompressed, LWECiphertextCompressed, LWESwitchingKeyCompressed,
LWEToGLWESwitchingKeyCompressed, TensorKeyCompressed,
AutomorphismKeyCompressed, GGLWECompressed, GGSWCompressed, GLWECompressed, GLWESwitchingKeyCompressed,
GLWEToLWESwitchingKeyCompressed, LWECompressed, LWESwitchingKeyCompressed, LWEToGLWESwitchingKeyCompressed,
TensorKeyCompressed,
},
};
@@ -20,51 +20,50 @@ const DSIZE: Dsize = Dsize(1);
#[test]
fn glwe_serialization() {
let original: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc_with(N_GLWE, BASE2K, K, RANK);
let original: GLWE<Vec<u8>> = GLWE::alloc(N_GLWE, BASE2K, K, RANK);
poulpy_hal::test_suite::serialization::test_reader_writer_interface(original);
}
#[test]
fn glwe_compressed_serialization() {
let original: GLWECiphertextCompressed<Vec<u8>> = GLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK);
let original: GLWECompressed<Vec<u8>> = GLWECompressed::alloc(N_GLWE, BASE2K, K, RANK);
test_reader_writer_interface(original);
}
#[test]
fn lwe_serialization() {
let original: LWECiphertext<Vec<u8>> = LWECiphertext::alloc_with(N_LWE, BASE2K, K);
let original: LWE<Vec<u8>> = LWE::alloc(N_LWE, BASE2K, K);
test_reader_writer_interface(original);
}
#[test]
fn lwe_compressed_serialization() {
let original: LWECiphertextCompressed<Vec<u8>> = LWECiphertextCompressed::alloc_with(BASE2K, K);
let original: LWECompressed<Vec<u8>> = LWECompressed::alloc(BASE2K, K);
test_reader_writer_interface(original);
}
#[test]
fn test_gglwe_serialization() {
let original: GGLWE<Vec<u8>> = GGLWE::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GGLWE<Vec<u8>> = GGLWE::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn test_gglwe_compressed_serialization() {
let original: GGLWECiphertextCompressed<Vec<u8>> =
GGLWECiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GGLWECompressed<Vec<u8>> = GGLWECompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn test_glwe_switching_key_serialization() {
let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
let original: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn test_glwe_switching_key_compressed_serialization() {
let original: GLWESwitchingKeyCompressed<Vec<u8>> =
GLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
GLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
@@ -76,39 +75,38 @@ fn test_automorphism_key_serialization() {
#[test]
fn test_automorphism_key_compressed_serialization() {
let original: AutomorphismKeyCompressed<Vec<u8>> =
AutomorphismKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn test_tensor_key_serialization() {
let original: TensorKey<Vec<u8>> = TensorKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: TensorKey<Vec<u8>> = TensorKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn test_tensor_key_compressed_serialization() {
let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: TensorKeyCompressed<Vec<u8>> = TensorKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn glwe_to_lwe_switching_key_serialization() {
let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM);
let original: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original);
}
#[test]
fn glwe_to_lwe_switching_key_compressed_serialization() {
let original: GLWEToLWESwitchingKeyCompressed<Vec<u8>> =
GLWEToLWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM);
GLWEToLWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original);
}
#[test]
fn lwe_to_glwe_switching_key_serialization() {
let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM);
let original: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(N_GLWE, BASE2K, K, RANK, DNUM);
test_reader_writer_interface(original);
}
@@ -121,24 +119,24 @@ fn lwe_to_glwe_switching_key_compressed_serialization() {
#[test]
fn lwe_switching_key_serialization() {
let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc_with(N_GLWE, BASE2K, K, DNUM);
let original: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(N_GLWE, BASE2K, K, DNUM);
test_reader_writer_interface(original);
}
#[test]
fn lwe_switching_key_compressed_serialization() {
let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc_with(N_GLWE, BASE2K, K, DNUM);
let original: LWESwitchingKeyCompressed<Vec<u8>> = LWESwitchingKeyCompressed::alloc(N_GLWE, BASE2K, K, DNUM);
test_reader_writer_interface(original);
}
#[test]
fn ggsw_serialization() {
let original: GGSW<Vec<u8>> = GGSW::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: GGSW<Vec<u8>> = GGSW::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}
#[test]
fn ggsw_compressed_serialization() {
let original: GGSWCiphertextCompressed<Vec<u8>> = GGSWCiphertextCompressed::alloc_with(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
let original: GGSWCompressed<Vec<u8>> = GGSWCompressed::alloc(N_GLWE, BASE2K, K, RANK, DNUM, DSIZE);
test_reader_writer_interface(original);
}

View File

@@ -111,9 +111,9 @@ where
rank: rank.into(),
};
let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_in_infos);
let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_out_infos);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_infos);
let mut auto_key_in: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_in_infos);
let mut auto_key_out: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_out_infos);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_infos);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -130,7 +130,7 @@ where
),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key_in);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key_in);
sk.fill_ternary_prob(0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
@@ -166,9 +166,9 @@ where
scratch.borrow(),
);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&auto_key_out_infos);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key_out_infos);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key_out_infos);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key_out_infos);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
for i in 0..rank {
module.vec_znx_automorphism(
@@ -311,8 +311,8 @@ where
rank: rank.into(),
};
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_apply_layout);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout);
let mut auto_key_apply: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_apply_layout);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -324,7 +324,7 @@ where
| AutomorphismKey::automorphism_inplace_scratch_space(module, &auto_key, &auto_key_apply),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);
sk.fill_ternary_prob(0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
@@ -355,9 +355,9 @@ 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<Vec<u8>> = GLWEPlaintext::alloc(&auto_key);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&auto_key);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc(&auto_key);
let mut sk_auto: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
for i in 0..rank {

View File

@@ -128,10 +128,10 @@ where
rank: rank.into(),
};
let mut ct_in: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_in_layout);
let mut ct_out: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_layout);
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_layout);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut ct_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_layout);
let mut ct_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout);
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_layout);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]);
@@ -147,7 +147,7 @@ where
let var_xs: f64 = 0.5;
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct_out);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
@@ -309,9 +309,9 @@ where
rank: rank.into(),
};
let mut ct: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_out_layout);
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc(&tensor_key_layout);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&auto_key_layout);
let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout);
let mut tensor_key: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&tensor_key_layout);
let mut auto_key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&auto_key_layout);
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
let mut source_xs: Source = Source::new([0u8; 32]);
@@ -327,7 +327,7 @@ where
let var_xs: f64 = 0.5;
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

View File

@@ -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<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos);
let mut ct_in: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_in_infos);
let mut ct_out: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos);
let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos);
let mut ct_in: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_in_infos);
let mut ct_out: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -113,12 +113,12 @@ where
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_scratch_space(module, &autokey)
| GLWECiphertext::decrypt_scratch_space(module, &ct_out)
| GLWECiphertext::encrypt_sk_scratch_space(module, &ct_in)
| GLWECiphertext::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey),
| GLWE::decrypt_scratch_space(module, &ct_out)
| GLWE::encrypt_sk_scratch_space(module, &ct_in)
| GLWE::automorphism_scratch_space(module, &ct_out, &ct_in, &autokey),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct_out);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());
@@ -219,7 +219,7 @@ 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<Vec<u8>> = AutomorphismKey::alloc(&autokey_infos);
let mut ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ct_out_infos);
let mut autokey: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&autokey_infos);
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ct_out_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ct_out_infos);
let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
@@ -247,12 +247,12 @@ where
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_scratch_space(module, &autokey)
| GLWECiphertext::decrypt_scratch_space(module, &ct)
| GLWECiphertext::encrypt_sk_scratch_space(module, &ct)
| GLWECiphertext::automorphism_inplace_scratch_space(module, &ct, &autokey),
| GLWE::decrypt_scratch_space(module, &ct)
| GLWE::encrypt_sk_scratch_space(module, &ct)
| GLWE::automorphism_inplace_scratch_space(module, &ct, &autokey),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(&ct);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct);
sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow());

View File

@@ -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<B> = ScratchOwned::alloc(
LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, &lwe_to_glwe_infos)
| GLWECiphertext::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
| GLWECiphertext::decrypt_scratch_space(module, &glwe_infos),
| GLWE::from_lwe_scratch_space(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
| GLWE::decrypt_scratch_space(module, &glwe_infos),
);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
@@ -112,13 +111,13 @@ where
let data: i64 = 17;
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos);
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
lwe_pt.encode_i64(data, k_lwe_pt);
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe);
let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc(&lwe_to_glwe_infos);
let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos);
ksk.encrypt_sk(
module,
@@ -129,13 +128,13 @@ where
scratch.borrow(),
);
let mut glwe_ct: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos);
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
glwe_ct.decrypt(module, &mut glwe_pt, &sk_glwe_prepared, scratch.borrow());
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
@@ -196,7 +195,7 @@ 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<B> = ScratchOwned::alloc(
GLWEToLWESwitchingKey::encrypt_sk_scratch_space(module, &glwe_to_lwe_infos)
| LWECiphertext::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
| GLWECiphertext::decrypt_scratch_space(module, &glwe_infos),
| LWE::from_glwe_scratch_space(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
| GLWE::decrypt_scratch_space(module, &glwe_infos),
);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
@@ -228,10 +227,10 @@ where
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
let data: i64 = 17;
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
let mut glwe_ct = GLWECiphertext::alloc(&glwe_infos);
let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos);
glwe_ct.encrypt_sk(
module,
&glwe_pt,
@@ -252,13 +251,13 @@ where
scratch.borrow(),
);
let mut lwe_ct: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos);
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);

Some files were not shown because too many files have changed in this diff Show More