Clippy check & update CI (#73)

* updated CI workflow with clippy & fmt
This commit is contained in:
Jean-Philippe Bossuat
2025-08-17 13:02:47 +02:00
committed by GitHub
parent 3a828740cc
commit 0be569eca0
125 changed files with 1033 additions and 530 deletions

View File

@@ -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<B: Backend>(
module: &Module<B>,
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 => {

View File

@@ -149,6 +149,7 @@ impl BlindRotationKeyCompressed<Vec<u8>, CGGI> {
}
impl<D: DataMut> BlindRotationKeyCompressed<D, CGGI> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSkGLWE, DataSkLWE, B: Backend>(
&mut self,
module: &Module<B>,

View File

@@ -20,6 +20,7 @@ pub trait BlindRotationKeyAlloc {
}
pub trait BlindRotationKeyEncryptSk<B: Backend> {
#[allow(clippy::too_many_arguments)]
fn encrypt_sk<DataSkGLWE, DataSkLWE>(
&mut self,
module: &Module<B>,

View File

@@ -108,18 +108,15 @@ where
self.dist = other.dist;
match other.dist {
Distribution::BinaryBlock(_) => {
let mut x_pow_a: Vec<SvpPPol<Vec<u8>, B>> = Vec::with_capacity(n << 1);
let mut buf: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
(0..n << 1).for_each(|i| {
let mut res: SvpPPol<Vec<u8>, 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<SvpPPol<Vec<u8>, B>> = Vec::with_capacity(n << 1);
let mut buf: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
(0..n << 1).for_each(|i| {
let mut res: SvpPPol<Vec<u8>, 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);
}
}
}

View File

@@ -69,10 +69,10 @@ impl LookUpTable {
self.rot_dir = rot_dir
}
pub fn set<B: Backend>(&mut self, module: &Module<B>, f: &Vec<i64>, k: usize)
pub fn set<B>(&mut self, module: &Module<B>, f: &[i64], k: usize)
where
Module<B>: VecZnxRotateInplace + VecZnxNormalizeInplace<B> + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy,
B: ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
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);
}
}

View File

@@ -26,7 +26,7 @@ use core::layouts::{
prepared::{GLWESecretPrepared, PrepareAlloc},
};
pub fn test_blind_rotation<B: Backend>(module: &Module<B>, n_lwe: usize, block_size: usize, extension_factor: usize)
pub fn test_blind_rotation<B>(module: &Module<B>, n_lwe: usize, block_size: usize, extension_factor: usize)
where
Module<B>: VecZnxBigAllocBytes
+ VecZnxDftAllocBytes
@@ -66,7 +66,8 @@ where
+ VmpPrepare<B>
+ VmpApply<B>
+ VmpApplyAdd<B>,
B: VecZnxDftAllocBytesImpl<B>
B: Backend
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
@@ -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;

View File

@@ -8,10 +8,10 @@ use backend::hal::{
use crate::tfhe::blind_rotation::{DivRound, LookUpTable};
pub fn test_lut_standard<B: Backend>(module: &Module<B>)
pub fn test_lut_standard<B>(module: &Module<B>)
where
Module<B>: VecZnxRotateInplace + VecZnxNormalizeInplace<B> + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy,
B: ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
let n: usize = module.n();
let basek: usize = 20;
@@ -44,10 +44,10 @@ where
});
}
pub fn test_lut_extended<B: Backend>(module: &Module<B>)
pub fn test_lut_extended<B>(module: &Module<B>)
where
Module<B>: VecZnxRotateInplace + VecZnxNormalizeInplace<B> + VecZnxNormalizeTmpBytes + VecZnxSwithcDegree + VecZnxCopy,
B: ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
{
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);

View File

@@ -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<D: DataRef, BRA: BlindRotationAlgo, B: Backend> CirtuitBootstrappingExecute<B> for CircuitBootstrappingKeyPrepared<D, BRA, B>
impl<D: DataRef, BRA: BlindRotationAlgo, B> CirtuitBootstrappingExecute<B> for CircuitBootstrappingKeyPrepared<D, BRA, B>
where
Module<B>: VecZnxRotateInplace
+ VecZnxNormalizeInplace<B>
@@ -53,7 +53,7 @@ where
+ VecZnxBigAllocBytes
+ VecZnxDftAddInplace<B>
+ VecZnxRotate,
B: ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
Scratch<B>: TakeVecZnx
+ TakeVecZnxDftSlice<B>
+ TakeVecZnxBig<B>
@@ -109,7 +109,8 @@ where
}
}
pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B: Backend>(
#[allow(clippy::too_many_arguments)]
pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
to_exponent: bool,
module: &Module<B>,
log_gap_out: usize,
@@ -150,7 +151,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B: Backe
+ VecZnxBigAllocBytes
+ VecZnxDftAddInplace<B>
+ VecZnxRotate,
B: ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
B: Backend + ScratchOwnedAllocImpl<B> + ScratchOwnedBorrowImpl<B>,
Scratch<B>: TakeVecZnxDftSlice<B>
+ TakeVecZnxBig<B>
+ TakeVecZnxDft<B>
@@ -201,7 +202,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B: Backe
let (mut res_glwe, scratch1) = scratch.take_glwe_ct(n, basek, k, rank);
let (mut tmp_gglwe, scratch2) = scratch1.take_gglwe(n, basek, k, rows, 1, rank.max(1), rank);
key.brk.execute(module, &mut res_glwe, &lwe, &lut, scratch2);
key.brk.execute(module, &mut res_glwe, lwe, &lut, scratch2);
let gap: usize = 2 * lut.drift / lut.extension_factor();
@@ -235,6 +236,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B: Backe
res.from_gglwe(module, &tmp_gglwe, &key.tsk, scratch2);
}
#[allow(clippy::too_many_arguments)]
fn post_process<DataRes, DataA, B: Backend>(
module: &Module<B>,
res: &mut GLWECiphertext<DataRes>,
@@ -282,9 +284,9 @@ fn post_process<DataRes, DataA, B: Backend>(
// [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<DataRes, DataA, B: Backend>(
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<Vec<u8>> = cts.remove(&0).unwrap();
@@ -353,14 +355,13 @@ pub fn pack<D: DataMut, B: Backend>(
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<Vec<u8>, B>;
if i == 0 {
auto_key = auto_keys.get(&-1).unwrap()
let auto_key: &GGLWEAutomorphismKeyPrepared<Vec<u8>, 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<GLWECiphertext<D>> = cts.remove(&j);
@@ -387,6 +388,7 @@ pub fn pack<D: DataMut, B: Backend>(
});
}
#[allow(clippy::too_many_arguments)]
fn combine<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>(
module: &Module<B>,
basek: usize,
@@ -472,18 +474,16 @@ fn combine<A: DataMut, D: DataMut, DataAK: DataRef, B: Backend>(
// 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);
}
}

View File

@@ -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<B: Backend> {
#[allow(clippy::too_many_arguments)]
fn encrypt_sk<DLwe, DGlwe>(
module: &Module<B>,
basek: usize,
@@ -96,12 +97,12 @@ where
Module<B>:,
{
let mut auto_keys: HashMap<i64, GGLWEAutomorphismKey<Vec<u8>>> = HashMap::new();
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(&module);
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
gal_els.iter().for_each(|gal_el| {
let mut key: GGLWEAutomorphismKey<Vec<u8>> =
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<Vec<u8>> = 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,

View File

@@ -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<B: Backend> {
scratch: &mut Scratch<B>,
);
#[allow(clippy::too_many_arguments)]
fn execute_to_exponent<DM: DataMut, DR: DataRef>(
&self,
module: &Module<B>,

View File

@@ -36,7 +36,7 @@ use core::layouts::{
prepared::{GGSWCiphertextPrepared, GLWESecretPrepared},
};
pub fn test_circuit_bootstrapping_to_exponent<B: Backend, BRA: BlindRotationAlgo>(module: &Module<B>)
pub fn test_circuit_bootstrapping_to_exponent<B, BRA: BlindRotationAlgo>(module: &Module<B>)
where
Module<B>: VecZnxFillUniform
+ VecZnxAddNormal
@@ -81,7 +81,8 @@ where
+ VecZnxBigAllocBytes
+ VecZnxDftAddInplace<B>
+ VecZnxRotate,
B: ScratchOwnedAllocImpl<B>
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B>
@@ -223,7 +224,7 @@ where
assert_eq!(pt_res.data.at(0, 0), pt_want);
}
pub fn test_circuit_bootstrapping_to_constant<B: Backend, BRA: BlindRotationAlgo>(module: &Module<B>)
pub fn test_circuit_bootstrapping_to_constant<B, BRA: BlindRotationAlgo>(module: &Module<B>)
where
Module<B>: VecZnxFillUniform
+ VecZnxAddNormal
@@ -268,7 +269,8 @@ where
+ VecZnxBigAllocBytes
+ VecZnxDftAddInplace<B>
+ VecZnxRotate,
B: ScratchOwnedAllocImpl<B>
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ TakeVecZnxDftImpl<B>
+ ScratchAvailableImpl<B>