Skip to content

Commit 1045d03

Browse files
authored
chacha20: minor improvements
- lint `cfg`s - add assertions - avoid needless conversions
1 parent e4afea5 commit 1045d03

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

chacha20/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ xchacha = ["cipher"]
4747
[package.metadata.docs.rs]
4848
all-features = true
4949
rustdoc-args = ["--cfg", "docsrs"]
50+
51+
[lints.rust.unexpected_cfgs]
52+
level = "warn"
53+
check-cfg = [
54+
'cfg(chacha20_force_soft)',
55+
'cfg(chacha20_force_sse2)',
56+
'cfg(chacha20_force_avx2)',
57+
]
58+
59+
[lints.clippy]
60+
needless_range_loop = "allow"

chacha20/src/backends/sse2.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ where
116116
#[cfg(feature = "rng")]
117117
impl<R: Rounds> Backend<R> {
118118
#[inline(always)]
119-
fn gen_ks_blocks(&mut self, block: &mut [u32]) {
119+
fn gen_ks_blocks(&mut self, block: &mut [u32; 64]) {
120+
const _: () = assert!(4 * PAR_BLOCKS * size_of::<__m128i>() == size_of::<[u32; 64]>());
120121
unsafe {
121122
let res = rounds::<R>(&self.v);
122123
self.v[3] = _mm_add_epi32(self.v[3], _mm_set_epi32(0, 0, 0, PAR_BLOCKS as i32));

chacha20/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@
105105
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
106106
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
107107
)]
108-
#![allow(clippy::needless_range_loop)]
109-
#![allow(unexpected_cfgs)]
110108
#![warn(missing_docs, rust_2018_idioms, trivial_casts, unused_qualifications)]
111109

112110
#[cfg(feature = "cipher")]
@@ -228,10 +226,13 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
228226
fn new(key: &[u8; 32], iv: &[u8]) -> Self {
229227
let mut state = [0u32; STATE_WORDS];
230228
state[0..4].copy_from_slice(&CONSTANTS);
229+
231230
let key_chunks = key.chunks_exact(4);
232231
for (val, chunk) in state[4..12].iter_mut().zip(key_chunks) {
233232
*val = u32::from_le_bytes(chunk.try_into().unwrap());
234233
}
234+
235+
assert_eq!(iv.len(), 4 * (16 - V::NONCE_INDEX));
235236
let iv_chunks = iv.as_ref().chunks_exact(4);
236237
for (val, chunk) in state[V::NONCE_INDEX..16].iter_mut().zip(iv_chunks) {
237238
*val = u32::from_le_bytes(chunk.try_into().unwrap());

chacha20/src/rng.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ const BUF_BLOCKS: u8 = BUFFER_SIZE as u8 >> 4;
214214
impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
215215
/// Generates 4 blocks in parallel with avx2 & neon, but merely fills
216216
/// 4 blocks with sse2 & soft
217-
#[cfg(feature = "rand_core")]
218217
fn generate(&mut self, buffer: &mut [u32; 64]) {
219218
cfg_if! {
220219
if #[cfg(chacha20_force_soft)] {
@@ -363,10 +362,6 @@ macro_rules! impl_chacha_rng {
363362
#[inline]
364363
fn generate(&mut self, r: &mut Self::Results) {
365364
self.0.generate(&mut r.0);
366-
#[cfg(target_endian = "big")]
367-
for word in r.0.iter_mut() {
368-
*word = word.to_le();
369-
}
370365
}
371366
}
372367

@@ -744,6 +739,8 @@ pub(crate) mod tests {
744739
let mut rng1 = ChaChaRng::from_seed(seed);
745740
assert_eq!(rng1.next_u32(), 137206642);
746741

742+
assert_eq!(rng1.get_seed(), seed);
743+
747744
let mut rng2 = ChaChaRng::from_rng(&mut rng1);
748745
assert_eq!(rng2.next_u32(), 1325750369);
749746
}

0 commit comments

Comments
 (0)