Skip to content

Commit b4cc70c

Browse files
committed
ml-kem: move lint config to workspace Cargo.toml
This lets us share a lint configuration among all the crates in the workspace
1 parent ef6a700 commit b4cc70c

File tree

12 files changed

+78
-18
lines changed

12 files changed

+78
-18
lines changed

.clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allow-unwrap-in-consts = true
2+
allow-unwrap-in-tests = true

.github/workflows/workspace.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
timeout-minutes: 45
2727
steps:
2828
- uses: actions/checkout@v6
29-
- uses: dtolnay/rust-toolchain@1.85
29+
- uses: dtolnay/rust-toolchain@1.93
3030
with:
3131
components: clippy
3232
- run: cargo clippy --all-features --all-targets -- -D warnings

Cargo.toml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,49 @@ members = [
88
"x-wing"
99
]
1010

11+
[workspace.lints.clippy]
12+
borrow_as_ptr = "warn"
13+
cast_lossless = "warn"
14+
cast_possible_truncation = "warn"
15+
cast_possible_wrap = "warn"
16+
cast_precision_loss = "warn"
17+
cast_sign_loss = "warn"
18+
checked_conversions = "warn"
19+
doc_markdown = "warn"
20+
from_iter_instead_of_collect = "warn"
21+
implicit_saturating_sub = "warn"
22+
integer_division_remainder_used = "warn"
23+
manual_assert = "warn"
24+
map_unwrap_or = "warn"
25+
missing_errors_doc = "warn"
26+
missing_panics_doc = "warn"
27+
mod_module_files = "warn"
28+
must_use_candidate = "warn"
29+
needless_range_loop = "allow"
30+
panic_in_result_fn = "warn"
31+
ptr_as_ptr = "warn"
32+
redundant_closure_for_method_calls = "warn"
33+
ref_as_ptr = "warn"
34+
return_self_not_must_use = "warn"
35+
semicolon_if_nothing_returned = "warn"
36+
trivially_copy_pass_by_ref = "warn"
37+
std_instead_of_alloc = "warn"
38+
std_instead_of_core = "warn"
39+
undocumented_unsafe_blocks = "warn"
40+
unnecessary_safety_comment = "warn"
41+
unwrap_in_result = "warn"
42+
unwrap_used = "warn"
43+
44+
[workspace.lints.rust]
45+
missing_copy_implementations = "warn"
46+
missing_debug_implementations = "warn"
47+
missing_docs = "warn"
48+
trivial_casts = "warn"
49+
trivial_numeric_casts = "warn"
50+
unsafe_code = "deny"
51+
unused_lifetimes = "warn"
52+
unused_qualifications = "warn"
53+
1154
[profile.bench]
1255
debug = true
1356

ml-kem/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ serde_json = "1.0.125"
5050
name = "mlkem"
5151
harness = false
5252

53+
[lints]
54+
workspace = true
55+
5356
[package.metadata.docs.rs]
5457
all-features = true

ml-kem/benches/mlkem.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! ML-KEM benchmarks.
2+
3+
#![allow(missing_docs, clippy::unwrap_used)]
4+
15
use ::kem::{Decapsulate, Encapsulate, Kem, KeyExport, KeyInit};
26
use core::hint::black_box;
37
use criterion::{Criterion, criterion_group, criterion_main};
@@ -14,7 +18,7 @@ fn criterion_benchmark(c: &mut Criterion) {
1418
let (dk, ek) = MlKem768::generate_keypair_from_rng(&mut rng);
1519
let _dk_bytes = black_box(dk.to_seed().unwrap());
1620
let _ek_bytes = black_box(ek.to_bytes());
17-
})
21+
});
1822
});
1923

2024
let (dk, ek) = MlKem768::generate_keypair_from_rng(&mut rng);
@@ -24,7 +28,7 @@ fn criterion_benchmark(c: &mut Criterion) {
2428

2529
// Encapsulation
2630
c.bench_function("encapsulate", |b| {
27-
b.iter(|| ek.encapsulate_with_rng(&mut rng))
31+
b.iter(|| ek.encapsulate_with_rng(&mut rng));
2832
});
2933
let (ct, _sk) = ek.encapsulate_with_rng(&mut rng);
3034

@@ -34,7 +38,7 @@ fn criterion_benchmark(c: &mut Criterion) {
3438
c.bench_function("decapsulate", |b| {
3539
b.iter(|| {
3640
dk.decapsulate(&ct);
37-
})
41+
});
3842
});
3943

4044
// Round trip
@@ -43,7 +47,7 @@ fn criterion_benchmark(c: &mut Criterion) {
4347
let (dk, ek) = MlKem768::generate_keypair_from_rng(&mut rng);
4448
let (ct, _sk) = ek.encapsulate_with_rng(&mut rng);
4549
dk.decapsulate(&ct);
46-
})
50+
});
4751
});
4852
}
4953

ml-kem/src/algebra.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn sample_ntt(B: &mut impl XofReader) -> NttPolynomial {
7474
self.start = end;
7575

7676
let d1 = Int::from(b[0]) + ((Int::from(b[1]) & 0xf) << 8);
77-
let d2 = (Int::from(b[1]) >> 4) + ((Int::from(b[2]) as Int) << 4);
77+
let d2 = (Int::from(b[1]) >> 4) + (Int::from(b[2]) << 4);
7878

7979
if d1 < BaseField::Q {
8080
if d2 < BaseField::Q {
@@ -315,7 +315,10 @@ const GAMMA: [Elem; 128] = {
315315

316316
#[cfg(test)]
317317
mod test {
318-
use super::*;
318+
use super::{
319+
Array, ArraySize, B32, BaseField, Elem, Field, Int, Ntt, NttInverse, NttMatrix,
320+
NttPolynomial, NttVector, PRF, Polynomial, U256, XOF,
321+
};
319322
use array::typenum::{U2, U3, U8};
320323
use module_lattice::utils::Flatten;
321324

@@ -519,14 +522,14 @@ mod test {
519522
let rho = B32::default();
520523
let sample: Array<Array<Elem, U256>, U8> = Array::from_fn(|i| {
521524
let mut xof = XOF(&rho, 0, i as u8);
522-
sample_ntt(&mut xof).into()
525+
super::sample_ntt(&mut xof).into()
523526
});
524527

525528
test_sample(&sample.flatten(), &UNIFORM);
526529
}
527530

528531
#[test]
529-
fn sample_cbd() {
532+
fn sample_poly_cbd() {
530533
// Eta = 2
531534
let sigma = B32::default();
532535
let prf_output = PRF::<U2>(&sigma, 0);

ml-kem/src/compress.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ where
1515
T: EncodingSize,
1616
{
1717
const POW2_HALF: u32 = 1 << (T::USIZE - 1);
18-
const MASK: Int = ((1 as Int) << T::USIZE) - 1;
18+
const MASK: Int = (1 << T::USIZE) - 1;
1919
const DIV_SHIFT: usize = 34;
2020
#[allow(clippy::integer_division_remainder_used, reason = "constant")]
2121
const DIV_MUL: u64 = (1 << T::DIV_SHIFT) / BaseField::QLL;

ml-kem/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
)]
88
#![allow(non_snake_case)] // Allow notation matching the spec
99
#![allow(clippy::clone_on_copy)] // Be explicit about moving data
10-
#![deny(missing_docs)] // Require all public interfaces to be documented
11-
#![warn(clippy::pedantic)] // Be pedantic by default
12-
#![warn(clippy::integer_division_remainder_used)] // Be judicious about using `/` and `%`
1310

1411
//! # Usage
1512
//!

ml-kem/src/pkcs8.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
}
9494

9595
#[cfg(feature = "alloc")]
96-
impl<P> pkcs8::EncodePublicKey for EncapsulationKey<P>
96+
impl<P> EncodePublicKey for EncapsulationKey<P>
9797
where
9898
P: KemParams + AssociatedAlgorithmIdentifier<Params = AnyRef<'static>>,
9999
{
@@ -152,7 +152,7 @@ where
152152
}
153153

154154
#[cfg(feature = "alloc")]
155-
impl<P> pkcs8::EncodePrivateKey for DecapsulationKey<P>
155+
impl<P> EncodePrivateKey for DecapsulationKey<P>
156156
where
157157
P: KemParams + AssociatedAlgorithmIdentifier<Params = AnyRef<'static>>,
158158
{

ml-kem/tests/encap-decap.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use ml_kem::*;
1+
//! Encapsulation/decapsulation tests, including ones against the NIST ACVP test vectors.
2+
3+
#![allow(clippy::unwrap_used)]
24

35
use ::kem::Decapsulate;
46
use array::{Array, ArrayN};
7+
use ml_kem::*;
58
use std::{fs::read_to_string, path::PathBuf};
69

7-
// A helper trait for deterministic encapsulation tests
10+
/// A helper trait for deterministic encapsulation tests
811
pub trait EncapsulateDeterministic {
9-
// Returns (ciphertext, shared_secret)
12+
/// Returns `(ciphertext, shared_secret)`.
1013
fn encapsulate_deterministic(&self, m: &ArrayN<u8, 32>) -> (Vec<u8>, Vec<u8>);
1114
}
1215

0 commit comments

Comments
 (0)