1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(not(test), no_std)]
#[cfg(all(test, feature = "no_panic"))]
extern crate no_panic;
#[macro_use]
mod convert;
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
mod aes_hash;
mod fallback_hash;
#[cfg(test)]
mod hash_quality_test;
#[cfg(feature = "compile-time-rng")]
use const_random::const_random;
use core::hash::BuildHasher;
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering;
#[cfg(all(test, feature = "no_panic"))]
use no_panic::no_panic;
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes"))]
pub use crate::aes_hash::AHasher;
#[cfg(not(all(any(target_arch = "x86", target_arch = "x86_64"), target_feature = "aes")))]
pub use crate::fallback_hash::AHasher;
const MULTIPLE: u64 = 6364136223846793005;
#[cfg(feature = "compile-time-rng")]
static SEED: AtomicUsize = AtomicUsize::new(const_random!(u64));
#[cfg(not(feature = "compile-time-rng"))]
static SEED: AtomicUsize = AtomicUsize::new(MULTIPLE as usize);
#[cfg(feature = "compile-time-rng")]
impl Default for AHasher {
#[inline]
fn default() -> AHasher {
AHasher::new_with_keys(const_random!(u64), const_random!(u64))
}
}
#[derive(Clone)]
pub struct ABuildHasher {
k0: u64,
k1: u64,
}
impl ABuildHasher {
#[inline]
pub fn new() -> ABuildHasher {
let previous = SEED.load(Ordering::Relaxed) as u64;
let stack_mem_loc = &previous as *const _ as u64;
let current_seed = previous.wrapping_mul(MULTIPLE).wrapping_add(stack_mem_loc).rotate_left(31);
SEED.store(current_seed as usize, Ordering::Relaxed);
return ABuildHasher {
k0: &SEED as *const _ as u64,
k1: current_seed
};
}
}
impl Default for ABuildHasher {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl BuildHasher for ABuildHasher {
type Hasher = AHasher;
#[inline]
fn build_hasher(&self) -> AHasher {
let (k0, k1) = scramble_keys(self.k0, self.k1);
return AHasher::new_with_keys(k0, k1);
}
}
pub(crate) fn scramble_keys(k0: u64, k1: u64) -> (u64, u64) {
let result1 = k0.wrapping_add(k1);
let k1 = k1 ^ k0;
let k0 = k0.rotate_left(24) ^ k1 ^ (k1.wrapping_shl(16));
let result2 = k0.wrapping_add(k1.rotate_left(37));
return (result2, result1);
}
#[cfg(all(test, feature = "no_panic"))]
#[inline(never)]
#[no_panic]
fn hash_test_final(num: i32, string: &str) -> (u64, u64) {
use core::hash::Hasher;
let builder = ABuildHasher::new();
let mut hasher1 = builder.build_hasher();
let mut hasher2 = builder.build_hasher();
hasher1.write_i32(num);
hasher2.write(string.as_bytes());
(hasher1.finish(), hasher2.finish())
}
#[cfg(test)]
mod test {
use crate::convert::Convert;
use crate::*;
use core::hash::BuildHasherDefault;
use std::collections::HashMap;
#[cfg(feature = "no_panic")]
#[inline(never)]
fn hash_test_final_wrapper(num: i32, string: &str) {
hash_test_final(num, string);
}
#[cfg(feature = "no_panic")]
#[test]
fn test_no_panic() {
hash_test_final_wrapper(2, "");
}
#[cfg(feature = "compile-time-rng")]
#[test]
fn test_default_builder() {
let mut map = HashMap::<u32, u64, BuildHasherDefault<AHasher>>::default();
map.insert(1, 3);
}
#[test]
fn test_builder() {
let mut map = HashMap::<u32, u64, ABuildHasher>::default();
map.insert(1, 3);
}
#[test]
fn test_conversion() {
let input: &[u8] = "dddddddd".as_bytes();
let bytes: u64 = as_array!(input, 8).convert();
assert_eq!(bytes, 0x6464646464646464);
}
#[test]
fn test_ahasher_construction() {
let _ = AHasher::new_with_keys(1245, 5678);
}
}