Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nstilt1 committed Dec 20, 2023
1 parent 32c7662 commit 5df80ed
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 95 deletions.
70 changes: 15 additions & 55 deletions chacha20/src/backends/neon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ use crate::{Rounds, STATE_WORDS};
use core::{arch::aarch64::*, marker::PhantomData};

#[cfg(feature = "rand_core")]
use crate::{ChaChaCore, variants::Variant};
use crate::{variants::Variant, ChaChaCore};

#[cfg(feature = "cipher")]
use crate::chacha::Block;

#[cfg(feature = "cipher")]
use cipher::{
StreamClosure,
consts::{U4, U64},
BlockSizeUser,
ParBlocks,
ParBlocksSizeUser,
StreamBackend
BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamBackend, StreamClosure,
};

#[inline]
Expand Down Expand Up @@ -49,7 +45,7 @@ where
pub(crate) unsafe fn rng_inner<R, V>(core: &mut ChaChaCore<R, V>, buffer: &mut [u32; 64])
where
R: Rounds,
V: Variant
V: Variant,
{
let mut backend = Backend::<R> {
state: [
Expand Down Expand Up @@ -621,60 +617,24 @@ impl<R: Rounds> Backend<R> {
r3_3 = add64!(r3_3, ctrs[2]);

vst1q_u8(blocks.offset(0), vreinterpretq_u8_u32(r0_0));
vst1q_u8(
blocks.offset(16),
vreinterpretq_u8_u32(r0_1),
);
vst1q_u8(
blocks.offset(2 * 16),
vreinterpretq_u8_u32(r0_2),
);
vst1q_u8(
blocks.offset(3 * 16),
vreinterpretq_u8_u32(r0_3),
);
vst1q_u8(blocks.offset(16), vreinterpretq_u8_u32(r0_1));
vst1q_u8(blocks.offset(2 * 16), vreinterpretq_u8_u32(r0_2));
vst1q_u8(blocks.offset(3 * 16), vreinterpretq_u8_u32(r0_3));

vst1q_u8(blocks.offset(64), vreinterpretq_u8_u32(r1_0));
vst1q_u8(
blocks.offset(16+64),
vreinterpretq_u8_u32(r1_1),
);
vst1q_u8(
blocks.offset(2 * 16 + 64),
vreinterpretq_u8_u32(r1_2),
);
vst1q_u8(
blocks.offset(3 * 16 + 64),
vreinterpretq_u8_u32(r1_3),
);
vst1q_u8(blocks.offset(16 + 64), vreinterpretq_u8_u32(r1_1));
vst1q_u8(blocks.offset(2 * 16 + 64), vreinterpretq_u8_u32(r1_2));
vst1q_u8(blocks.offset(3 * 16 + 64), vreinterpretq_u8_u32(r1_3));

vst1q_u8(blocks.offset(128), vreinterpretq_u8_u32(r2_0));
vst1q_u8(
blocks.offset(16 + 128),
vreinterpretq_u8_u32(r2_1),
);
vst1q_u8(
blocks.offset(2 * 16 + 128),
vreinterpretq_u8_u32(r2_2),
);
vst1q_u8(
blocks.offset(3 * 16 + 128),
vreinterpretq_u8_u32(r2_3),
);
vst1q_u8(blocks.offset(16 + 128), vreinterpretq_u8_u32(r2_1));
vst1q_u8(blocks.offset(2 * 16 + 128), vreinterpretq_u8_u32(r2_2));
vst1q_u8(blocks.offset(3 * 16 + 128), vreinterpretq_u8_u32(r2_3));

vst1q_u8(blocks.offset(192), vreinterpretq_u8_u32(r3_0));
vst1q_u8(
blocks.offset(16 + 192),
vreinterpretq_u8_u32(r3_1),
);
vst1q_u8(
blocks.offset(2 * 16 + 192),
vreinterpretq_u8_u32(r3_2),
);
vst1q_u8(
blocks.offset(3 * 16 + 192),
vreinterpretq_u8_u32(r3_3),
);
vst1q_u8(blocks.offset(16 + 192), vreinterpretq_u8_u32(r3_1));
vst1q_u8(blocks.offset(2 * 16 + 192), vreinterpretq_u8_u32(r3_2));
vst1q_u8(blocks.offset(3 * 16 + 192), vreinterpretq_u8_u32(r3_3));

self.state[3] = add64!(self.state[3], ctrs[3]);
}
Expand Down
14 changes: 6 additions & 8 deletions chacha20/src/backends/soft.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//! Portable implementation which does not rely on architecture-specific
//! intrinsics.

use crate::{ChaChaCore, Rounds, STATE_WORDS, Variant};
use crate::{ChaChaCore, Rounds, Variant, STATE_WORDS};

#[cfg(feature = "cipher")]
use crate::chacha::Block;
#[cfg(feature = "cipher")]
use cipher::{
BlockSizeUser,
ParBlocksSizeUser,
consts::{U64, U1},
StreamBackend
consts::{U1, U64},
BlockSizeUser, ParBlocksSizeUser, StreamBackend,
};
#[cfg(feature = "cipher")]
use crate::chacha::Block;

pub(crate) struct Backend<'a, R: Rounds, V: Variant>(pub(crate) &'a mut ChaChaCore<R, V>);

Expand Down Expand Up @@ -46,7 +44,7 @@ impl<'a, R: Rounds, V: Variant> Backend<'a, R, V> {
let res = run_rounds::<R>(&self.0.state);
self.0.state[12] = self.0.state[12].wrapping_add(1);

for (word, val) in buffer[i << 4..(i+1) << 4].iter_mut().zip(res.iter()) {
for (word, val) in buffer[i << 4..(i + 1) << 4].iter_mut().zip(res.iter()) {
*word = val.to_le();
}
}
Expand Down
8 changes: 4 additions & 4 deletions chacha20/src/chacha.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub use cipher::{
consts::{U12, U32, U64},
generic_array::GenericArray, IvSizeUser,
KeyIvInit, KeySizeUser, StreamCipherCoreWrapper,
generic_array::GenericArray,
IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper,
};

use crate::{ChaChaCore, Rounds, R20, R8, R12, variants::Ietf};
use crate::{variants::Ietf, ChaChaCore, Rounds, R12, R20, R8};

/// Key type used by all ChaCha variants.
pub type Key = GenericArray<u8, U32>;
Expand Down Expand Up @@ -35,4 +35,4 @@ impl<R: Rounds> KeyIvInit for ChaChaCore<R, Ietf> {
fn new(key: &Key, iv: &Nonce) -> Self {
ChaChaCore::<R, Ietf>::new(key.as_ref(), iv.as_ref())
}
}
}
7 changes: 3 additions & 4 deletions chacha20/src/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Legacy version of ChaCha20 with a 64-bit nonce

use crate::chacha::Key;
use crate::{ChaChaCore, R20};
use cipher::{
consts::{U32, U8},
generic_array::GenericArray,
IvSizeUser, KeySizeUser,
KeyIvInit, StreamCipherCoreWrapper
IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper,
};
use crate::{ChaChaCore, R20};

/// Nonce type used by [`ChaCha20Legacy`].
pub type LegacyNonce = GenericArray<u8, U8>;
Expand Down Expand Up @@ -37,4 +36,4 @@ impl KeyIvInit for ChaCha20LegacyCore {
fn new(key: &Key, iv: &LegacyNonce) -> Self {
ChaChaCore::<R20, Legacy>::new(key.as_ref(), iv.as_ref())
}
}
}
16 changes: 8 additions & 8 deletions chacha20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ cfg_if! {

/// The ChaCha core function.
#[cfg_attr(feature = "rand_core", derive(Clone))]
pub struct ChaChaCore<R: Rounds, V:Variant> {
pub struct ChaChaCore<R: Rounds, V: Variant> {
/// Internal state of the core function
state: [u32; STATE_WORDS],
/// CPU target feature tokens
Expand All @@ -218,12 +218,12 @@ pub struct ChaChaCore<R: Rounds, V:Variant> {
/// Number of rounds to perform
rounds: PhantomData<R>,
/// the variant of the implementation
variant: PhantomData<V>
variant: PhantomData<V>,
}

impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
/// Constructs a ChaChaCore with the specified key, iv, and amount of rounds.
/// You must ensure that the iv is of the correct size when using this method
/// You must ensure that the iv is of the correct size when using this method
/// directly.
fn new(key: &[u8; 32], iv: &[u8]) -> Self {
let mut state = [0u32; STATE_WORDS];
Expand Down Expand Up @@ -254,15 +254,15 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
let tokens = ();
}
}
Self {
state,
tokens,
Self {
state,
tokens,
rounds: PhantomData,
variant: PhantomData
variant: PhantomData,
}
}

/// Generates 4 blocks in parallel with avx2 & neon, but merely fills
/// Generates 4 blocks in parallel with avx2 & neon, but merely fills
/// 4 blocks with sse2 & soft
#[cfg(feature = "rand_core")]
fn generate(&mut self, buffer: &mut [u32; 64]) {
Expand Down
21 changes: 10 additions & 11 deletions chacha20/src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ macro_rules! impl_chacha_rng {
pub struct $ChaChaXRng {
core: $ChaChaXCore,
buffer: [u32; BUFFER_SIZE],
index: usize
index: usize,
}

/// The ChaCha core random number generator
Expand All @@ -217,7 +217,7 @@ macro_rules! impl_chacha_rng {
Self {
core: ChaChaCore::<$rounds, Ietf>::from_seed(seed.into()),
buffer: [0u32; BUFFER_SIZE],
index: BUFFER_SIZE
index: BUFFER_SIZE,
}
}
}
Expand Down Expand Up @@ -324,11 +324,8 @@ macro_rules! impl_chacha_rng {
pub fn get_word_pos(&self) -> u64 {
// block_pos is a multiple of 4, and offset by 4; therefore, it already has the
// last 2 bits set to 0, allowing us to shift it left 4 and add the index
let mut result = u64::from(
self.core
.state[12]
.wrapping_sub(BUF_BLOCKS.into()),
) << 4;
let mut result =
u64::from(self.core.state[12].wrapping_sub(BUF_BLOCKS.into())) << 4;
result += self.index as u64;
// eliminate the 36th bit
result & 0xfffffffff
Expand All @@ -352,8 +349,7 @@ macro_rules! impl_chacha_rng {
// when not using `set_word_pos`, the block_pos is always a multiple of 4.
// This change follows those conventions, as well as maintaining the 6-bit
// index
self.core
.state[12] = (u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap()));
self.core.state[12] = (u32::from_le_bytes(word_offset.0[0..4].try_into().unwrap()));
// generate will increase block_pos by 4
self.generate_and_set((word_offset.0[4] & 0x0F) as usize);
}
Expand Down Expand Up @@ -387,7 +383,10 @@ macro_rules! impl_chacha_rng {
#[inline]
pub fn get_stream(&self) -> u128 {
let mut result = [0u8; 16];
for (i, &big) in self.core.state[Ietf::NONCE_INDEX..BLOCK_WORDS as usize].iter().enumerate() {
for (i, &big) in self.core.state[Ietf::NONCE_INDEX..BLOCK_WORDS as usize]
.iter()
.enumerate()
{
let index = i * 4;
result[index + 0] = big as u8;
result[index + 1] = (big >> 8) as u8;
Expand Down Expand Up @@ -453,7 +452,7 @@ macro_rules! impl_chacha_rng {
$ChaChaXRng {
core,
buffer: [0u32; BUFFER_SIZE],
index: BUFFER_SIZE
index: BUFFER_SIZE,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion chacha20/src/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pub struct Legacy();
#[cfg(feature = "legacy")]
impl Variant for Legacy {
const NONCE_INDEX: usize = 14;
}
}
7 changes: 3 additions & 4 deletions chacha20/src/xchacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
use cipher::{
consts::{U16, U24, U32, U64},
generic_array::GenericArray,
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser,
StreamCipherCoreWrapper, StreamClosure
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCoreWrapper, StreamClosure,
};

use crate::{
ChaChaCore, variants::Ietf, STATE_WORDS, CONSTANTS,
Rounds, R20, R12, R8, StreamCipherCore, StreamCipherSeekCore,
variants::Ietf, ChaChaCore, Rounds, StreamCipherCore, StreamCipherSeekCore, CONSTANTS, R12,
R20, R8, STATE_WORDS,
};

#[cfg(feature = "zeroize")]
Expand Down

0 comments on commit 5df80ed

Please sign in to comment.