]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Use slightly better fast PRNGs
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 31 Oct 2019 09:25:09 +0000 (09:25 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 31 Oct 2019 09:25:09 +0000 (09:25 +0000)
src/libutil/util.c
src/libutil/util.h
src/lua/lua_task.c

index 5fe8301fcd5131ce79ca5bcad1f0ca56cb5fc98c..5ada3a27ebb87180c498e2a9b437787317cd1538 100644 (file)
@@ -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)
 {
index c482a2d9fb3223da9ac6833ec072371fa69d0cce..d6f0232052624378f2bdb4b692b7f5d070d8e0ba 100644 (file)
@@ -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);
 
 /**
index 263aa83fa6a30393ac73548194fb9d3fb11caba6..e59bd36859871b6e91e8b6377a10991383a8ee2b 100644 (file)
@@ -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;
        }