Skip to content

Commit

Permalink
blake2: add Cargo feature to optimize for code size. (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Dec 16, 2022
1 parent 13dcab4 commit f8b2eab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions blake2/Cargo.toml
Expand Up @@ -25,3 +25,4 @@ reset = [] # Enable reset functionality
simd = []
simd_opt = ["simd"]
simd_asm = ["simd_opt"]
size_opt = [] # Optimize for code size. Removes some `inline(always)`
8 changes: 4 additions & 4 deletions blake2/src/macros.rs
Expand Up @@ -107,29 +107,29 @@ macro_rules! blake2_impl {
fn compress(&mut self, block: &Block<Self>, f0: $word, f1: $word) {
use $crate::consts::SIGMA;

#[inline(always)]
#[cfg_attr(not(feature = "size_opt"), inline(always))]
fn quarter_round(v: &mut [$vec; 4], rd: u32, rb: u32, m: $vec) {
v[0] = v[0].wrapping_add(v[1]).wrapping_add(m.from_le());
v[3] = (v[3] ^ v[0]).rotate_right_const(rd);
v[2] = v[2].wrapping_add(v[3]);
v[1] = (v[1] ^ v[2]).rotate_right_const(rb);
}

#[inline(always)]
#[cfg_attr(not(feature = "size_opt"), inline(always))]
fn shuffle(v: &mut [$vec; 4]) {
v[1] = v[1].shuffle_left_1();
v[2] = v[2].shuffle_left_2();
v[3] = v[3].shuffle_left_3();
}

#[inline(always)]
#[cfg_attr(not(feature = "size_opt"), inline(always))]
fn unshuffle(v: &mut [$vec; 4]) {
v[1] = v[1].shuffle_right_1();
v[2] = v[2].shuffle_right_2();
v[3] = v[3].shuffle_right_3();
}

#[inline(always)]
#[cfg_attr(not(feature = "size_opt"), inline(always))]
fn round(v: &mut [$vec; 4], m: &[$word; 16], s: &[usize; 16]) {
quarter_round(v, $R1, $R2, $vec::gather(m, s[0], s[2], s[4], s[6]));
quarter_round(v, $R3, $R4, $vec::gather(m, s[1], s[3], s[5], s[7]));
Expand Down

0 comments on commit f8b2eab

Please sign in to comment.