Skip to content

Commit 2f45194

Browse files
committed
blake2: fix build + warnings on no_std targets
Many of the warnings were occurring because the parallel backend requires x86 SIMD intrinsics (AVX2 or SSE41) and therefore the parallel code is dead on other targets. This commit feature gates all such code, ensuring that everything will build warning-free on other targets, and that users who attempt to use the parallel APIs on unsupported targets get compile errors rather than warnings.
1 parent f2caf71 commit 2f45194

File tree

11 files changed

+77
-56
lines changed

11 files changed

+77
-56
lines changed

.github/workflows/blake2.yml

+22-24
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@ env:
1717
RUSTFLAGS: "-Dwarnings"
1818

1919
jobs:
20-
# TODO(tarcieri): re-enable these when failures are addressed:
21-
# <https://github.com/RustCrypto/hashes/pull/228/checks?check_run_id=1831937705>
22-
# build:
23-
# runs-on: ubuntu-latest
24-
# strategy:
25-
# matrix:
26-
# rust:
27-
# - 1.41.0 # MSRV
28-
# - stable
29-
# target:
30-
# - thumbv7em-none-eabi
31-
# - wasm32-unknown-unknown
32-
# steps:
33-
# - uses: actions/checkout@v1
34-
# - uses: actions-rs/toolchain@v1
35-
# with:
36-
# profile: minimal
37-
# toolchain: ${{ matrix.rust }}
38-
# target: ${{ matrix.target }}
39-
# override: true
40-
# - run: cargo build --target ${{ matrix.target }} --release --no-default-features
41-
# - run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2b
42-
# - run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2s
43-
# - run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2b,blake2s
20+
build:
21+
runs-on: ubuntu-latest
22+
strategy:
23+
matrix:
24+
rust:
25+
- 1.41.0 # MSRV
26+
- stable
27+
target:
28+
- thumbv7em-none-eabi
29+
- wasm32-unknown-unknown
30+
steps:
31+
- uses: actions/checkout@v1
32+
- uses: actions-rs/toolchain@v1
33+
with:
34+
profile: minimal
35+
toolchain: ${{ matrix.rust }}
36+
target: ${{ matrix.target }}
37+
override: true
38+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features
39+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2b
40+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2s
41+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features blake2b,blake2s
4442

4543
test:
4644
runs-on: ubuntu-latest

blake2/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ arrayvec = { version = "0.5", default-features = false }
1717
crypto-mac = "0.8"
1818
digest = "0.9"
1919
opaque-debug = "0.3"
20-
subtle = ">=2, <2.5"
20+
subtle = { version = ">=2, <2.5", default-features = false }
2121

2222
[dev-dependencies]
2323
crypto-mac = { version = "0.8", features = ["dev"] }
@@ -39,3 +39,7 @@ blake2s = []
3939
# performance. This feature disables some inlining, improving the performance
4040
# of the portable implementation in that specific case.
4141
uninline_portable = []
42+
43+
[package.metadata.docs.rs]
44+
all-features = true
45+
rustdoc-args = ["--cfg", "docsrs"]

blake2/src/blake2b.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
//! ```
2525
2626
pub(crate) mod backend;
27-
pub mod many;
2827
pub(crate) mod state;
2928

29+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
30+
#[cfg_attr(docsrs, doc(cfg(any(target_arch = "x86", target_arch = "x86_64"))))]
31+
pub mod many;
32+
3033
mod hash;
3134
mod params;
3235
#[cfg(test)]
@@ -280,10 +283,10 @@ pub(crate) fn paint_test_input(buf: &mut [u8]) {
280283

281284
// This module is pub for internal benchmarks only. Please don't use it.
282285
#[doc(hidden)]
286+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
283287
pub mod benchmarks {
284-
use crate::blake2bp;
285-
286288
use super::*;
289+
use crate::blake2bp;
287290

288291
pub fn force_portable(params: &mut Params) {
289292
params.implementation = backend::Implementation::portable();

blake2/src/blake2b/backend.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use core::cmp;
1313
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1414
pub const MAX_DEGREE: usize = 4;
1515

16-
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
17-
pub const MAX_DEGREE: usize = 1;
18-
1916
/// Variants other than Portable are unreachable in no_std, unless CPU features
2017
/// are explicitly enabled for the build with e.g. RUSTFLAGS="-C target-feature=avx2".
2118
/// This might change in the future if is_x86_feature_detected moves into libcore.
@@ -91,11 +88,10 @@ impl Implementation {
9188
None
9289
}
9390

91+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9492
pub fn degree(&self) -> usize {
9593
match self.0 {
96-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9794
Platform::Avx2 => avx2::DEGREE,
98-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9995
Platform::Sse41 => sse41::DEGREE,
10096
Platform::Portable => 1,
10197
}
@@ -123,19 +119,19 @@ impl Implementation {
123119
}
124120
}
125121

122+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
126123
pub fn compress2_loop(&self, jobs: &mut [Job<'_, '_>; 2], finalize: Finalize, stride: Stride) {
127124
match self.0 {
128-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
129125
Platform::Avx2 | Platform::Sse41 => unsafe {
130126
sse41::compress2_loop(jobs, finalize, stride)
131127
},
132128
_ => panic!("unsupported"),
133129
}
134130
}
135131

132+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
136133
pub fn compress4_loop(&self, jobs: &mut [Job<'_, '_>; 4], finalize: Finalize, stride: Stride) {
137134
match self.0 {
138-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
139135
Platform::Avx2 => unsafe { avx2::compress4_loop(jobs, finalize, stride) },
140136
_ => panic!("unsupported"),
141137
}
@@ -196,14 +192,16 @@ impl LastNode {
196192

197193
#[derive(Clone, Copy, Debug)]
198194
pub enum Stride {
199-
Serial, // BLAKE2b/BLAKE2s
195+
Serial, // BLAKE2b/BLAKE2s
196+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
200197
Parallel, // BLAKE2bp/BLAKE2sp
201198
}
202199

203200
impl Stride {
204201
pub fn padded_blockbytes(&self) -> usize {
205202
match self {
206203
Stride::Serial => BLOCKBYTES,
204+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
207205
Stride::Parallel => crate::blake2bp::DEGREE * BLOCKBYTES,
208206
}
209207
}
@@ -217,6 +215,7 @@ pub(crate) fn count_high(count: Count) -> Word {
217215
(count >> (8 * size_of::<Word>())) as Word
218216
}
219217

218+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
220219
pub(crate) fn assemble_count(low: Word, high: Word) -> Count {
221220
low as Count + ((high as Count) << (8 * size_of::<Word>()))
222221
}

blake2/src/blake2b/test.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::*;
2+
3+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
24
use crate::blake2bp;
35

46
const EMPTY_HASH: &str = "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419\
@@ -120,6 +122,7 @@ fn test_all_parameters() {
120122
);
121123
}
122124

125+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
123126
#[test]
124127
fn test_all_parameters_blake2bp() {
125128
let mut params = crate::blake2bp::Params::new();
@@ -183,18 +186,21 @@ fn test_long_inner_hash_length_panics() {
183186
Params::new().inner_hash_length(OUTBYTES + 1);
184187
}
185188

189+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
186190
#[test]
187191
#[should_panic]
188192
fn test_blake2bp_short_hash_length_panics() {
189193
blake2bp::Params::new().hash_length(0);
190194
}
191195

196+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
192197
#[test]
193198
#[should_panic]
194199
fn test_blake2bp_long_hash_length_panics() {
195200
blake2bp::Params::new().hash_length(OUTBYTES + 1);
196201
}
197202

203+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
198204
#[test]
199205
#[should_panic]
200206
fn test_blake2bp_long_key_panics() {

blake2/src/blake2bp.rs

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ use crate::blake2b::{
2727
};
2828
use core::{cmp, fmt, mem::size_of};
2929

30-
#[cfg(feature = "std")]
31-
use std;
32-
3330
pub(crate) const DEGREE: usize = 4;
3431

3532
/// Compute the BLAKE2bp hash of a slice of bytes all at once, using default

blake2/src/blake2s.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
//! ```
2424
2525
pub(crate) mod backend;
26-
pub mod many;
2726
pub(crate) mod state;
2827

28+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
29+
#[cfg_attr(docsrs, doc(cfg(any(target_arch = "x86", target_arch = "x86_64"))))]
30+
pub mod many;
31+
2932
mod hash;
3033
mod params;
3134
#[cfg(test)]
3235
mod test;
3336

3437
pub use self::{hash::Hash, params::Params, state::State};
3538

36-
use crate::blake2sp;
3739
use core::{fmt, mem::size_of};
3840
use crypto_mac::{InvalidKeyLength, Mac, NewMac};
3941
use digest::{
@@ -270,8 +272,10 @@ pub(crate) fn paint_test_input(buf: &mut [u8]) {
270272

271273
// This module is pub for internal benchmarks only. Please don't use it.
272274
#[doc(hidden)]
275+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
273276
pub mod benchmarks {
274277
use super::*;
278+
use crate::blake2sp;
275279

276280
pub fn force_portable(params: &mut Params) {
277281
params.implementation = backend::Implementation::portable();

blake2/src/blake2s/backend.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ use core::cmp;
1313
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1414
pub const MAX_DEGREE: usize = 8;
1515

16-
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
17-
pub const MAX_DEGREE: usize = 1;
18-
1916
/// Variants other than Portable are unreachable in no_std, unless CPU features
2017
/// are explicitly enabled for the build with e.g. RUSTFLAGS="-C target-feature=avx2".
2118
/// This might change in the future if is_x86_feature_detected moves into libcore.
@@ -91,11 +88,10 @@ impl Implementation {
9188
None
9289
}
9390

91+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9492
pub fn degree(&self) -> usize {
9593
match self.0 {
96-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9794
Platform::Avx2 => avx2::DEGREE,
98-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9995
Platform::Sse41 => sse41::DEGREE,
10096
Platform::Portable => 1,
10197
}
@@ -121,19 +117,19 @@ impl Implementation {
121117
}
122118
}
123119

120+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
124121
pub fn compress4_loop(&self, jobs: &mut [Job<'_, '_>; 4], finalize: Finalize, stride: Stride) {
125122
match self.0 {
126-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
127123
Platform::Avx2 | Platform::Sse41 => unsafe {
128124
sse41::compress4_loop(jobs, finalize, stride)
129125
},
130126
_ => panic!("unsupported"),
131127
}
132128
}
133129

130+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
134131
pub fn compress8_loop(&self, jobs: &mut [Job<'_, '_>; 8], finalize: Finalize, stride: Stride) {
135132
match self.0 {
136-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
137133
Platform::Avx2 => unsafe { avx2::compress8_loop(jobs, finalize, stride) },
138134
_ => panic!("unsupported"),
139135
}
@@ -194,14 +190,16 @@ impl LastNode {
194190

195191
#[derive(Clone, Copy, Debug)]
196192
pub enum Stride {
197-
Serial, // BLAKE2b/BLAKE2s
193+
Serial, // BLAKE2b/BLAKE2s
194+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
198195
Parallel, // BLAKE2bp/BLAKE2sp
199196
}
200197

201198
impl Stride {
202199
pub fn padded_blockbytes(&self) -> usize {
203200
match self {
204201
Stride::Serial => BLOCKBYTES,
202+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
205203
Stride::Parallel => crate::blake2sp::DEGREE * BLOCKBYTES,
206204
}
207205
}
@@ -215,6 +213,7 @@ pub(crate) fn count_high(count: Count) -> Word {
215213
(count >> (8 * size_of::<Word>())) as Word
216214
}
217215

216+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
218217
pub(crate) fn assemble_count(low: Word, high: Word) -> Count {
219218
low as Count + ((high as Count) << (8 * size_of::<Word>()))
220219
}

blake2/src/blake2s/test.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use super::*;
22

3+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
4+
use crate::blake2sp;
5+
36
const EMPTY_HASH: &str = "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9";
47
const ABC_HASH: &str = "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982";
58
const ONE_BLOCK_HASH: &str = "ae09db7cd54f42b490ef09b6bc541af688e4959bb8c53f359a6f56e38ab454a3";
@@ -115,6 +118,7 @@ fn test_all_parameters() {
115118
);
116119
}
117120

121+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
118122
#[test]
119123
fn test_all_parameters_blake2sp() {
120124
let mut params = blake2sp::Params::new();
@@ -178,29 +182,34 @@ fn test_long_inner_hash_length_panics() {
178182
Params::new().inner_hash_length(OUTBYTES + 1);
179183
}
180184

185+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
181186
#[test]
182187
#[should_panic]
183188
fn test_blake2sp_short_hash_length_panics() {
184189
blake2sp::Params::new().hash_length(0);
185190
}
186191

192+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
187193
#[test]
188194
#[should_panic]
189195
fn test_blake2sp_long_hash_length_panics() {
190196
blake2sp::Params::new().hash_length(OUTBYTES + 1);
191197
}
192198

199+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
193200
#[test]
194201
#[should_panic]
195202
fn test_blake2sp_long_key_panics() {
196203
blake2sp::Params::new().key(&[0; KEYBYTES + 1]);
197204
}
198205

206+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
199207
#[test]
200208
fn test_blake2sp_max_offset_ok() {
201209
Params::new().node_offset((1 << 48) - 1);
202210
}
203211

212+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
204213
#[test]
205214
#[should_panic]
206215
fn test_blake2sp_offset_too_large_panics() {

blake2/src/blake2sp.rs

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ use crate::blake2s::{
2727
};
2828
use core::{cmp, fmt, mem::size_of};
2929

30-
#[cfg(feature = "std")]
31-
use std;
32-
3330
pub(crate) const DEGREE: usize = 8;
3431

3532
/// Compute the BLAKE2sp hash of a slice of bytes all at once, using default

0 commit comments

Comments
 (0)