Skip to content

Commit 11a9bf1

Browse files
committed
poly1305: add lint configs to Cargo.toml
Uses most of the standard lints we've applied elsewhere, with a couple of TODOs
1 parent 4d64c9a commit 11a9bf1

7 files changed

Lines changed: 78 additions & 35 deletions

File tree

poly1305/Cargo.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,48 @@ cpufeatures = "0.3"
2222
[dev-dependencies]
2323
hex-literal = "1"
2424

25+
[lints.rust]
26+
missing_copy_implementations = "warn"
27+
missing_debug_implementations = "warn"
28+
missing_docs = "warn"
29+
trivial_casts = "warn"
30+
trivial_numeric_casts = "warn"
31+
unused_lifetimes = "warn"
32+
unused_qualifications = "warn"
33+
unreachable_pub = "warn"
34+
2535
[lints.rust.unexpected_cfgs]
2636
level = "warn"
2737
check-cfg = [
2838
'cfg(fuzzing)',
2939
'cfg(poly1305_backend, values("soft"))'
3040
]
41+
42+
[lints.clippy]
43+
borrow_as_ptr = "warn"
44+
cast_lossless = "warn"
45+
cast_possible_truncation = "allow" # TODO(tarcieri): warn
46+
cast_possible_wrap = "warn"
47+
cast_precision_loss = "warn"
48+
cast_sign_loss = "warn"
49+
checked_conversions = "warn"
50+
from_iter_instead_of_collect = "warn"
51+
implicit_saturating_sub = "warn"
52+
manual_assert = "warn"
53+
map_unwrap_or = "warn"
54+
missing_errors_doc = "warn"
55+
missing_panics_doc = "warn"
56+
mod_module_files = "warn"
57+
must_use_candidate = "warn"
58+
needless_range_loop = "allow"
59+
ptr_as_ptr = "warn"
60+
redundant_closure_for_method_calls = "warn"
61+
ref_as_ptr = "warn"
62+
return_self_not_must_use = "warn"
63+
semicolon_if_nothing_returned = "warn"
64+
trivially_copy_pass_by_ref = "warn"
65+
std_instead_of_alloc = "warn"
66+
std_instead_of_core = "warn"
67+
undocumented_unsafe_blocks = "allow" # TODO(tarcieri): warn
68+
unnecessary_safety_comment = "warn"
69+
unwrap_used = "allow" # TODO(tarcieri): warn

poly1305/benches/poly1305.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Poly1305 benchmarks.
2+
13
#![feature(test)]
24

35
extern crate test;

poly1305/src/backend/autodetect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::mem::ManuallyDrop;
88

99
cpufeatures::new!(avx2_cpuid, "avx2");
1010

11-
pub struct State {
11+
pub(crate) struct State {
1212
inner: Inner,
1313
token: avx2_cpuid::InitToken,
1414
}
@@ -95,8 +95,8 @@ impl Clone for State {
9595
impl Drop for State {
9696
fn drop(&mut self) {
9797
use zeroize::Zeroize;
98-
const SIZE: usize = core::mem::size_of::<State>();
99-
let state = unsafe { &mut *(self as *mut State as *mut [u8; SIZE]) };
98+
const SIZE: usize = size_of::<State>();
99+
let state = unsafe { &mut *core::ptr::from_mut::<State>(self).cast::<[u8; SIZE]>() };
100100
state.zeroize();
101101
}
102102
}

poly1305/src/backend/avx2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl State {
113113
// finalization).
114114
let (m, r4) = SpacedMultiplier4x130::new(self.r1, self.r2);
115115

116-
self.initialized = Some(Initialized { p, m, r4 })
116+
self.initialized = Some(Initialized { p, m, r4 });
117117
}
118118
}
119119

poly1305/src/backend/avx2/helpers.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const fn set02(x3: u8, x2: u8, x1: u8, x0: u8) -> i32 {
1818

1919
/// Helper for Display impls of aligned values.
2020
fn write_130(f: &mut fmt::Formatter<'_>, limbs: [u32; 5]) -> fmt::Result {
21-
let r0 = limbs[0] as u128;
22-
let r1 = limbs[1] as u128;
23-
let r2 = limbs[2] as u128;
24-
let r3 = limbs[3] as u128;
25-
let r4 = limbs[4] as u128;
21+
let r0 = u128::from(limbs[0]);
22+
let r1 = u128::from(limbs[1]);
23+
let r2 = u128::from(limbs[2]);
24+
let r3 = u128::from(limbs[3]);
25+
let r4 = u128::from(limbs[4]);
2626

2727
// Reduce into two u128s
2828
let l0 = r0 + (r1 << 26) + (r2 << 52) + (r3 << 78);
@@ -34,11 +34,11 @@ fn write_130(f: &mut fmt::Formatter<'_>, limbs: [u32; 5]) -> fmt::Result {
3434

3535
/// Helper for Display impls of unreduced values.
3636
fn write_130_wide(f: &mut fmt::Formatter<'_>, limbs: [u64; 5]) -> fmt::Result {
37-
let r0 = limbs[0] as u128;
38-
let r1 = limbs[1] as u128;
39-
let r2 = limbs[2] as u128;
40-
let r3 = limbs[3] as u128;
41-
let r4 = limbs[4] as u128;
37+
let r0 = u128::from(limbs[0]);
38+
let r1 = u128::from(limbs[1]);
39+
let r2 = u128::from(limbs[2]);
40+
let r3 = u128::from(limbs[3]);
41+
let r4 = u128::from(limbs[4]);
4242

4343
// Reduce into two u128s
4444
let l0 = r0 + (r1 << 26) + (r2 << 52);
@@ -53,7 +53,7 @@ fn write_130_wide(f: &mut fmt::Formatter<'_>, limbs: [u64; 5]) -> fmt::Result {
5353
#[target_feature(enable = "avx2")]
5454
pub(super) unsafe fn prepare_keys(key: &Key) -> (AdditionKey, PrecomputedMultiplier) {
5555
// [k7, k6, k5, k4, k3, k2, k1, k0]
56-
let key = _mm256_loadu_si256(key.as_ptr() as *const _);
56+
let key = _mm256_loadu_si256(key.as_ptr().cast());
5757

5858
// Prepare addition key: [0, k7, 0, k6, 0, k5, 0, k4]
5959
let k = AdditionKey(_mm256_and_si256(
@@ -80,7 +80,7 @@ impl fmt::Display for Aligned130 {
8080
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8181
let mut v0 = [0u8; 32];
8282
unsafe {
83-
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.0);
83+
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.0);
8484
}
8585

8686
write!(f, "Aligned130(")?;
@@ -106,7 +106,7 @@ impl Aligned130 {
106106
Aligned130::new(_mm256_or_si256(
107107
_mm256_and_si256(
108108
// Load the 128-bit block into a 256-bit vector.
109-
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr() as *const _)),
109+
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr().cast())),
110110
// Mask off the upper 128 bits (undefined by _mm256_castsi128_si256).
111111
_mm256_set_epi64x(0, 0, -1, -1),
112112
),
@@ -122,7 +122,7 @@ impl Aligned130 {
122122
pub(super) unsafe fn from_partial_block(block: &Block) -> Self {
123123
Aligned130::new(_mm256_and_si256(
124124
// Load the 128-bit block into a 256-bit vector.
125-
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr() as *const _)),
125+
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr().cast())),
126126
// Mask off the upper 128 bits (undefined by _mm256_castsi128_si256).
127127
_mm256_set_epi64x(0, 0, -1, -1),
128128
))
@@ -419,8 +419,8 @@ impl fmt::Display for Unreduced130 {
419419
let mut v0 = [0u8; 32];
420420
let mut v1 = [0u8; 32];
421421
unsafe {
422-
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
423-
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
422+
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
423+
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
424424
}
425425

426426
write!(f, "Unreduced130(")?;
@@ -899,9 +899,9 @@ impl fmt::Display for Aligned4x130 {
899899
let mut v1 = [0u8; 32];
900900
let mut v2 = [0u8; 32];
901901
unsafe {
902-
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
903-
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
904-
_mm256_storeu_si256(v2.as_mut_ptr() as *mut _, self.v2);
902+
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
903+
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
904+
_mm256_storeu_si256(v2.as_mut_ptr().cast(), self.v2);
905905
}
906906

907907
writeln!(f, "Aligned4x130([")?;
@@ -967,8 +967,8 @@ impl Aligned4x130 {
967967
#[target_feature(enable = "avx2")]
968968
pub(super) unsafe fn from_blocks(src: &[Block; 4]) -> Self {
969969
let (lo, hi) = src.split_at(2);
970-
let blocks_23 = _mm256_loadu_si256(hi.as_ptr() as *const _);
971-
let blocks_01 = _mm256_loadu_si256(lo.as_ptr() as *const _);
970+
let blocks_23 = _mm256_loadu_si256(hi.as_ptr().cast());
971+
let blocks_01 = _mm256_loadu_si256(lo.as_ptr().cast());
972972

973973
Self::from_loaded_blocks(blocks_01, blocks_23)
974974
}
@@ -978,8 +978,8 @@ impl Aligned4x130 {
978978
#[target_feature(enable = "avx2")]
979979
pub(super) unsafe fn from_par_blocks(src: &ParBlocks) -> Self {
980980
let (lo, hi) = src.split_at(2);
981-
let blocks_23 = _mm256_loadu_si256(hi.as_ptr() as *const _);
982-
let blocks_01 = _mm256_loadu_si256(lo.as_ptr() as *const _);
981+
let blocks_23 = _mm256_loadu_si256(hi.as_ptr().cast());
982+
let blocks_01 = _mm256_loadu_si256(lo.as_ptr().cast());
983983

984984
Self::from_loaded_blocks(blocks_01, blocks_23)
985985
}
@@ -1598,11 +1598,11 @@ impl fmt::Display for Unreduced4x130 {
15981598
let mut v3 = [0u8; 32];
15991599
let mut v4 = [0u8; 32];
16001600
unsafe {
1601-
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
1602-
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
1603-
_mm256_storeu_si256(v2.as_mut_ptr() as *mut _, self.v2);
1604-
_mm256_storeu_si256(v3.as_mut_ptr() as *mut _, self.v3);
1605-
_mm256_storeu_si256(v4.as_mut_ptr() as *mut _, self.v4);
1601+
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
1602+
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
1603+
_mm256_storeu_si256(v2.as_mut_ptr().cast(), self.v2);
1604+
_mm256_storeu_si256(v3.as_mut_ptr().cast(), self.v3);
1605+
_mm256_storeu_si256(v4.as_mut_ptr().cast(), self.v4);
16061606
}
16071607

16081608
writeln!(f, "Unreduced4x130([")?;
@@ -1986,7 +1986,7 @@ impl From<AdditionKey> for IntegerTag {
19861986
impl IntegerTag {
19871987
pub(super) fn write(self, tag: &mut [u8]) {
19881988
unsafe {
1989-
_mm_storeu_si128(tag.as_mut_ptr() as *mut _, self.0);
1989+
_mm_storeu_si128(tag.as_mut_ptr().cast(), self.0);
19901990
}
19911991
}
19921992
}

poly1305/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
55
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
66
)]
7-
#![warn(missing_docs)]
87

98
pub use universal_hash;
109

@@ -95,6 +94,7 @@ impl Poly1305 {
9594
/// Compute unpadded Poly1305 for the given input data.
9695
///
9796
/// The main use case for this is XSalsa20Poly1305.
97+
#[must_use]
9898
pub fn compute_unpadded(mut self, data: &[u8]) -> Tag {
9999
let (blocks, remaining) = Block::slice_as_chunks(data);
100100

poly1305/tests/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
//! Poly1305 integration tests.
2+
3+
use core::iter::repeat_n;
14
use hex_literal::hex;
25
use poly1305::{
36
Block, KEY_SIZE, Poly1305,
47
universal_hash::{KeyInit, UniversalHash},
58
};
6-
use std::iter::repeat_n;
79

810
#[test]
911
fn test_nacl_vector() {

0 commit comments

Comments
 (0)