Skip to main content

rapidhash/v3/
mod.rs

1//! Portable hashing: rapidhash V3 algorithm.
2
3mod rapid_const;
4#[cfg(any(feature = "std", docsrs))]
5mod rapid_file;
6mod seed;
7mod rapid_stream_hasher;
8
9#[doc(inline)]
10pub use rapid_const::*;
11
12#[doc(inline)]
13#[cfg(any(feature = "std", docsrs))]
14pub use rapid_file::*;
15
16#[doc(inline)]
17pub use rapid_stream_hasher::*;
18
19#[doc(inline)]
20pub use seed::*;
21
22#[cfg(test)]
23mod tests {
24    extern crate std;
25
26    use rand::Rng;
27    use crate::util::macros::{compare_to_c, flip_bit_trial};
28    use super::*;
29
30    flip_bit_trial!(flip_bit_trial_v3, rapidhash_v3_inline::<true, false, false>);
31    flip_bit_trial!(flip_bit_trial_v3_micro, rapidhash_v3_micro_inline::<true, false>);
32    flip_bit_trial!(flip_bit_trial_v3_nano, rapidhash_v3_nano_inline::<true, false>);
33    compare_to_c!(compare_to_c_v3, rapidhash_v3_inline::<true, false, false>, rapidhash_v3_inline::<true, true, false>, rapidhashcc_v3);
34    compare_to_c!(compare_to_c_v3_micro, rapidhash_v3_micro_inline::<true, false>, rapidhash_v3_micro_inline::<true, false>, rapidhashcc_v3_micro);
35    compare_to_c!(compare_to_c_v3_nano, rapidhash_v3_nano_inline::<true, false>, rapidhash_v3_nano_inline::<true, false>, rapidhashcc_v3_nano);
36
37    /// Compare the main rapidhash version matches micro (80 btyes) and nano (48 bytes) up to
38    /// the expected length.
39    #[test]
40    fn compare_micro_nano_v3() {
41        // test zero-length input
42        let hash_v3 = rapidhash_v3_inline::<true, false, false>(&[], &DEFAULT_RAPID_SECRETS);
43        let hash_micro = rapidhash_v3_micro_inline::<true, false>(&[], &DEFAULT_RAPID_SECRETS);
44        let hash_nano = rapidhash_v3_nano_inline::<true, false>(&[], &DEFAULT_RAPID_SECRETS);
45        assert_eq!(hash_v3, hash_micro, "Mismatch with micro on zero length input");
46        assert_eq!(hash_v3, hash_nano, "Mismatch with nano on zero length input");
47
48        for len in 0..=82 {
49            let mut data = std::vec![0; len];
50            rand::rng().fill(&mut data[..]);
51
52            for byte in 0..len {
53                for bit in 0..8 {
54                    let mut data = data.clone();
55                    data[byte] ^= 1 << bit;
56
57                    let hash_v3 = rapidhash_v3_inline::<true, false, false>(&data, &DEFAULT_RAPID_SECRETS);
58                    let hash_micro = rapidhash_v3_micro_inline::<true, false>(&data, &DEFAULT_RAPID_SECRETS);
59                    let hash_nano = rapidhash_v3_nano_inline::<true, false>(&data, &DEFAULT_RAPID_SECRETS);
60
61                    if len <= 80 {
62                        assert_eq!(hash_v3, hash_micro, "Mismatch with mico on input {} byte {} bit {}", len, byte, bit);
63                    } else {
64                        assert_ne!(hash_v3, hash_micro, "Micro should mismatch on input {} byte {} bit {}", len, byte, bit);
65                    }
66
67                    if len <= 48 {
68                        assert_eq!(hash_v3, hash_nano, "Mismatch with nano on input {} byte {} bit {}", len, byte, bit);
69                    } else {
70                        assert_ne!(hash_v3, hash_nano, "Nano should mismatch on input {} byte {} bit {}", len, byte, bit);
71                    }
72                }
73            }
74        }
75    }
76}