Skip to content

Commit

Permalink
argon2: fold compress_avx2 into an inner function (#444)
Browse files Browse the repository at this point in the history
This changes `Argon2::compress` to contain all AVX2-related logic, so it
doesn't bleed into the `Block` type yet (especially since that lacks any
AVX2-specific implementation).
  • Loading branch information
tarcieri committed Jul 13, 2023
1 parent 19e0cdf commit 9e5c86b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
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)]
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);
}
}
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

0 comments on commit 9e5c86b

Please sign in to comment.