Commit b82dcef0 authored by Tom Almeida's avatar Tom Almeida 📚

Merge branch 'fix_chunking' into 'master'

stream: Fix chunking sensitivity.

Closes #1

See merge request !2
parents c1cf7cff 703e3f06
Pipeline #6924 passed with stage
in 18 seconds
......@@ -2,6 +2,9 @@
extern crate test;
extern crate seahash;
extern crate core;
use core::hash::Hasher;
#[bench]
fn gigabyte(b: &mut test::Bencher) {
......@@ -17,3 +20,17 @@ fn gigabyte(b: &mut test::Bencher) {
x
})
}
#[bench]
fn gigabyte_stream(b: &mut test::Bencher) {
b.iter(|| {
let mut buf = [15;4096];
let mut hasher = seahash::SeaHasher::default();
for _ in 0..250_000 {
Hasher::write(&mut hasher,&buf);
buf[0] += buf[0].wrapping_add(1);
}
hasher.finish()
})
}
......@@ -69,7 +69,7 @@ impl State {
// Handle the excessive bytes.
match excessive {
0 => {},
1...7 => {
1..=7 => {
// 1 or more excessive.
// Write the last excessive bytes (<8 bytes).
......@@ -87,7 +87,7 @@ impl State {
// Diffuse.
a = helper::diffuse(a);
},
9...15 => {
9..=15 => {
// More than 8 bytes excessive.
// Mix in the partial block.
......@@ -106,14 +106,10 @@ impl State {
// 16 bytes excessive.
// Mix in the partial block.
a ^= helper::read_u64(ptr);
b ^= helper::read_u64(ptr.offset(8));
// Diffuse.
a = helper::diffuse(a);
b = helper::diffuse(b);
a = helper::diffuse(a ^ helper::read_u64(ptr));
b = helper::diffuse(b ^ helper::read_u64(ptr.offset(8)));
},
17...23 => {
17..=23 => {
// 16 bytes or more excessive.
// Mix in the partial block.
......
......@@ -73,7 +73,7 @@ pub unsafe fn read_u64(ptr: *const u8) -> u64 {
///
/// This is a bijective function emitting chaotic behavior. Such functions are used as building
/// blocks for hash functions.
pub fn diffuse(mut x: u64) -> u64 {
pub const fn diffuse(mut x: u64) -> u64 {
// These are derived from the PCG RNG's round. Thanks to @Veedrac for proposing this. The basic
// idea is that we use dynamic shifts, which are determined by the input itself. The shift is
// chosen by the higher bits, which means that changing those flips the lower bits, which
......@@ -89,7 +89,7 @@ pub fn diffuse(mut x: u64) -> u64 {
}
/// Reverse the `diffuse` function.
pub fn undiffuse(mut x: u64) -> u64 {
pub const fn undiffuse(mut x: u64) -> u64 {
// 0x2f72b4215a3d8caf is the modular multiplicative inverse of the constant used in `diffuse`.
x = x.wrapping_mul(0x2f72b4215a3d8caf);
......
This diff is collapsed.
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