From 0be569eca0ee6d675da11359bb957e7abe21e58d Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Sun, 17 Aug 2025 13:02:47 +0200 Subject: [PATCH] Clippy check & update CI (#73) * updated CI workflow with clippy & fmt --- .github/workflows/ci.yml | 8 + backend/src/hal/api/module.rs | 4 +- backend/src/hal/api/vec_znx.rs | 8 +- backend/src/hal/api/vec_znx_big.rs | 4 + backend/src/hal/api/vmp_pmat.rs | 2 + backend/src/hal/delegates/vec_znx.rs | 4 +- backend/src/hal/layouts/encoding.rs | 4 +- backend/src/hal/layouts/mat_znx.rs | 20 +- backend/src/hal/layouts/mod.rs | 5 + backend/src/hal/layouts/module.rs | 11 +- backend/src/hal/layouts/scalar_znx.rs | 31 +-- backend/src/hal/layouts/vec_znx.rs | 34 ++-- backend/src/hal/layouts/vec_znx_dft.rs | 4 +- backend/src/hal/oep/module.rs | 4 + backend/src/hal/oep/scratch.rs | 60 ++++++ backend/src/hal/oep/svp_ppol.rs | 24 +++ backend/src/hal/oep/vec_znx.rs | 74 ++++---- backend/src/hal/oep/vec_znx_big.rs | 96 ++++++++++ backend/src/hal/oep/vec_znx_dft.rs | 60 ++++++ backend/src/hal/oep/vmp_pmat.rs | 38 ++++ .../implementation/cpu_spqlios/ffi/module.rs | 1 + .../src/implementation/cpu_spqlios/scratch.rs | 2 +- .../src/implementation/cpu_spqlios/vec_znx.rs | 178 +++++------------- .../cpu_spqlios/vec_znx_dft_fft64.rs | 16 +- backend/src/lib.rs | 3 +- core/benches/external_product_glwe_fft64.rs | 6 +- core/benches/keyswitch_glwe_fft64.rs | 10 +- core/src/automorphism/gglwe_atk.rs | 5 +- core/src/automorphism/ggsw_ct.rs | 2 + core/src/automorphism/glwe_ct.rs | 1 + core/src/decryption/glwe_ct.rs | 2 +- core/src/decryption/lwe_ct.rs | 4 +- core/src/encryption/compressed/gglwe_atk.rs | 3 +- core/src/encryption/compressed/gglwe_ct.rs | 1 + core/src/encryption/compressed/gglwe_ksk.rs | 1 + core/src/encryption/compressed/gglwe_tsk.rs | 2 +- core/src/encryption/compressed/ggsw_ct.rs | 1 + core/src/encryption/compressed/glwe_ct.rs | 2 + core/src/encryption/gglwe_atk.rs | 3 +- core/src/encryption/gglwe_ct.rs | 1 + core/src/encryption/gglwe_ksk.rs | 1 + core/src/encryption/gglwe_tsk.rs | 2 +- core/src/encryption/ggsw_ct.rs | 1 + core/src/encryption/glwe_ct.rs | 28 +-- core/src/encryption/glwe_pk.rs | 10 +- core/src/encryption/glwe_to_lwe_ksk.rs | 1 + core/src/encryption/lwe_ct.rs | 4 +- core/src/encryption/lwe_ksk.rs | 1 + core/src/encryption/lwe_to_glwe_ksk.rs | 3 +- core/src/external_product/gglwe_atk.rs | 1 + core/src/external_product/gglwe_ksk.rs | 1 + core/src/external_product/ggsw_ct.rs | 1 + core/src/external_product/glwe_ct.rs | 3 +- core/src/glwe_packing.rs | 48 +++-- core/src/glwe_trace.rs | 10 +- core/src/keyswitching/gglwe_ct.rs | 2 + core/src/keyswitching/ggsw_ct.rs | 2 + core/src/keyswitching/glwe_ct.rs | 5 +- core/src/layouts/compressed/gglwe_atk.rs | 2 +- core/src/layouts/compressed/gglwe_ct.rs | 2 +- core/src/layouts/compressed/gglwe_ksk.rs | 2 +- core/src/layouts/compressed/gglwe_tsk.rs | 4 +- core/src/layouts/compressed/ggsw_ct.rs | 4 +- core/src/layouts/compressed/glwe_ct.rs | 2 +- .../src/layouts/compressed/glwe_to_lwe_ksk.rs | 2 +- core/src/layouts/compressed/lwe_ct.rs | 6 +- core/src/layouts/compressed/lwe_ksk.rs | 2 +- .../src/layouts/compressed/lwe_to_glwe_ksk.rs | 2 +- core/src/layouts/gglwe_atk.rs | 2 +- core/src/layouts/gglwe_ct.rs | 2 +- core/src/layouts/gglwe_tsk.rs | 4 +- core/src/layouts/ggsw_ct.rs | 2 +- core/src/layouts/glwe_ct.rs | 23 +-- core/src/layouts/glwe_pk.rs | 4 +- core/src/layouts/glwe_pt.rs | 2 +- core/src/layouts/glwe_to_lwe_ksk.rs | 2 +- core/src/layouts/lwe_ct.rs | 4 +- core/src/layouts/lwe_ksk.rs | 2 +- core/src/layouts/lwe_pt.rs | 4 +- core/src/layouts/lwe_to_glwe_ksk.rs | 2 +- core/src/layouts/prepared/gglwe_atk.rs | 2 +- core/src/layouts/prepared/gglwe_ct.rs | 4 +- core/src/layouts/prepared/gglwe_ksk.rs | 2 + core/src/layouts/prepared/gglwe_tsk.rs | 2 +- core/src/layouts/prepared/ggsw_ct.rs | 2 +- core/src/layouts/prepared/glwe_ct.rs | 177 +++++++++++++++++ core/src/layouts/prepared/glwe_pk.rs | 4 +- core/src/layouts/prepared/glwe_to_lwe_ksk.rs | 2 +- core/src/layouts/prepared/lwe_ksk.rs | 2 +- core/src/layouts/prepared/lwe_to_glwe_ksk.rs | 2 +- core/src/noise/gglwe_ct.rs | 6 +- core/src/noise/ggsw_ct.rs | 12 +- core/src/noise/glwe_ct.rs | 6 +- core/src/noise/mod.rs | 4 + core/src/scratch.rs | 12 +- .../tests/generics/automorphism/gglwe_atk.rs | 12 +- .../tests/generics/automorphism/ggsw_ct.rs | 16 +- .../tests/generics/automorphism/glwe_ct.rs | 12 +- core/src/tests/generics/conversion.rs | 10 +- .../tests/generics/encryption/gglwe_atk.rs | 10 +- .../src/tests/generics/encryption/gglwe_ct.rs | 10 +- core/src/tests/generics/encryption/ggsw_ct.rs | 22 +-- core/src/tests/generics/encryption/glwe_ct.rs | 28 ++- .../src/tests/generics/encryption/glwe_tsk.rs | 10 +- .../generics/external_product/gglwe_ksk.rs | 12 +- .../generics/external_product/ggsw_ct.rs | 16 +- .../generics/external_product/glwe_ct.rs | 12 +- core/src/tests/generics/keyswitch/gglwe_ct.rs | 12 +- core/src/tests/generics/keyswitch/ggsw_ct.rs | 16 +- core/src/tests/generics/keyswitch/glwe_ct.rs | 11 +- core/src/tests/generics/keyswitch/lwe_ct.rs | 5 +- core/src/tests/generics/packing.rs | 9 +- core/src/tests/generics/trace.rs | 5 +- poulpy/Cargo.toml | 6 + schemes/src/tfhe/blind_rotation/cggi_algo.rs | 18 +- schemes/src/tfhe/blind_rotation/cggi_key.rs | 1 + schemes/src/tfhe/blind_rotation/key.rs | 1 + .../src/tfhe/blind_rotation/key_prepared.rs | 21 +-- schemes/src/tfhe/blind_rotation/lut.rs | 8 +- .../tests/generic_blind_rotation.rs | 7 +- .../tfhe/blind_rotation/tests/generic_lut.rs | 12 +- .../{circuit_bootstrapping.rs => circuit.rs} | 54 +++--- schemes/src/tfhe/circuit_bootstrapping/key.rs | 9 +- schemes/src/tfhe/circuit_bootstrapping/mod.rs | 5 +- .../tests/circuit_bootstrapping.rs | 10 +- 125 files changed, 1033 insertions(+), 530 deletions(-) create mode 100644 core/src/layouts/prepared/glwe_ct.rs rename schemes/src/tfhe/circuit_bootstrapping/{circuit_bootstrapping.rs => circuit.rs} (91%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f96cc5c..e3e2259 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,8 @@ jobs: - name: Install Rust nightly uses: dtolnay/rust-toolchain@nightly + with: + components: clippy, rustfmt - name: Cache cargo dependencies uses: actions/cache@v4 @@ -33,5 +35,11 @@ jobs: - name: Build run: cargo build --all-targets + - name: Clippy (deny warnings) + run: cargo clippy --workspace --all-targets --all-features -- -D warnings + + - name: rustfmt (check only) + run: cargo fmt --all --check + - name: Run tests run: cargo test --all \ No newline at end of file diff --git a/backend/src/hal/api/module.rs b/backend/src/hal/api/module.rs index 412c70f..7ab8672 100644 --- a/backend/src/hal/api/module.rs +++ b/backend/src/hal/api/module.rs @@ -1,6 +1,6 @@ -use crate::hal::layouts::{Backend, Module}; +use crate::hal::layouts::Backend; /// Instantiate a new [crate::hal::layouts::Module]. pub trait ModuleNew { - fn new(n: u64) -> Module; + fn new(n: u64) -> Self; } diff --git a/backend/src/hal/api/vec_znx.rs b/backend/src/hal/api/vec_znx.rs index 421199b..fa645a6 100644 --- a/backend/src/hal/api/vec_znx.rs +++ b/backend/src/hal/api/vec_znx.rs @@ -164,7 +164,7 @@ pub trait VecZnxSplit { /// /// This method requires that all [crate::hal::layouts::VecZnx] of b have the same ring degree /// and that b.n() * b.len() <= a.n() - fn vec_znx_split(&self, res: &mut Vec, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + fn vec_znx_split(&self, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) where R: VecZnxToMut, A: VecZnxToRef; @@ -177,7 +177,7 @@ pub trait VecZnxMerge { /// /// This method requires that all [crate::hal::layouts::VecZnx] of a have the same ring degree /// and that a.n() * a.len() <= b.n() - fn vec_znx_merge(&self, res: &mut R, res_col: usize, a: Vec, a_col: usize) + fn vec_znx_merge(&self, res: &mut R, res_col: usize, a: &[A], a_col: usize) where R: VecZnxToMut, A: VecZnxToRef; @@ -204,6 +204,7 @@ pub trait VecZnxFillUniform { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] pub trait VecZnxFillDistF64 { fn vec_znx_fill_dist_f64>( &self, @@ -218,6 +219,7 @@ pub trait VecZnxFillDistF64 { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] pub trait VecZnxAddDistF64 { /// Adds vector sampled according to the provided distribution, scaled by 2^{-k} and bounded to \[-bound, bound\]. fn vec_znx_add_dist_f64>( @@ -233,6 +235,7 @@ pub trait VecZnxAddDistF64 { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] pub trait VecZnxFillNormal { fn vec_znx_fill_normal( &self, @@ -247,6 +250,7 @@ pub trait VecZnxFillNormal { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] pub trait VecZnxAddNormal { /// Adds a discrete normal vector scaled by 2^{-k} with the provided standard deviation and bounded to \[-bound, bound\]. fn vec_znx_add_normal( diff --git a/backend/src/hal/api/vec_znx_big.rs b/backend/src/hal/api/vec_znx_big.rs index 249e078..e376116 100644 --- a/backend/src/hal/api/vec_znx_big.rs +++ b/backend/src/hal/api/vec_znx_big.rs @@ -19,6 +19,7 @@ pub trait VecZnxBigFromBytes { fn vec_znx_big_from_bytes(&self, n: usize, cols: usize, size: usize, bytes: Vec) -> VecZnxBigOwned; } +#[allow(clippy::too_many_arguments)] /// Add a discrete normal distribution on res. /// /// # Arguments @@ -42,6 +43,7 @@ pub trait VecZnxBigAddNormal { ); } +#[allow(clippy::too_many_arguments)] pub trait VecZnxBigFillNormal { fn vec_znx_big_fill_normal>( &self, @@ -55,6 +57,7 @@ pub trait VecZnxBigFillNormal { ); } +#[allow(clippy::too_many_arguments)] pub trait VecZnxBigFillDistF64 { fn vec_znx_big_fill_dist_f64, D: Distribution>( &self, @@ -68,6 +71,7 @@ pub trait VecZnxBigFillDistF64 { ); } +#[allow(clippy::too_many_arguments)] pub trait VecZnxBigAddDistF64 { fn vec_znx_big_add_dist_f64, D: Distribution>( &self, diff --git a/backend/src/hal/api/vmp_pmat.rs b/backend/src/hal/api/vmp_pmat.rs index bdead9c..9f06ea1 100644 --- a/backend/src/hal/api/vmp_pmat.rs +++ b/backend/src/hal/api/vmp_pmat.rs @@ -33,6 +33,7 @@ pub trait VmpPrepare { A: MatZnxToRef; } +#[allow(clippy::too_many_arguments)] pub trait VmpApplyTmpBytes { fn vmp_apply_tmp_bytes( &self, @@ -78,6 +79,7 @@ pub trait VmpApply { C: VmpPMatToRef; } +#[allow(clippy::too_many_arguments)] pub trait VmpApplyAddTmpBytes { fn vmp_apply_add_tmp_bytes( &self, diff --git a/backend/src/hal/delegates/vec_znx.rs b/backend/src/hal/delegates/vec_znx.rs index 0c45506..68d0290 100644 --- a/backend/src/hal/delegates/vec_znx.rs +++ b/backend/src/hal/delegates/vec_znx.rs @@ -274,7 +274,7 @@ impl VecZnxSplit for Module where B: Backend + VecZnxSplitImpl, { - fn vec_znx_split(&self, res: &mut Vec, res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + fn vec_znx_split(&self, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) where R: VecZnxToMut, A: VecZnxToRef, @@ -287,7 +287,7 @@ impl VecZnxMerge for Module where B: Backend + VecZnxMergeImpl, { - fn vec_znx_merge(&self, res: &mut R, res_col: usize, a: Vec, a_col: usize) + fn vec_znx_merge(&self, res: &mut R, res_col: usize, a: &[A], a_col: usize) where R: VecZnxToMut, A: VecZnxToRef, diff --git a/backend/src/hal/layouts/encoding.rs b/backend/src/hal/layouts/encoding.rs index e12e729..abf27a6 100644 --- a/backend/src/hal/layouts/encoding.rs +++ b/backend/src/hal/layouts/encoding.rs @@ -15,7 +15,7 @@ impl VecZnx { let a: VecZnx<&mut [u8]> = self.to_mut(); assert!( size <= a.size(), - "invalid argument k: k.div_ceil(basek)={} > a.size()={}", + "invalid argument k.div_ceil(basek)={} > a.size()={}", size, a.size() ); @@ -69,7 +69,7 @@ impl VecZnx { assert!(idx < a.n()); assert!( size <= a.size(), - "invalid argument k: k.div_ceil(basek)={} > a.size()={}", + "invalid argument k.div_ceil(basek)={} > a.size()={}", size, a.size() ); diff --git a/backend/src/hal/layouts/mat_znx.rs b/backend/src/hal/layouts/mat_znx.rs index 7021d02..467dd12 100644 --- a/backend/src/hal/layouts/mat_znx.rs +++ b/backend/src/hal/layouts/mat_znx.rs @@ -2,7 +2,7 @@ use crate::{ alloc_aligned, hal::{ api::{DataView, DataViewMut, FillUniform, Reset, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero}, - layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ToOwnedDeep, VecZnx, WriterTo}, }, }; use std::fmt; @@ -21,6 +21,20 @@ pub struct MatZnx { cols_out: usize, } +impl ToOwnedDeep for MatZnx { + type Owned = MatZnx>; + fn to_owned_deep(&self) -> Self::Owned { + MatZnx { + data: self.data.as_ref().to_vec(), + n: self.n, + size: self.size, + rows: self.rows, + cols_in: self.cols_in, + cols_out: self.cols_out, + } + } +} + impl fmt::Debug for MatZnx { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self) @@ -86,7 +100,7 @@ impl MatZnx> { pub fn alloc(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> Self { let data: Vec = alloc_aligned(Self::alloc_bytes(n, rows, cols_in, cols_out, size)); Self { - data: data.into(), + data, n, size, rows, @@ -99,7 +113,7 @@ impl MatZnx> { let data: Vec = bytes.into(); assert!(data.len() == Self::alloc_bytes(n, rows, cols_in, cols_out, size)); Self { - data: data.into(), + data, n, size, rows, diff --git a/backend/src/hal/layouts/mod.rs b/backend/src/hal/layouts/mod.rs index 5d402ff..505dd74 100644 --- a/backend/src/hal/layouts/mod.rs +++ b/backend/src/hal/layouts/mod.rs @@ -25,3 +25,8 @@ pub use vmp_pmat::*; pub trait Data = PartialEq + Eq + Sized; pub trait DataRef = Data + AsRef<[u8]>; pub trait DataMut = DataRef + AsMut<[u8]>; + +pub trait ToOwnedDeep { + type Owned; + fn to_owned_deep(&self) -> Self::Owned; +} diff --git a/backend/src/hal/layouts/module.rs b/backend/src/hal/layouts/module.rs index 14d2d21..be55cb8 100644 --- a/backend/src/hal/layouts/module.rs +++ b/backend/src/hal/layouts/module.rs @@ -2,6 +2,7 @@ use std::{marker::PhantomData, ptr::NonNull}; use crate::GALOISGENERATOR; +#[allow(clippy::missing_safety_doc)] pub trait Backend: Sized { type Handle: 'static; unsafe fn destroy(handle: NonNull); @@ -17,6 +18,7 @@ impl Module { /// Construct from a raw pointer managed elsewhere. /// SAFETY: `ptr` must be non-null and remain valid for the lifetime of this Module. #[inline] + #[allow(clippy::missing_safety_doc)] pub unsafe fn from_raw_parts(ptr: *mut B::Handle, n: u64) -> Self { Self { ptr: NonNull::new(ptr).expect("null module ptr"), @@ -25,6 +27,7 @@ impl Module { } } + #[allow(clippy::missing_safety_doc)] #[inline] pub unsafe fn ptr(&self) -> *mut ::Handle { self.ptr.as_ptr() @@ -55,7 +58,8 @@ impl Module { if generator == 0 { return 1; } - ((mod_exp_u64(GALOISGENERATOR, generator.abs() as usize) & (self.cyclotomic_order() - 1)) as i64) * generator.signum() + ((mod_exp_u64(GALOISGENERATOR, generator.unsigned_abs() as usize) & (self.cyclotomic_order() - 1)) as i64) + * generator.signum() } // Returns gen^-1 @@ -64,7 +68,10 @@ impl Module { if gal_el == 0 { panic!("cannot invert 0") } - ((mod_exp_u64(gal_el.abs() as u64, (self.cyclotomic_order() - 1) as usize) & (self.cyclotomic_order() - 1)) as i64) + ((mod_exp_u64( + gal_el.unsigned_abs(), + (self.cyclotomic_order() - 1) as usize, + ) & (self.cyclotomic_order() - 1)) as i64) * gal_el.signum() } } diff --git a/backend/src/hal/layouts/scalar_znx.rs b/backend/src/hal/layouts/scalar_znx.rs index 068b116..1277df9 100644 --- a/backend/src/hal/layouts/scalar_znx.rs +++ b/backend/src/hal/layouts/scalar_znx.rs @@ -7,7 +7,7 @@ use crate::{ alloc_aligned, hal::{ api::{DataView, DataViewMut, FillUniform, Reset, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero}, - layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ToOwnedDeep, VecZnx, WriterTo}, }, }; @@ -18,6 +18,17 @@ pub struct ScalarZnx { pub(crate) cols: usize, } +impl ToOwnedDeep for ScalarZnx { + type Owned = ScalarZnx>; + fn to_owned_deep(&self) -> Self::Owned { + ScalarZnx { + data: self.data.as_ref().to_vec(), + n: self.n, + cols: self.cols, + } + } +} + impl ZnxInfos for ScalarZnx { fn cols(&self) -> usize { self.cols @@ -63,7 +74,7 @@ impl ScalarZnx { pub fn fill_ternary_prob(&mut self, col: usize, prob: f64, source: &mut Source) { let choices: [i64; 3] = [-1, 0, 1]; let weights: [f64; 3] = [prob / 2.0, 1.0 - prob, prob / 2.0]; - let dist: WeightedIndex = WeightedIndex::new(&weights).unwrap(); + let dist: WeightedIndex = WeightedIndex::new(weights).unwrap(); self.at_mut(col, 0) .iter_mut() .for_each(|x: &mut i64| *x = choices[dist.sample(source)]); @@ -80,7 +91,7 @@ impl ScalarZnx { pub fn fill_binary_prob(&mut self, col: usize, prob: f64, source: &mut Source) { let choices: [i64; 2] = [0, 1]; let weights: [f64; 2] = [1.0 - prob, prob]; - let dist: WeightedIndex = WeightedIndex::new(&weights).unwrap(); + let dist: WeightedIndex = WeightedIndex::new(weights).unwrap(); self.at_mut(col, 0) .iter_mut() .for_each(|x: &mut i64| *x = choices[dist.sample(source)]); @@ -95,7 +106,7 @@ impl ScalarZnx { } pub fn fill_binary_block(&mut self, col: usize, block_size: usize, source: &mut Source) { - assert!(self.n() % block_size == 0); + assert!(self.n().is_multiple_of(block_size)); let max_idx: u64 = (block_size + 1) as u64; let mask_idx: u64 = (1 << ((u64::BITS - max_idx.leading_zeros()) as u64)) - 1; for block in self.at_mut(col, 0).chunks_mut(block_size) { @@ -114,21 +125,13 @@ impl ScalarZnx> { pub fn alloc(n: usize, cols: usize) -> Self { let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols)); - Self { - data: data.into(), - n, - cols, - } + Self { data, n, cols } } pub fn from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); assert!(data.len() == Self::alloc_bytes(n, cols)); - Self { - data: data.into(), - n, - cols, - } + Self { data, n, cols } } } diff --git a/backend/src/hal/layouts/vec_znx.rs b/backend/src/hal/layouts/vec_znx.rs index b9afc1b..acb64aa 100644 --- a/backend/src/hal/layouts/vec_znx.rs +++ b/backend/src/hal/layouts/vec_znx.rs @@ -4,11 +4,11 @@ use crate::{ alloc_aligned, hal::{ api::{DataView, DataViewMut, FillUniform, Reset, ZnxInfos, ZnxSliceSize, ZnxView, ZnxViewMut, ZnxZero}, - layouts::{Data, DataMut, DataRef, ReaderFrom, WriterTo}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ToOwnedDeep, WriterTo}, }, }; -#[derive(PartialEq, Eq, Clone)] +#[derive(PartialEq, Eq, Clone, Copy)] pub struct VecZnx { pub(crate) data: D, pub(crate) n: usize, @@ -17,6 +17,19 @@ pub struct VecZnx { pub(crate) max_size: usize, } +impl ToOwnedDeep for VecZnx { + type Owned = VecZnx>; + fn to_owned_deep(&self) -> Self::Owned { + VecZnx { + data: self.data.as_ref().to_vec(), + n: self.n, + cols: self.cols, + size: self.size, + max_size: self.max_size, + } + } +} + impl fmt::Debug for VecZnx { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self) @@ -87,7 +100,7 @@ impl VecZnx> { pub fn alloc(n: usize, cols: usize, size: usize) -> Self { let data: Vec = alloc_aligned::(Self::alloc_bytes(n, cols, size)); Self { - data: data.into(), + data, n, cols, size, @@ -99,7 +112,7 @@ impl VecZnx> { let data: Vec = bytes.into(); assert!(data.len() == Self::alloc_bytes(n, cols, size)); Self { - data: data.into(), + data, n, cols, size, @@ -207,19 +220,6 @@ impl VecZnxToMut for VecZnx { } } -impl VecZnx { - pub fn clone(&self) -> VecZnx> { - let self_ref: VecZnx<&[u8]> = self.to_ref(); - VecZnx { - data: self_ref.data.to_vec(), - n: self_ref.n, - cols: self_ref.cols, - size: self_ref.size, - max_size: self_ref.max_size, - } - } -} - use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use rand::RngCore; use sampling::source::Source; diff --git a/backend/src/hal/layouts/vec_znx_dft.rs b/backend/src/hal/layouts/vec_znx_dft.rs index c814532..709a52a 100644 --- a/backend/src/hal/layouts/vec_znx_dft.rs +++ b/backend/src/hal/layouts/vec_znx_dft.rs @@ -94,7 +94,7 @@ where let data: Vec = alloc_aligned::(Self::bytes_of(n, cols, size)); Self { data: data.into(), - n: n, + n, cols, size, max_size: size, @@ -107,7 +107,7 @@ where assert!(data.len() == Self::bytes_of(n, cols, size)); Self { data: data.into(), - n: n, + n, cols, size, max_size: size, diff --git a/backend/src/hal/oep/module.rs b/backend/src/hal/oep/module.rs index f2daa9b..df01329 100644 --- a/backend/src/hal/oep/module.rs +++ b/backend/src/hal/oep/module.rs @@ -1,5 +1,9 @@ use crate::hal::layouts::{Backend, Module}; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait ModuleNewImpl { fn new_impl(n: u64) -> Module; } diff --git a/backend/src/hal/oep/scratch.rs b/backend/src/hal/oep/scratch.rs index 894f530..cd7f92c 100644 --- a/backend/src/hal/oep/scratch.rs +++ b/backend/src/hal/oep/scratch.rs @@ -3,38 +3,74 @@ use crate::hal::{ layouts::{Backend, DataRef, MatZnx, ScalarZnx, Scratch, ScratchOwned, SvpPPol, VecZnx, VecZnxBig, VecZnxDft, VmpPMat}, }; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait ScratchOwnedAllocImpl { fn scratch_owned_alloc_impl(size: usize) -> ScratchOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait ScratchOwnedBorrowImpl { fn scratch_owned_borrow_impl(scratch: &mut ScratchOwned) -> &mut Scratch; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait ScratchFromBytesImpl { fn scratch_from_bytes_impl(data: &mut [u8]) -> &mut Scratch; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait ScratchAvailableImpl { fn scratch_available_impl(scratch: &Scratch) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeSliceImpl { fn take_slice_impl(scratch: &mut Scratch, len: usize) -> (&mut [T], &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeScalarZnxImpl { fn take_scalar_znx_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (ScalarZnx<&mut [u8]>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeSvpPPolImpl { fn take_svp_ppol_impl(scratch: &mut Scratch, n: usize, cols: usize) -> (SvpPPol<&mut [u8], B>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVecZnxImpl { fn take_vec_znx_impl(scratch: &mut Scratch, n: usize, cols: usize, size: usize) -> (VecZnx<&mut [u8]>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVecZnxSliceImpl { fn take_vec_znx_slice_impl( scratch: &mut Scratch, @@ -45,6 +81,10 @@ pub unsafe trait TakeVecZnxSliceImpl { ) -> (Vec>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVecZnxBigImpl { fn take_vec_znx_big_impl( scratch: &mut Scratch, @@ -54,6 +94,10 @@ pub unsafe trait TakeVecZnxBigImpl { ) -> (VecZnxBig<&mut [u8], B>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVecZnxDftImpl { fn take_vec_znx_dft_impl( scratch: &mut Scratch, @@ -63,6 +107,10 @@ pub unsafe trait TakeVecZnxDftImpl { ) -> (VecZnxDft<&mut [u8], B>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVecZnxDftSliceImpl { fn take_vec_znx_dft_slice_impl( scratch: &mut Scratch, @@ -73,6 +121,10 @@ pub unsafe trait TakeVecZnxDftSliceImpl { ) -> (Vec>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeVmpPMatImpl { fn take_vmp_pmat_impl( scratch: &mut Scratch, @@ -84,6 +136,10 @@ pub unsafe trait TakeVmpPMatImpl { ) -> (VmpPMat<&mut [u8], B>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait TakeMatZnxImpl { fn take_mat_znx_impl( scratch: &mut Scratch, @@ -95,6 +151,10 @@ pub unsafe trait TakeMatZnxImpl { ) -> (MatZnx<&mut [u8]>, &mut Scratch); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub trait TakeLikeImpl<'a, B: Backend, T> { type Output; fn take_like_impl(scratch: &'a mut Scratch, template: &T) -> (Self::Output, &'a mut Scratch); diff --git a/backend/src/hal/oep/svp_ppol.rs b/backend/src/hal/oep/svp_ppol.rs index aea822d..7509532 100644 --- a/backend/src/hal/oep/svp_ppol.rs +++ b/backend/src/hal/oep/svp_ppol.rs @@ -2,18 +2,34 @@ use crate::hal::layouts::{ Backend, Module, ScalarZnxToRef, SvpPPolOwned, SvpPPolToMut, SvpPPolToRef, VecZnxDftToMut, VecZnxDftToRef, }; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolFromBytesImpl { fn svp_ppol_from_bytes_impl(n: usize, cols: usize, bytes: Vec) -> SvpPPolOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocImpl { fn svp_ppol_alloc_impl(n: usize, cols: usize) -> SvpPPolOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPPolAllocBytesImpl { fn svp_ppol_alloc_bytes_impl(n: usize, cols: usize) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpPrepareImpl { fn svp_prepare_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -21,6 +37,10 @@ pub unsafe trait SvpPrepareImpl { A: ScalarZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpApplyImpl { fn svp_apply_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where @@ -29,6 +49,10 @@ pub unsafe trait SvpApplyImpl { C: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait SvpApplyInplaceImpl: Backend { fn svp_apply_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where diff --git a/backend/src/hal/oep/vec_znx.rs b/backend/src/hal/oep/vec_znx.rs index 4ebf42b..e921491 100644 --- a/backend/src/hal/oep/vec_znx.rs +++ b/backend/src/hal/oep/vec_znx.rs @@ -6,7 +6,7 @@ use crate::hal::layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_normalize_base2k_tmp_bytes_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L245C17-L245C55) for reference code. /// * See [crate::hal::api::VecZnxNormalizeTmpBytes] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNormalizeTmpBytesImpl { fn vec_znx_normalize_tmp_bytes_impl(module: &Module, n: usize) -> usize; } @@ -14,7 +14,7 @@ pub unsafe trait VecZnxNormalizeTmpBytesImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_normalize_base2k_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L212) for reference code. /// * See [crate::hal::api::VecZnxNormalize] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNormalizeImpl { fn vec_znx_normalize_impl( module: &Module, @@ -32,7 +32,7 @@ pub unsafe trait VecZnxNormalizeImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_normalize_base2k_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L212) for reference code. /// * See [crate::hal::api::VecZnxNormalizeInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNormalizeInplaceImpl { fn vec_znx_normalize_inplace_impl(module: &Module, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch) where @@ -42,7 +42,7 @@ pub unsafe trait VecZnxNormalizeInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_add_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L86) for reference code. /// * See [crate::hal::api::VecZnxAdd] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAddImpl { fn vec_znx_add_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where @@ -54,7 +54,7 @@ pub unsafe trait VecZnxAddImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_add_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L86) for reference code. /// * See [crate::hal::api::VecZnxAddInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAddInplaceImpl { fn vec_znx_add_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -65,7 +65,7 @@ pub unsafe trait VecZnxAddInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_add_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L86) for reference code. /// * See [crate::hal::api::VecZnxAddScalarInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAddScalarInplaceImpl { fn vec_znx_add_scalar_inplace_impl( module: &Module, @@ -82,7 +82,7 @@ pub unsafe trait VecZnxAddScalarInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_sub_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L125) for reference code. /// * See [crate::hal::api::VecZnxSub] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSubImpl { fn vec_znx_sub_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where @@ -94,7 +94,7 @@ pub unsafe trait VecZnxSubImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_sub_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L125) for reference code. /// * See [crate::hal::api::VecZnxSubABInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSubABInplaceImpl { fn vec_znx_sub_ab_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -105,7 +105,7 @@ pub unsafe trait VecZnxSubABInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_sub_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L125) for reference code. /// * See [crate::hal::api::VecZnxSubBAInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSubBAInplaceImpl { fn vec_znx_sub_ba_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -116,7 +116,7 @@ pub unsafe trait VecZnxSubBAInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_sub_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L125) for reference code. /// * See [crate::hal::api::VecZnxSubScalarInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSubScalarInplaceImpl { fn vec_znx_sub_scalar_inplace_impl( module: &Module, @@ -133,7 +133,7 @@ pub unsafe trait VecZnxSubScalarInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_negate_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L322C13-L322C31) for reference code. /// * See [crate::hal::api::VecZnxNegate] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNegateImpl { fn vec_znx_negate_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -144,7 +144,7 @@ pub unsafe trait VecZnxNegateImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_negate_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L322C13-L322C31) for reference code. /// * See [crate::hal::api::VecZnxNegateInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxNegateInplaceImpl { fn vec_znx_negate_inplace_impl(module: &Module, a: &mut A, a_col: usize) where @@ -154,7 +154,7 @@ pub unsafe trait VecZnxNegateInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_rsh_inplace_ref] for reference code. /// * See [crate::hal::api::VecZnxRshInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxRshInplaceImpl { fn vec_znx_rsh_inplace_impl(module: &Module, basek: usize, k: usize, a: &mut A) where @@ -164,7 +164,7 @@ pub unsafe trait VecZnxRshInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_lsh_inplace_ref] for reference code. /// * See [crate::hal::api::VecZnxLshInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxLshInplaceImpl { fn vec_znx_lsh_inplace_impl(module: &Module, basek: usize, k: usize, a: &mut A) where @@ -174,7 +174,7 @@ pub unsafe trait VecZnxLshInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_rotate_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L164) for reference code. /// * See [crate::hal::api::VecZnxRotate] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxRotateImpl { fn vec_znx_rotate_impl(module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -185,7 +185,7 @@ pub unsafe trait VecZnxRotateImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_rotate_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L164) for reference code. /// * See [crate::hal::api::VecZnxRotateInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxRotateInplaceImpl { fn vec_znx_rotate_inplace_impl(module: &Module, k: i64, a: &mut A, a_col: usize) where @@ -195,7 +195,7 @@ pub unsafe trait VecZnxRotateInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_automorphism_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L188) for reference code. /// * See [crate::hal::api::VecZnxAutomorphism] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAutomorphismImpl { fn vec_znx_automorphism_impl(module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -206,7 +206,7 @@ pub unsafe trait VecZnxAutomorphismImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_automorphism_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/32a3f5fcce9863b58e949f2dfd5abc1bfbaa09b4/spqlios/arithmetic/vec_znx.c#L188) for reference code. /// * See [crate::hal::api::VecZnxAutomorphismInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAutomorphismInplaceImpl { fn vec_znx_automorphism_inplace_impl(module: &Module, k: i64, a: &mut A, a_col: usize) where @@ -216,7 +216,7 @@ pub unsafe trait VecZnxAutomorphismInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_mul_xp_minus_one_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/7160f588da49712a042931ea247b4259b95cefcc/spqlios/arithmetic/vec_znx.c#L200C13-L200C41) for reference code. /// * See [crate::hal::api::VecZnxMulXpMinusOne] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxMulXpMinusOneImpl { fn vec_znx_mul_xp_minus_one_impl(module: &Module, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -227,7 +227,7 @@ pub unsafe trait VecZnxMulXpMinusOneImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [vec_znx_mul_xp_minus_one_ref](https://github.com/phantomzone-org/spqlios-arithmetic/blob/7160f588da49712a042931ea247b4259b95cefcc/spqlios/arithmetic/vec_znx.c#L200C13-L200C41) for reference code. /// * See [crate::hal::api::VecZnxMulXpMinusOneInplace] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxMulXpMinusOneInplaceImpl { fn vec_znx_mul_xp_minus_one_inplace_impl(module: &Module, p: i64, res: &mut R, res_col: usize) where @@ -237,16 +237,10 @@ pub unsafe trait VecZnxMulXpMinusOneInplaceImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_split_ref] for reference code. /// * See [crate::hal::api::VecZnxSplit] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSplitImpl { - fn vec_znx_split_impl( - module: &Module, - res: &mut Vec, - res_col: usize, - a: &A, - a_col: usize, - scratch: &mut Scratch, - ) where + fn vec_znx_split_impl(module: &Module, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + where R: VecZnxToMut, A: VecZnxToRef; } @@ -254,9 +248,9 @@ pub unsafe trait VecZnxSplitImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_merge_ref] for reference code. /// * See [crate::hal::api::VecZnxMerge] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxMergeImpl { - fn vec_znx_merge_impl(module: &Module, res: &mut R, res_col: usize, a: Vec, a_col: usize) + fn vec_znx_merge_impl(module: &Module, res: &mut R, res_col: usize, a: &[A], a_col: usize) where R: VecZnxToMut, A: VecZnxToRef; @@ -265,7 +259,7 @@ pub unsafe trait VecZnxMergeImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_switch_degree_ref] for reference code. /// * See [crate::hal::api::VecZnxSwithcDegree] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxSwithcDegreeImpl { fn vec_znx_switch_degree_impl( module: &Module, @@ -279,7 +273,7 @@ pub unsafe trait VecZnxSwithcDegreeImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::implementation::cpu_spqlios::vec_znx::vec_znx_copy_ref] for reference code. /// * See [crate::hal::api::VecZnxCopy] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxCopyImpl { fn vec_znx_copy_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -289,16 +283,17 @@ pub unsafe trait VecZnxCopyImpl { /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxFillUniform] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxFillUniformImpl { fn vec_znx_fill_uniform_impl(module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) where R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxFillDistF64] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxFillDistF64Impl { fn vec_znx_fill_dist_f64_impl>( module: &Module, @@ -313,9 +308,10 @@ pub unsafe trait VecZnxFillDistF64Impl { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxAddDistF64] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAddDistF64Impl { fn vec_znx_add_dist_f64_impl>( module: &Module, @@ -330,9 +326,10 @@ pub unsafe trait VecZnxAddDistF64Impl { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxFillNormal] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxFillNormalImpl { fn vec_znx_fill_normal_impl( module: &Module, @@ -347,9 +344,10 @@ pub unsafe trait VecZnxFillNormalImpl { R: VecZnxToMut; } +#[allow(clippy::too_many_arguments)] /// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) /// * See [crate::hal::api::VecZnxAddNormal] for corresponding public API. -/// * See [crate::doc::backend_safety] for safety contract. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxAddNormalImpl { fn vec_znx_add_normal_impl( module: &Module, diff --git a/backend/src/hal/oep/vec_znx_big.rs b/backend/src/hal/oep/vec_znx_big.rs index 8ff0564..7cf1396 100644 --- a/backend/src/hal/oep/vec_znx_big.rs +++ b/backend/src/hal/oep/vec_znx_big.rs @@ -3,18 +3,35 @@ use sampling::source::Source; use crate::hal::layouts::{Backend, Module, Scratch, VecZnxBigOwned, VecZnxBigToMut, VecZnxBigToRef, VecZnxToMut, VecZnxToRef}; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocImpl { fn vec_znx_big_alloc_impl(n: usize, cols: usize, size: usize) -> VecZnxBigOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigFromBytesImpl { fn vec_znx_big_from_bytes_impl(n: usize, cols: usize, size: usize, bytes: Vec) -> VecZnxBigOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAllocBytesImpl { fn vec_znx_big_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddNormalImpl { fn add_normal_impl>( module: &Module, @@ -28,6 +45,11 @@ pub unsafe trait VecZnxBigAddNormalImpl { ); } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigFillNormalImpl { fn fill_normal_impl>( module: &Module, @@ -41,6 +63,11 @@ pub unsafe trait VecZnxBigFillNormalImpl { ); } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigFillDistF64Impl { fn fill_dist_f64_impl, D: Distribution>( module: &Module, @@ -54,6 +81,11 @@ pub unsafe trait VecZnxBigFillDistF64Impl { ); } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddDistF64Impl { fn add_dist_f64_impl, D: Distribution>( module: &Module, @@ -67,6 +99,10 @@ pub unsafe trait VecZnxBigAddDistF64Impl { ); } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddImpl { fn vec_znx_big_add_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where @@ -75,6 +111,10 @@ pub unsafe trait VecZnxBigAddImpl { C: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddInplaceImpl { fn vec_znx_big_add_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -82,6 +122,10 @@ pub unsafe trait VecZnxBigAddInplaceImpl { A: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddSmallImpl { fn vec_znx_big_add_small_impl( module: &Module, @@ -97,6 +141,10 @@ pub unsafe trait VecZnxBigAddSmallImpl { C: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAddSmallInplaceImpl { fn vec_znx_big_add_small_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -104,6 +152,10 @@ pub unsafe trait VecZnxBigAddSmallInplaceImpl { A: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubImpl { fn vec_znx_big_sub_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where @@ -112,6 +164,10 @@ pub unsafe trait VecZnxBigSubImpl { C: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubABInplaceImpl { fn vec_znx_big_sub_ab_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -119,6 +175,10 @@ pub unsafe trait VecZnxBigSubABInplaceImpl { A: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubBAInplaceImpl { fn vec_znx_big_sub_ba_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -126,6 +186,10 @@ pub unsafe trait VecZnxBigSubBAInplaceImpl { A: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubSmallAImpl { fn vec_znx_big_sub_small_a_impl( module: &Module, @@ -141,6 +205,10 @@ pub unsafe trait VecZnxBigSubSmallAImpl { C: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubSmallAInplaceImpl { fn vec_znx_big_sub_small_a_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -148,6 +216,10 @@ pub unsafe trait VecZnxBigSubSmallAInplaceImpl { A: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubSmallBImpl { fn vec_znx_big_sub_small_b_impl( module: &Module, @@ -163,6 +235,10 @@ pub unsafe trait VecZnxBigSubSmallBImpl { C: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigSubSmallBInplaceImpl { fn vec_znx_big_sub_small_b_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -170,16 +246,28 @@ pub unsafe trait VecZnxBigSubSmallBInplaceImpl { A: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigNegateInplaceImpl { fn vec_znx_big_negate_inplace_impl(module: &Module, a: &mut A, a_col: usize) where A: VecZnxBigToMut; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigNormalizeTmpBytesImpl { fn vec_znx_big_normalize_tmp_bytes_impl(module: &Module, n: usize) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigNormalizeImpl { fn vec_znx_big_normalize_impl( module: &Module, @@ -194,6 +282,10 @@ pub unsafe trait VecZnxBigNormalizeImpl { A: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAutomorphismImpl { fn vec_znx_big_automorphism_impl(module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -201,6 +293,10 @@ pub unsafe trait VecZnxBigAutomorphismImpl { A: VecZnxBigToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxBigAutomorphismInplaceImpl { fn vec_znx_big_automorphism_inplace_impl(module: &Module, k: i64, a: &mut A, a_col: usize) where diff --git a/backend/src/hal/oep/vec_znx_dft.rs b/backend/src/hal/oep/vec_znx_dft.rs index e20c710..3ab7a7e 100644 --- a/backend/src/hal/oep/vec_znx_dft.rs +++ b/backend/src/hal/oep/vec_znx_dft.rs @@ -3,22 +3,42 @@ use crate::hal::layouts::{ VecZnxToRef, }; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocImpl { fn vec_znx_dft_alloc_impl(n: usize, cols: usize, size: usize) -> VecZnxDftOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftFromBytesImpl { fn vec_znx_dft_from_bytes_impl(n: usize, cols: usize, size: usize, bytes: Vec) -> VecZnxDftOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAllocBytesImpl { fn vec_znx_dft_alloc_bytes_impl(n: usize, cols: usize, size: usize) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftToVecZnxBigTmpBytesImpl { fn vec_znx_dft_to_vec_znx_big_tmp_bytes_impl(module: &Module, n: usize) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftToVecZnxBigImpl { fn vec_znx_dft_to_vec_znx_big_impl( module: &Module, @@ -32,6 +52,10 @@ pub unsafe trait VecZnxDftToVecZnxBigImpl { A: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftToVecZnxBigTmpAImpl { fn vec_znx_dft_to_vec_znx_big_tmp_a_impl(module: &Module, res: &mut R, res_col: usize, a: &mut A, a_col: usize) where @@ -39,12 +63,20 @@ pub unsafe trait VecZnxDftToVecZnxBigTmpAImpl { A: VecZnxDftToMut; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftToVecZnxBigConsumeImpl { fn vec_znx_dft_to_vec_znx_big_consume_impl(module: &Module, a: VecZnxDft) -> VecZnxBig where VecZnxDft: VecZnxDftToMut; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAddImpl { fn vec_znx_dft_add_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &D, b_col: usize) where @@ -53,6 +85,10 @@ pub unsafe trait VecZnxDftAddImpl { D: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftAddInplaceImpl { fn vec_znx_dft_add_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -60,6 +96,10 @@ pub unsafe trait VecZnxDftAddInplaceImpl { A: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftSubImpl { fn vec_znx_dft_sub_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &D, b_col: usize) where @@ -68,6 +108,10 @@ pub unsafe trait VecZnxDftSubImpl { D: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftSubABInplaceImpl { fn vec_znx_dft_sub_ab_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -75,6 +119,10 @@ pub unsafe trait VecZnxDftSubABInplaceImpl { A: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftSubBAInplaceImpl { fn vec_znx_dft_sub_ba_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where @@ -82,6 +130,10 @@ pub unsafe trait VecZnxDftSubBAInplaceImpl { A: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftCopyImpl { fn vec_znx_dft_copy_impl( module: &Module, @@ -96,6 +148,10 @@ pub unsafe trait VecZnxDftCopyImpl { A: VecZnxDftToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftFromVecZnxImpl { fn vec_znx_dft_from_vec_znx_impl( module: &Module, @@ -110,6 +166,10 @@ pub unsafe trait VecZnxDftFromVecZnxImpl { A: VecZnxToRef; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VecZnxDftZeroImpl { fn vec_znx_dft_zero_impl(module: &Module, res: &mut R) where diff --git a/backend/src/hal/oep/vmp_pmat.rs b/backend/src/hal/oep/vmp_pmat.rs index 926ae27..1f7aeb6 100644 --- a/backend/src/hal/oep/vmp_pmat.rs +++ b/backend/src/hal/oep/vmp_pmat.rs @@ -2,14 +2,26 @@ use crate::hal::layouts::{ Backend, MatZnxToRef, Module, Scratch, VecZnxDftToMut, VecZnxDftToRef, VmpPMatOwned, VmpPMatToMut, VmpPMatToRef, }; +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocImpl { fn vmp_pmat_alloc_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> VmpPMatOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatAllocBytesImpl { fn vmp_pmat_alloc_bytes_impl(n: usize, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatFromBytesImpl { fn vmp_pmat_from_bytes_impl( n: usize, @@ -21,6 +33,10 @@ pub unsafe trait VmpPMatFromBytesImpl { ) -> VmpPMatOwned; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPrepareTmpBytesImpl { fn vmp_prepare_tmp_bytes_impl( module: &Module, @@ -32,6 +48,10 @@ pub unsafe trait VmpPrepareTmpBytesImpl { ) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpPMatPrepareImpl { fn vmp_prepare_impl(module: &Module, res: &mut R, a: &A, scratch: &mut Scratch) where @@ -39,6 +59,11 @@ pub unsafe trait VmpPMatPrepareImpl { A: MatZnxToRef; } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpApplyTmpBytesImpl { fn vmp_apply_tmp_bytes_impl( module: &Module, @@ -52,6 +77,10 @@ pub unsafe trait VmpApplyTmpBytesImpl { ) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpApplyImpl { fn vmp_apply_impl(module: &Module, res: &mut R, a: &A, b: &C, scratch: &mut Scratch) where @@ -60,6 +89,11 @@ pub unsafe trait VmpApplyImpl { C: VmpPMatToRef; } +#[allow(clippy::too_many_arguments)] +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpApplyAddTmpBytesImpl { fn vmp_apply_add_tmp_bytes_impl( module: &Module, @@ -73,6 +107,10 @@ pub unsafe trait VmpApplyAddTmpBytesImpl { ) -> usize; } +/// # THIS TRAIT IS AN OPEN EXTENSION POINT (unsafe) +/// * See TODO for reference code. +/// * See TODO for corresponding public API. +/// # Safety [crate::doc::backend_safety] for safety contract. pub unsafe trait VmpApplyAddImpl { // Same as [MatZnxDftOps::vmp_apply] except result is added on R instead of overwritting R. fn vmp_apply_add_impl(module: &Module, res: &mut R, a: &A, b: &C, scale: usize, scratch: &mut Scratch) diff --git a/backend/src/implementation/cpu_spqlios/ffi/module.rs b/backend/src/implementation/cpu_spqlios/ffi/module.rs index 011c419..b593448 100644 --- a/backend/src/implementation/cpu_spqlios/ffi/module.rs +++ b/backend/src/implementation/cpu_spqlios/ffi/module.rs @@ -5,6 +5,7 @@ pub struct module_info_t { pub type module_type_t = ::std::os::raw::c_uint; pub use self::module_type_t as MODULE_TYPE; +#[allow(clippy::upper_case_acronyms)] pub type MODULE = module_info_t; unsafe extern "C" { diff --git a/backend/src/implementation/cpu_spqlios/scratch.rs b/backend/src/implementation/cpu_spqlios/scratch.rs index 9dad5e7..3ebb6d2 100644 --- a/backend/src/implementation/cpu_spqlios/scratch.rs +++ b/backend/src/implementation/cpu_spqlios/scratch.rs @@ -260,7 +260,7 @@ fn take_slice_aligned(data: &mut [u8], take_len: usize) -> (&mut [u8], &mut [u8] let take_slice: &mut [u8] = &mut *std::ptr::slice_from_raw_parts_mut(ptr.add(aligned_offset), take_len); - return (take_slice, rem_slice); + (take_slice, rem_slice) } } else { panic!( diff --git a/backend/src/implementation/cpu_spqlios/vec_znx.rs b/backend/src/implementation/cpu_spqlios/vec_znx.rs index 87d99a8..db8a9c6 100644 --- a/backend/src/implementation/cpu_spqlios/vec_znx.rs +++ b/backend/src/implementation/cpu_spqlios/vec_znx.rs @@ -34,10 +34,7 @@ where } } -unsafe impl VecZnxNormalizeImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxNormalizeImpl for B { fn vec_znx_normalize_impl( module: &Module, basek: usize, @@ -77,10 +74,7 @@ where } } -unsafe impl VecZnxNormalizeInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxNormalizeInplaceImpl for B { fn vec_znx_normalize_inplace_impl(module: &Module, basek: usize, a: &mut A, a_col: usize, scratch: &mut Scratch) where A: VecZnxToMut, @@ -106,10 +100,7 @@ where } } -unsafe impl VecZnxAddImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAddImpl for B { fn vec_znx_add_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where R: VecZnxToMut, @@ -143,10 +134,7 @@ where } } -unsafe impl VecZnxAddInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAddInplaceImpl for B { fn vec_znx_add_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -176,10 +164,7 @@ where } } -unsafe impl VecZnxAddScalarInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAddScalarInplaceImpl for B { fn vec_znx_add_scalar_inplace_impl( module: &Module, res: &mut R, @@ -203,23 +188,20 @@ where vec_znx::vec_znx_add( module.ptr() as *const module_info_t, res.at_mut_ptr(res_col, res_limb), - 1 as u64, + 1_u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, res.at_ptr(res_col, res_limb), - 1 as u64, + 1_u64, res.sl() as u64, ) } } } -unsafe impl VecZnxSubImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxSubImpl for B { fn vec_znx_sub_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &C, b_col: usize) where R: VecZnxToMut, @@ -253,10 +235,7 @@ where } } -unsafe impl VecZnxSubABInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxSubABInplaceImpl for B { fn vec_znx_sub_ab_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -285,10 +264,7 @@ where } } -unsafe impl VecZnxSubBAInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxSubBAInplaceImpl for B { fn vec_znx_sub_ba_inplace_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -317,10 +293,7 @@ where } } -unsafe impl VecZnxSubScalarInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxSubScalarInplaceImpl for B { fn vec_znx_sub_scalar_inplace_impl( module: &Module, res: &mut R, @@ -344,23 +317,20 @@ where vec_znx::vec_znx_sub( module.ptr() as *const module_info_t, res.at_mut_ptr(res_col, res_limb), - 1 as u64, + 1_u64, res.sl() as u64, a.at_ptr(a_col, 0), a.size() as u64, a.sl() as u64, res.at_ptr(res_col, res_limb), - 1 as u64, + 1_u64, res.sl() as u64, ) } } } -unsafe impl VecZnxNegateImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxNegateImpl for B { fn vec_znx_negate_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -386,10 +356,7 @@ where } } -unsafe impl VecZnxNegateInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxNegateInplaceImpl for B { fn vec_znx_negate_inplace_impl(module: &Module, a: &mut A, a_col: usize) where A: VecZnxToMut, @@ -409,10 +376,7 @@ where } } -unsafe impl VecZnxLshInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxLshInplaceImpl for B { fn vec_znx_lsh_inplace_impl(_module: &Module, basek: usize, k: usize, a: &mut A) where A: VecZnxToMut, @@ -453,10 +417,7 @@ where } } -unsafe impl VecZnxRshInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxRshInplaceImpl for B { fn vec_znx_rsh_inplace_impl(_module: &Module, basek: usize, k: usize, a: &mut A) where A: VecZnxToMut, @@ -500,10 +461,7 @@ where } } -unsafe impl VecZnxRotateImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxRotateImpl for B { fn vec_znx_rotate_impl(_module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -528,10 +486,7 @@ where } } -unsafe impl VecZnxRotateInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxRotateInplaceImpl for B { fn vec_znx_rotate_inplace_impl(_module: &Module, k: i64, a: &mut A, a_col: usize) where A: VecZnxToMut, @@ -545,10 +500,7 @@ where } } -unsafe impl VecZnxAutomorphismImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAutomorphismImpl for B { fn vec_znx_automorphism_impl(module: &Module, k: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -575,10 +527,7 @@ where } } -unsafe impl VecZnxAutomorphismInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAutomorphismInplaceImpl for B { fn vec_znx_automorphism_inplace_impl(module: &Module, k: i64, a: &mut A, a_col: usize) where A: VecZnxToMut, @@ -607,10 +556,7 @@ where } } -unsafe impl VecZnxMulXpMinusOneImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxMulXpMinusOneImpl for B { fn vec_znx_mul_xp_minus_one_impl(module: &Module, p: i64, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -638,10 +584,7 @@ where } } -unsafe impl VecZnxMulXpMinusOneInplaceImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxMulXpMinusOneInplaceImpl for B { fn vec_znx_mul_xp_minus_one_inplace_impl(module: &Module, p: i64, res: &mut R, res_col: usize) where R: VecZnxToMut, @@ -666,18 +609,9 @@ where } } -unsafe impl VecZnxSplitImpl for B -where - B: CPUAVX, -{ - fn vec_znx_split_impl( - module: &Module, - res: &mut Vec, - res_col: usize, - a: &A, - a_col: usize, - scratch: &mut Scratch, - ) where +unsafe impl VecZnxSplitImpl for B { + fn vec_znx_split_impl(module: &Module, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch) + where R: VecZnxToMut, A: VecZnxToRef, { @@ -685,15 +619,15 @@ where } } -pub fn vec_znx_split_ref( +pub fn vec_znx_split_ref( module: &Module, - res: &mut Vec, + res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch, ) where - B: CPUAVX, + B: Backend + CPUAVX, R: VecZnxToMut, A: VecZnxToRef, { @@ -720,17 +654,14 @@ pub fn vec_znx_split_ref( module.vec_znx_switch_degree(bi, res_col, &a, a_col); module.vec_znx_rotate(-1, &mut buf, 0, &a, a_col); } else { - module.vec_znx_switch_degree(bi, res_col, &mut buf, a_col); + module.vec_znx_switch_degree(bi, res_col, &buf, a_col); module.vec_znx_rotate_inplace(-1, &mut buf, a_col); } }) } -unsafe impl VecZnxMergeImpl for B -where - B: CPUAVX, -{ - fn vec_znx_merge_impl(module: &Module, res: &mut R, res_col: usize, a: Vec, a_col: usize) +unsafe impl VecZnxMergeImpl for B { + fn vec_znx_merge_impl(module: &Module, res: &mut R, res_col: usize, a: &[A], a_col: usize) where R: VecZnxToMut, A: VecZnxToRef, @@ -739,9 +670,9 @@ where } } -pub fn vec_znx_merge_ref(module: &Module, res: &mut R, res_col: usize, a: Vec, a_col: usize) +pub fn vec_znx_merge_ref(module: &Module, res: &mut R, res_col: usize, a: &[A], a_col: usize) where - B: CPUAVX, + B: Backend + CPUAVX, R: VecZnxToMut, A: VecZnxToRef, { @@ -761,7 +692,7 @@ where ) }); - a.iter().enumerate().for_each(|(_, ai)| { + a.iter().for_each(|ai| { module.vec_znx_switch_degree(&mut res, res_col, ai, a_col); module.vec_znx_rotate_inplace(-1, &mut res, res_col); }); @@ -769,10 +700,7 @@ where module.vec_znx_rotate_inplace(a.len() as i64, &mut res, res_col); } -unsafe impl VecZnxSwithcDegreeImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxSwithcDegreeImpl for B { fn vec_znx_switch_degree_impl(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -782,9 +710,9 @@ where } } -pub fn vec_znx_switch_degree_ref(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) +pub fn vec_znx_switch_degree_ref(module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where - B: CPUAVX, + B: Backend + CPUAVX, R: VecZnxToMut, A: VecZnxToRef, { @@ -817,10 +745,7 @@ where }); } -unsafe impl VecZnxCopyImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxCopyImpl for B { fn vec_znx_copy_impl(_module: &Module, res: &mut R, res_col: usize, a: &A, a_col: usize) where R: VecZnxToMut, @@ -850,10 +775,7 @@ where }) } -unsafe impl VecZnxFillUniformImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxFillUniformImpl for B { fn vec_znx_fill_uniform_impl(_module: &Module, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source) where R: VecZnxToMut, @@ -870,10 +792,7 @@ where } } -unsafe impl VecZnxFillDistF64Impl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxFillDistF64Impl for B { fn vec_znx_fill_dist_f64_impl>( _module: &Module, basek: usize, @@ -916,10 +835,7 @@ where } } -unsafe impl VecZnxAddDistF64Impl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAddDistF64Impl for B { fn vec_znx_add_dist_f64_impl>( _module: &Module, basek: usize, @@ -962,10 +878,7 @@ where } } -unsafe impl VecZnxFillNormalImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxFillNormalImpl for B { fn vec_znx_fill_normal_impl( module: &Module, basek: usize, @@ -990,10 +903,7 @@ where } } -unsafe impl VecZnxAddNormalImpl for B -where - B: CPUAVX, -{ +unsafe impl VecZnxAddNormalImpl for B { fn vec_znx_add_normal_impl( module: &Module, basek: usize, diff --git a/backend/src/implementation/cpu_spqlios/vec_znx_dft_fft64.rs b/backend/src/implementation/cpu_spqlios/vec_znx_dft_fft64.rs index 768e5c4..182f89a 100644 --- a/backend/src/implementation/cpu_spqlios/vec_znx_dft_fft64.rs +++ b/backend/src/implementation/cpu_spqlios/vec_znx_dft_fft64.rs @@ -91,9 +91,9 @@ unsafe impl VecZnxDftToVecZnxBigImpl for FFT64 { vec_znx_dft::vec_znx_idft( module.ptr(), res.at_mut_ptr(res_col, j) as *mut vec_znx_big::vec_znx_big_t, - 1 as u64, + 1_u64, a.at_ptr(a_col, j) as *const vec_znx_dft::vec_znx_dft_t, - 1 as u64, + 1_u64, tmp_bytes.as_mut_ptr(), ) }); @@ -120,9 +120,9 @@ unsafe impl VecZnxDftToVecZnxBigTmpAImpl for FFT64 { vec_znx_dft::vec_znx_idft_tmp_a( module.ptr(), res_mut.at_mut_ptr(res_col, j) as *mut vec_znx_big::vec_znx_big_t, - 1 as u64, + 1_u64, a_mut.at_mut_ptr(a_col, j) as *mut vec_znx_dft::vec_znx_dft_t, - 1 as u64, + 1_u64, ) }); (min_size..res_mut.size()).for_each(|j| { @@ -146,9 +146,9 @@ unsafe impl VecZnxDftToVecZnxBigConsumeImpl for FFT64 { vec_znx_dft::vec_znx_idft_tmp_a( module.ptr(), a_mut.at_mut_ptr(i, j) as *mut vec_znx_big::vec_znx_big_t, - 1 as u64, + 1_u64, a_mut.at_mut_ptr(i, j) as *mut vec_znx_dft::vec_znx_dft_t, - 1 as u64, + 1_u64, ) }); }); @@ -182,9 +182,9 @@ unsafe impl VecZnxDftFromVecZnxImpl for FFT64 { vec_znx_dft::vec_znx_dft( module.ptr(), res_mut.at_mut_ptr(res_col, j) as *mut vec_znx_dft::vec_znx_dft_t, - 1 as u64, + 1_u64, a_ref.at_ptr(a_col, limb), - 1 as u64, + 1_u64, a_ref.sl() as u64, ) } diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 8923775..981c679 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -17,7 +17,7 @@ pub const GALOISGENERATOR: u64 = 5; pub const DEFAULTALIGN: usize = 64; fn is_aligned_custom(ptr: *const T, align: usize) -> bool { - (ptr as usize) % align == 0 + (ptr as usize).is_multiple_of(align) } pub fn is_aligned(ptr: *const T) -> bool { @@ -37,6 +37,7 @@ pub fn cast(data: &[T]) -> &[V] { unsafe { std::slice::from_raw_parts(ptr, len) } } +#[allow(clippy::mut_from_ref)] pub fn cast_mut(data: &[T]) -> &mut [V] { let ptr: *mut V = data.as_ptr() as *mut V; let len: usize = data.len() / size_of::(); diff --git a/core/benches/external_product_glwe_fft64.rs b/core/benches/external_product_glwe_fft64.rs index ee74159..90a88d4 100644 --- a/core/benches/external_product_glwe_fft64.rs +++ b/core/benches/external_product_glwe_fft64.rs @@ -90,7 +90,8 @@ fn bench_external_product_glwe_fft64(c: &mut Criterion) { let ggsw_prepared: GGSWCiphertextPrepared, FFT64> = ct_ggsw.prepare_alloc(&module, scratch.borrow()); move || { - black_box(ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow())); + ct_glwe_out.external_product(&module, &ct_glwe_in, &ggsw_prepared, scratch.borrow()); + black_box(()); } } @@ -185,7 +186,8 @@ fn bench_external_product_glwe_inplace_fft64(c: &mut Criterion) { move || { let scratch_borrow = scratch.borrow(); - black_box(ct_glwe.external_product_inplace(&module, &ggsw_prepared, scratch_borrow)); + ct_glwe.external_product_inplace(&module, &ggsw_prepared, scratch_borrow); + black_box(()); } } diff --git a/core/benches/keyswitch_glwe_fft64.rs b/core/benches/keyswitch_glwe_fft64.rs index 3f17dfc..85e66e2 100644 --- a/core/benches/keyswitch_glwe_fft64.rs +++ b/core/benches/keyswitch_glwe_fft64.rs @@ -96,7 +96,8 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let ksk_prepared: GGLWEAutomorphismKeyPrepared, _> = ksk.prepare_alloc(&module, scratch.borrow()); move || { - black_box(ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow())); + ct_out.automorphism(&module, &ct_in, &ksk_prepared, scratch.borrow()); + black_box(()); } } @@ -105,11 +106,11 @@ fn bench_keyswitch_glwe_fft64(c: &mut Criterion) { let params_set: Vec = vec![Params { log_n: 15, - basek: basek, + basek, k_ct_in: 874 - digits * basek, k_ct_out: 874 - digits * basek, k_ksk: 874, - digits: digits, + digits, rank_in: 1, rank_out: 1, }]; @@ -191,7 +192,8 @@ fn bench_keyswitch_glwe_inplace_fft64(c: &mut Criterion) { let ksk_prepared: GGLWESwitchingKeyPrepared, FFT64> = ksk.prepare_alloc(&module, scratch.borrow()); move || { - black_box(ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow())); + ct.keyswitch_inplace(&module, &ksk_prepared, scratch.borrow()); + black_box(()); } } diff --git a/core/src/automorphism/gglwe_atk.rs b/core/src/automorphism/gglwe_atk.rs index d66b335..0a321d2 100644 --- a/core/src/automorphism/gglwe_atk.rs +++ b/core/src/automorphism/gglwe_atk.rs @@ -10,6 +10,7 @@ use backend::hal::{ use crate::layouts::{GGLWEAutomorphismKey, GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared}; impl GGLWEAutomorphismKey> { + #[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space( module: &Module, n: usize, @@ -43,7 +44,7 @@ impl GGLWEAutomorphismKey> { } impl GGLWEAutomorphismKey { - pub fn automorphism<'a, DataLhs: DataRef, DataRhs: DataRef, B: Backend>( + pub fn automorphism( &mut self, module: &Module, lhs: &GGLWEAutomorphismKey, @@ -149,7 +150,7 @@ impl GGLWEAutomorphismKey { { unsafe { let self_ptr: *mut GGLWEAutomorphismKey = self as *mut GGLWEAutomorphismKey; - self.automorphism(&module, &*self_ptr, rhs, scratch); + self.automorphism(module, &*self_ptr, rhs, scratch); } } } diff --git a/core/src/automorphism/ggsw_ct.rs b/core/src/automorphism/ggsw_ct.rs index 73e6b59..cbcb755 100644 --- a/core/src/automorphism/ggsw_ct.rs +++ b/core/src/automorphism/ggsw_ct.rs @@ -14,6 +14,7 @@ use crate::layouts::{ }; impl GGSWCiphertext> { + #[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space( module: &Module, n: usize, @@ -38,6 +39,7 @@ impl GGSWCiphertext> { ci_dft + (ks_internal | expand) } + #[allow(clippy::too_many_arguments)] pub fn automorphism_inplace_scratch_space( module: &Module, n: usize, diff --git a/core/src/automorphism/glwe_ct.rs b/core/src/automorphism/glwe_ct.rs index 69e5a38..64f8af9 100644 --- a/core/src/automorphism/glwe_ct.rs +++ b/core/src/automorphism/glwe_ct.rs @@ -10,6 +10,7 @@ use backend::hal::{ use crate::layouts::{GLWECiphertext, Infos, prepared::GGLWEAutomorphismKeyPrepared}; impl GLWECiphertext> { + #[allow(clippy::too_many_arguments)] pub fn automorphism_scratch_space( module: &Module, n: usize, diff --git a/core/src/decryption/glwe_ct.rs b/core/src/decryption/glwe_ct.rs index 732ca25..a18f86f 100644 --- a/core/src/decryption/glwe_ct.rs +++ b/core/src/decryption/glwe_ct.rs @@ -64,7 +64,7 @@ impl GLWECiphertext { module.vec_znx_big_add_small_inplace(&mut c0_big, 0, &self.data, 0); // pt = norm(BIG(m + e)) - module.vec_znx_big_normalize(self.basek(), &mut pt.data, 0, &mut c0_big, 0, scratch_1); + module.vec_znx_big_normalize(self.basek(), &mut pt.data, 0, &c0_big, 0, scratch_1); pt.basek = self.basek(); pt.k = pt.k().min(self.k()); diff --git a/core/src/decryption/lwe_ct.rs b/core/src/decryption/lwe_ct.rs index 663a1cb..98cdaae 100644 --- a/core/src/decryption/lwe_ct.rs +++ b/core/src/decryption/lwe_ct.rs @@ -10,12 +10,12 @@ impl LWECiphertext where DataSelf: DataRef, { - pub fn decrypt(&self, module: &Module, pt: &mut LWEPlaintext, sk: &LWESecret) + pub fn decrypt(&self, module: &Module, pt: &mut LWEPlaintext, sk: &LWESecret) where DataPt: DataMut, DataSk: DataRef, Module: VecZnxNormalizeInplace, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/compressed/gglwe_atk.rs b/core/src/encryption/compressed/gglwe_atk.rs index e055953..7fa23cc 100644 --- a/core/src/encryption/compressed/gglwe_atk.rs +++ b/core/src/encryption/compressed/gglwe_atk.rs @@ -27,6 +27,7 @@ impl GGLWEAutomorphismKeyCompressed> { } impl GGLWEAutomorphismKeyCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, @@ -96,7 +97,7 @@ impl GGLWEAutomorphismKeyCompressed { } self.key - .encrypt_sk(module, &sk, &sk_out, seed_xa, source_xe, sigma, scratch_1); + .encrypt_sk(module, sk, &sk_out, seed_xa, source_xe, sigma, scratch_1); self.p = p; } diff --git a/core/src/encryption/compressed/gglwe_ct.rs b/core/src/encryption/compressed/gglwe_ct.rs index e98739b..5308d08 100644 --- a/core/src/encryption/compressed/gglwe_ct.rs +++ b/core/src/encryption/compressed/gglwe_ct.rs @@ -24,6 +24,7 @@ impl GGLWECiphertextCompressed> { } impl GGLWECiphertextCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/compressed/gglwe_ksk.rs b/core/src/encryption/compressed/gglwe_ksk.rs index 21cacd8..a98da59 100644 --- a/core/src/encryption/compressed/gglwe_ksk.rs +++ b/core/src/encryption/compressed/gglwe_ksk.rs @@ -33,6 +33,7 @@ impl GGLWESwitchingKeyCompressed> { } impl GGLWESwitchingKeyCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/compressed/gglwe_tsk.rs b/core/src/encryption/compressed/gglwe_tsk.rs index 10681bc..4607361 100644 --- a/core/src/encryption/compressed/gglwe_tsk.rs +++ b/core/src/encryption/compressed/gglwe_tsk.rs @@ -73,7 +73,7 @@ impl GGLWETensorKeyCompressed { let rank: usize = self.rank(); let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank); - sk_dft_prep.prepare(module, &sk, scratch1); + sk_dft_prep.prepare(module, sk, scratch1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); diff --git a/core/src/encryption/compressed/ggsw_ct.rs b/core/src/encryption/compressed/ggsw_ct.rs index 6842768..b329891 100644 --- a/core/src/encryption/compressed/ggsw_ct.rs +++ b/core/src/encryption/compressed/ggsw_ct.rs @@ -24,6 +24,7 @@ impl GGSWCiphertextCompressed> { } impl GGSWCiphertextCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/compressed/glwe_ct.rs b/core/src/encryption/compressed/glwe_ct.rs index 543eb81..4d2275f 100644 --- a/core/src/encryption/compressed/glwe_ct.rs +++ b/core/src/encryption/compressed/glwe_ct.rs @@ -23,6 +23,7 @@ impl GLWECiphertextCompressed> { } impl GLWECiphertextCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, @@ -59,6 +60,7 @@ impl GLWECiphertextCompressed { ); } + #[allow(clippy::too_many_arguments)] pub(crate) fn encrypt_sk_internal( &mut self, module: &Module, diff --git a/core/src/encryption/gglwe_atk.rs b/core/src/encryption/gglwe_atk.rs index ea1a323..f848393 100644 --- a/core/src/encryption/gglwe_atk.rs +++ b/core/src/encryption/gglwe_atk.rs @@ -28,6 +28,7 @@ impl GGLWEAutomorphismKey> { } impl GGLWEAutomorphismKey { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, @@ -91,7 +92,7 @@ impl GGLWEAutomorphismKey { } self.key - .encrypt_sk(module, &sk, &sk_out, source_xa, source_xe, sigma, scratch_1); + .encrypt_sk(module, sk, &sk_out, source_xa, source_xe, sigma, scratch_1); self.p = p; } diff --git a/core/src/encryption/gglwe_ct.rs b/core/src/encryption/gglwe_ct.rs index 7fa8d53..f4c4adc 100644 --- a/core/src/encryption/gglwe_ct.rs +++ b/core/src/encryption/gglwe_ct.rs @@ -28,6 +28,7 @@ impl GGLWECiphertext> { } impl GGLWECiphertext { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/gglwe_ksk.rs b/core/src/encryption/gglwe_ksk.rs index 3c5b155..195003b 100644 --- a/core/src/encryption/gglwe_ksk.rs +++ b/core/src/encryption/gglwe_ksk.rs @@ -44,6 +44,7 @@ impl GGLWESwitchingKey> { } impl GGLWESwitchingKey { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/gglwe_tsk.rs b/core/src/encryption/gglwe_tsk.rs index d425fd6..b28c259 100644 --- a/core/src/encryption/gglwe_tsk.rs +++ b/core/src/encryption/gglwe_tsk.rs @@ -75,7 +75,7 @@ impl GGLWETensorKey { let rank: usize = self.rank(); let (mut sk_dft_prep, scratch1) = scratch.take_glwe_secret_prepared(n, rank); - sk_dft_prep.prepare(module, &sk, scratch1); + sk_dft_prep.prepare(module, sk, scratch1); let (mut sk_dft, scratch2) = scratch1.take_vec_znx_dft(n, rank, 1); diff --git a/core/src/encryption/ggsw_ct.rs b/core/src/encryption/ggsw_ct.rs index 3b8282f..56fcf13 100644 --- a/core/src/encryption/ggsw_ct.rs +++ b/core/src/encryption/ggsw_ct.rs @@ -27,6 +27,7 @@ impl GGSWCiphertext> { } impl GGSWCiphertext { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/glwe_ct.rs b/core/src/encryption/glwe_ct.rs index a23e568..a25b287 100644 --- a/core/src/encryption/glwe_ct.rs +++ b/core/src/encryption/glwe_ct.rs @@ -38,6 +38,7 @@ impl GLWECiphertext> { } impl GLWECiphertext { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, @@ -133,6 +134,7 @@ impl GLWECiphertext { ); } + #[allow(clippy::too_many_arguments)] pub(crate) fn encrypt_sk_internal( &mut self, module: &Module, @@ -175,6 +177,7 @@ impl GLWECiphertext { ); } + #[allow(clippy::too_many_arguments)] pub fn encrypt_pk( &mut self, module: &Module, @@ -232,6 +235,7 @@ impl GLWECiphertext { ); } + #[allow(clippy::too_many_arguments)] pub(crate) fn encrypt_pk_internal( &mut self, module: &Module, @@ -307,10 +311,10 @@ impl GLWECiphertext { ); // ci_big = u * pk[i] + e + m (if col = i) - if let Some((pt, col)) = pt { - if col == i { - module.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.data, 0); - } + if let Some((pt, col)) = pt + && col == i + { + module.vec_znx_big_add_small_inplace(&mut ci_big, 0, &pt.data, 0); } // ct[i] = norm(ci_big) @@ -319,6 +323,7 @@ impl GLWECiphertext { } } +#[allow(clippy::too_many_arguments)] pub(crate) fn glwe_encrypt_sk_internal( module: &Module, basek: usize, @@ -373,12 +378,7 @@ pub(crate) fn glwe_encrypt_sk_internal GLWEPublicKey { - pub fn generate_from_sk( + pub fn generate_from_sk( &mut self, module: &Module, sk: &GLWESecretPrepared, @@ -34,7 +34,8 @@ impl GLWEPublicKey { + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeVecZnxDftImpl + ScratchAvailableImpl @@ -46,9 +47,8 @@ impl GLWEPublicKey { assert_eq!(self.n(), sk.n()); - match sk.dist { - Distribution::NONE => panic!("invalid sk: SecretDistribution::NONE"), - _ => {} + if sk.dist == Distribution::NONE { + panic!("invalid sk: SecretDistribution::NONE") } } diff --git a/core/src/encryption/glwe_to_lwe_ksk.rs b/core/src/encryption/glwe_to_lwe_ksk.rs index d68662f..530b323 100644 --- a/core/src/encryption/glwe_to_lwe_ksk.rs +++ b/core/src/encryption/glwe_to_lwe_ksk.rs @@ -26,6 +26,7 @@ impl GLWEToLWESwitchingKey> { } impl GLWEToLWESwitchingKey { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/lwe_ct.rs b/core/src/encryption/lwe_ct.rs index ca13157..12ca2db 100644 --- a/core/src/encryption/lwe_ct.rs +++ b/core/src/encryption/lwe_ct.rs @@ -13,7 +13,7 @@ use crate::{ }; impl LWECiphertext { - pub fn encrypt_sk( + pub fn encrypt_sk( &mut self, module: &Module, pt: &LWEPlaintext, @@ -25,7 +25,7 @@ impl LWECiphertext { DataPt: DataRef, DataSk: DataRef, Module: VecZnxFillUniform + VecZnxAddNormal + VecZnxNormalizeInplace, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { #[cfg(debug_assertions)] { diff --git a/core/src/encryption/lwe_ksk.rs b/core/src/encryption/lwe_ksk.rs index 5894347..9d50bc5 100644 --- a/core/src/encryption/lwe_ksk.rs +++ b/core/src/encryption/lwe_ksk.rs @@ -26,6 +26,7 @@ impl LWESwitchingKey> { } impl LWESwitchingKey { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/core/src/encryption/lwe_to_glwe_ksk.rs b/core/src/encryption/lwe_to_glwe_ksk.rs index 1fbb16e..5374a6b 100644 --- a/core/src/encryption/lwe_to_glwe_ksk.rs +++ b/core/src/encryption/lwe_to_glwe_ksk.rs @@ -24,6 +24,7 @@ impl LWEToGLWESwitchingKey> { } impl LWEToGLWESwitchingKey { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, @@ -69,7 +70,7 @@ impl LWEToGLWESwitchingKey { self.0.encrypt_sk( module, &sk_lwe_as_glwe, - &sk_glwe, + sk_glwe, source_xa, source_xe, sigma, diff --git a/core/src/external_product/gglwe_atk.rs b/core/src/external_product/gglwe_atk.rs index 7b5dd4b..3c2941c 100644 --- a/core/src/external_product/gglwe_atk.rs +++ b/core/src/external_product/gglwe_atk.rs @@ -9,6 +9,7 @@ use backend::hal::{ use crate::layouts::{GGLWEAutomorphismKey, GGLWESwitchingKey, prepared::GGSWCiphertextPrepared}; impl GGLWEAutomorphismKey> { + #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, n: usize, diff --git a/core/src/external_product/gglwe_ksk.rs b/core/src/external_product/gglwe_ksk.rs index c68a4c4..17e72af 100644 --- a/core/src/external_product/gglwe_ksk.rs +++ b/core/src/external_product/gglwe_ksk.rs @@ -9,6 +9,7 @@ use backend::hal::{ use crate::layouts::{GGLWESwitchingKey, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}; impl GGLWESwitchingKey> { + #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, n: usize, diff --git a/core/src/external_product/ggsw_ct.rs b/core/src/external_product/ggsw_ct.rs index bdc5845..3a93690 100644 --- a/core/src/external_product/ggsw_ct.rs +++ b/core/src/external_product/ggsw_ct.rs @@ -9,6 +9,7 @@ use backend::hal::{ use crate::layouts::{GGSWCiphertext, GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}; impl GGSWCiphertext> { + #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, n: usize, diff --git a/core/src/external_product/glwe_ct.rs b/core/src/external_product/glwe_ct.rs index 9ab438b..1837b00 100644 --- a/core/src/external_product/glwe_ct.rs +++ b/core/src/external_product/glwe_ct.rs @@ -9,6 +9,7 @@ use backend::hal::{ use crate::layouts::{GLWECiphertext, Infos, prepared::GGSWCiphertextPrepared}; impl GLWECiphertext> { + #[allow(clippy::too_many_arguments)] pub fn external_product_scratch_space( module: &Module, n: usize, @@ -160,7 +161,7 @@ impl GLWECiphertext { { unsafe { let self_ptr: *mut GLWECiphertext = self as *mut GLWECiphertext; - self.external_product(&module, &*self_ptr, rhs, scratch); + self.external_product(module, &*self_ptr, rhs, scratch); } } } diff --git a/core/src/glwe_packing.rs b/core/src/glwe_packing.rs index 020fb23..79e1f2b 100644 --- a/core/src/glwe_packing.rs +++ b/core/src/glwe_packing.rs @@ -55,14 +55,14 @@ impl Accumulator { impl GLWEPacker { /// Instantiates a new [GLWEPacker]. /// - /// #Arguments + /// # Arguments /// /// * `module`: static backend FFT tables. /// * `log_batch`: packs coefficients which are multiples of X^{N/2^log_batch}. - /// i.e. with `log_batch=0` only the constant coefficient is packed - /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients - /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts - /// can be packed. + /// i.e. with `log_batch=0` only the constant coefficient is packed + /// and N GLWE ciphertext can be packed. With `log_batch=2` all coefficients + /// which are multiples of X^{N/4} are packed. Meaning that N/4 ciphertexts + /// can be packed. /// * `basek`: base 2 logarithm of the GLWE ciphertext in memory digit representation. /// * `k`: base 2 precision of the GLWE ciphertext precision over the Torus. /// * `rank`: rank of the GLWE ciphertext. @@ -71,7 +71,7 @@ impl GLWEPacker { let log_n: usize = (usize::BITS - (n - 1).leading_zeros()) as _; (0..log_n - log_batch).for_each(|_| accumulators.push(Accumulator::alloc(n, basek, k, rank))); Self { - accumulators: accumulators, + accumulators, log_batch, counter: 0, } @@ -111,7 +111,7 @@ impl GLWEPacker { /// /// * `module`: static backend FFT tables. /// * `res`: space to append fully packed ciphertext. Only when the number - /// of packed ciphertexts reaches N/2^log_batch is a result written. + /// of packed ciphertexts reaches N/2^log_batch is a result written. /// * `a`: ciphertext to pack. Can optionally give None to pack a 0 ciphertext. /// * `auto_keys`: a [HashMap] containing the [AutomorphismKeyExec]s. /// * `scratch`: scratch space of size at least [Self::scratch_space]. @@ -329,13 +329,11 @@ fn combine( let k: usize = a.k(); let rank: usize = a.rank(); - let gal_el: i64; - - if i == 0 { - gal_el = -1; + let gal_el: i64 = if i == 0 { + -1 } else { - gal_el = module.galois_element(1 << (i - 1)) - } + module.galois_element(1 << (i - 1)) + }; let t: i64 = 1 << (log_n - i - 1); @@ -390,20 +388,18 @@ fn combine( panic!("auto_key[{}] not found", gal_el); } } - } else { - if let Some(b) = b { - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank); - tmp_b.rotate(module, 1 << (log_n - i - 1), b); - tmp_b.rsh(module, 1); + } else if let Some(b) = b { + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank); + tmp_b.rotate(module, 1 << (log_n - i - 1), b); + tmp_b.rsh(module, 1); - // a = (b* X^t - phi(b* X^t)) - if let Some(key) = auto_keys.get(&gal_el) { - a.automorphism_sub_ba(module, &tmp_b, key, scratch_1); - } else { - panic!("auto_key[{}] not found", gal_el); - } - - acc.value = true; + // a = (b* X^t - phi(b* X^t)) + if let Some(key) = auto_keys.get(&gal_el) { + a.automorphism_sub_ba(module, &tmp_b, key, scratch_1); + } else { + panic!("auto_key[{}] not found", gal_el); } + + acc.value = true; } } diff --git a/core/src/glwe_trace.rs b/core/src/glwe_trace.rs index 253f1e3..92b7e82 100644 --- a/core/src/glwe_trace.rs +++ b/core/src/glwe_trace.rs @@ -27,6 +27,7 @@ impl GLWECiphertext> { gal_els } + #[allow(clippy::too_many_arguments)] pub fn trace_scratch_space( module: &Module, n: usize, @@ -111,12 +112,11 @@ impl GLWECiphertext { (start..end).for_each(|i| { self.rsh(module, 1); - let p: i64; - if i == 0 { - p = -1; + let p: i64 = if i == 0 { + -1 } else { - p = module.galois_element(1 << (i - 1)); - } + module.galois_element(1 << (i - 1)) + }; if let Some(key) = auto_keys.get(&p) { self.automorphism_add_inplace(module, key, scratch); diff --git a/core/src/keyswitching/gglwe_ct.rs b/core/src/keyswitching/gglwe_ct.rs index 0a66bb6..ad00bd6 100644 --- a/core/src/keyswitching/gglwe_ct.rs +++ b/core/src/keyswitching/gglwe_ct.rs @@ -12,6 +12,7 @@ use crate::layouts::{ }; impl GGLWEAutomorphismKey> { + #[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space( module: &Module, n: usize, @@ -88,6 +89,7 @@ impl GGLWEAutomorphismKey { } impl GGLWESwitchingKey> { + #[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space( module: &Module, n: usize, diff --git a/core/src/keyswitching/ggsw_ct.rs b/core/src/keyswitching/ggsw_ct.rs index 4c78421..ea40fec 100644 --- a/core/src/keyswitching/ggsw_ct.rs +++ b/core/src/keyswitching/ggsw_ct.rs @@ -49,6 +49,7 @@ impl GGSWCiphertext> { tmp_dft_i + ((tmp_a + vmp) | (tmp_idft + norm)) } + #[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space( module: &Module, n: usize, @@ -74,6 +75,7 @@ impl GGSWCiphertext> { res_znx + ci_dft + (ks | expand_rows | res_dft) } + #[allow(clippy::too_many_arguments)] pub fn keyswitch_inplace_scratch_space( module: &Module, n: usize, diff --git a/core/src/keyswitching/glwe_ct.rs b/core/src/keyswitching/glwe_ct.rs index 58c4564..1830d4c 100644 --- a/core/src/keyswitching/glwe_ct.rs +++ b/core/src/keyswitching/glwe_ct.rs @@ -9,6 +9,7 @@ use backend::hal::{ use crate::layouts::{GLWECiphertext, Infos, prepared::GGLWESwitchingKeyPrepared}; impl GLWECiphertext> { + #[allow(clippy::too_many_arguments)] pub fn keyswitch_scratch_space( module: &Module, n: usize, @@ -38,7 +39,7 @@ impl GLWECiphertext> { ksk_size, ) + module.vec_znx_dft_alloc_bytes(n, rank_in, in_size); let normalize: usize = module.vec_znx_big_normalize_tmp_bytes(n); - return res_dft + ((ai_dft + vmp) | normalize); + res_dft + ((ai_dft + vmp) | normalize) } pub fn keyswitch_inplace_scratch_space( @@ -180,7 +181,7 @@ impl GLWECiphertext { { unsafe { let self_ptr: *mut GLWECiphertext = self as *mut GLWECiphertext; - self.keyswitch(&module, &*self_ptr, rhs, scratch); + self.keyswitch(module, &*self_ptr, rhs, scratch); } } } diff --git a/core/src/layouts/compressed/gglwe_atk.rs b/core/src/layouts/compressed/gglwe_atk.rs index 388da7e..8f788e6 100644 --- a/core/src/layouts/compressed/gglwe_atk.rs +++ b/core/src/layouts/compressed/gglwe_atk.rs @@ -61,7 +61,7 @@ impl Infos for GGLWEAutomorphismKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.key.inner() + self.key.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/compressed/gglwe_ct.rs b/core/src/layouts/compressed/gglwe_ct.rs index a617f27..4b232c8 100644 --- a/core/src/layouts/compressed/gglwe_ct.rs +++ b/core/src/layouts/compressed/gglwe_ct.rs @@ -76,7 +76,7 @@ impl GGLWECiphertextCompressed> { Self { data: MatZnx::alloc(n, rows, rank_in, 1, size), - basek: basek, + basek, k, rank_out, digits, diff --git a/core/src/layouts/compressed/gglwe_ksk.rs b/core/src/layouts/compressed/gglwe_ksk.rs index bc8607e..a439ddd 100644 --- a/core/src/layouts/compressed/gglwe_ksk.rs +++ b/core/src/layouts/compressed/gglwe_ksk.rs @@ -54,7 +54,7 @@ impl Infos for GGLWESwitchingKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.key.inner() + self.key.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/compressed/gglwe_tsk.rs b/core/src/layouts/compressed/gglwe_tsk.rs index 784a088..9e1d00f 100644 --- a/core/src/layouts/compressed/gglwe_tsk.rs +++ b/core/src/layouts/compressed/gglwe_tsk.rs @@ -59,7 +59,7 @@ impl GGLWETensorKeyCompressed> { n, basek, k, rows, digits, 1, rank, )); }); - Self { keys: keys } + Self { keys } } pub fn bytes_of(n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize { @@ -72,7 +72,7 @@ impl Infos for GGLWETensorKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.keys[0].inner() + self.keys[0].inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/compressed/ggsw_ct.rs b/core/src/layouts/compressed/ggsw_ct.rs index 8e25e58..07c9e15 100644 --- a/core/src/layouts/compressed/ggsw_ct.rs +++ b/core/src/layouts/compressed/ggsw_ct.rs @@ -76,7 +76,7 @@ impl GGSWCiphertextCompressed> { Self { data: MatZnx::alloc(n, rows, rank + 1, 1, k.div_ceil(basek)), basek, - k: k, + k, digits, rank, seed: Vec::new(), @@ -123,7 +123,7 @@ impl GGSWCiphertextCompressed { data: self.data.at_mut(row, col), basek: self.basek, k: self.k, - rank: rank, + rank, seed: self.seed[row * (rank + 1) + col], } } diff --git a/core/src/layouts/compressed/glwe_ct.rs b/core/src/layouts/compressed/glwe_ct.rs index 65a2057..9b5aede 100644 --- a/core/src/layouts/compressed/glwe_ct.rs +++ b/core/src/layouts/compressed/glwe_ct.rs @@ -96,7 +96,7 @@ impl ReaderFrom for GLWECiphertextCompressed { self.k = reader.read_u64::()? as usize; self.basek = reader.read_u64::()? as usize; self.rank = reader.read_u64::()? as usize; - reader.read(&mut self.seed)?; + reader.read_exact(&mut self.seed)?; self.data.read_from(reader) } } diff --git a/core/src/layouts/compressed/glwe_to_lwe_ksk.rs b/core/src/layouts/compressed/glwe_to_lwe_ksk.rs index 3d65027..8fc3f3a 100644 --- a/core/src/layouts/compressed/glwe_to_lwe_ksk.rs +++ b/core/src/layouts/compressed/glwe_to_lwe_ksk.rs @@ -43,7 +43,7 @@ impl Infos for GLWEToLWESwitchingKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/compressed/lwe_ct.rs b/core/src/layouts/compressed/lwe_ct.rs index 44b153a..ff08ca6 100644 --- a/core/src/layouts/compressed/lwe_ct.rs +++ b/core/src/layouts/compressed/lwe_ct.rs @@ -57,8 +57,8 @@ impl LWECiphertextCompressed> { pub fn alloc(basek: usize, k: usize) -> Self { Self { data: VecZnx::alloc(1, 1, k.div_ceil(basek)), - k: k, - basek: basek, + k, + basek, seed: [0u8; 32], } } @@ -103,7 +103,7 @@ impl ReaderFrom for LWECiphertextCompressed { fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { self.k = reader.read_u64::()? as usize; self.basek = reader.read_u64::()? as usize; - reader.read(&mut self.seed)?; + reader.read_exact(&mut self.seed)?; self.data.read_from(reader) } } diff --git a/core/src/layouts/compressed/lwe_ksk.rs b/core/src/layouts/compressed/lwe_ksk.rs index bbb304b..4a6c9fd 100644 --- a/core/src/layouts/compressed/lwe_ksk.rs +++ b/core/src/layouts/compressed/lwe_ksk.rs @@ -44,7 +44,7 @@ impl Infos for LWESwitchingKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/compressed/lwe_to_glwe_ksk.rs b/core/src/layouts/compressed/lwe_to_glwe_ksk.rs index ee3b8d0..7ede80a 100644 --- a/core/src/layouts/compressed/lwe_to_glwe_ksk.rs +++ b/core/src/layouts/compressed/lwe_to_glwe_ksk.rs @@ -45,7 +45,7 @@ impl Infos for LWEToGLWESwitchingKeyCompressed { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/gglwe_atk.rs b/core/src/layouts/gglwe_atk.rs index 3ab67a0..6440cbe 100644 --- a/core/src/layouts/gglwe_atk.rs +++ b/core/src/layouts/gglwe_atk.rs @@ -59,7 +59,7 @@ impl Infos for GGLWEAutomorphismKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.key.inner() + self.key.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/gglwe_ct.rs b/core/src/layouts/gglwe_ct.rs index 791e59b..73ec651 100644 --- a/core/src/layouts/gglwe_ct.rs +++ b/core/src/layouts/gglwe_ct.rs @@ -87,7 +87,7 @@ impl GGLWECiphertext> { Self { data: MatZnx::alloc(n, rows, rank_in, rank_out + 1, size), - basek: basek, + basek, k, digits, } diff --git a/core/src/layouts/gglwe_tsk.rs b/core/src/layouts/gglwe_tsk.rs index d092774..c47f88f 100644 --- a/core/src/layouts/gglwe_tsk.rs +++ b/core/src/layouts/gglwe_tsk.rs @@ -55,7 +55,7 @@ impl GGLWETensorKey> { (0..pairs).for_each(|_| { keys.push(GGLWESwitchingKey::alloc(n, basek, k, rows, digits, 1, rank)); }); - Self { keys: keys } + Self { keys } } pub fn bytes_of(n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize { @@ -68,7 +68,7 @@ impl Infos for GGLWETensorKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.keys[0].inner() + self.keys[0].inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/ggsw_ct.rs b/core/src/layouts/ggsw_ct.rs index 5927ec8..6bec38d 100644 --- a/core/src/layouts/ggsw_ct.rs +++ b/core/src/layouts/ggsw_ct.rs @@ -88,7 +88,7 @@ impl GGSWCiphertext> { Self { data: MatZnx::alloc(n, rows, rank + 1, rank + 1, k.div_ceil(basek)), basek, - k: k, + k, digits, } } diff --git a/core/src/layouts/glwe_ct.rs b/core/src/layouts/glwe_ct.rs index 3d5828f..9c2f594 100644 --- a/core/src/layouts/glwe_ct.rs +++ b/core/src/layouts/glwe_ct.rs @@ -1,6 +1,6 @@ use backend::hal::{ api::{FillUniform, Reset}, - layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo}, + layouts::{Data, DataMut, DataRef, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo}, }; use sampling::source::Source; @@ -15,6 +15,17 @@ pub struct GLWECiphertext { pub k: usize, } +impl ToOwnedDeep for GLWECiphertext { + type Owned = GLWECiphertext>; + fn to_owned_deep(&self) -> Self::Owned { + GLWECiphertext { + data: self.data.to_owned_deep(), + basek: self.basek, + k: self.k, + } + } +} + impl fmt::Debug for GLWECiphertext { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self) @@ -86,16 +97,6 @@ impl GLWECiphertext { } } -impl GLWECiphertext { - pub fn clone(&self) -> GLWECiphertext> { - GLWECiphertext { - data: self.data.clone(), - basek: self.basek(), - k: self.k(), - } - } -} - impl SetMetaData for GLWECiphertext { fn set_k(&mut self, k: usize) { self.k = k diff --git a/core/src/layouts/glwe_pk.rs b/core/src/layouts/glwe_pk.rs index 92c0175..5390b97 100644 --- a/core/src/layouts/glwe_pk.rs +++ b/core/src/layouts/glwe_pk.rs @@ -15,8 +15,8 @@ impl GLWEPublicKey> { pub fn alloc(n: usize, basek: usize, k: usize, rank: usize) -> Self { Self { data: VecZnx::alloc(n, rank + 1, k.div_ceil(basek)), - basek: basek, - k: k, + basek, + k, dist: Distribution::NONE, } } diff --git a/core/src/layouts/glwe_pt.rs b/core/src/layouts/glwe_pt.rs index abfd867..bf5ed9b 100644 --- a/core/src/layouts/glwe_pt.rs +++ b/core/src/layouts/glwe_pt.rs @@ -52,7 +52,7 @@ impl GLWEPlaintext> { pub fn alloc(n: usize, basek: usize, k: usize) -> Self { Self { data: VecZnx::alloc(n, 1, k.div_ceil(basek)), - basek: basek, + basek, k, } } diff --git a/core/src/layouts/glwe_to_lwe_ksk.rs b/core/src/layouts/glwe_to_lwe_ksk.rs index 7c22c1c..79920a0 100644 --- a/core/src/layouts/glwe_to_lwe_ksk.rs +++ b/core/src/layouts/glwe_to_lwe_ksk.rs @@ -39,7 +39,7 @@ impl Infos for GLWEToLWESwitchingKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/lwe_ct.rs b/core/src/layouts/lwe_ct.rs index 7c0a145..1f495e7 100644 --- a/core/src/layouts/lwe_ct.rs +++ b/core/src/layouts/lwe_ct.rs @@ -64,8 +64,8 @@ impl LWECiphertext> { pub fn alloc(n: usize, basek: usize, k: usize) -> Self { Self { data: VecZnx::alloc(n + 1, 1, k.div_ceil(basek)), - k: k, - basek: basek, + k, + basek, } } } diff --git a/core/src/layouts/lwe_ksk.rs b/core/src/layouts/lwe_ksk.rs index 5da7fb9..d18720b 100644 --- a/core/src/layouts/lwe_ksk.rs +++ b/core/src/layouts/lwe_ksk.rs @@ -44,7 +44,7 @@ impl Infos for LWESwitchingKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/lwe_pt.rs b/core/src/layouts/lwe_pt.rs index 45807b8..5fc397e 100644 --- a/core/src/layouts/lwe_pt.rs +++ b/core/src/layouts/lwe_pt.rs @@ -14,8 +14,8 @@ impl LWEPlaintext> { pub fn alloc(basek: usize, k: usize) -> Self { Self { data: VecZnx::alloc(1, 1, k.div_ceil(basek)), - k: k, - basek: basek, + k, + basek, } } } diff --git a/core/src/layouts/lwe_to_glwe_ksk.rs b/core/src/layouts/lwe_to_glwe_ksk.rs index 3e1fdbf..686081e 100644 --- a/core/src/layouts/lwe_to_glwe_ksk.rs +++ b/core/src/layouts/lwe_to_glwe_ksk.rs @@ -38,7 +38,7 @@ impl Infos for LWEToGLWESwitchingKey { type Inner = MatZnx; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/prepared/gglwe_atk.rs b/core/src/layouts/prepared/gglwe_atk.rs index e5ff632..ecc2554 100644 --- a/core/src/layouts/prepared/gglwe_atk.rs +++ b/core/src/layouts/prepared/gglwe_atk.rs @@ -37,7 +37,7 @@ impl Infos for GGLWEAutomorphismKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { - &self.key.inner() + self.key.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/prepared/gglwe_ct.rs b/core/src/layouts/prepared/gglwe_ct.rs index 4ae2613..3f7195b 100644 --- a/core/src/layouts/prepared/gglwe_ct.rs +++ b/core/src/layouts/prepared/gglwe_ct.rs @@ -17,6 +17,7 @@ pub struct GGLWECiphertextPrepared { } impl GGLWECiphertextPrepared, B> { + #[allow(clippy::too_many_arguments)] pub fn alloc( module: &Module, n: usize, @@ -48,12 +49,13 @@ impl GGLWECiphertextPrepared, B> { Self { data: module.vmp_pmat_alloc(n, rows, rank_in, rank_out + 1, size), - basek: basek, + basek, k, digits, } } + #[allow(clippy::too_many_arguments)] pub fn bytes_of( module: &Module, n: usize, diff --git a/core/src/layouts/prepared/gglwe_ksk.rs b/core/src/layouts/prepared/gglwe_ksk.rs index a269dbb..36f52e6 100644 --- a/core/src/layouts/prepared/gglwe_ksk.rs +++ b/core/src/layouts/prepared/gglwe_ksk.rs @@ -16,6 +16,7 @@ pub struct GGLWESwitchingKeyPrepared { } impl GGLWESwitchingKeyPrepared, B> { + #[allow(clippy::too_many_arguments)] pub fn alloc( module: &Module, n: usize, @@ -36,6 +37,7 @@ impl GGLWESwitchingKeyPrepared, B> { } } + #[allow(clippy::too_many_arguments)] pub fn bytes_of( module: &Module, n: usize, diff --git a/core/src/layouts/prepared/gglwe_tsk.rs b/core/src/layouts/prepared/gglwe_tsk.rs index 1b4cdd3..0ef0186 100644 --- a/core/src/layouts/prepared/gglwe_tsk.rs +++ b/core/src/layouts/prepared/gglwe_tsk.rs @@ -41,7 +41,7 @@ impl Infos for GGLWETensorKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { - &self.keys[0].inner() + self.keys[0].inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/prepared/ggsw_ct.rs b/core/src/layouts/prepared/ggsw_ct.rs index 3034d5e..a1fd05a 100644 --- a/core/src/layouts/prepared/ggsw_ct.rs +++ b/core/src/layouts/prepared/ggsw_ct.rs @@ -42,7 +42,7 @@ impl GGSWCiphertextPrepared, B> { Self { data: module.vmp_pmat_alloc(n, rows, rank + 1, rank + 1, k.div_ceil(basek)), basek, - k: k, + k, digits, } } diff --git a/core/src/layouts/prepared/glwe_ct.rs b/core/src/layouts/prepared/glwe_ct.rs new file mode 100644 index 0000000..9b5aede --- /dev/null +++ b/core/src/layouts/prepared/glwe_ct.rs @@ -0,0 +1,177 @@ +use backend::hal::{ + api::{FillUniform, Reset, VecZnxCopy, VecZnxFillUniform}, + layouts::{Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, WriterTo}, +}; +use sampling::source::Source; + +use crate::layouts::{GLWECiphertext, Infos, compressed::Decompress}; +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use std::fmt; + +#[derive(PartialEq, Eq, Clone)] +pub struct GLWECiphertextCompressed { + pub(crate) data: VecZnx, + pub(crate) basek: usize, + pub(crate) k: usize, + pub(crate) rank: usize, + pub(crate) seed: [u8; 32], +} + +impl fmt::Debug for GLWECiphertextCompressed { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self) + } +} + +impl fmt::Display for GLWECiphertextCompressed { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "GLWECiphertextCompressed: basek={} k={} rank={} seed={:?}: {}", + self.basek(), + self.k(), + self.rank, + self.seed, + self.data + ) + } +} + +impl Reset for GLWECiphertextCompressed { + fn reset(&mut self) { + self.data.reset(); + self.basek = 0; + self.k = 0; + self.rank = 0; + self.seed = [0u8; 32]; + } +} + +impl FillUniform for GLWECiphertextCompressed { + fn fill_uniform(&mut self, source: &mut Source) { + self.data.fill_uniform(source); + } +} + +impl Infos for GLWECiphertextCompressed { + type Inner = VecZnx; + + fn inner(&self) -> &Self::Inner { + &self.data + } + + fn basek(&self) -> usize { + self.basek + } + + fn k(&self) -> usize { + self.k + } +} + +impl GLWECiphertextCompressed { + pub fn rank(&self) -> usize { + self.rank + } +} + +impl GLWECiphertextCompressed> { + pub fn alloc(n: usize, basek: usize, k: usize, rank: usize) -> Self { + Self { + data: VecZnx::alloc(n, 1, k.div_ceil(basek)), + basek, + k, + rank, + seed: [0u8; 32], + } + } + + pub fn bytes_of(n: usize, basek: usize, k: usize) -> usize { + GLWECiphertext::bytes_of(n, basek, k, 1) + } +} + +impl ReaderFrom for GLWECiphertextCompressed { + fn read_from(&mut self, reader: &mut R) -> std::io::Result<()> { + self.k = reader.read_u64::()? as usize; + self.basek = reader.read_u64::()? as usize; + self.rank = reader.read_u64::()? as usize; + reader.read_exact(&mut self.seed)?; + self.data.read_from(reader) + } +} + +impl WriterTo for GLWECiphertextCompressed { + fn write_to(&self, writer: &mut W) -> std::io::Result<()> { + writer.write_u64::(self.k as u64)?; + writer.write_u64::(self.basek as u64)?; + writer.write_u64::(self.rank as u64)?; + writer.write_all(&self.seed)?; + self.data.write_to(writer) + } +} + +impl Decompress> for GLWECiphertext { + fn decompress(&mut self, module: &Module, other: &GLWECiphertextCompressed) + where + Module: VecZnxCopy + VecZnxFillUniform, + { + #[cfg(debug_assertions)] + { + use backend::hal::api::ZnxInfos; + + assert_eq!( + self.n(), + other.data.n(), + "invalid receiver: self.n()={} != other.n()={}", + self.n(), + other.data.n() + ); + assert_eq!( + self.size(), + other.size(), + "invalid receiver: self.size()={} != other.size()={}", + self.size(), + other.size() + ); + assert_eq!( + self.rank(), + other.rank(), + "invalid receiver: self.rank()={} != other.rank()={}", + self.rank(), + other.rank() + ); + } + + let mut source: Source = Source::new(other.seed); + self.decompress_internal(module, other, &mut source); + } +} + +impl GLWECiphertext { + pub(crate) fn decompress_internal( + &mut self, + module: &Module, + other: &GLWECiphertextCompressed, + source: &mut Source, + ) where + DataOther: DataRef, + Module: VecZnxCopy + VecZnxFillUniform, + { + #[cfg(debug_assertions)] + { + assert_eq!(self.rank(), other.rank()) + } + + let k: usize = other.k; + let basek: usize = other.basek; + let cols: usize = other.rank() + 1; + module.vec_znx_copy(&mut self.data, 0, &other.data, 0); + (1..cols).for_each(|i| { + module.vec_znx_fill_uniform(basek, &mut self.data, i, k, source); + }); + + self.basek = basek; + self.k = k; + } +} diff --git a/core/src/layouts/prepared/glwe_pk.rs b/core/src/layouts/prepared/glwe_pk.rs index 9885edb..defbe29 100644 --- a/core/src/layouts/prepared/glwe_pk.rs +++ b/core/src/layouts/prepared/glwe_pk.rs @@ -48,8 +48,8 @@ impl GLWEPublicKeyPrepared, B> { { Self { data: module.vec_znx_dft_alloc(n, rank + 1, k.div_ceil(basek)), - basek: basek, - k: k, + basek, + k, dist: Distribution::NONE, } } diff --git a/core/src/layouts/prepared/glwe_to_lwe_ksk.rs b/core/src/layouts/prepared/glwe_to_lwe_ksk.rs index 4befe50..0da24e8 100644 --- a/core/src/layouts/prepared/glwe_to_lwe_ksk.rs +++ b/core/src/layouts/prepared/glwe_to_lwe_ksk.rs @@ -15,7 +15,7 @@ impl Infos for GLWEToLWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/prepared/lwe_ksk.rs b/core/src/layouts/prepared/lwe_ksk.rs index 358498f..0fe12ab 100644 --- a/core/src/layouts/prepared/lwe_ksk.rs +++ b/core/src/layouts/prepared/lwe_ksk.rs @@ -15,7 +15,7 @@ impl Infos for LWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/layouts/prepared/lwe_to_glwe_ksk.rs b/core/src/layouts/prepared/lwe_to_glwe_ksk.rs index 806f534..eb9b45d 100644 --- a/core/src/layouts/prepared/lwe_to_glwe_ksk.rs +++ b/core/src/layouts/prepared/lwe_to_glwe_ksk.rs @@ -16,7 +16,7 @@ impl Infos for LWEToGLWESwitchingKeyPrepared { type Inner = VmpPMat; fn inner(&self) -> &Self::Inner { - &self.0.inner() + self.0.inner() } fn basek(&self) -> usize { diff --git a/core/src/noise/gglwe_ct.rs b/core/src/noise/gglwe_ct.rs index 78c82ca..d8f0768 100644 --- a/core/src/noise/gglwe_ct.rs +++ b/core/src/noise/gglwe_ct.rs @@ -11,7 +11,7 @@ use backend::hal::{ use crate::layouts::{GGLWECiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}; impl GGLWECiphertext { - pub fn assert_noise( + pub fn assert_noise( self, module: &Module, sk: &GLWESecretPrepared, @@ -30,7 +30,7 @@ impl GGLWECiphertext { + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + VecZnxSubScalarInplace, - B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let digits: usize = self.digits(); let basek: usize = self.basek(); @@ -47,7 +47,7 @@ impl GGLWECiphertext { (0..self.rank_in()).for_each(|col_i| { (0..self.rows()).for_each(|row_i| { self.at(row_i, col_i) - .decrypt(&module, &mut pt, &sk, scratch.borrow()); + .decrypt(module, &mut pt, sk, scratch.borrow()); module.vec_znx_sub_scalar_inplace( &mut pt.data, diff --git a/core/src/noise/ggsw_ct.rs b/core/src/noise/ggsw_ct.rs index 27cc6a2..a2895a1 100644 --- a/core/src/noise/ggsw_ct.rs +++ b/core/src/noise/ggsw_ct.rs @@ -12,7 +12,7 @@ use backend::hal::{ use crate::layouts::{GGSWCiphertext, GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared}; impl GGSWCiphertext { - pub fn assert_noise( + pub fn assert_noise( &self, module: &Module, sk_prepared: &GLWESecretPrepared, @@ -36,7 +36,7 @@ impl GGSWCiphertext { + VecZnxDftToVecZnxBigTmpA + VecZnxAddScalarInplace + VecZnxSubABInplace, - B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, F: Fn(usize) -> f64, { let basek: usize = self.basek(); @@ -65,7 +65,7 @@ impl GGSWCiphertext { } self.at(row_i, col_j) - .decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); + .decrypt(module, &mut pt_have, sk_prepared, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0); @@ -81,7 +81,7 @@ impl GGSWCiphertext { } impl GGSWCiphertext { - pub fn print_noise( + pub fn print_noise( &self, module: &Module, sk_prepared: &GLWESecretPrepared, @@ -104,7 +104,7 @@ impl GGSWCiphertext { + VecZnxDftToVecZnxBigTmpA + VecZnxAddScalarInplace + VecZnxSubABInplace, - B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let basek: usize = self.basek(); let k: usize = self.k(); @@ -132,7 +132,7 @@ impl GGSWCiphertext { } self.at(row_i, col_j) - .decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); + .decrypt(module, &mut pt_have, sk_prepared, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt.data, 0); diff --git a/core/src/noise/glwe_ct.rs b/core/src/noise/glwe_ct.rs index a4e7fb7..0ed72f4 100644 --- a/core/src/noise/glwe_ct.rs +++ b/core/src/noise/glwe_ct.rs @@ -15,7 +15,7 @@ use crate::{ }; impl GLWECiphertext { - pub fn assert_noise( + pub fn assert_noise( &self, module: &Module, sk_prepared: &GLWESecretPrepared, @@ -35,7 +35,7 @@ impl GLWECiphertext { + VecZnxNormalizeTmpBytes + VecZnxSubABInplace + VecZnxNormalizeInplace, - B: TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(self.n(), self.basek(), self.k()); @@ -46,7 +46,7 @@ impl GLWECiphertext { self.k(), )); - self.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); + self.decrypt(module, &mut pt_have, sk_prepared, scratch.borrow()); module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0); module.vec_znx_normalize_inplace(self.basek(), &mut pt_have.data, 0, scratch.borrow()); diff --git a/core/src/noise/mod.rs b/core/src/noise/mod.rs index 54ffab0..0592ab0 100644 --- a/core/src/noise/mod.rs +++ b/core/src/noise/mod.rs @@ -2,6 +2,7 @@ mod gglwe_ct; mod ggsw_ct; mod glwe_ct; +#[allow(clippy::too_many_arguments)] #[allow(dead_code)] pub(crate) fn var_noise_gglwe_product( n: f64, @@ -33,6 +34,7 @@ pub(crate) fn var_noise_gglwe_product( noise } +#[allow(clippy::too_many_arguments)] #[allow(dead_code)] pub(crate) fn log2_std_noise_gglwe_product( n: f64, @@ -62,6 +64,7 @@ pub(crate) fn log2_std_noise_gglwe_product( noise.log2().min(-1.0).max(-(a_logq as f64)) // max noise is [-2^{-1}, 2^{-1}] } +#[allow(clippy::too_many_arguments)] #[allow(dead_code)] pub(crate) fn noise_ggsw_product( n: f64, @@ -95,6 +98,7 @@ pub(crate) fn noise_ggsw_product( noise.log2().min(-1.0) // max noise is [-2^{-1}, 2^{-1}] } +#[allow(clippy::too_many_arguments)] #[allow(dead_code)] pub(crate) fn noise_ggsw_keyswitch( n: f64, diff --git a/core/src/scratch.rs b/core/src/scratch.rs index 39a364f..f6a3e3b 100644 --- a/core/src/scratch.rs +++ b/core/src/scratch.rs @@ -41,6 +41,7 @@ pub trait TakeGLWEPt { } pub trait TakeGGLWE { + #[allow(clippy::too_many_arguments)] fn take_gglwe( &mut self, n: usize, @@ -54,6 +55,7 @@ pub trait TakeGGLWE { } pub trait TakeGGLWEPrepared { + #[allow(clippy::too_many_arguments)] fn take_gglwe_prepared( &mut self, n: usize, @@ -113,6 +115,7 @@ pub trait TakeGLWEPkPrepared { } pub trait TakeGLWESwitchingKey { + #[allow(clippy::too_many_arguments)] fn take_glwe_switching_key( &mut self, n: usize, @@ -126,6 +129,7 @@ pub trait TakeGLWESwitchingKey { } pub trait TakeGLWESwitchingKeyPrepared { + #[allow(clippy::too_many_arguments)] fn take_glwe_switching_key_prepared( &mut self, n: usize, @@ -292,8 +296,8 @@ where ); ( GGLWECiphertext { - data: data, - basek: basek, + data, + basek, k, digits, }, @@ -353,8 +357,8 @@ where ); ( GGLWECiphertextPrepared { - data: data, - basek: basek, + data, + basek, k, digits, }, diff --git a/core/src/tests/generics/automorphism/gglwe_atk.rs b/core/src/tests/generics/automorphism/gglwe_atk.rs index ff72e0d..a2cc1bf 100644 --- a/core/src/tests/generics/automorphism/gglwe_atk.rs +++ b/core/src/tests/generics/automorphism/gglwe_atk.rs @@ -23,7 +23,8 @@ use crate::{ noise::log2_std_noise_gglwe_product, }; -pub fn test_gglwe_automorphism_key_automorphism( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_automorphism_key_automorphism( module: &Module, p0: i64, p1: i64, @@ -65,7 +66,8 @@ pub fn test_gglwe_automorphism_key_automorphism( + SvpPPolAlloc + VecZnxBigAddInplace + VecZnxSubScalarInplace, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchAvailableImpl + TakeScalarZnxImpl @@ -186,7 +188,8 @@ pub fn test_gglwe_automorphism_key_automorphism( }); } -pub fn test_gglwe_automorphism_key_automorphism_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_automorphism_key_automorphism_inplace( module: &Module, p0: i64, p1: i64, @@ -241,7 +244,8 @@ pub fn test_gglwe_automorphism_key_automorphism_inplace( + VecZnxCopy + VmpPMatAlloc + VmpPrepare, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchAvailableImpl + TakeScalarZnxImpl diff --git a/core/src/tests/generics/automorphism/ggsw_ct.rs b/core/src/tests/generics/automorphism/ggsw_ct.rs index dd56e2b..aa7b43f 100644 --- a/core/src/tests/generics/automorphism/ggsw_ct.rs +++ b/core/src/tests/generics/automorphism/ggsw_ct.rs @@ -24,7 +24,8 @@ use crate::{ noise::noise_ggsw_keyswitch, }; -pub fn test_ggsw_automorphism( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_automorphism( p: i64, module: &Module, basek: usize, @@ -72,7 +73,8 @@ pub fn test_ggsw_automorphism( + VecZnxSwithcDegree + VecZnxAutomorphismInplace + VecZnxAutomorphism, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -178,10 +180,11 @@ pub fn test_ggsw_automorphism( ) + 0.5 }; - ct_out.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); + ct_out.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); } -pub fn test_ggsw_automorphism_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_automorphism_inplace( p: i64, module: &Module, basek: usize, @@ -229,7 +232,8 @@ pub fn test_ggsw_automorphism_inplace( + VecZnxSwithcDegree + VecZnxAutomorphismInplace + VecZnxAutomorphism, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -325,5 +329,5 @@ pub fn test_ggsw_automorphism_inplace( ) + 0.5 }; - ct.assert_noise(module, &sk_prepared, &pt_scalar, &max_noise); + ct.assert_noise(module, &sk_prepared, &pt_scalar, max_noise); } diff --git a/core/src/tests/generics/automorphism/glwe_ct.rs b/core/src/tests/generics/automorphism/glwe_ct.rs index 1bd5821..39cfc75 100644 --- a/core/src/tests/generics/automorphism/glwe_ct.rs +++ b/core/src/tests/generics/automorphism/glwe_ct.rs @@ -23,7 +23,8 @@ use crate::{ noise::log2_std_noise_gglwe_product, }; -pub fn test_glwe_automorphism( +#[allow(clippy::too_many_arguments)] +pub fn test_glwe_automorphism( module: &Module, basek: usize, p: i64, @@ -63,7 +64,8 @@ pub fn test_glwe_automorphism( + VecZnxAutomorphismInplace + VmpPMatAlloc + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -150,7 +152,8 @@ pub fn test_glwe_automorphism( ct_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 1.0); } -pub fn test_glwe_automorphism_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_glwe_automorphism_inplace( module: &Module, basek: usize, p: i64, @@ -189,7 +192,8 @@ pub fn test_glwe_automorphism_inplace( + VecZnxAutomorphismInplace + VmpPMatAlloc + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/conversion.rs b/core/src/tests/generics/conversion.rs index e2c702c..b821579 100644 --- a/core/src/tests/generics/conversion.rs +++ b/core/src/tests/generics/conversion.rs @@ -21,7 +21,7 @@ use crate::layouts::{ prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc}, }; -pub fn test_lwe_to_glwe(module: &Module) +pub fn test_lwe_to_glwe(module: &Module) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -51,7 +51,8 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxAutomorphismInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -131,7 +132,7 @@ where assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]); } -pub fn test_glwe_to_lwe(module: &Module) +pub fn test_glwe_to_lwe(module: &Module) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -161,7 +162,8 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxAutomorphismInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/encryption/gglwe_atk.rs b/core/src/tests/generics/encryption/gglwe_atk.rs index ce58e8c..85bfebd 100644 --- a/core/src/tests/generics/encryption/gglwe_atk.rs +++ b/core/src/tests/generics/encryption/gglwe_atk.rs @@ -21,7 +21,7 @@ use crate::layouts::{ prepared::{GLWESecretPrepared, PrepareAlloc}, }; -pub fn test_gglwe_automorphisk_key_encrypt_sk( +pub fn test_gglwe_automorphisk_key_encrypt_sk( module: &Module, basek: usize, k_ksk: usize, @@ -60,7 +60,8 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk( + VecZnxCopy + VmpPMatAlloc + VmpPrepare, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchAvailableImpl + TakeScalarZnxImpl @@ -114,7 +115,7 @@ pub fn test_gglwe_automorphisk_key_encrypt_sk( .assert_noise(module, &sk_out_prepared, &sk.data, sigma); } -pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( +pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( module: &Module, basek: usize, k_ksk: usize, @@ -153,7 +154,8 @@ pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk( + VecZnxCopy + VmpPMatAlloc + VmpPrepare, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchAvailableImpl + TakeScalarZnxImpl diff --git a/core/src/tests/generics/encryption/gglwe_ct.rs b/core/src/tests/generics/encryption/gglwe_ct.rs index d51949d..1c3e7b1 100644 --- a/core/src/tests/generics/encryption/gglwe_ct.rs +++ b/core/src/tests/generics/encryption/gglwe_ct.rs @@ -20,7 +20,7 @@ use crate::layouts::{ prepared::{GLWESecretPrepared, PrepareAlloc}, }; -pub fn test_gglwe_switching_key_encrypt_sk( +pub fn test_gglwe_switching_key_encrypt_sk( module: &Module, basek: usize, k_ksk: usize, @@ -54,7 +54,8 @@ pub fn test_gglwe_switching_key_encrypt_sk( + VecZnxCopy + VmpPMatAlloc + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -99,7 +100,7 @@ pub fn test_gglwe_switching_key_encrypt_sk( .assert_noise(module, &sk_out_prepared, &sk_in.data, sigma); } -pub fn test_gglwe_switching_key_compressed_encrypt_sk( +pub fn test_gglwe_switching_key_compressed_encrypt_sk( module: &Module, basek: usize, k_ksk: usize, @@ -133,7 +134,8 @@ pub fn test_gglwe_switching_key_compressed_encrypt_sk( + VecZnxCopy + VmpPMatAlloc + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl diff --git a/core/src/tests/generics/encryption/ggsw_ct.rs b/core/src/tests/generics/encryption/ggsw_ct.rs index 2a1ec82..1c82198 100644 --- a/core/src/tests/generics/encryption/ggsw_ct.rs +++ b/core/src/tests/generics/encryption/ggsw_ct.rs @@ -20,7 +20,7 @@ use crate::layouts::{ prepared::{GLWESecretPrepared, PrepareAlloc}, }; -pub fn test_ggsw_encrypt_sk(module: &Module, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64) +pub fn test_ggsw_encrypt_sk(module: &Module, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -49,7 +49,8 @@ where + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes + VecZnxDftToVecZnxBigTmpA, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -94,17 +95,11 @@ where let noise_f = |_col_i: usize| -(k as f64) + sigma.log2() + 0.5; - ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f); + ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); } -pub fn test_ggsw_compressed_encrypt_sk( - module: &Module, - basek: usize, - k: usize, - digits: usize, - rank: usize, - sigma: f64, -) where +pub fn test_ggsw_compressed_encrypt_sk(module: &Module, basek: usize, k: usize, digits: usize, rank: usize, sigma: f64) +where Module: VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftFromVecZnx @@ -132,7 +127,8 @@ pub fn test_ggsw_compressed_encrypt_sk( + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes + VecZnxDftToVecZnxBigTmpA, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -180,5 +176,5 @@ pub fn test_ggsw_compressed_encrypt_sk( let mut ct: GGSWCiphertext> = GGSWCiphertext::alloc(n, basek, k, rows, digits, rank); ct.decompress(module, &ct_compressed); - ct.assert_noise(module, &sk_prepared, &pt_scalar, &noise_f); + ct.assert_noise(module, &sk_prepared, &pt_scalar, noise_f); } diff --git a/core/src/tests/generics/encryption/glwe_ct.rs b/core/src/tests/generics/encryption/glwe_ct.rs index 43bbe24..9293a6b 100644 --- a/core/src/tests/generics/encryption/glwe_ct.rs +++ b/core/src/tests/generics/encryption/glwe_ct.rs @@ -23,7 +23,7 @@ use crate::{ operations::GLWEOperations, }; -pub fn test_glwe_encrypt_sk(module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) +pub fn test_glwe_encrypt_sk(module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxBigAllocBytes @@ -60,7 +60,8 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -109,14 +110,8 @@ where assert!(noise_have <= noise_want + 0.2); } -pub fn test_glwe_compressed_encrypt_sk( - module: &Module, - basek: usize, - k_ct: usize, - k_pt: usize, - sigma: f64, - rank: usize, -) where +pub fn test_glwe_compressed_encrypt_sk(module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) +where Module: VecZnxDftAllocBytes + VecZnxBigAllocBytes + VecZnxDftFromVecZnx @@ -153,7 +148,8 @@ pub fn test_glwe_compressed_encrypt_sk( + VecZnxNormalize + VecZnxSub + VecZnxCopy, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -213,7 +209,7 @@ pub fn test_glwe_compressed_encrypt_sk( ); } -pub fn test_glwe_encrypt_zero_sk(module: &Module, basek: usize, k_ct: usize, sigma: f64, rank: usize) +pub fn test_glwe_encrypt_zero_sk(module: &Module, basek: usize, k_ct: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxBigAllocBytes @@ -250,7 +246,8 @@ where + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -290,7 +287,7 @@ where assert!((sigma - pt.data.std(basek, 0) * (k_ct as f64).exp2()) <= 0.2); } -pub fn test_glwe_encrypt_pk(module: &Module, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize) +pub fn test_glwe_encrypt_pk(module: &Module, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -315,7 +312,8 @@ where + VecZnxDftAlloc + SvpApply + VecZnxBigAddNormal, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/encryption/glwe_tsk.rs b/core/src/tests/generics/encryption/glwe_tsk.rs index d4504f4..c1890ef 100644 --- a/core/src/tests/generics/encryption/glwe_tsk.rs +++ b/core/src/tests/generics/encryption/glwe_tsk.rs @@ -20,7 +20,7 @@ use crate::layouts::{ prepared::{GLWESecretPrepared, PrepareAlloc}, }; -pub fn test_glwe_tensor_key_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) +pub fn test_glwe_tensor_key_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -49,7 +49,8 @@ where + VecZnxAddScalarInplace + VecZnxSwithcDegree + VecZnxSubScalarInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -130,7 +131,7 @@ where }) } -pub fn test_glwe_tensor_key_compressed_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) +pub fn test_glwe_tensor_key_compressed_encrypt_sk(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -159,7 +160,8 @@ where + VecZnxAddScalarInplace + VecZnxSwithcDegree + VecZnxSubScalarInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl diff --git a/core/src/tests/generics/external_product/gglwe_ksk.rs b/core/src/tests/generics/external_product/gglwe_ksk.rs index 36762dd..9847214 100644 --- a/core/src/tests/generics/external_product/gglwe_ksk.rs +++ b/core/src/tests/generics/external_product/gglwe_ksk.rs @@ -23,7 +23,8 @@ use crate::{ noise::noise_ggsw_product, }; -pub fn test_gglwe_switching_key_external_product( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_switching_key_external_product( module: &Module, basek: usize, k_out: usize, @@ -63,7 +64,8 @@ pub fn test_gglwe_switching_key_external_product( + VmpApply + VmpApplyAdd + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -165,7 +167,8 @@ pub fn test_gglwe_switching_key_external_product( .assert_noise(module, &sk_out_prepared, &sk_in.data, max_noise + 0.5); } -pub fn test_gglwe_switching_key_external_product_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_switching_key_external_product_inplace( module: &Module, basek: usize, k_ct: usize, @@ -204,7 +207,8 @@ pub fn test_gglwe_switching_key_external_product_inplace( + VmpApply + VmpApplyAdd + VmpPrepare, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl diff --git a/core/src/tests/generics/external_product/ggsw_ct.rs b/core/src/tests/generics/external_product/ggsw_ct.rs index a6d32e7..4811e73 100644 --- a/core/src/tests/generics/external_product/ggsw_ct.rs +++ b/core/src/tests/generics/external_product/ggsw_ct.rs @@ -23,7 +23,8 @@ use crate::{ noise::noise_ggsw_product, }; -pub fn test_ggsw_external_product( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_external_product( module: &Module, basek: usize, k_in: usize, @@ -64,7 +65,8 @@ pub fn test_ggsw_external_product( + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes + VecZnxDftToVecZnxBigTmpA, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -154,10 +156,11 @@ pub fn test_ggsw_external_product( ) + 0.5 }; - ct_ggsw_lhs_out.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise); + ct_ggsw_lhs_out.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, max_noise); } -pub fn test_ggsw_external_product_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_external_product_inplace( module: &Module, basek: usize, k_ct: usize, @@ -197,7 +200,8 @@ pub fn test_ggsw_external_product_inplace( + VecZnxDftAlloc + VecZnxBigNormalizeTmpBytes + VecZnxDftToVecZnxBigTmpA, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -287,5 +291,5 @@ pub fn test_ggsw_external_product_inplace( ) + 0.5 }; - ct_ggsw_lhs.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, &max_noise); + ct_ggsw_lhs.assert_noise(module, &sk_prepared, &pt_ggsw_lhs, max_noise); } diff --git a/core/src/tests/generics/external_product/glwe_ct.rs b/core/src/tests/generics/external_product/glwe_ct.rs index 69d0345..ca044c8 100644 --- a/core/src/tests/generics/external_product/glwe_ct.rs +++ b/core/src/tests/generics/external_product/glwe_ct.rs @@ -22,7 +22,8 @@ use crate::{ noise::noise_ggsw_product, }; -pub fn test_glwe_external_product( +#[allow(clippy::too_many_arguments)] +pub fn test_glwe_external_product( module: &Module, basek: usize, k_out: usize, @@ -58,7 +59,8 @@ pub fn test_glwe_external_product( + VmpApplyTmpBytes + VmpApply + VmpApplyAdd, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -158,7 +160,8 @@ pub fn test_glwe_external_product( ct_glwe_out.assert_noise(module, &sk_prepared, &pt_want, max_noise + 0.5); } -pub fn test_glwe_external_product_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_glwe_external_product_inplace( module: &Module, basek: usize, k_ct: usize, @@ -193,7 +196,8 @@ pub fn test_glwe_external_product_inplace( + VmpApplyTmpBytes + VmpApply + VmpApplyAdd, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/keyswitch/gglwe_ct.rs b/core/src/tests/generics/keyswitch/gglwe_ct.rs index c0aef14..6461283 100644 --- a/core/src/tests/generics/keyswitch/gglwe_ct.rs +++ b/core/src/tests/generics/keyswitch/gglwe_ct.rs @@ -22,7 +22,8 @@ use crate::{ noise::log2_std_noise_gglwe_product, }; -pub fn test_gglwe_switching_key_keyswitch( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_switching_key_keyswitch( module: &Module, basek: usize, k_out: usize, @@ -62,7 +63,8 @@ pub fn test_gglwe_switching_key_keyswitch( + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxSubScalarInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -174,7 +176,8 @@ pub fn test_gglwe_switching_key_keyswitch( .assert_noise(module, &sk2_prepared, &sk0.data, max_noise + 0.5); } -pub fn test_gglwe_switching_key_keyswitch_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_gglwe_switching_key_keyswitch_inplace( module: &Module, basek: usize, k_ct: usize, @@ -212,7 +215,8 @@ pub fn test_gglwe_switching_key_keyswitch_inplace( + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxSubScalarInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/keyswitch/ggsw_ct.rs b/core/src/tests/generics/keyswitch/ggsw_ct.rs index 8261463..43835fb 100644 --- a/core/src/tests/generics/keyswitch/ggsw_ct.rs +++ b/core/src/tests/generics/keyswitch/ggsw_ct.rs @@ -23,7 +23,8 @@ use crate::{ noise::noise_ggsw_keyswitch, }; -pub fn test_ggsw_keyswitch( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_keyswitch( module: &Module, basek: usize, k_out: usize, @@ -67,7 +68,8 @@ pub fn test_ggsw_keyswitch( + VecZnxDftAddInplace + VecZnxBigAlloc + VecZnxDftAlloc, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -167,10 +169,11 @@ pub fn test_ggsw_keyswitch( ) + 0.5 }; - ct_out.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise); + ct_out.assert_noise(module, &sk_out_prepared, &pt_scalar, max_noise); } -pub fn test_ggsw_keyswitch_inplace( +#[allow(clippy::too_many_arguments)] +pub fn test_ggsw_keyswitch_inplace( module: &Module, basek: usize, k_ct: usize, @@ -213,7 +216,8 @@ pub fn test_ggsw_keyswitch_inplace( + VecZnxDftAddInplace + VecZnxBigAlloc + VecZnxDftAlloc, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -304,5 +308,5 @@ pub fn test_ggsw_keyswitch_inplace( ) + 0.5 }; - ct.assert_noise(module, &sk_out_prepared, &pt_scalar, &max_noise); + ct.assert_noise(module, &sk_out_prepared, &pt_scalar, max_noise); } diff --git a/core/src/tests/generics/keyswitch/glwe_ct.rs b/core/src/tests/generics/keyswitch/glwe_ct.rs index f8ebe6f..edea772 100644 --- a/core/src/tests/generics/keyswitch/glwe_ct.rs +++ b/core/src/tests/generics/keyswitch/glwe_ct.rs @@ -22,7 +22,8 @@ use crate::{ noise::log2_std_noise_gglwe_product, }; -pub fn test_glwe_keyswitch( +#[allow(clippy::too_many_arguments)] +pub fn test_glwe_keyswitch( module: &Module, basek: usize, k_out: usize, @@ -60,7 +61,8 @@ pub fn test_glwe_keyswitch( + VmpApplyAdd + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -147,7 +149,7 @@ pub fn test_glwe_keyswitch( ct_out.assert_noise(module, &sk_out_prepared, &pt_want, max_noise + 0.5); } -pub fn test_glwe_keyswitch_inplace( +pub fn test_glwe_keyswitch_inplace( module: &Module, basek: usize, k_ct: usize, @@ -183,7 +185,8 @@ pub fn test_glwe_keyswitch_inplace( + VmpApplyAdd + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/keyswitch/lwe_ct.rs b/core/src/tests/generics/keyswitch/lwe_ct.rs index 416e7ca..9dcccb7 100644 --- a/core/src/tests/generics/keyswitch/lwe_ct.rs +++ b/core/src/tests/generics/keyswitch/lwe_ct.rs @@ -20,7 +20,7 @@ use crate::layouts::{ prepared::{LWESwitchingKeyPrepared, PrepareAlloc}, }; -pub fn test_lwe_keyswitch(module: &Module) +pub fn test_lwe_keyswitch(module: &Module) where Module: VecZnxDftAllocBytes + VecZnxBigNormalize @@ -50,7 +50,8 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxAutomorphismInplace, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/core/src/tests/generics/packing.rs b/core/src/tests/generics/packing.rs index db57cb9..e92687f 100644 --- a/core/src/tests/generics/packing.rs +++ b/core/src/tests/generics/packing.rs @@ -26,7 +26,7 @@ use crate::{ }, }; -pub fn test_glwe_packing(module: &Module) +pub fn test_glwe_packing(module: &Module) where Module: VecZnxDftAllocBytes + VecZnxAutomorphism @@ -64,7 +64,8 @@ where + VecZnxSwithcDegree + VecZnxAutomorphismInplace + VecZnxCopy, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl @@ -155,7 +156,7 @@ where pt.rotate_inplace(module, -(1 << log_batch)); // X^-batch * pt - if reverse_bits_msb(i, log_n as u32) % 5 == 0 { + if reverse_bits_msb(i, log_n as u32).is_multiple_of(5) { packer.add(module, Some(&ct), &auto_keys, scratch.borrow()); } else { packer.add( @@ -173,7 +174,7 @@ where let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_ct); let mut data: Vec = vec![0i64; n]; data.iter_mut().enumerate().for_each(|(i, x)| { - if i % 5 == 0 { + if i.is_multiple_of(5) { *x = reverse_bits_msb(i, log_n as u32) as i64; } }); diff --git a/core/src/tests/generics/trace.rs b/core/src/tests/generics/trace.rs index 30803e4..54af462 100644 --- a/core/src/tests/generics/trace.rs +++ b/core/src/tests/generics/trace.rs @@ -26,7 +26,7 @@ use crate::{ noise::var_noise_gglwe_product, }; -pub fn test_glwe_trace_inplace(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) +pub fn test_glwe_trace_inplace(module: &Module, basek: usize, k: usize, sigma: f64, rank: usize) where Module: VecZnxDftAllocBytes + VecZnxAutomorphism @@ -61,7 +61,8 @@ where + VecZnxBigNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, - B: TakeVecZnxDftImpl + B: Backend + + TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl diff --git a/poulpy/Cargo.toml b/poulpy/Cargo.toml index 8889a9a..20e445d 100644 --- a/poulpy/Cargo.toml +++ b/poulpy/Cargo.toml @@ -2,6 +2,12 @@ name = "poulpy" version = "0.1.0" edition = "2024" +description = "An efficient & modular FHE library using bivariate polynomial representation" +licence = "Apache 2.0" +readme = "README.md" +repository = "https://github.com/phantomzone-org/poulpy" +homepage = "https://github.com/phantomzone-org/poulpy" +documentation = "https://docs.rs/poulpy" [dependencies] backend = {path="../backend"} diff --git a/schemes/src/tfhe/blind_rotation/cggi_algo.rs b/schemes/src/tfhe/blind_rotation/cggi_algo.rs index 6ef302b..8d2c867 100644 --- a/schemes/src/tfhe/blind_rotation/cggi_algo.rs +++ b/schemes/src/tfhe/blind_rotation/cggi_algo.rs @@ -20,6 +20,7 @@ use crate::tfhe::blind_rotation::{ BlincRotationExecute, BlindRotationKeyPrepared, CGGI, LookUpTable, LookUpTableRotationDirection, }; +#[allow(clippy::too_many_arguments)] pub fn cggi_blind_rotate_scratch_space( module: &Module, n: usize, @@ -49,20 +50,17 @@ where let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(n, 1, brk_size); let acc_dft_add: usize = vmp_res; let vmp: usize = module.vmp_apply_tmp_bytes(n, brk_size, rows, rows, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2) - - let acc: usize; - if extension_factor > 1 { - acc = VecZnx::alloc_bytes(n, cols, k_res.div_ceil(basek)) * extension_factor; + let acc: usize = if extension_factor > 1 { + VecZnx::alloc_bytes(n, cols, k_res.div_ceil(basek)) * extension_factor } else { - acc = 0; - } + 0 + }; - return acc - + acc_dft + acc + acc_dft + acc_dft_add + vmp_res + vmp_xai - + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes(n) | module.vec_znx_dft_to_vec_znx_big_tmp_bytes(n)))); + + (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes(n) | module.vec_znx_dft_to_vec_znx_big_tmp_bytes(n)))) } else { GLWECiphertext::bytes_of(n, basek, k_res, rank) + GLWECiphertext::external_product_scratch_space(module, n, basek, k_res, k_res, k_brk, 1, rank) @@ -520,7 +518,7 @@ pub(crate) fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8] let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1; - res.copy_from_slice(&lwe.data().at(0, 0)); + res.copy_from_slice(lwe.data().at(0, 0)); match rot_dir { LookUpTableRotationDirection::Left => { diff --git a/schemes/src/tfhe/blind_rotation/cggi_key.rs b/schemes/src/tfhe/blind_rotation/cggi_key.rs index 77e8665..f2a170c 100644 --- a/schemes/src/tfhe/blind_rotation/cggi_key.rs +++ b/schemes/src/tfhe/blind_rotation/cggi_key.rs @@ -149,6 +149,7 @@ impl BlindRotationKeyCompressed, CGGI> { } impl BlindRotationKeyCompressed { + #[allow(clippy::too_many_arguments)] pub fn encrypt_sk( &mut self, module: &Module, diff --git a/schemes/src/tfhe/blind_rotation/key.rs b/schemes/src/tfhe/blind_rotation/key.rs index a788eb9..9382e4e 100644 --- a/schemes/src/tfhe/blind_rotation/key.rs +++ b/schemes/src/tfhe/blind_rotation/key.rs @@ -20,6 +20,7 @@ pub trait BlindRotationKeyAlloc { } pub trait BlindRotationKeyEncryptSk { + #[allow(clippy::too_many_arguments)] fn encrypt_sk( &mut self, module: &Module, diff --git a/schemes/src/tfhe/blind_rotation/key_prepared.rs b/schemes/src/tfhe/blind_rotation/key_prepared.rs index 0d72bab..17e3241 100644 --- a/schemes/src/tfhe/blind_rotation/key_prepared.rs +++ b/schemes/src/tfhe/blind_rotation/key_prepared.rs @@ -108,18 +108,15 @@ where self.dist = other.dist; - match other.dist { - Distribution::BinaryBlock(_) => { - let mut x_pow_a: Vec, B>> = Vec::with_capacity(n << 1); - let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); - (0..n << 1).for_each(|i| { - let mut res: SvpPPol, B> = module.svp_ppol_alloc(n, 1); - set_xai_plus_y(module, i, 0, &mut res, &mut buf); - x_pow_a.push(res); - }); - self.x_pow_a = Some(x_pow_a); - } - _ => {} + if let Distribution::BinaryBlock(_) = other.dist { + let mut x_pow_a: Vec, B>> = Vec::with_capacity(n << 1); + let mut buf: ScalarZnx> = ScalarZnx::alloc(n, 1); + (0..n << 1).for_each(|i| { + let mut res: SvpPPol, B> = module.svp_ppol_alloc(n, 1); + set_xai_plus_y(module, i, 0, &mut res, &mut buf); + x_pow_a.push(res); + }); + self.x_pow_a = Some(x_pow_a); } } } diff --git a/schemes/src/tfhe/blind_rotation/lut.rs b/schemes/src/tfhe/blind_rotation/lut.rs index 3e405a3..a2d89b1 100644 --- a/schemes/src/tfhe/blind_rotation/lut.rs +++ b/schemes/src/tfhe/blind_rotation/lut.rs @@ -69,10 +69,10 @@ impl LookUpTable { self.rot_dir = rot_dir } - pub fn set(&mut self, module: &Module, f: &Vec, k: usize) + pub fn set(&mut self, module: &Module, f: &[i64], k: usize) where Module: VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { assert!(f.len() <= module.n()); @@ -93,7 +93,7 @@ impl LookUpTable { // Scaling factor let mut scale = 1; - if k % basek != 0 { + if !k.is_multiple_of(basek) { scale <<= basek - (k % basek); } @@ -168,7 +168,7 @@ impl LookUpTable { module.vec_znx_rotate_inplace(k_hi as i64 + 1, &mut self.data[i], 0); }); - self.data.rotate_right(k_lo as usize); + self.data.rotate_right(k_lo); } } diff --git a/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs b/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs index 53d752e..0cb5121 100644 --- a/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs +++ b/schemes/src/tfhe/blind_rotation/tests/generic_blind_rotation.rs @@ -26,7 +26,7 @@ use core::layouts::{ prepared::{GLWESecretPrepared, PrepareAlloc}, }; -pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) +pub fn test_blind_rotation(module: &Module, n_lwe: usize, block_size: usize, extension_factor: usize) where Module: VecZnxBigAllocBytes + VecZnxDftAllocBytes @@ -66,7 +66,8 @@ where + VmpPrepare + VmpApply + VmpApplyAdd, - B: VecZnxDftAllocBytesImpl + B: Backend + + VecZnxDftAllocBytesImpl + VecZnxBigAllocBytesImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl @@ -82,7 +83,7 @@ where let k_lwe: usize = 24; let k_brk: usize = 3 * basek; let rows_brk: usize = 2; // Ensures first limb is noise-free. - let k_lut: usize = 1 * basek; + let k_lut: usize = basek; let k_res: usize = 2 * basek; let rank: usize = 1; diff --git a/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs b/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs index f8e46d3..01c108b 100644 --- a/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs +++ b/schemes/src/tfhe/blind_rotation/tests/generic_lut.rs @@ -8,10 +8,10 @@ use backend::hal::{ use crate::tfhe::blind_rotation::{DivRound, LookUpTable}; -pub fn test_lut_standard(module: &Module) +pub fn test_lut_standard(module: &Module) where Module: VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let n: usize = module.n(); let basek: usize = 20; @@ -44,10 +44,10 @@ where }); } -pub fn test_lut_extended(module: &Module) +pub fn test_lut_extended(module: &Module) where Module: VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, { let n: usize = module.n(); let basek: usize = 20; @@ -63,10 +63,10 @@ where .for_each(|(i, x)| *x = (i as i64) - 8); let mut lut: LookUpTable = LookUpTable::alloc(n, basek, k_lut, extension_factor); - lut.set(&module, &f, log_scale); + lut.set(module, &f, log_scale); let half_step: i64 = lut.domain_size().div_round(message_modulus << 1) as i64; - lut.rotate(&module, half_step); + lut.rotate(module, half_step); let step: usize = module.n().div_round(message_modulus); diff --git a/schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs b/schemes/src/tfhe/circuit_bootstrapping/circuit.rs similarity index 91% rename from schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs rename to schemes/src/tfhe/circuit_bootstrapping/circuit.rs index f0990df..f7b03ef 100644 --- a/schemes/src/tfhe/circuit_bootstrapping/circuit_bootstrapping.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/circuit.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, usize}; +use std::collections::HashMap; use backend::hal::{ api::{ @@ -9,7 +9,7 @@ use backend::hal::{ VecZnxNegateInplace, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, }, - layouts::{Backend, DataMut, DataRef, Module, Scratch}, + layouts::{Backend, DataMut, DataRef, Module, Scratch, ToOwnedDeep}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, }; @@ -24,7 +24,7 @@ use crate::tfhe::{ circuit_bootstrapping::{CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute}, }; -impl CirtuitBootstrappingExecute for CircuitBootstrappingKeyPrepared +impl CirtuitBootstrappingExecute for CircuitBootstrappingKeyPrepared where Module: VecZnxRotateInplace + VecZnxNormalizeInplace @@ -53,7 +53,7 @@ where + VecZnxBigAllocBytes + VecZnxDftAddInplace + VecZnxRotate, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, Scratch: TakeVecZnx + TakeVecZnxDftSlice + TakeVecZnxBig @@ -109,7 +109,8 @@ where } } -pub fn circuit_bootstrap_core( +#[allow(clippy::too_many_arguments)] +pub fn circuit_bootstrap_core( to_exponent: bool, module: &Module, log_gap_out: usize, @@ -150,7 +151,7 @@ pub fn circuit_bootstrap_core + VecZnxRotate, - B: ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, + B: Backend + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl, Scratch: TakeVecZnxDftSlice + TakeVecZnxBig + TakeVecZnxDft @@ -201,7 +202,7 @@ pub fn circuit_bootstrap_core( module: &Module, res: &mut GLWECiphertext, @@ -282,9 +284,9 @@ fn post_process( // [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0] res.trace( module, - module.log_n() - log_gap_in as usize + 1, + module.log_n() - log_gap_in + 1, log_n, - &a, + a, auto_keys, scratch, ); @@ -297,7 +299,7 @@ fn post_process( if i != 0 { res.rotate_inplace(module, -(1 << log_gap_in)); } - cts.insert(i as usize * (1 << log_gap_out), res.clone()); + cts.insert(i as usize * (1 << log_gap_out), res.to_owned_deep()); }); pack(module, &mut cts, log_gap_out, auto_keys, scratch); let packed: GLWECiphertext> = cts.remove(&0).unwrap(); @@ -353,14 +355,13 @@ pub fn pack( let rank: usize = cts.get(&0).unwrap().rank(); (0..log_n - log_gap_out).for_each(|i| { - let t = 16.min(1 << (log_n - 1 - i)); + let t: usize = 16.min(1 << (log_n - 1 - i)); - let auto_key: &GGLWEAutomorphismKeyPrepared, B>; - if i == 0 { - auto_key = auto_keys.get(&-1).unwrap() + let auto_key: &GGLWEAutomorphismKeyPrepared, B> = if i == 0 { + auto_keys.get(&-1).unwrap() } else { - auto_key = auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap(); - } + auto_keys.get(&module.galois_element(1 << (i - 1))).unwrap() + }; (0..t).for_each(|j| { let mut a: Option> = cts.remove(&j); @@ -387,6 +388,7 @@ pub fn pack( }); } +#[allow(clippy::too_many_arguments)] fn combine( module: &Module, basek: usize, @@ -472,18 +474,16 @@ fn combine( // a = a + phi(a) a.automorphism_add_inplace(module, auto_key, scratch); } - } else { - if let Some(b) = b { - let n: usize = b.n(); - let log_n: usize = (u64::BITS - (n - 1).leading_zeros()) as _; - let t: i64 = 1 << (log_n - i - 1); + } else if let Some(b) = b { + let n: usize = b.n(); + let log_n: usize = (u64::BITS - (n - 1).leading_zeros()) as _; + let t: i64 = 1 << (log_n - i - 1); - let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank); - tmp_b.rotate(module, t, b); - tmp_b.rsh(module, 1); + let (mut tmp_b, scratch_1) = scratch.take_glwe_ct(n, basek, k, rank); + tmp_b.rotate(module, t, b); + tmp_b.rsh(module, 1); - // a = (b* X^t - phi(b* X^t)) - b.automorphism_sub_ba(module, &tmp_b, auto_key, scratch_1); - } + // a = (b* X^t - phi(b* X^t)) + b.automorphism_sub_ba(module, &tmp_b, auto_key, scratch_1); } } diff --git a/schemes/src/tfhe/circuit_bootstrapping/key.rs b/schemes/src/tfhe/circuit_bootstrapping/key.rs index 4600aa3..619a496 100644 --- a/schemes/src/tfhe/circuit_bootstrapping/key.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/key.rs @@ -2,7 +2,7 @@ use core::layouts::{ GGLWEAutomorphismKey, GGLWETensorKey, GLWECiphertext, GLWESecret, LWESecret, prepared::{GGLWEAutomorphismKeyPrepared, GGLWETensorKeyPrepared, GLWESecretPrepared, PrepareAlloc}, }; -use std::{collections::HashMap, usize}; +use std::collections::HashMap; use backend::hal::{ api::{ @@ -21,6 +21,7 @@ use crate::tfhe::blind_rotation::{ }; pub trait CircuitBootstrappingKeyEncryptSk { + #[allow(clippy::too_many_arguments)] fn encrypt_sk( module: &Module, basek: usize, @@ -96,12 +97,12 @@ where Module:, { let mut auto_keys: HashMap>> = HashMap::new(); - let gal_els: Vec = GLWECiphertext::trace_galois_elements(&module); + let gal_els: Vec = GLWECiphertext::trace_galois_elements(module); gal_els.iter().for_each(|gal_el| { let mut key: GGLWEAutomorphismKey> = GGLWEAutomorphismKey::alloc(sk_glwe.n(), basek, k_trace, rows_trace, 1, sk_glwe.rank()); key.encrypt_sk( - &module, *gal_el, &sk_glwe, source_xa, source_xe, sigma, scratch, + module, *gal_el, sk_glwe, source_xa, source_xe, sigma, scratch, ); auto_keys.insert(*gal_el, key); }); @@ -128,7 +129,7 @@ where ); let mut tsk: GGLWETensorKey> = GGLWETensorKey::alloc(sk_glwe.n(), basek, k_tsk, rows_tsk, 1, sk_glwe.rank()); - tsk.encrypt_sk(module, &sk_glwe, source_xa, source_xe, sigma, scratch); + tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, sigma, scratch); Self { brk, diff --git a/schemes/src/tfhe/circuit_bootstrapping/mod.rs b/schemes/src/tfhe/circuit_bootstrapping/mod.rs index 937de24..827156c 100644 --- a/schemes/src/tfhe/circuit_bootstrapping/mod.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/mod.rs @@ -1,8 +1,8 @@ -mod circuit_bootstrapping; +mod circuit; mod key; pub mod tests; -pub use circuit_bootstrapping::*; +pub use circuit::*; pub use key::*; use core::layouts::{GGSWCiphertext, LWECiphertext}; @@ -20,6 +20,7 @@ pub trait CirtuitBootstrappingExecute { scratch: &mut Scratch, ); + #[allow(clippy::too_many_arguments)] fn execute_to_exponent( &self, module: &Module, diff --git a/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs b/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs index eb04845..c38533c 100644 --- a/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs +++ b/schemes/src/tfhe/circuit_bootstrapping/tests/circuit_bootstrapping.rs @@ -36,7 +36,7 @@ use core::layouts::{ prepared::{GGSWCiphertextPrepared, GLWESecretPrepared}, }; -pub fn test_circuit_bootstrapping_to_exponent(module: &Module) +pub fn test_circuit_bootstrapping_to_exponent(module: &Module) where Module: VecZnxFillUniform + VecZnxAddNormal @@ -81,7 +81,8 @@ where + VecZnxBigAllocBytes + VecZnxDftAddInplace + VecZnxRotate, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeVecZnxDftImpl + ScratchAvailableImpl @@ -223,7 +224,7 @@ where assert_eq!(pt_res.data.at(0, 0), pt_want); } -pub fn test_circuit_bootstrapping_to_constant(module: &Module) +pub fn test_circuit_bootstrapping_to_constant(module: &Module) where Module: VecZnxFillUniform + VecZnxAddNormal @@ -268,7 +269,8 @@ where + VecZnxBigAllocBytes + VecZnxDftAddInplace + VecZnxRotate, - B: ScratchOwnedAllocImpl + B: Backend + + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + TakeVecZnxDftImpl + ScratchAvailableImpl