Skip to content

Commit

Permalink
Hasher::write_u*() are expected keep the native endianness
Browse files Browse the repository at this point in the history
So that write_u32(42) will produce different outputs on big and little
endian targets.

I couldn't even find that behavior properly documented. But this is
what the default trait implementations do.

This is absolutely awful. I don't even understand why they chose to do
that. Anyway, like a lot of things in Rust, we can just shut up and
comply.
  • Loading branch information
jedisct1 committed Aug 22, 2023
1 parent e085f93 commit e5264da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
12 changes: 3 additions & 9 deletions src/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,6 @@ impl<S: Sip> Hasher<S> {
// The hashing of multi-byte integers depends on endianness. E.g.:
// - little-endian: `write_u32(0xDDCCBBAA)` == `write([0xAA, 0xBB, 0xCC, 0xDD])`
// - big-endian: `write_u32(0xDDCCBBAA)` == `write([0xDD, 0xCC, 0xBB, 0xAA])`
//
// This function does the right thing for little-endian hardware. On
// big-endian hardware `x` must be byte-swapped first to give the right
// behaviour. After any byte-swapping, the input must be zero-extended to
// 64-bits. The caller is responsible for the byte-swapping and
// zero-extension.
#[inline]
fn short_write<T>(&mut self, _x: T, x: u64) {
let size = mem::size_of::<T>();
Expand Down Expand Up @@ -443,7 +437,7 @@ impl hash::Hasher for SipHasher24 {
impl<S: Sip> hash::Hasher for Hasher<S> {
#[inline]
fn write_usize(&mut self, i: usize) {
self.short_write(i, i.to_le() as u64);
self.short_write(i, i as u64);
}

#[inline]
Expand All @@ -453,12 +447,12 @@ impl<S: Sip> hash::Hasher for Hasher<S> {

#[inline]
fn write_u32(&mut self, i: u32) {
self.short_write(i, i.to_le() as u64);
self.short_write(i, i as u64);
}

#[inline]
fn write_u64(&mut self, i: u64) {
self.short_write(i, i.to_le());
self.short_write(i, i);
}

#[inline]
Expand Down
12 changes: 3 additions & 9 deletions src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,6 @@ impl<S: Sip> Hasher<S> {
// The hashing of multi-byte integers depends on endianness. E.g.:
// - little-endian: `write_u32(0xDDCCBBAA)` == `write([0xAA, 0xBB, 0xCC, 0xDD])`
// - big-endian: `write_u32(0xDDCCBBAA)` == `write([0xDD, 0xCC, 0xBB, 0xAA])`
//
// This function does the right thing for little-endian hardware. On
// big-endian hardware `x` must be byte-swapped first to give the right
// behaviour. After any byte-swapping, the input must be zero-extended to
// 64-bits. The caller is responsible for the byte-swapping and
// zero-extension.
#[inline]
fn short_write<T>(&mut self, _x: T, x: u64) {
let size = mem::size_of::<T>();
Expand Down Expand Up @@ -511,7 +505,7 @@ impl hash::Hasher for SipHasher24 {
impl<S: Sip> hash::Hasher for Hasher<S> {
#[inline]
fn write_usize(&mut self, i: usize) {
self.short_write(i, i.to_le() as u64);
self.short_write(i, i as u64);
}

#[inline]
Expand All @@ -521,12 +515,12 @@ impl<S: Sip> hash::Hasher for Hasher<S> {

#[inline]
fn write_u32(&mut self, i: u32) {
self.short_write(i, i.to_le() as u64);
self.short_write(i, i as u64);
}

#[inline]
fn write_u64(&mut self, i: u64) {
self.short_write(i, i.to_le());
self.short_write(i, i);
}

#[inline]
Expand Down

0 comments on commit e5264da

Please sign in to comment.