diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 18:35:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 18:35:44 +0000 |
commit | c0db95d3dda1865d4c6bf0666b0e7439b40b9bf2 (patch) | |
tree | 74359a4b4954f9380cb0fb23b59f0c53d9355f5d /third_party/rust/ahash/src | |
parent | Releasing progress-linux version 115.9.1esr-1~deb12u1progress7u1. (diff) | |
download | firefox-esr-c0db95d3dda1865d4c6bf0666b0e7439b40b9bf2.tar.xz firefox-esr-c0db95d3dda1865d4c6bf0666b0e7439b40b9bf2.zip |
Merging upstream version 115.10.0esr.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ahash/src')
-rw-r--r-- | third_party/rust/ahash/src/aes_hash.rs | 13 | ||||
-rw-r--r-- | third_party/rust/ahash/src/hash_quality_test.rs | 36 | ||||
-rw-r--r-- | third_party/rust/ahash/src/random_state.rs | 7 |
3 files changed, 48 insertions, 8 deletions
diff --git a/third_party/rust/ahash/src/aes_hash.rs b/third_party/rust/ahash/src/aes_hash.rs index 1c98582cee..7e619b520b 100644 --- a/third_party/rust/ahash/src/aes_hash.rs +++ b/third_party/rust/ahash/src/aes_hash.rs @@ -170,10 +170,10 @@ impl Hasher for AHasher { let tail = data.read_last_u128x4(); let mut current: [u128; 4] = [self.key; 4]; current[0] = aesenc(current[0], tail[0]); - current[1] = aesenc(current[1], tail[1]); + current[1] = aesdec(current[1], tail[1]); current[2] = aesenc(current[2], tail[2]); - current[3] = aesenc(current[3], tail[3]); - let mut sum: [u128; 2] = [self.key, self.key]; + current[3] = aesdec(current[3], tail[3]); + let mut sum: [u128; 2] = [self.key, !self.key]; sum[0] = add_by_64s(sum[0].convert(), tail[0].convert()).convert(); sum[1] = add_by_64s(sum[1].convert(), tail[1].convert()).convert(); sum[0] = shuffle_and_add(sum[0], tail[2]); @@ -190,8 +190,9 @@ impl Hasher for AHasher { sum[1] = shuffle_and_add(sum[1], blocks[3]); data = rest; } - self.hash_in_2(aesenc(current[0], current[1]), aesenc(current[2], current[3])); - self.hash_in(add_by_64s(sum[0].convert(), sum[1].convert()).convert()); + self.hash_in_2(current[0], current[1]); + self.hash_in_2(current[2], current[3]); + self.hash_in_2(sum[0], sum[1]); } else { //len 33-64 let (head, _) = data.read_u128x2(); @@ -215,7 +216,7 @@ impl Hasher for AHasher { fn finish(&self) -> u64 { let combined = aesdec(self.sum, self.enc); let result: [u64; 2] = aesenc(aesenc(combined, self.key), combined).convert(); - result[0] + result[1] } } diff --git a/third_party/rust/ahash/src/hash_quality_test.rs b/third_party/rust/ahash/src/hash_quality_test.rs index 4cd3156afe..17228d4716 100644 --- a/third_party/rust/ahash/src/hash_quality_test.rs +++ b/third_party/rust/ahash/src/hash_quality_test.rs @@ -1,5 +1,5 @@ use core::hash::{Hash, Hasher}; -use std::collections::HashMap; +use std::collections::{HashMap}; fn assert_sufficiently_different(a: u64, b: u64, tolerance: i32) { let (same_byte_count, same_nibble_count) = count_same_bytes_and_nibbles(a, b); @@ -326,6 +326,28 @@ fn test_length_extension<T: Hasher>(hasher: impl Fn(u128, u128) -> T) { } } +fn test_sparse<T: Hasher>(hasher: impl Fn() -> T) { + let mut buf = [0u8; 256]; + let mut hashes = HashMap::new(); + for idx_1 in 0..256 { + for idx_2 in idx_1+1..256 { + for value_1 in [1, 2, 4, 8, 16, 32, 64, 128] { + for value_2 in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129, 192, 254, 255] { + buf[idx_1] = value_1; + buf[idx_2] = value_2; + let hash_value = hash_with(&buf, &mut hasher()); + let keys = hashes.entry(hash_value).or_insert(Vec::new()); + keys.push((idx_1, value_1, idx_2, value_2)); + buf[idx_1] = 0; + buf[idx_2] = 0; + } + } + } + } + hashes.retain(|_key, value| value.len() != 1); + assert_eq!(0, hashes.len(), "Collision with: {:?}", hashes); +} + #[cfg(test)] mod fallback_tests { use crate::fallback_hash::*; @@ -392,6 +414,12 @@ mod fallback_tests { fn fallback_length_extension() { test_length_extension(|a, b| AHasher::new_with_keys(a, b)); } + + #[test] + fn test_no_sparse_collisions() { + test_sparse(|| AHasher::new_with_keys(0, 0)); + test_sparse(|| AHasher::new_with_keys(1, 2)); + } } ///Basic sanity tests of the cypto properties of aHash. @@ -480,4 +508,10 @@ mod aes_tests { fn aes_length_extension() { test_length_extension(|a, b| AHasher::test_with_keys(a, b)); } + + #[test] + fn aes_no_sparse_collisions() { + test_sparse(|| AHasher::test_with_keys(0, 0)); + test_sparse(|| AHasher::test_with_keys(1, 2)); + } } diff --git a/third_party/rust/ahash/src/random_state.rs b/third_party/rust/ahash/src/random_state.rs index c3628bf145..9ac2f3ec43 100644 --- a/third_party/rust/ahash/src/random_state.rs +++ b/third_party/rust/ahash/src/random_state.rs @@ -29,8 +29,13 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std as alloc; +#[cfg(feature = "atomic-polyfill")] +use atomic_polyfill as atomic; +#[cfg(not(feature = "atomic-polyfill"))] +use core::sync::atomic; + use alloc::boxed::Box; -use core::sync::atomic::{AtomicUsize, Ordering}; +use atomic::{AtomicUsize, Ordering}; #[cfg(not(all(target_arch = "arm", target_os = "none")))] use once_cell::race::OnceBox; |