Skip to content

Commit ef6a700

Browse files
authored
ml-kem: add reason to all clippy div/rem allows (#231)
For allow instances of `allow(clippy::integer_division_remainder_used)` adds a `reason` field which is now one of "constant" (as in bound to an all-caps-named `const` value) or "tests".
1 parent 135d8a8 commit ef6a700

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

ml-kem/src/algebra.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ fn base_case_multiply(a0: Elem, a1: Elem, b0: Elem, b1: Elem, i: usize) -> (Elem
264264
///
265265
/// The values computed here match those provided in Appendix A of FIPS 203.
266266
/// `ZETA_POW_BITREV` corresponds to the first table, and `GAMMA` to the second table.
267-
#[allow(clippy::cast_possible_truncation)]
267+
#[allow(clippy::integer_division_remainder_used, reason = "constant")]
268268
const ZETA_POW_BITREV: [Elem; 128] = {
269269
const ZETA: u64 = 17;
270-
#[allow(clippy::integer_division_remainder_used)]
270+
271271
const fn bitrev7(x: usize) -> usize {
272272
((x >> 6) % 2)
273273
| (((x >> 5) % 2) << 1)
@@ -282,9 +282,9 @@ const ZETA_POW_BITREV: [Elem; 128] = {
282282
let mut pow = [Elem::new(0); 128];
283283
let mut i = 0;
284284
let mut curr = 1u64;
285-
#[allow(clippy::integer_division_remainder_used)]
285+
286286
while i < 128 {
287-
pow[i] = Elem::new(curr as u16);
287+
pow[i] = Elem::new((curr & 0xFFFF) as u16);
288288
i += 1;
289289
curr = (curr * ZETA) % BaseField::QLL;
290290
}
@@ -299,16 +299,15 @@ const ZETA_POW_BITREV: [Elem; 128] = {
299299
pow_bitrev
300300
};
301301

302-
#[allow(clippy::cast_possible_truncation)]
302+
#[allow(clippy::integer_division_remainder_used, reason = "constant")]
303303
const GAMMA: [Elem; 128] = {
304304
const ZETA: u64 = 17;
305305
let mut gamma = [Elem::new(0); 128];
306306
let mut i = 0;
307307
while i < 128 {
308308
let zpr = ZETA_POW_BITREV[i].0 as u64;
309-
#[allow(clippy::integer_division_remainder_used)]
310309
let g = (zpr * zpr * ZETA) % BaseField::QLL;
311-
gamma[i] = Elem::new(g as u16);
310+
gamma[i] = Elem::new((g & 0xFFFF) as u16);
312311
i += 1;
313312
}
314313
gamma

ml-kem/src/compress.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ where
1717
const POW2_HALF: u32 = 1 << (T::USIZE - 1);
1818
const MASK: Int = ((1 as Int) << T::USIZE) - 1;
1919
const DIV_SHIFT: usize = 34;
20-
#[allow(clippy::integer_division_remainder_used)]
20+
#[allow(clippy::integer_division_remainder_used, reason = "constant")]
2121
const DIV_MUL: u64 = (1 << T::DIV_SHIFT) / BaseField::QLL;
2222
}
2323

@@ -87,25 +87,24 @@ impl<K: ArraySize> Compress for Vector<K> {
8787
}
8888

8989
#[cfg(test)]
90-
pub(crate) mod test {
90+
#[allow(clippy::cast_possible_truncation, reason = "tests")]
91+
#[allow(clippy::integer_division_remainder_used, reason = "tests")]
92+
pub(crate) mod tests {
9193
use super::*;
9294
use array::typenum::{U1, U4, U5, U6, U10, U11, U12};
9395
use num_rational::Ratio;
9496

95-
#[allow(clippy::cast_possible_truncation)]
9697
fn rational_compress<D: CompressionFactor>(input: u16) -> u16 {
9798
let fraction = Ratio::new(u32::from(input) * (1 << D::USIZE), BaseField::QL);
9899
(fraction.round().to_integer() as u16) & D::MASK
99100
}
100101

101-
#[allow(clippy::cast_possible_truncation)]
102102
fn rational_decompress<D: CompressionFactor>(input: u16) -> u16 {
103103
let fraction = Ratio::new(u32::from(input) * BaseField::QL, 1 << D::USIZE);
104104
fraction.round().to_integer() as u16
105105
}
106106

107107
// Verify against inequality 4.7
108-
#[allow(clippy::integer_division_remainder_used)]
109108
fn compression_decompression_inequality<D: CompressionFactor>() {
110109
const QI32: i32 = BaseField::Q as i32;
111110
let error_threshold = i32::from(Ratio::new(BaseField::Q, 1 << D::USIZE).to_integer());

ml-kem/src/param.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ pub trait CbdSamplingSize: ArraySize {
4242
const ONES: Array<Elem, Self::OnesSize>;
4343
}
4444

45-
// To speed up CBD sampling, we pre-compute all the bit-manipulations:
46-
//
47-
// * Splitting a sampled integer into two parts
48-
// * Counting the ones in each part
49-
// * Taking the difference between the two counts mod q
50-
//
51-
// We have to allow the use of `as` here because we can't use our nice Truncate trait, because
52-
// const functions don't support traits.
53-
#[allow(clippy::cast_possible_truncation)]
45+
/// To speed up CBD sampling, we pre-compute all the bit-manipulations:
46+
///
47+
/// * Splitting a sampled integer into two parts
48+
/// * Counting the ones in each part
49+
/// * Taking the difference between the two counts mod q
50+
#[allow(clippy::integer_division_remainder_used, reason = "constant")]
5451
const fn ones_array<const B: usize, const N: usize, U>() -> Array<Elem, U>
5552
where
5653
U: ArraySize<ArrayType<Elem> = [Elem; N]>,
@@ -61,10 +58,9 @@ where
6158
let mut x = 0usize;
6259
while x < max {
6360
let mut y = 0usize;
64-
#[allow(clippy::integer_division_remainder_used)]
6561
while y < max {
66-
let x_ones = x.count_ones() as u16;
67-
let y_ones = y.count_ones() as u16;
62+
let x_ones = (x.count_ones() & 0xFFFF) as u16;
63+
let y_ones = (y.count_ones() & 0xFFFF) as u16;
6864
let i = x + (y << B);
6965
out[i] = Elem::new((x_ones + BaseField::Q - y_ones) % BaseField::Q);
7066

@@ -87,8 +83,9 @@ impl CbdSamplingSize for U3 {
8783
const ONES: Array<Elem, U64> = ones_array::<3, 64, U64>();
8884
}
8985

90-
/// A `ParameterSet` captures the parameters that describe a particular instance of ML-KEM. There
91-
/// are three variants, corresponding to three different security levels.
86+
/// A `ParameterSet` captures the parameters that describe a particular instance of ML-KEM.
87+
///
88+
/// There are three variants, corresponding to three different security levels.
9289
pub trait ParameterSet: Default + Clone + Debug + PartialEq {
9390
/// The dimensionality of vectors and arrays
9491
type K: ArraySize;

0 commit comments

Comments
 (0)