Skip to content

Commit

Permalink
Remove nightly ARM flag. (Requires version bump) (#217)
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
  • Loading branch information
tkaitchuck committed Mar 4, 2024
1 parent 8c3f257 commit b424dc4
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 33 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ no-rng = []
# in case this is being used on an architecture lacking core::sync::atomic::AtomicUsize and friends
atomic-polyfill = [ "dep:atomic-polyfill", "once_cell/atomic-polyfill"]

# Nightly-only support for AES intrinsics on 32-bit ARM
nightly-arm-aes = []

[[bench]]
name = "ahash"
path = "tests/bench.rs"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ The aHash package has the following flags:
This is done using the [getrandom](https://github.com/rust-random/getrandom) crate.
* `compile-time-rng`: For OS targets without access to a random number generator, `compile-time-rng` provides an alternative.
If `getrandom` is unavailable and `compile-time-rng` is enabled, aHash will generate random numbers at compile time and embed them in the binary.
* `nightly-arm-aes`: To use AES instructions on 32-bit ARM, which requires nightly. This is not needed on AArch64.
This allows for DOS resistance even if there is no random number generator available at runtime (assuming the compiled binary is not public).
This makes the binary non-deterministic. (If non-determinism is a problem see [constrandom's documentation](https://github.com/tkaitchuck/constrandom#deterministic-builds))

Expand Down
24 changes: 18 additions & 6 deletions src/aes_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ impl AHasher {
let result: [u64; 2] = aesdec(combined, combined).convert();
result[0]
}

#[inline]
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
fn final_mix(&self) -> u128 {
let sum = aesenc(self.sum, self.key);
aesdec(aesdec(sum, self.enc), sum)
}

#[inline]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn final_mix(&self) -> u128 {
let combined = aesenc(self.sum, self.enc);
aesdec(aesdec(combined, self.key), combined)
}
}

/// Provides [Hasher] methods to hash all of the primitive types.
Expand Down Expand Up @@ -207,10 +221,10 @@ impl Hasher for AHasher {
}
}
}

#[inline]
fn finish(&self) -> u64 {
let combined = aesenc(self.sum, self.enc);
let result: [u64; 2] = aesdec(aesdec(combined, self.key), combined).convert();
let result: [u64; 2] = self.final_mix().convert();
result[0]
}
}
Expand Down Expand Up @@ -329,15 +343,13 @@ impl Hasher for AHasherStr {
fn write(&mut self, bytes: &[u8]) {
if bytes.len() > 8 {
self.0.write(bytes);
self.0.enc = aesenc(self.0.sum, self.0.enc);
self.0.enc = aesdec(aesdec(self.0.enc, self.0.key), self.0.enc);
self.0.enc = self.0.final_mix();
} else {
add_in_length(&mut self.0.enc, bytes.len() as u64);

let value = read_small(bytes).convert();
self.0.sum = shuffle_and_add(self.0.sum, value);
self.0.enc = aesenc(self.0.sum, self.0.enc);
self.0.enc = aesdec(aesdec(self.0.enc, self.0.key), self.0.enc);
self.0.enc = self.0.final_mix();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hash_quality_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ mod fallback_tests {
#[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[cfg(test)]
mod aes_tests {
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ Note the import of [HashMapExt]. This is needed for the constructor.
#![allow(clippy::pedantic, clippy::cast_lossless, clippy::unreadable_literal, unused_imports)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(feature = "specialize", feature(min_specialization))]
#![cfg_attr(feature = "nightly-arm-aes", feature(stdarch_arm_neon_intrinsics))]

#[macro_use]
mod convert;
Expand All @@ -108,8 +107,8 @@ mod fallback_hash;
cfg_if::cfg_if! {
if #[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
))] {
mod aes_hash;
pub use crate::aes_hash::AHasher;
Expand Down
16 changes: 6 additions & 10 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
}

#[cfg(any(
all(feature = "nightly-arm-aes", target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[allow(unused)]
#[inline(always)]
Expand All @@ -122,9 +122,7 @@ pub(crate) fn aesenc(value: u128, xor: u128) -> u128 {
use core::arch::aarch64::*;
#[cfg(target_arch = "arm")]
use core::arch::arm::*;
let res = unsafe { vaesmcq_u8(vaeseq_u8(transmute!(value), transmute!(0u128))) };
let value: u128 = transmute!(res);
xor ^ value
unsafe { transmute!(vaeseq_u8(transmute!(value), transmute!(xor))) }
}

#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)))]
Expand All @@ -142,8 +140,8 @@ pub(crate) fn aesdec(value: u128, xor: u128) -> u128 {
}

#[cfg(any(
all(feature = "nightly-arm-aes", target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
))]
#[allow(unused)]
#[inline(always)]
Expand All @@ -152,9 +150,7 @@ pub(crate) fn aesdec(value: u128, xor: u128) -> u128 {
use core::arch::aarch64::*;
#[cfg(target_arch = "arm")]
use core::arch::arm::*;
let res = unsafe { vaesimcq_u8(vaesdq_u8(transmute!(value), transmute!(0u128))) };
let value: u128 = transmute!(res);
xor ^ value
unsafe { transmute!(vaesdq_u8(transmute!(value), transmute!(xor))) }
}

#[allow(unused)]
Expand Down
4 changes: 2 additions & 2 deletions src/random_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use core::hash::Hash;
cfg_if::cfg_if! {
if #[cfg(any(
all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "aarch64", target_feature = "aes", not(miri)),
all(feature = "nightly-arm-aes", target_arch = "arm", target_feature = "aes", not(miri)),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
))] {
use crate::aes_hash::*;
} else {
Expand Down
9 changes: 2 additions & 7 deletions tests/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ const AHASH_IMPL: &str = if cfg!(any(
target_feature = "aes",
not(miri),
),
all(feature = "nightly-arm-aes", target_arch = "aarch64", target_feature = "aes", not(miri)),
all(
feature = "nightly-arm-aes",
target_arch = "arm",
target_feature = "aes",
not(miri)
),
all(target_arch = "aarch64", target_feature = "aes", not(miri)),
all(target_arch = "arm", target_feature = "aes", not(miri)),
)) {
"aeshash"
} else {
Expand Down

0 comments on commit b424dc4

Please sign in to comment.