Skip to main content

rapidhash/v1/
mod.rs

1//! Portable hashing: rapidhash V1 algorithm.
2//!
3//! For new code, please use [`crate::v3`] instead, as it is a superior hashing algorithm.
4
5mod rapid_const;
6#[cfg(any(feature = "std", docsrs))]
7mod rapid_file;
8mod seed;
9
10#[doc(inline)]
11pub use rapid_const::*;
12#[doc(inline)]
13#[cfg(any(feature = "std", docsrs))]
14pub use rapid_file::*;
15#[doc(inline)]
16pub use seed::*;
17
18#[cfg(test)]
19mod tests {
20    #![allow(deprecated)]
21
22    extern crate std;
23
24    use crate::util::macros::{compare_to_c, flip_bit_trial};
25    use super::*;
26
27    flip_bit_trial!(flip_bit_trial_v1, rapidhash_v1_inline::<true, false, false, false>);
28    flip_bit_trial!(flip_bit_trial_v1_bug, rapidhash_v1_inline::<true, false, false, true>);
29    compare_to_c!(compare_to_c_v1, rapidhash_v1_inline::<true, false, false, false>, rapidhash_v1_inline::<true, true, false, false>, rapidhashcc_v1);
30
31    #[test]
32    fn test_v1_bug() {
33        fn rapidhash_bug(data: &str) -> u64 {
34            rapidhash_v1_inline::<true, false, false, true>(data.as_bytes(), &DEFAULT_RAPID_SECRETS)
35        }
36
37        // The v1.x.x bug was for the 48-byte case
38        // The v2.x.x attempted fix ended up not hashing a bunch of data beyond 48 bytes... :facepalm:
39        assert_eq!(5006746792674864303, rapidhash_bug("\n"));
40        assert_eq!(4933522537766704430, rapidhash_bug("abcdef\n"));
41        assert_eq!(3345456103814863532, rapidhash_bug("abcdefghijklmnopqrstuvwxyz12345678901234567890\n"));
42        assert_eq!(8825074939507110130, rapidhash_bug("abcdefghijklmnopqrstuvwxyz123456789012345678901\n"));
43        assert_eq!(2762901732509801681, rapidhash_bug("abcdefghijklmnopqrstuvwxyz1234567890123456789012\n"));
44        assert_eq!( 934306286158757431, rapidhash_bug("abcdefghijklmnopqrstuvwxyz12345678901234567890123\n"));
45    }
46
47    #[test]
48    fn test_hardcoded_v1() {
49        assert_eq!(6516417773221693515, rapidhash_v1(&[]));
50        assert_eq!(5006746792674864303, rapidhash_v1("\n".as_bytes()));
51        assert_eq!(15965596575264898037, rapidhash_v1("something\n".as_bytes()));
52        // below is 47 bytes, an extra character would hit the V1 bug
53        assert_eq!(10644405912457645442, rapidhash_v1("abcdefghijklmnopqrstuvwxyz01234567890123456789\n".as_bytes()));
54        assert_eq!(7545813847373533788, rapidhash_v1("abcdefghijklmnopqrstuvwxyz012345678901234567890abcdefghijklmnopqrstuvwxyz012345678901234567890abcdefghijklmnopqrstuvwxyz012345678901234567890\n".as_bytes()));
55    }
56}