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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! # Meow Hasher
//!
//! An implementation of the [Meow hasher][meow-hasher] in native Rust providing
//! the [Digest][Digest] trait.
//!
//! The [Meow hasher][meow-hasher] is a hashing algorithm designed for hashing
//! large data sets (on the order of gigabytes) very efficiently. It takes about
//! 60 milliseconds to hash 1 gigabyte of data on an i7-7700 at 2.8GHz.
//!
//! It is *not* cryptographically secure.
//!
//! This implementation currently only supports the `x86`, `x86_64` and
//! `aarch64` architectures.
//!
//! [meow-hasher]: https://mollyrocket.com/meowhash
//! [Digest]: https://docs.rs/digest/latest/digest/trait.Digest.html

#![no_std]
#![forbid(rust_2018_idioms)]
#![deny(nonstandard_style)]
#![warn(unreachable_pub, missing_docs)]
#![cfg_attr(target_arch = "aarch64", feature(aarch64_target_feature, stdsimd))]

use core::mem;
use core::ptr;
use digest::generic_array::{
    typenum::{consts::*, Unsigned},
    GenericArray,
};
use digest::Digest;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
mod x86;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use x86::{aes_merge, aes_rotate, Simd128};

#[cfg(target_arch = "aarch64")]
mod arm;
#[cfg(target_arch = "aarch64")]
use arm::{aes_merge, aes_rotate, Simd128};

#[derive(Clone, Copy)]
struct MeowLane {
    l0: Simd128,
    l1: Simd128,
    l2: Simd128,
    l3: Simd128,
}

#[inline]
unsafe fn aes_rotate_lanes(a: &mut MeowLane, b: &mut [MeowLane]) {
    aes_rotate(a, &mut b[0]);
    aes_rotate(a, &mut b[1]);
    aes_rotate(a, &mut b[2]);
    aes_rotate(a, &mut b[3]);
}

#[inline]
unsafe fn aes_merge_lanes(a: &mut [MeowLane], b: &[MeowLane]) {
    aes_merge(&mut a[0], &b[0]);
    aes_merge(&mut a[1], &b[1]);
    aes_merge(&mut a[2], &b[2]);
    aes_merge(&mut a[3], &b[3]);
}

impl MeowLane {
    fn new(seed: u128) -> Self {
        unsafe { core::mem::transmute([seed, seed, seed, seed]) }
    }

    fn as_bytes(&self) -> &[u8] {
        unsafe {
            core::slice::from_raw_parts(
                self as *const _ as *const u8,
                core::mem::size_of::<MeowLane>(),
            )
        }
    }
}
/// Meow hasher.
///
/// An implementation of the [Meow hasher][meow-hasher] providing the
/// [Digest][Digest] trait.
///
/// [meow-hasher]: https://mollyrocket.com/meowhash
/// [Digest]: https://docs.rs/digest/latest/digest/trait.Digest.html
pub struct MeowHasher {
    lanes: [MeowLane; 4],
    buf: [MeowLane; 4],
    index: usize,
    seed: u128,
}

impl Default for MeowHasher {
    fn default() -> Self {
        Self::new()
    }
}

impl MeowHasher {
    /// Compute the hash of a chunk of data directly using the provided seed.
    pub fn digest_with_seed(seed: u128, data: &[u8]) -> GenericArray<u8, U64> {
        let mut hasher = MeowHasher::with_seed(seed);
        hasher.input(&data);
        hasher.result()
    }

    /// Create a new hasher instance with the provided seed.
    pub fn with_seed(seed: u128) -> Self {
        MeowHasher {
            lanes: [
                MeowLane::new(seed),
                MeowLane::new(seed),
                MeowLane::new(seed),
                MeowLane::new(seed),
            ],
            buf: [
                MeowLane::new(seed),
                MeowLane::new(seed),
                MeowLane::new(seed),
                MeowLane::new(seed),
            ],
            index: 0,
            seed,
        }
    }

    #[inline]
    fn block(&self) -> [MeowLane; 4] {
        [
            MeowLane::new(self.seed),
            MeowLane::new(self.seed),
            MeowLane::new(self.seed),
            MeowLane::new(self.seed),
        ]
    }

    #[inline]
    fn block_size() -> usize {
        mem::size_of::<[MeowLane; 4]>()
    }

    #[inline]
    fn left(&self) -> usize {
        Self::block_size() - self.index
    }

    #[inline]
    unsafe fn buf_ptr(&mut self) -> *mut u8 {
        (self.buf.as_ptr() as *mut u8).add(self.index)
    }

    unsafe fn feed(&mut self, data: &[u8]) {
        let mut src_ptr = data.as_ptr();
        let mut src_left = data.len();
        let mut buf_left = self.left();

        while src_left >= buf_left {
            ptr::copy_nonoverlapping(src_ptr, self.buf_ptr(), buf_left);

            aes_merge_lanes(&mut self.lanes, &self.buf);

            src_left -= buf_left;
            src_ptr = src_ptr.add(buf_left);
            buf_left = Self::block_size();
            self.index = 0;
        }

        if src_left > 0 {
            ptr::copy_nonoverlapping(src_ptr, self.buf_ptr(), src_left);
            self.index += src_left;
        }
    }

    unsafe fn finalise(&mut self) -> MeowLane {
        let mut r0 = MeowLane::new(self.seed);
        let empty = MeowLane::new(self.seed);

        if self.index > 0 {
            // Pad the last block if needed and merge it.
            let mut empty_block = self.block();
            let src_ptr = (&mut empty_block as *mut _ as *mut u8).add(self.index);
            let dest_ptr = self.buf_ptr();
            ptr::copy_nonoverlapping(src_ptr, dest_ptr, self.left());
            aes_merge_lanes(&mut self.lanes, &self.buf);
        }

        aes_rotate_lanes(&mut r0, &mut self.lanes);
        aes_rotate_lanes(&mut r0, &mut self.lanes);
        aes_rotate_lanes(&mut r0, &mut self.lanes);
        aes_rotate_lanes(&mut r0, &mut self.lanes);

        aes_merge(&mut r0, &empty);
        aes_merge(&mut r0, &empty);
        aes_merge(&mut r0, &empty);
        aes_merge(&mut r0, &empty);
        aes_merge(&mut r0, &empty);

        r0
    }
}

impl Digest for MeowHasher {
    type OutputSize = U64;

    fn new() -> Self {
        Self::with_seed(0)
    }

    fn input<B: AsRef<[u8]>>(&mut self, data: B) {
        unsafe { self.feed(data.as_ref()) }
    }

    fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self {
        self.input(data);
        self
    }

    fn result(mut self) -> GenericArray<u8, Self::OutputSize> {
        GenericArray::clone_from_slice(unsafe { self.finalise() }.as_bytes())
    }

    fn reset(&mut self) {
        *self = Self::with_seed(self.seed);
    }

    fn result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> {
        let result = unsafe { self.finalise() };
        self.reset();
        GenericArray::clone_from_slice(result.as_bytes())
    }

    fn output_size() -> usize {
        Self::OutputSize::USIZE
    }

    /// Compute the hash of a chunk of data directly.
    fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> {
        let mut hasher = MeowHasher::with_seed(0);
        hasher.input(&data);
        hasher.result()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use proptest::collection::vec;
    use proptest::num::{u128, u8, usize};
    use proptest::proptest;

    proptest! {
        #[test]
        fn hash_same_data(seed in u128::ANY, blob in vec(u8::ANY, 0..65536)) {
            let mut hasher = MeowHasher::with_seed(seed);
            hasher.input(&blob);
            let hash1 = hasher.result();
            let hash2 = MeowHasher::digest_with_seed(seed, &blob);
            // Two hashes of the same data are equal
            assert_eq!(hash1, hash2);
        }

        #[test]
        fn hash_different_seeds(seed in u128::ANY, blob in vec(u8::ANY, 0..65536)) {
            let hash1 = MeowHasher::digest_with_seed(seed, &blob);
            let hash2 = MeowHasher::digest_with_seed(seed ^ 1, &blob);
            // Hashes with different seeds are not equal
            assert_ne!(hash1, hash2);
        }

        #[test]
        fn hash_different_data(seed in u128::ANY, mut blob in vec(u8::ANY, 1..65536), modify in usize::ANY) {
            let hash1 = MeowHasher::digest_with_seed(seed, &blob);
            let modify = modify % blob.len();
            blob[modify] ^= 1;
            let hash2 = MeowHasher::digest_with_seed(seed, &blob);
            // A blob with one bit modified hashes differently
            assert_ne!(hash1, hash2);
        }
    }
}