From d20c3a0d6b884be65c8bb427e5731a4139d47c39 Mon Sep 17 00:00:00 2001 From: lisicky Date: Mon, 26 Dec 2022 03:06:28 +0400 Subject: [PATCH 1/4] fix avx preprocessor checks --- src/hashing/blake2/mod.rs | 26 ++++++++++++++++++-------- src/hashing/sha2/impl256/mod.rs | 19 +++++++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/hashing/blake2/mod.rs b/src/hashing/blake2/mod.rs index 0e3f350..2ae2f2f 100644 --- a/src/hashing/blake2/mod.rs +++ b/src/hashing/blake2/mod.rs @@ -9,10 +9,10 @@ mod reference; pub use common::LastBlock; -#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx"))] mod avx; -#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx2"))] mod avx2; use common::{b, s}; @@ -59,8 +59,11 @@ impl EngineS { #[cfg(not(target_feature = "avx"))] const HAS_AVX: bool = false; - if HAS_AVX { - return avx::compress_s(&mut self.h, &mut self.t, buf, last); + #[cfg(target_feature = "avx")] + { + if HAS_AVX { + return avx::compress_s(&mut self.h, &mut self.t, buf, last); + } } } reference::compress_s(&mut self.h, &mut self.t, buf, last) @@ -115,11 +118,18 @@ impl EngineB { #[cfg(not(target_feature = "avx2"))] const HAS_AVX2: bool = false; - if HAS_AVX2 { - return avx2::compress_b(&mut self.h, &mut self.t, buf, last); + #[cfg(target_feature = "avx2")] + { + if HAS_AVX2 { + return avx2::compress_b(&mut self.h, &mut self.t, buf, last); + } } - if HAS_AVX { - return avx::compress_b(&mut self.h, &mut self.t, buf, last); + + #[cfg(target_feature = "avx")] + { + if HAS_AVX { + return avx::compress_b(&mut self.h, &mut self.t, buf, last); + } } } reference::compress_b(&mut self.h, &mut self.t, buf, last) diff --git a/src/hashing/sha2/impl256/mod.rs b/src/hashing/sha2/impl256/mod.rs index 30365ab..89a35a9 100644 --- a/src/hashing/sha2/impl256/mod.rs +++ b/src/hashing/sha2/impl256/mod.rs @@ -10,9 +10,9 @@ #[cfg(all(target_arch = "aarch64", feature = "use-stdsimd"))] mod aarch64; -#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] +#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx"))] mod avx; -#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] +#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse4.1"))] mod sse41; //TODO not finished yet //#[cfg(all(target_arch = "x86_64", target_feature = "sha"))] @@ -35,11 +35,18 @@ pub(crate) fn digest_block(state: &mut [u32; 8], block: &[u8]) { #[cfg(not(target_feature = "sse4.1"))] const HAS_SSE41: bool = false; - if HAS_AVX { - return avx::digest_block(state, block); + #[cfg(target_feature = "avx")] + { + if HAS_AVX { + return avx::digest_block(state, block); + } } - if HAS_SSE41 { - return sse41::digest_block(state, block); + + #[cfg(target_feature = "sse4.1")] + { + if HAS_SSE41 { + return sse41::digest_block(state, block); + } } } #[cfg(target_arch = "aarch64")] From d7c86b0f0a3d8f05f9163f8ede2584df64943e4c Mon Sep 17 00:00:00 2001 From: lisicky Date: Thu, 5 Jan 2023 02:52:51 +0400 Subject: [PATCH 2/4] fmt --- src/hashing/blake2/mod.rs | 10 ++++++++-- src/hashing/sha2/impl256/mod.rs | 12 ++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/hashing/blake2/mod.rs b/src/hashing/blake2/mod.rs index 2ae2f2f..7da4ae1 100644 --- a/src/hashing/blake2/mod.rs +++ b/src/hashing/blake2/mod.rs @@ -9,10 +9,16 @@ mod reference; pub use common::LastBlock; -#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx"))] +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + target_feature = "avx" +))] mod avx; -#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx2"))] +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + target_feature = "avx2" +))] mod avx2; use common::{b, s}; diff --git a/src/hashing/sha2/impl256/mod.rs b/src/hashing/sha2/impl256/mod.rs index 89a35a9..a8ef0cb 100644 --- a/src/hashing/sha2/impl256/mod.rs +++ b/src/hashing/sha2/impl256/mod.rs @@ -10,9 +10,17 @@ #[cfg(all(target_arch = "aarch64", feature = "use-stdsimd"))] mod aarch64; -#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "avx"))] + +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + target_feature = "avx" +))] mod avx; -#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "sse4.1"))] + +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + target_feature = "sse4.1" +))] mod sse41; //TODO not finished yet //#[cfg(all(target_arch = "x86_64", target_feature = "sha"))] From 5ebcd135a5c55f3a09e488e1f38d1769eb89b87f Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 10 Jan 2023 16:48:10 +0400 Subject: [PATCH 3/4] add runtime simd check --- src/hashing/blake2/mod.rs | 39 ++++++++++----------------------- src/hashing/sha2/impl256/mod.rs | 30 ++++++++++--------------- src/simd_check.rs | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 46 deletions(-) create mode 100644 src/simd_check.rs diff --git a/src/hashing/blake2/mod.rs b/src/hashing/blake2/mod.rs index 7da4ae1..6f30d12 100644 --- a/src/hashing/blake2/mod.rs +++ b/src/hashing/blake2/mod.rs @@ -21,6 +21,12 @@ mod avx; ))] mod avx2; +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + any(target_feature = "avx", target_feature = "avx2") +))] +use crate::simd_check::*; + use common::{b, s}; /// Blake2s Context @@ -61,15 +67,8 @@ impl EngineS { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { #[cfg(target_feature = "avx")] - const HAS_AVX: bool = true; - #[cfg(not(target_feature = "avx"))] - const HAS_AVX: bool = false; - - #[cfg(target_feature = "avx")] - { - if HAS_AVX { - return avx::compress_s(&mut self.h, &mut self.t, buf, last); - } + if avx_available() { + return avx::compress_s(&mut self.h, &mut self.t, buf, last); } } reference::compress_s(&mut self.h, &mut self.t, buf, last) @@ -114,28 +113,14 @@ impl EngineB { pub fn compress(&mut self, buf: &[u8], last: LastBlock) { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { - #[cfg(target_feature = "avx")] - const HAS_AVX: bool = true; - #[cfg(not(target_feature = "avx"))] - const HAS_AVX: bool = false; - - #[cfg(target_feature = "avx2")] - const HAS_AVX2: bool = true; - #[cfg(not(target_feature = "avx2"))] - const HAS_AVX2: bool = false; - #[cfg(target_feature = "avx2")] - { - if HAS_AVX2 { - return avx2::compress_b(&mut self.h, &mut self.t, buf, last); - } + if avx2_available() { + return avx2::compress_b(&mut self.h, &mut self.t, buf, last); } #[cfg(target_feature = "avx")] - { - if HAS_AVX { - return avx::compress_b(&mut self.h, &mut self.t, buf, last); - } + if avx_available() { + return avx::compress_b(&mut self.h, &mut self.t, buf, last); } } reference::compress_b(&mut self.h, &mut self.t, buf, last) diff --git a/src/hashing/sha2/impl256/mod.rs b/src/hashing/sha2/impl256/mod.rs index a8ef0cb..82d8f29 100644 --- a/src/hashing/sha2/impl256/mod.rs +++ b/src/hashing/sha2/impl256/mod.rs @@ -26,37 +26,29 @@ mod sse41; //#[cfg(all(target_arch = "x86_64", target_feature = "sha"))] //mod x64sha; +#[cfg(all( + any(target_arch = "x86", target_arch = "x86_64"), + any(target_feature = "avx", target_feature = "sse4.1") +))] +use crate::simd_check::*; + // software implementation valid for all architectures mod reference; pub(crate) fn digest_block(state: &mut [u32; 8], block: &[u8]) { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { - /// in waiting for https://github.com/rust-lang/rfcs/pull/2725 #[cfg(target_feature = "avx")] - const HAS_AVX: bool = true; - #[cfg(not(target_feature = "avx"))] - const HAS_AVX: bool = false; - - #[cfg(target_feature = "sse4.1")] - const HAS_SSE41: bool = true; - #[cfg(not(target_feature = "sse4.1"))] - const HAS_SSE41: bool = false; - - #[cfg(target_feature = "avx")] - { - if HAS_AVX { - return avx::digest_block(state, block); - } + if avx_available() { + return avx::digest_block(state, block); } #[cfg(target_feature = "sse4.1")] - { - if HAS_SSE41 { - return sse41::digest_block(state, block); - } + if sse4_1_available() { + return sse41::digest_block(state, block); } } + #[cfg(target_arch = "aarch64")] { #[cfg(feature = "use-stdsimd")] diff --git a/src/simd_check.rs b/src/simd_check.rs new file mode 100644 index 0000000..a7c5acf --- /dev/null +++ b/src/simd_check.rs @@ -0,0 +1,38 @@ +#![allow(unreachable_code)] +#![allow(dead_code)] + +pub fn avx_available() -> bool { + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { + #[cfg(feature = "std")] { + return std::is_x86_feature_detected!("avx"); + } + #[cfg(all(not(feature = "std"), target_feature = "avx"))] { + return true; + } + } + return false; +} + +pub fn avx2_available() -> bool { + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { + #[cfg(feature = "std")] { + return std::is_x86_feature_detected!("avx2"); + } + #[cfg(all(not(feature = "std"), target_feature = "avx2"))] { + return true; + } + } + return false; +} + +pub fn sse4_1_available() -> bool { + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { + #[cfg(feature = "std")] { + return std::is_x86_feature_detected!("sse4.1"); + } + #[cfg(all(not(feature = "std"), target_feature = "sse4.1"))] { + return true; + } + } + return false; +} \ No newline at end of file From cbc69c01642b263541c6225a28c00257db1875fa Mon Sep 17 00:00:00 2001 From: lisicky Date: Tue, 10 Jan 2023 16:48:30 +0400 Subject: [PATCH 4/4] add std by default --- Cargo.toml | 3 ++- src/lib.rs | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 28868a5..32811d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ doctest = false [dependencies] [features] -default = ["blake2", "sha1", "sha2", "sha3", "ripemd160", "chacha", "salsa", "hkdf", "hmac", "pbkdf2", "poly1305", "scrypt", "curve25519", "ed25519", "x25519"] +default = ["std", "blake2", "sha1", "sha2", "sha3", "ripemd160", "chacha", "salsa", "hkdf", "hmac", "pbkdf2", "poly1305", "scrypt", "curve25519", "ed25519", "x25519"] blake2 = ["digest", "mac"] sha1 = ["digest"] sha2 = ["digest"] @@ -45,3 +45,4 @@ x25519 = ["curve25519"] with-bench = [] force-32bits = [] use-stdsimd = [] +std = [] diff --git a/src/lib.rs b/src/lib.rs index 70a7a53..e97a3c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,20 +32,18 @@ #![allow(clippy::wrong_self_convention)] #![allow(clippy::identity_op)] #![allow(clippy::many_single_char_names)] -#![no_std] #![cfg_attr(feature = "with-bench", feature(test))] #![cfg_attr(feature = "use-stdsimd", feature(stdsimd))] +#![cfg_attr(not(any(feature = "std", test)), no_std)] #[cfg(test)] #[cfg(feature = "with-bench")] extern crate test; -#[cfg(not(feature = "std"))] +#[cfg(not(any(feature = "std", test)))] extern crate alloc; - -#[cfg(test)] -#[macro_use] -extern crate std; +#[cfg(any(feature = "std", test))] +extern crate std as alloc; #[cfg(feature = "blake2")] pub mod blake2b; @@ -107,5 +105,6 @@ pub mod ripemd160; mod cryptoutil; mod simd; +mod simd_check; pub mod constant_time;