fmt: Run cargo fmt

parent b18a90ca
Pipeline #8765 failed with stage
in 9 seconds
......@@ -68,7 +68,7 @@ impl State {
let mut excessive = buf.len() as usize + buf.as_ptr() as usize - end_ptr as usize;
// Handle the excessive bytes.
match excessive {
0 => {},
0 => {}
1..=7 => {
// 1 or more excessive.
......@@ -77,7 +77,7 @@ impl State {
// Diffuse.
a = helper::diffuse(a);
},
}
8 => {
// 8 bytes excessive.
......@@ -86,7 +86,7 @@ impl State {
// Diffuse.
a = helper::diffuse(a);
},
}
9..=15 => {
// More than 8 bytes excessive.
......@@ -100,15 +100,14 @@ impl State {
// Diffuse.
a = helper::diffuse(a);
b = helper::diffuse(b);
},
}
16 => {
// 16 bytes excessive.
// Mix in the partial block.
a = helper::diffuse(a ^ helper::read_u64(ptr));
b = helper::diffuse(b ^ helper::read_u64(ptr.offset(8)));
},
}
17..=23 => {
// 16 bytes or more excessive.
......@@ -124,7 +123,7 @@ impl State {
a = helper::diffuse(a);
b = helper::diffuse(b);
c = helper::diffuse(c);
},
}
24 => {
// 24 bytes excessive.
......@@ -137,7 +136,7 @@ impl State {
a = helper::diffuse(a);
b = helper::diffuse(b);
c = helper::diffuse(c);
},
}
_ => {
// More than 24 bytes excessive.
......@@ -209,7 +208,13 @@ impl State {
/// Finalize the state.
#[inline]
pub fn finalize(self) -> u64 {
let State { written, mut a, b, mut c, d } = self;
let State {
written,
mut a,
b,
mut c,
d,
} = self;
// XOR the states together. Even though XOR is commutative, it doesn't matter, because the
// state vector's initial components are mutually distinct, and thus swapping even and odd
......@@ -247,7 +252,13 @@ impl State {
///
/// The seed of this hash function is prechosen.
pub fn hash(buf: &[u8]) -> u64 {
hash_seeded(buf, 0x16f11fe89b0d677c, 0xb480a793d8e6c86c, 0x6fe2e5aaf078ebc9, 0x14f994a4c5259381)
hash_seeded(
buf,
0x16f11fe89b0d677c,
0xb480a793d8e6c86c,
0x6fe2e5aaf078ebc9,
0x14f994a4c5259381,
)
}
/// Hash some buffer according to a chosen seed.
......@@ -277,11 +288,26 @@ mod tests {
fn hash_match(a: &[u8]) {
assert_eq!(hash(a), reference::hash(a));
assert_eq!(hash_seeded(a, 1, 1, 1, 1), reference::hash_seeded(a, 1, 1, 1, 1));
assert_eq!(hash_seeded(a, 500, 2873, 2389, 9283), reference::hash_seeded(a, 500, 2873, 2389, 9283));
assert_eq!(hash_seeded(a, 238945723984, 872894734, 239478243, 28937498234), reference::hash_seeded(a, 238945723984, 872894734, 239478243, 28937498234));
assert_eq!(hash_seeded(a, !0, !0, !0, !0), reference::hash_seeded(a, !0, !0, !0, !0));
assert_eq!(hash_seeded(a, 0, 0, 0, 0), reference::hash_seeded(a, 0, 0, 0, 0));
assert_eq!(
hash_seeded(a, 1, 1, 1, 1),
reference::hash_seeded(a, 1, 1, 1, 1)
);
assert_eq!(
hash_seeded(a, 500, 2873, 2389, 9283),
reference::hash_seeded(a, 500, 2873, 2389, 9283)
);
assert_eq!(
hash_seeded(a, 238945723984, 872894734, 239478243, 28937498234),
reference::hash_seeded(a, 238945723984, 872894734, 239478243, 28937498234)
);
assert_eq!(
hash_seeded(a, !0, !0, !0, !0),
reference::hash_seeded(a, !0, !0, !0, !0)
);
assert_eq!(
hash_seeded(a, 0, 0, 0, 0),
reference::hash_seeded(a, 0, 0, 0, 0)
);
}
#[test]
......@@ -302,7 +328,6 @@ mod tests {
hash_match(&buf);
}
#[test]
fn position_depedent() {
let mut buf1 = [0; 4098];
......@@ -353,7 +378,16 @@ mod tests {
state.push(!0);
state.push(0);
assert_eq!(hash_seeded(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0], 1, 2, 3, 4), state.finalize());
assert_eq!(
hash_seeded(
&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0],
1,
2,
3,
4
),
state.finalize()
);
}
#[test]
......@@ -363,6 +397,15 @@ mod tests {
state.push(0);
state.pop(0);
assert_eq!(hash_seeded(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], 1, 2, 3, 4), state.finalize());
assert_eq!(
hash_seeded(
&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
1,
2,
3,
4
),
state.finalize()
);
}
}
......@@ -23,7 +23,7 @@ pub fn read_int(buf: &[u8]) -> u64 {
let b = *ptr.offset(2) as u64;
a | (b << 16)
},
}
// u32.
4 => (ptr as *const u32).read_unaligned().to_le() as u64,
// u32 + u8.
......@@ -32,14 +32,14 @@ pub fn read_int(buf: &[u8]) -> u64 {
let b = *ptr.offset(4) as u64;
a | (b << 32)
},
}
// u32 + u16.
6 => {
let a = (ptr as *const u32).read_unaligned().to_le() as u64;
let b = (ptr.offset(4) as *const u16).read_unaligned().to_le() as u64;
a | (b << 32)
},
}
// u32 + u16 + u8.
7 => {
let a = (ptr as *const u32).read_unaligned().to_le() as u64;
......@@ -47,7 +47,7 @@ pub fn read_int(buf: &[u8]) -> u64 {
let c = *ptr.offset(6) as u64;
a | (b << 32) | (c << 48)
},
}
_ => 0,
}
}
......
......@@ -18,10 +18,10 @@ mod tests {
#[test]
fn hash_write_trait() {
let reader: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00];
let reader: &[u8] = &[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
let mut hasher = SeaHasher::new();
// io::copy consumes the mutable reader -> cloning the buffer
let _ = io::copy(&mut reader.clone(), &mut hasher).unwrap();
......
......@@ -159,9 +159,9 @@ extern crate core as std;
pub use buffer::{hash, hash_seeded, State};
pub use stream::SeaHasher;
pub mod reference;
mod buffer;
mod helper;
pub mod reference;
mod stream;
#[cfg(feature = "use_std")]
......
......@@ -35,8 +35,11 @@ use helper;
/// Read an integer in little-endian.
fn read_int(int: &[u8]) -> u64 {
debug_assert!(int.len() <= 8, "The buffer length of the integer must be less than or equal to \
the one of an u64.");
debug_assert!(
int.len() <= 8,
"The buffer length of the integer must be less than or equal to \
the one of an u64."
);
// Start at 0.
let mut x = 0;
......@@ -90,7 +93,7 @@ impl State {
self.a ^ self.b ^ self.c ^ self.d
// We XOR in the number of written bytes to make it zero-sensitive when excessive bytes
// are written (0u32.0u8 ≠ 0u16.0u8).
^ total as u64
^ total as u64,
)
}
......@@ -118,7 +121,7 @@ pub fn hash(buf: &[u8]) -> u64 {
0x16f11fe89b0d677c,
0xb480a793d8e6c86c,
0x6fe2e5aaf078ebc9,
0x14f994a4c5259381
0x14f994a4c5259381,
)
}
......
......@@ -18,7 +18,12 @@ pub struct SeaHasher {
impl Default for SeaHasher {
fn default() -> SeaHasher {
SeaHasher::with_seeds(0x16f11fe89b0d677c, 0xb480a793d8e6c86c, 0x6fe2e5aaf078ebc9, 0x14f994a4c5259381)
SeaHasher::with_seeds(
0x16f11fe89b0d677c,
0xb480a793d8e6c86c,
0x6fe2e5aaf078ebc9,
0x14f994a4c5259381,
)
}
}
......@@ -69,7 +74,7 @@ impl SeaHasher {
// We've done the existing tail, now just do the rest in chunks of 4 x u64.
ptr = ptr.offset(copied as isize);
let end_ptr = ptr.offset((bytes.len()-copied) as isize & !0x1F);
let end_ptr = ptr.offset((bytes.len() - copied) as isize & !0x1F);
while end_ptr > ptr {
self.state.0 = helper::diffuse(self.state.0 ^ helper::read_u64(ptr));
self.state.1 = helper::diffuse(self.state.1 ^ helper::read_u64(ptr.offset(8)));
......@@ -83,24 +88,26 @@ impl SeaHasher {
match excessive {
0 => {
// input was a multiple of 4 x u64 bytes long; no new tail bytes.
},
}
1..=7 => {
self.tail = helper::read_int(slice::from_raw_parts(ptr as *const u8, excessive));
self.tail =
helper::read_int(slice::from_raw_parts(ptr as *const u8, excessive));
self.ntail = excessive;
// self.written does not need to be updated as we only gathered self.tail
// bytes after larger chunks.
},
}
8 => {
self.push(helper::read_u64(ptr));
// self.written is updated by self.push
},
}
9..=15 => {
self.push(helper::read_u64(ptr));
excessive -= 8;
self.tail = helper::read_int(slice::from_raw_parts(ptr.offset(8), excessive));
self.tail =
helper::read_int(slice::from_raw_parts(ptr.offset(8), excessive));
self.ntail = excessive;
// self.written is updated by self.push
},
}
16 => {
let a = helper::diffuse(self.state.0 ^ helper::read_u64(ptr));
let b = helper::diffuse(self.state.1 ^ helper::read_u64(ptr.offset(8)));
......@@ -110,7 +117,7 @@ impl SeaHasher {
self.state.2 = a;
self.state.3 = b;
self.written += 16;
},
}
17..=23 => {
let a = helper::diffuse(self.state.0 ^ helper::read_u64(ptr));
let b = helper::diffuse(self.state.1 ^ helper::read_u64(ptr.offset(8)));
......@@ -120,10 +127,11 @@ impl SeaHasher {
self.state.2 = a;
self.state.3 = b;
excessive -= 16;
self.tail = helper::read_int(slice::from_raw_parts(ptr.offset(16), excessive));
self.tail =
helper::read_int(slice::from_raw_parts(ptr.offset(16), excessive));
self.ntail = excessive;
self.written += 16;
},
}
24 => {
let a = helper::diffuse(self.state.0 ^ helper::read_u64(ptr));
let b = helper::diffuse(self.state.1 ^ helper::read_u64(ptr.offset(8)));
......@@ -133,7 +141,7 @@ impl SeaHasher {
self.state.2 = b;
self.state.3 = c;
self.written += 24;
},
}
_ => {
let a = helper::diffuse(self.state.0 ^ helper::read_u64(ptr));
let b = helper::diffuse(self.state.1 ^ helper::read_u64(ptr.offset(8)));
......@@ -143,7 +151,8 @@ impl SeaHasher {
self.state.2 = b;
self.state.3 = c;
excessive -= 24;
self.tail = helper::read_int(slice::from_raw_parts(ptr.offset(24), excessive));
self.tail =
helper::read_int(slice::from_raw_parts(ptr.offset(24), excessive));
self.ntail = excessive;
self.written += 24;
}
......@@ -161,7 +170,9 @@ impl Hasher for SeaHasher {
} else {
self.state.0
};
helper::diffuse(a ^ self.state.1 ^ self.state.2 ^ self.state.3 ^ self.written + self.ntail as u64)
helper::diffuse(
a ^ self.state.1 ^ self.state.2 ^ self.state.3 ^ self.written + self.ntail as u64,
)
}
fn write(&mut self, bytes: &[u8]) {
......@@ -217,25 +228,22 @@ mod tests {
#[test]
fn chunked_equiv() {
let test_buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00];
let test_buf: &[u8] = &[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
let mut stream_hasher1 = SeaHasher::default();
Hasher::write(&mut stream_hasher1, test_buf);
let mut stream_hasher2 = SeaHasher::default();
Hasher::write(&mut stream_hasher2, &test_buf[..8]);
Hasher::write(&mut stream_hasher2, &test_buf[8..]);
let mut stream_hasher3 = SeaHasher::default();
Hasher::write(&mut stream_hasher3, &test_buf[..3]);
Hasher::write(&mut stream_hasher3, &test_buf[3..]);
let mut stream_hasher4 = SeaHasher::default();
Hasher::write_u16(&mut stream_hasher4, 0xffff);
Hasher::write_u16(&mut stream_hasher4, 0xffff);
......@@ -249,16 +257,27 @@ mod tests {
#[test]
fn match_optimized() {
let test_buf: &[u8] = &[0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00];
let mut sea_hasher = SeaHasher::with_seeds(0xe7b0c93ca8525013, 0x011d02b854ae8182, 0x7bcc5cf9c39cec76, 0xfa336285d102d083);
let test_buf: &[u8] = &[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
let mut sea_hasher = SeaHasher::with_seeds(
0xe7b0c93ca8525013,
0x011d02b854ae8182,
0x7bcc5cf9c39cec76,
0xfa336285d102d083,
);
sea_hasher.write(test_buf);
let stream_hash = sea_hasher.finish();
let buffer_hash = hash_seeded(test_buf, 0xe7b0c93ca8525013, 0x011d02b854ae8182, 0x7bcc5cf9c39cec76, 0xfa336285d102d083);
let buffer_hash = hash_seeded(
test_buf,
0xe7b0c93ca8525013,
0x011d02b854ae8182,
0x7bcc5cf9c39cec76,
0xfa336285d102d083,
);
assert_eq!(buffer_hash, stream_hash)
}
......
......@@ -7,7 +7,8 @@ use std::hash::Hasher;
fn hash_chunking_vs_not() {
// originally from https://gitlab.redox-os.org/redox-os/seahash/issues/5
let c1: &[u8] = b"This hashing algorithm was extracted from the Rustc compiler.";
let c2: &[u8] = b" This is the same hashing algoirthm used for some internal operations in FireFox.";
let c2: &[u8] =
b" This is the same hashing algoirthm used for some internal operations in FireFox.";
let c3: &[u8] = b" The strength of this algorithm is in hashing 8 bytes at a time on 64-bit platforms, where the FNV algorithm works on one byte at a time.";
let mut h1 = H::default();
......@@ -43,7 +44,8 @@ fn hash_chunking_vs_not() {
fn test_different_chunk_sizes() {
let v = {
let c1: &[u8] = b"This hashing algorithm was extracted from the Rustc compiler.";
let c2: &[u8] = b" This is the same hashing algoirthm used for some internal operations in FireFox.";
let c2: &[u8] =
b" This is the same hashing algoirthm used for some internal operations in FireFox.";
let c3: &[u8] = b" The strength of this algorithm is in hashing 8 bytes at a time on 64-bit platforms, where the FNV algorithm works on one byte at a time.";
[c1, c2, c3].concat()
......
......@@ -4,11 +4,11 @@ extern crate seahash;
extern crate quickcheck;
use quickcheck::TestResult;
use seahash::SeaHasher;
use seahash::hash;
use seahash::reference::hash as reference;
use seahash::SeaHasher;
use std::hash::Hasher;
use std::num::{NonZeroUsize, NonZeroU8};
use std::num::{NonZeroU8, NonZeroUsize};
quickcheck! {
#[cfg_attr(miri, ignore)] // very slow to run on miri
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment