From 62fb0aeba5648ab32d4669ed9b9960f3304b6656 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 31 Oct 2019 09:25:09 +0000 Subject: [PATCH] [Minor] Use slightly better fast PRNGs --- src/libutil/util.c | 52 +++++++++++++++++++++++++++++++--------------- src/libutil/util.h | 2 +- src/lua/lua_task.c | 4 ++-- 3 files changed, 38 insertions(+), 20 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); /** diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 263aa83fa..e59bd3685 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -2118,7 +2118,7 @@ struct lua_tree_cb_data { gint mask; gint need_images; gdouble skip_prob; - guint64 xoroshiro_state[2]; + guint64 xoroshiro_state[4]; }; static void @@ -2161,7 +2161,7 @@ lua_task_urls_adjust_skip_prob (struct rspamd_task *task, memcpy (&cb->xoroshiro_state[0], &task->task_timestamp, MIN (sizeof (cb->xoroshiro_state[0]), sizeof (task->task_timestamp))); memcpy (&cb->xoroshiro_state[1], MESSAGE_FIELD (task, digest), - sizeof (cb->xoroshiro_state[1])); + sizeof (cb->xoroshiro_state[1]) * 3); sz = max_urls; } -- 2.39.5