diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-10-31 09:25:09 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-10-31 09:25:09 +0000 |
commit | 62fb0aeba5648ab32d4669ed9b9960f3304b6656 (patch) | |
tree | 996572cc1a1ab1a97ee9c25798adf8c4c0a05716 /src/libutil | |
parent | 120d445b92033253b8466e8a52c45f6f2dfcf24c (diff) | |
download | rspamd-62fb0aeba5648ab32d4669ed9b9960f3304b6656.tar.gz rspamd-62fb0aeba5648ab32d4669ed9b9960f3304b6656.zip |
[Minor] Use slightly better fast PRNGs
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/util.c | 52 | ||||
-rw-r--r-- | src/libutil/util.h | 2 |
2 files changed, 36 insertions, 18 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c index 5fe8301fc..5ada3a27e 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -2638,7 +2638,7 @@ rspamd_random_double (void) } -static guint64 xorshifto_seed[2]; +static guint64 xorshifto_seed[4]; static inline guint64 xoroshiro_rotl (const guint64 x, int k) { @@ -2651,34 +2651,52 @@ rspamd_random_double_fast (void) return rspamd_random_double_fast_seed (xorshifto_seed); } -gdouble -rspamd_random_double_fast_seed (guint64 seed[2]) +/* xoshiro256+ */ +inline gdouble +rspamd_random_double_fast_seed (guint64 seed[4]) { - const guint64 s0 = seed[0]; - guint64 s1 = seed[1]; - const guint64 result = s0 + s1; + const uint64_t result = seed[0] + seed[3]; + + const uint64_t t = seed[1] << 17; + + seed[2] ^= seed[0]; + seed[3] ^= seed[1]; + seed[1] ^= seed[2]; + seed[0] ^= seed[3]; + + seed[2] ^= t; - s1 ^= s0; - seed[0] = xoroshiro_rotl(s0, 55) ^ s1 ^ (s1 << 14); - seed[1] = xoroshiro_rotl (s1, 36); + seed[3] = xoroshiro_rotl (seed[3], 45); return rspamd_double_from_int64 (result); } -guint64 -rspamd_random_uint64_fast (void) +/* xoroshiro256** */ +static inline guint64 +rspamd_random_uint64_fast_seed (guint64 seed[4]) { - const guint64 s0 = xorshifto_seed[0]; - guint64 s1 = xorshifto_seed[1]; - const guint64 result = s0 + s1; + const uint64_t result = xoroshiro_rotl (seed[1] * 5, 7) * 9; + + const uint64_t t = seed[1] << 17; - s1 ^= s0; - xorshifto_seed[0] = xoroshiro_rotl(s0, 55) ^ s1 ^ (s1 << 14); - xorshifto_seed[1] = xoroshiro_rotl (s1, 36); + seed[2] ^= seed[0]; + seed[3] ^= seed[1]; + seed[1] ^= seed[2]; + seed[0] ^= seed[3]; + + seed[2] ^= t; + + seed[3] = xoroshiro_rotl (seed[3], 45); return result; } +guint64 +rspamd_random_uint64_fast (void) +{ + return rspamd_random_uint64_fast_seed (xorshifto_seed); +} + void rspamd_random_seed_fast (void) { diff --git a/src/libutil/util.h b/src/libutil/util.h index c482a2d9f..d6f023205 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -424,7 +424,7 @@ gdouble rspamd_random_double (void); * @return */ gdouble rspamd_random_double_fast (void); -gdouble rspamd_random_double_fast_seed (guint64 seed[2]); +gdouble rspamd_random_double_fast_seed (guint64 seed[4]); guint64 rspamd_random_uint64_fast (void); /** |