Skip to content

Commit b1f64e4

Browse files
authored
blake2: remove SIMD support (#898)
The current SIMD support is severely outdated and should be re-implemented.
1 parent f72fa5f commit b1f64e4

12 files changed

Lines changed: 125 additions & 618 deletions

File tree

blake2/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ default = ["alloc"]
2424
alloc = ["digest/alloc"]
2525
zeroize = ["digest/zeroize"]
2626
reset = [] # Enable reset functionality
27-
#simd = []
28-
#simd_opt = ["simd"]
29-
#simd_asm = ["simd_opt"]
3027
size_opt = [] # Optimize for code size. Removes some `inline(always)`
3128

3229
[package.metadata.docs.rs]

blake2/src/as_bytes.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

blake2/src/consts.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::unreadable_literal)]
2-
31
pub(crate) static SIGMA: [[usize; 16]; 12] = [
42
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
53
[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
@@ -26,22 +24,6 @@ pub(crate) static BLAKE2B_IV: [u64; 8] = [
2624
0x5be0cd19137e2179,
2725
];
2826

29-
/*
30-
pub const BLAKE2B_BLOCKBYTES : usize = 128;
31-
pub const BLAKE2B_OUTBYTES : usize = 64;
32-
pub const BLAKE2B_KEYBYTES : usize = 64;
33-
pub const BLAKE2B_SALTBYTES : usize = 16;
34-
pub const BLAKE2B_PERSONALBYTES : usize = 16;
35-
*/
36-
3727
pub(crate) static BLAKE2S_IV: [u32; 8] = [
3828
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
3929
];
40-
41-
/*
42-
pub const BLAKE2S_BLOCKBYTES : usize = 64;
43-
pub const BLAKE2S_OUTBYTES : usize = 32;
44-
pub const BLAKE2S_KEYBYTES : usize = 32;
45-
pub const BLAKE2S_SALTBYTES : usize = 8;
46-
pub const BLAKE2S_PERSONALBYTES : usize = 8;
47-
*/

blake2/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
66
)]
77
#![cfg_attr(docsrs, feature(doc_cfg))]
8-
#![allow(unexpected_cfgs)] // `simd` feature is broken
9-
#![warn(missing_docs, unreachable_pub)]
10-
#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
11-
#![cfg_attr(feature = "simd", allow(incomplete_features))]
128

139
pub use digest::{self, Digest};
1410

@@ -31,17 +27,15 @@ use digest::{FixedOutputReset, Reset};
3127
#[cfg(feature = "zeroize")]
3228
use digest::zeroize::{Zeroize, ZeroizeOnDrop};
3329

34-
mod as_bytes;
3530
mod consts;
3631

3732
mod simd;
3833

3934
#[macro_use]
4035
mod macros;
4136

42-
use as_bytes::AsBytes;
4337
use consts::{BLAKE2B_IV, BLAKE2S_IV};
44-
use simd::{Vector4, u32x4, u64x4};
38+
use simd::{u32x4, u64x4};
4539

4640
blake2_impl!(
4741
Blake2bVarCore,

blake2/src/macros.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ macro_rules! blake2_impl {
9898
out: &mut Output<Self>,
9999
) {
100100
self.compress(final_block, !0, flag);
101-
let buf = [self.h[0].to_le(), self.h[1].to_le()];
102-
out.copy_from_slice(buf.as_bytes())
101+
let n = size_of::<$vec>();
102+
out[..n].copy_from_slice(self.h[0].to_le().as_bytes());
103+
out[n..].copy_from_slice(self.h[1].to_le().as_bytes());
103104
}
104105

105106
fn compress(&mut self, block: &Block<Self>, f0: $word, f1: $word) {

blake2/src/simd.rs

Lines changed: 121 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,46 @@
1-
// Copyright 2015 blake2-rfc Developers
2-
//
3-
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4-
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5-
// http://opensource.org/licenses/MIT>, at your option. This file may not be
6-
// copied, modified, or distributed except according to those terms.
7-
8-
mod simd_opt;
9-
mod simdint;
10-
mod simdop;
11-
mod simdty;
12-
13-
pub(crate) use self::simdty::{u32x4, u64x4};
14-
15-
pub(crate) trait Vector4<T>: Copy {
16-
fn gather(src: &[T], i0: usize, i1: usize, i2: usize, i3: usize) -> Self;
17-
18-
#[allow(clippy::wrong_self_convention)]
19-
fn from_le(self) -> Self;
20-
fn to_le(self) -> Self;
21-
22-
fn wrapping_add(self, rhs: Self) -> Self;
23-
24-
fn rotate_right_const(self, n: u32) -> Self;
25-
26-
fn shuffle_left_1(self) -> Self;
27-
fn shuffle_left_2(self) -> Self;
28-
fn shuffle_left_3(self) -> Self;
29-
30-
#[inline(always)]
31-
fn shuffle_right_1(self) -> Self {
32-
self.shuffle_left_3()
33-
}
34-
#[inline(always)]
35-
fn shuffle_right_2(self) -> Self {
36-
self.shuffle_left_2()
37-
}
38-
#[inline(always)]
39-
fn shuffle_right_3(self) -> Self {
40-
self.shuffle_left_1()
41-
}
42-
}
1+
use core::ops::{Add, BitXor, Shl, Shr};
2+
3+
#[cfg(feature = "zeroize")]
4+
use digest::zeroize::Zeroize;
435

446
macro_rules! impl_vector4 {
457
($vec:ident, $word:ident) => {
46-
impl Vector4<$word> for $vec {
8+
#[derive(Clone, Copy, Debug)]
9+
#[repr(C)]
10+
pub(crate) struct $vec(
11+
pub(crate) $word,
12+
pub(crate) $word,
13+
pub(crate) $word,
14+
pub(crate) $word,
15+
);
16+
17+
impl $vec {
18+
#[inline(always)]
19+
pub(crate) fn new(e0: $word, e1: $word, e2: $word, e3: $word) -> Self {
20+
Self(e0, e1, e2, e3)
21+
}
22+
4723
#[inline(always)]
48-
fn gather(src: &[$word], i0: usize, i1: usize, i2: usize, i3: usize) -> Self {
24+
pub(crate) fn gather(
25+
src: &[$word],
26+
i0: usize,
27+
i1: usize,
28+
i2: usize,
29+
i3: usize,
30+
) -> Self {
4931
$vec::new(src[i0], src[i1], src[i2], src[i3])
5032
}
5133

5234
#[cfg(target_endian = "little")]
5335
#[inline(always)]
54-
fn from_le(self) -> Self {
36+
#[allow(clippy::wrong_self_convention)]
37+
pub(crate) fn from_le(self) -> Self {
5538
self
5639
}
5740

5841
#[cfg(not(target_endian = "little"))]
5942
#[inline(always)]
60-
fn from_le(self) -> Self {
43+
pub(crate) fn from_le(self) -> Self {
6144
$vec::new(
6245
$word::from_le(self.0),
6346
$word::from_le(self.1),
@@ -68,13 +51,13 @@ macro_rules! impl_vector4 {
6851

6952
#[cfg(target_endian = "little")]
7053
#[inline(always)]
71-
fn to_le(self) -> Self {
54+
pub(crate) fn to_le(self) -> Self {
7255
self
7356
}
7457

7558
#[cfg(not(target_endian = "little"))]
7659
#[inline(always)]
77-
fn to_le(self) -> Self {
60+
pub(crate) fn to_le(self) -> Self {
7861
$vec::new(
7962
self.0.to_le(),
8063
self.1.to_le(),
@@ -84,55 +67,118 @@ macro_rules! impl_vector4 {
8467
}
8568

8669
#[inline(always)]
87-
fn wrapping_add(self, rhs: Self) -> Self {
70+
pub(crate) fn wrapping_add(self, rhs: Self) -> Self {
8871
self + rhs
8972
}
9073

9174
#[inline(always)]
92-
fn rotate_right_const(self, n: u32) -> Self {
93-
simd_opt::$vec::rotate_right_const(self, n)
75+
pub(crate) fn rotate_right_const(self, n: u32) -> Self {
76+
$vec::new(
77+
self.0.rotate_right(n),
78+
self.1.rotate_right(n),
79+
self.2.rotate_right(n),
80+
self.3.rotate_right(n),
81+
)
9482
}
9583

96-
#[cfg(feature = "simd")]
9784
#[inline(always)]
98-
fn shuffle_left_1(self) -> Self {
99-
use crate::simd::simdint::simd_shuffle4;
100-
const IDX: [u32; 4] = [1, 2, 3, 0];
101-
unsafe { simd_shuffle4(self, self, IDX) }
85+
pub(crate) fn shuffle_left_1(self) -> Self {
86+
$vec::new(self.1, self.2, self.3, self.0)
10287
}
10388

104-
#[cfg(not(feature = "simd"))]
10589
#[inline(always)]
106-
fn shuffle_left_1(self) -> Self {
107-
$vec::new(self.1, self.2, self.3, self.0)
90+
pub(crate) fn shuffle_left_2(self) -> Self {
91+
$vec::new(self.2, self.3, self.0, self.1)
10892
}
10993

110-
#[cfg(feature = "simd")]
11194
#[inline(always)]
112-
fn shuffle_left_2(self) -> Self {
113-
use crate::simd::simdint::simd_shuffle4;
114-
const IDX: [u32; 4] = [2, 3, 0, 1];
115-
unsafe { simd_shuffle4(self, self, IDX) }
95+
pub(crate) fn shuffle_left_3(self) -> Self {
96+
$vec::new(self.3, self.0, self.1, self.2)
11697
}
11798

118-
#[cfg(not(feature = "simd"))]
11999
#[inline(always)]
120-
fn shuffle_left_2(self) -> Self {
121-
$vec::new(self.2, self.3, self.0, self.1)
100+
pub(crate) fn shuffle_right_1(self) -> Self {
101+
self.shuffle_left_3()
102+
}
103+
#[inline(always)]
104+
pub(crate) fn shuffle_right_2(self) -> Self {
105+
self.shuffle_left_2()
106+
}
107+
#[inline(always)]
108+
pub(crate) fn shuffle_right_3(self) -> Self {
109+
self.shuffle_left_1()
122110
}
123111

124-
#[cfg(feature = "simd")]
125112
#[inline(always)]
126-
fn shuffle_left_3(self) -> Self {
127-
use crate::simd::simdint::simd_shuffle4;
128-
const IDX: [u32; 4] = [3, 0, 1, 2];
129-
unsafe { simd_shuffle4(self, self, IDX) }
113+
pub(crate) fn as_bytes(&self) -> &[u8] {
114+
let p = self as *const Self as *const u8;
115+
unsafe { core::slice::from_raw_parts(p, core::mem::size_of::<Self>()) }
130116
}
117+
}
118+
119+
impl Add for $vec {
120+
type Output = Self;
131121

132-
#[cfg(not(feature = "simd"))]
133122
#[inline(always)]
134-
fn shuffle_left_3(self) -> Self {
135-
$vec::new(self.3, self.0, self.1, self.2)
123+
fn add(self, rhs: Self) -> Self::Output {
124+
$vec::new(
125+
self.0.wrapping_add(rhs.0),
126+
self.1.wrapping_add(rhs.1),
127+
self.2.wrapping_add(rhs.2),
128+
self.3.wrapping_add(rhs.3),
129+
)
130+
}
131+
}
132+
133+
impl BitXor for $vec {
134+
type Output = Self;
135+
136+
#[inline(always)]
137+
fn bitxor(self, rhs: Self) -> Self::Output {
138+
$vec::new(
139+
self.0 ^ rhs.0,
140+
self.1 ^ rhs.1,
141+
self.2 ^ rhs.2,
142+
self.3 ^ rhs.3,
143+
)
144+
}
145+
}
146+
147+
impl Shl<$vec> for $vec {
148+
type Output = Self;
149+
150+
#[inline(always)]
151+
fn shl(self, rhs: Self) -> Self::Output {
152+
$vec::new(
153+
self.0 << rhs.0,
154+
self.1 << rhs.1,
155+
self.2 << rhs.2,
156+
self.3 << rhs.3,
157+
)
158+
}
159+
}
160+
161+
impl Shr<$vec> for $vec {
162+
type Output = Self;
163+
164+
#[inline(always)]
165+
fn shr(self, rhs: Self) -> Self::Output {
166+
$vec::new(
167+
self.0 >> rhs.0,
168+
self.1 >> rhs.1,
169+
self.2 >> rhs.2,
170+
self.3 >> rhs.3,
171+
)
172+
}
173+
}
174+
175+
#[cfg(feature = "zeroize")]
176+
impl Zeroize for $vec {
177+
fn zeroize(&mut self) {
178+
self.0.zeroize();
179+
self.1.zeroize();
180+
self.2.zeroize();
181+
self.3.zeroize();
136182
}
137183
}
138184
};

0 commit comments

Comments
 (0)