Skip to content

Commit 15258ae

Browse files
committed
move quarter_round function to the root
1 parent a61e846 commit 15258ae

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

chacha20/src/backends/soft.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Portable implementation which does not rely on architecture-specific
22
//! intrinsics.
33
4-
use crate::{Block, ChaChaCore, Unsigned, STATE_WORDS};
4+
use crate::{quarter_round, Block, ChaChaCore, Unsigned, STATE_WORDS};
55
use cipher::{
66
consts::{U1, U64},
77
BlockSizeUser, ParBlocksSizeUser, StreamBackend,
@@ -52,28 +52,3 @@ fn run_rounds<R: Unsigned>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
5252
}
5353
res
5454
}
55-
56-
/// The ChaCha20 quarter round function
57-
pub(crate) fn quarter_round(
58-
a: usize,
59-
b: usize,
60-
c: usize,
61-
d: usize,
62-
state: &mut [u32; STATE_WORDS],
63-
) {
64-
state[a] = state[a].wrapping_add(state[b]);
65-
state[d] ^= state[a];
66-
state[d] = state[d].rotate_left(16);
67-
68-
state[c] = state[c].wrapping_add(state[d]);
69-
state[b] ^= state[c];
70-
state[b] = state[b].rotate_left(12);
71-
72-
state[a] = state[a].wrapping_add(state[b]);
73-
state[d] ^= state[a];
74-
state[d] = state[d].rotate_left(8);
75-
76-
state[c] = state[c].wrapping_add(state[d]);
77-
state[b] ^= state[c];
78-
state[b] = state[b].rotate_left(7);
79-
}

chacha20/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,31 @@ impl<R: Unsigned> Drop for ChaChaCore<R> {
315315
#[cfg(feature = "zeroize")]
316316
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
317317
impl<R: Unsigned> ZeroizeOnDrop for ChaChaCore<R> {}
318+
319+
/// The ChaCha20 quarter round function
320+
///
321+
/// We located this function in the root of the crate as we want it to be available
322+
/// for the soft backend and for xchacha.
323+
pub(crate) fn quarter_round(
324+
a: usize,
325+
b: usize,
326+
c: usize,
327+
d: usize,
328+
state: &mut [u32; STATE_WORDS],
329+
) {
330+
state[a] = state[a].wrapping_add(state[b]);
331+
state[d] ^= state[a];
332+
state[d] = state[d].rotate_left(16);
333+
334+
state[c] = state[c].wrapping_add(state[d]);
335+
state[b] ^= state[c];
336+
state[b] = state[b].rotate_left(12);
337+
338+
state[a] = state[a].wrapping_add(state[b]);
339+
state[d] ^= state[a];
340+
state[d] = state[d].rotate_left(8);
341+
342+
state[c] = state[c].wrapping_add(state[d]);
343+
state[b] ^= state[c];
344+
state[b] = state[b].rotate_left(7);
345+
}

chacha20/src/xchacha.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! XChaCha is an extended nonce variant of ChaCha
22
3-
use super::{ChaChaCore, Key, Nonce, CONSTANTS, STATE_WORDS};
3+
use super::{quarter_round, ChaChaCore, Key, Nonce, CONSTANTS, STATE_WORDS};
44
use cipher::{
55
consts::{U10, U16, U24, U32, U4, U6, U64},
66
generic_array::{typenum::Unsigned, GenericArray},
77
BlockSizeUser, IvSizeUser, KeyIvInit, KeySizeUser, StreamCipherCore, StreamCipherCoreWrapper,
88
StreamCipherSeekCore, StreamClosure,
99
};
1010

11-
use crate::backends::soft::quarter_round;
12-
1311
#[cfg(feature = "zeroize")]
1412
use cipher::zeroize::ZeroizeOnDrop;
1513

0 commit comments

Comments
 (0)