Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions argon2/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ impl Block {
unsafe { &mut *(self.0.as_mut_ptr() as *mut [u8; Self::SIZE]) }
}

/// NOTE: do not call this directly. It should only be called via
/// `Argon2::compress`.
#[inline(always)]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this would be fine as #[inline] but haven't benchmarked yet

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK #[inline] is effectively useless for crate-private functions. You could remove the attribute completely to check whether compiler decides to inline it by itself. But to be on the safe side and to be more robust in the face of future compiler changes, I think it's fine to use #[inline(always)].

pub(crate) fn compress_soft(rhs: &Self, lhs: &Self) -> Self {
pub(crate) fn compress(rhs: &Self, lhs: &Self) -> Self {
let r = *rhs ^ lhs;

// Apply permutations rowwise
Expand Down Expand Up @@ -102,12 +104,6 @@ impl Block {
q ^= &r;
q
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
pub(crate) unsafe fn compress_avx2(rhs: &Self, lhs: &Self) -> Self {
Self::compress_soft(rhs, lhs)
}
}

impl Default for Block {
Expand Down Expand Up @@ -151,21 +147,3 @@ impl Zeroize for Block {
self.0.zeroize();
}
}

#[cfg(test)]
mod test {
use super::*;

#[cfg(target_arch = "x86_64")]
#[test]
fn compress_avx2() {
let mut lhs = Block([0; 128]);
lhs.0[0..7].copy_from_slice(&[0, 0, 0, 2048, 4, 2, 1]);
let rhs = Block([0; 128]);

let result = Block::compress_soft(&rhs, &lhs);
let result_avx2 = unsafe { Block::compress_avx2(&rhs, &lhs) };

assert_eq!(result.0, result_avx2.0);
}
}
Comment on lines -155 to -171

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test feels a bit superfluous as for now the implementation is just Block::compress with optimizations enabled

11 changes: 9 additions & 2 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,18 @@ impl<'key> Argon2<'key> {
fn compress(&self, rhs: &Block, lhs: &Block) -> Block {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
/// Enable AVX2 optimizations.
#[target_feature(enable = "avx2")]
unsafe fn compress_avx2(rhs: &Block, lhs: &Block) -> Block {
Block::compress(rhs, lhs)
}

if self.cpu_feat_avx2.get() {
return unsafe { Block::compress_avx2(rhs, lhs) };
return unsafe { compress_avx2(rhs, lhs) };
}
}
Block::compress_soft(rhs, lhs)

Block::compress(rhs, lhs)
}

/// Get default configured [`Params`].
Expand Down