From 934ad1a2e94a378c08844157ee7412ada87f0183 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Sat, 16 Sep 2023 10:42:56 +0200 Subject: [PATCH] rust fixups: bad wyhash seeds, no whirlpool 512 bits I couldnt make a 256 slice work --- Hashes.cpp | 8 ++++++++ Hashes.h | 8 ++++++++ README.md | 2 +- main.cpp | 14 ++++++++++++-- rust-hashes/rust_hashes.h | 2 ++ rust-hashes/src/lib.rs | 9 +++++++-- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Hashes.cpp b/Hashes.cpp index dc78a43c..aa6a7597 100644 --- a/Hashes.cpp +++ b/Hashes.cpp @@ -1317,3 +1317,11 @@ void polymur_test ( const void *key, int len, uint32_t seed, void *out) { *(uint64_t*)out = polymur_hash((const uint8_t*)key, (size_t)len, &g_polymurhashparams, (uint64_t)seed); } + +#ifdef RUST_ENABLED +void wyhash_rs_seed_init (uint32_t &seed) { + if (wyhash_rs_badseed (seed)) + seed++; +} +#endif + diff --git a/Hashes.h b/Hashes.h index c2a3ae83..c853ea71 100644 --- a/Hashes.h +++ b/Hashes.h @@ -1367,3 +1367,11 @@ void khashv64_test ( const void *key, int len, uint32_t seed, void *out); extern PolymurHashParams g_polymurhashparams; void polymur_seed_init (size_t &seed); void polymur_test ( const void *key, int len, uint32_t seed, void *out); + +#ifdef RUST_ENABLED +// rust crate wyhash 0.5.0 +static bool wyhash_rs_badseed(const uint32_t seed) { + return (0x52e45cf4 == seed || 0x9d4d660c == seed); +} +void wyhash_rs_seed_init (uint32_t &seed); +#endif diff --git a/README.md b/README.md index 2d8696ca..2c0576f5 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ SMhasher | [asconhashv12_64](doc/asconhashv12_64.txt) | 159.68 | 386.90 | 480.86 (4) |6490 | | | [sha3-256](doc/sha3-256.txt) | 100.58 | 3877.18 |4159.79 (37)| | PerlinNoise | | [sha3-256_64](doc/sha3-256_64.txt) | 100.57 | 3909.00 |4174.63 (16)| | PerlinNoise | -| [FxHash32](doc/FxHash32.txt) | 0.00 | 0.00 | 0.00 (0) | | rust, fixed seed | +| [FxHash32](doc/FxHash32.txt) | 3080.54 | 29.35 | 401.61 (2) | | rust, fixed seed | | [FxHash64](doc/FxHash64.txt) | 0.00 | 0.00 | 0.00 (0) | | rust, fixed seed | | [hasshe2](doc/hasshe2.txt) | 2773.89 | 64.35 | 282.30 (3) | 445 | Permutation,TwoBytes,Zeroes,Seed| | [poly_1_mersenne](doc/poly_1_mersenne.txt) | 1369.21 | 61.59 | 248.86 (4) | 479 | fails most tests | diff --git a/main.cpp b/main.cpp index 87ad5b69..7b757a27 100644 --- a/main.cpp +++ b/main.cpp @@ -760,6 +760,7 @@ HashInfo g_hashes[] = #endif { komihash_test, 64, 0x8157FF6D, "komihash", "komihash 5.7", GOOD, {} }, { polymur_test, 64, 0x4F894810, "polymur", "github.com/orlp/polymur-hash v1", GOOD, {} }, + #ifdef RUST_ENABLED { adler_rs, 32, 0x6E8E8F4D, "Adler_rs", "Adler-32 checksum implementation (used by `zlib`, crate `adler`)", GOOD, {} }, // instable verif, as the library @@ -818,8 +819,9 @@ HashInfo g_hashes[] = { siphash24_rs, 64, 0x57B661ED, "SipHash_2-4_rs", "SipHash with 2 rounds and 4 finalization rounds (crate `siphasher`)", GOOD, {} }, { siphash128_13_rs, 64, 0x8E66D3B7, "SipHash128_1-3_rs", "SipHash128 with 1 round and 3 finalization rounds (crate `siphasher`)", GOOD, {} }, { siphash128_24_rs, 64, 0xA158D579, "SipHash128_2-4_rs", "SipHash128 with 2 rounds and 4 finalization rounds (crate `siphasher`)", GOOD, {} }, -{ whirlpool_rs, 512, 0xF7E0FD50, "Whirlpool_rs", "Whirlpool algorithm (crate `whirlpool`)", GOOD, {} }, -{ wyhash_rs, 64, 0x1196BB84, "WyHash_rs", "WyHash algorithm (crate `wyhash`)", POOR, {} }, +//{ whirlpool_rs, 512, 0xF7E0FD50, "Whirlpool_rs", "Whirlpool algorithm (crate `whirlpool`)", GOOD, {} }, +//{ whirlpool256_rs, 256, 0xF7E0FD50, "Whirlpool256_rs", "Whirlpool algorithm (256 bits) (crate `whirlpool`)", GOOD, {} }, +{ wyhash_rs, 64, 0x1196BB84, "WyHash_rs", "WyHash 0.5.0 (crate `wyhash`)", GOOD, { 0x52e45cf4, 0x9d4d660c } }, { xxhash3_rs, 64, 0x9A636405, "xxHash3_rs", "64-bit xxHash implementation (crate `xxhash-rust`)", POOR, {} }, { xxhash128_rs, 128, 0x88B45661, "xxHash128_rs", "128-bit xxHash implementation (crate `xxhash-rust`)", POOR, {} }, { xxhash32_rs, 32, 0xBA88B743, "xxHash32_rs", "32-bit xxHash implementation (crate `xxhash-rust`)", POOR, {} }, @@ -929,6 +931,10 @@ void Bad_Seed_init (pfHash hash, uint32_t &seed) { else if(hash == wyhash32low) wyhash32low_seed_init(seed); #endif +#ifdef RUST_ENABLED + else if (hash == wyhash_rs) + wyhash_rs_seed_init (seed); +#endif #ifdef HAVE_INT64 else if(hash == mirhash_test) mirhash_seed_init(seed); @@ -962,6 +968,10 @@ bool Hash_Seed_init (pfHash hash, size_t seed) { // VHASH_seed_init(seed); if(hash == tabulation_32_test) tabulation_32_seed_init(seed); +#ifdef RUST_ENABLED + else if (hash == wyhash_rs) + wyhash_rs_seed_init (seed32); +#endif #ifdef __SIZEOF_INT128__ else if(hash == multiply_shift || hash == pair_multiply_shift) multiply_shift_seed_init(seed32); diff --git a/rust-hashes/rust_hashes.h b/rust-hashes/rust_hashes.h index 72ceccd4..a9b57232 100644 --- a/rust-hashes/rust_hashes.h +++ b/rust-hashes/rust_hashes.h @@ -103,6 +103,8 @@ void siphash128_24_rs(const void *blob, int len, unsigned int seed, void *out); void whirlpool_rs(const void *blob, int len, unsigned int seed, void *out); +void whirlpool256_rs(const void *blob, int len, unsigned int seed, void *out); + void wyhash_rs(const void *blob, int len, unsigned int seed, void *out); void xxhash3_rs(const void *blob, int len, unsigned int seed, void *out); diff --git a/rust-hashes/src/lib.rs b/rust-hashes/src/lib.rs index c01e3b12..7bd195ec 100644 --- a/rust-hashes/src/lib.rs +++ b/rust-hashes/src/lib.rs @@ -11,7 +11,7 @@ use digest::{Digest, ExtendableOutput, XofReader}; /// Default domain separation const TURBO_DOMAIN_SEP: u8 = 0x1f; -/// This maco does the following: +/// This macro does the following: /// - Accepts a repeating list of `identifer: function` /// - Creates a C function with that identifier /// - Turn data into a Rust slice @@ -161,7 +161,12 @@ hashes! { siphash128_24_rs: seeded_wrapper(|seed: u32| siphasher::sip128::SipHasher24::new_with_keys(seed.into(), 0) ), - whirlpool_rs: digest_wrapper::, + //whirlpool_rs: digest_wrapper::, + //whirlpool256_rs: |buf, _seed, out: *mut c_void| { + // let mut hash = whirlpool::Whirlpool::new(); + // hash.update(buf); + // hash.finalize_into(unsafe { &mut *out.cast() }); + //}, wyhash_rs: seeded_wrapper(wyhash::WyHash::with_seed), xxhash3_rs: seeded_wrapper(xxhash_rust::xxh3::Xxh3::with_seed), xxhash128_rs: |buf, _seed, out: *mut c_void| unsafe {