/// A [`Hasher`] instance implementing foldhash, optimized for speed. /// /// While you can create one directly with [`FoldHasher::with_seed`], you /// most likely want to use [`RandomState`], [`SeedableRandomState`] or /// [`FixedState`] to create [`FoldHasher`]s. #[derive(Clone)] pubstruct FoldHasher<'a> {
accumulator: u64,
sponge: u128,
sponge_len: u8,
seeds: &'a [u64; 6],
}
impl<'a> FoldHasher<'a> { /// Initializes this [`FoldHasher`] with the given per-hasher seed and /// [`SharedSeed`]. #[inline] pubconstfn with_seed(per_hasher_seed: u64, shared_seed: &'a SharedSeed) -> FoldHasher<'a> {
FoldHasher {
accumulator: per_hasher_seed,
sponge: 0,
sponge_len: 0,
seeds: &shared_seed.seeds,
}
}
#[inline(always)] fn write_num<T: Into<u128>>(&mutself, x: T) { let bits: usize = 8 * core::mem::size_of::<T>(); ifself.sponge_len as usize + bits > 128 { let lo = self.sponge as u64; let hi = (self.sponge >> 64) as u64; self.accumulator = folded_multiply(lo ^ self.accumulator, hi ^ self.seeds[0]); self.sponge = x.into(); self.sponge_len = bits as u8;
} else { self.sponge |= x.into() << self.sponge_len; self.sponge_len += bits as u8;
}
}
}
impl<'a> Hasher for FoldHasher<'a> { #[inline(always)] fn write(&mutself, bytes: &[u8]) { // We perform overlapping reads in the byte hash which could lead to // trivial length-extension attacks. These should be defeated by // adding a length-dependent rotation on our unpredictable seed // which costs only a single cycle (or none if executed with // instruction-level parallelism). let len = bytes.len(); self.accumulator = rotate_right(self.accumulator, len as u32); if len <= 16 { self.accumulator = hash_bytes_short(bytes, self.accumulator, self.seeds);
} else { unsafe { // SAFETY: we checked that the length is > 16 bytes. self.accumulator = hash_bytes_long(bytes, self.accumulator, self.seeds);
}
}
}
#[inline(always)] fn write_u128(&mutself, i: u128) { let lo = i as u64; let hi = (i >> 64) as u64; self.accumulator = folded_multiply(lo ^ self.accumulator, hi ^ self.seeds[0]);
}
#[inline(always)] fn finish(&self) -> u64 { ifself.sponge_len > 0 { let lo = self.sponge as u64; let hi = (self.sponge >> 64) as u64;
folded_multiply(lo ^ self.accumulator, hi ^ self.seeds[0])
} else { self.accumulator
}
}
}
/// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that is randomly initialized. #[derive(Clone, Debug)] pubstruct RandomState {
per_hasher_seed: u64,
global_seed: GlobalSeed,
}
/// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that is randomly /// initialized by default, but can also be initialized with a specific seed. /// /// This can be useful for e.g. testing, but the downside is that this type /// has a size of 16 bytes rather than the 8 bytes [`RandomState`] is. #[derive(Clone, Debug)] pubstruct SeedableRandomState {
per_hasher_seed: u64,
shared_seed: &'static SharedSeed,
}
/// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that always has the same fixed seed. /// /// Not recommended unless you absolutely need determinism. #[derive(Clone, Debug)] pubstruct FixedState {
per_hasher_seed: u64,
}
impl FixedState { /// Creates a [`FixedState`] with the given per-hasher-seed. #[inline(always)] pubconstfn with_seed(per_hasher_seed: u64) -> Self { // XOR with ARBITRARY3 such that with_seed(0) matches default. Self {
per_hasher_seed: per_hasher_seed ^ ARBITRARY3,
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.