From 9c93e72d81e88fcf9ea0447ac1f0ea4f07a04755 Mon Sep 17 00:00:00 2001
From: Kaspar Schleiser <kaspar@schleiser.de>
Date: Mon, 12 Oct 2015 16:05:17 +0200
Subject: [PATCH] sys: random: add simple LCG PRNGs

---
 Makefile.pseudomodules |  3 ++
 sys/random/Makefile    | 13 ++++++++
 sys/random/minstd.c    | 73 ++++++++++++++++++++++++++++++++++++++++++
 sys/random/musl_lcg.c  | 45 ++++++++++++++++++++++++++
 4 files changed, 134 insertions(+)
 create mode 100644 sys/random/minstd.c
 create mode 100644 sys/random/musl_lcg.c

diff --git a/Makefile.pseudomodules b/Makefile.pseudomodules
index 86aca801eb..7567b1a783 100644
--- a/Makefile.pseudomodules
+++ b/Makefile.pseudomodules
@@ -23,3 +23,6 @@ PSEUDOMODULES += netif
 # include variants of the AT86RF2xx drivers as pseudo modules
 PSEUDOMODULES += at86rf23%
 PSEUDOMODULES += at86rf21%
+
+# add all pseudo random number generator variants as pseudomodules
+PSEUDOMODULES += prng_%
diff --git a/sys/random/Makefile b/sys/random/Makefile
index 48422e909a..7e79cc14ab 100644
--- a/sys/random/Makefile
+++ b/sys/random/Makefile
@@ -1 +1,14 @@
+ifeq (,$(filter prng_%,$(USEMODULE)))
+    USEMODULE += prng_mersenne
+endif
+ifneq (,$(filter prng_mersenne,$(USEMODULE)))
+    SRC += mersenne.c
+endif
+ifneq (,$(filter prng_minstd,$(USEMODULE)))
+    SRC += minstd.c
+endif
+ifneq (,$(filter prng_musl_lcg,$(USEMODULE)))
+    SRC += musl_lcg.c
+endif
+
 include $(RIOTBASE)/Makefile.base
diff --git a/sys/random/minstd.c b/sys/random/minstd.c
new file mode 100644
index 0000000000..038b81c4c5
--- /dev/null
+++ b/sys/random/minstd.c
@@ -0,0 +1,73 @@
+/**
+ * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ *
+ * Code taken from C FAQ (http://c-faq.com/lib/rand.html).
+ */
+
+ /**
+ * @ingroup sys_random
+ * @{
+ * @file
+ *
+ * @brief Simple Park & Miller "minimal standard" PRNG
+ *
+ * This file contains a simple Park-Miller pseudo random number generator.
+ *
+ * While not very random when considering crypto requirements, this is probably
+ * random enough anywhere where pseudo-randomness is sufficient, e.g., when
+ * provided with a sensible seed source, for MAC algorithms.
+ *
+ * The implementation is taken from the C FAQ, but modified to use magic number
+ * division and adapted to RIOT's coding conventions..
+ *
+ * @author Kaspar Schleiser <kaspar@schleiser.de>
+ * @}
+ */
+
+#include <stdint.h>
+
+#include "div.h"
+
+#define a 48271
+#define m 2147483647
+#define q (m / a) /* 44488 */
+#define r (m % a) /* 3399 */
+
+static uint32_t _seed = 1;
+
+int rand_minstd(void)
+{
+    uint32_t hi = div_u32_by_44488(_seed);
+    uint32_t lo = div_u32_mod_44488(_seed);
+    uint32_t test = (a * lo) - (r * hi);
+
+    if(test > 0) {
+        _seed = test;
+    }
+    else {
+        _seed = test + m;
+    }
+
+    return _seed;
+}
+
+uint32_t genrand_uint32(void)
+{
+    /* minstd as implemented returns only values from 1 to 2147483647,
+     * so run it two times to get 32bits */
+    uint16_t A = (rand_minstd() >> 15);
+    uint16_t B = (rand_minstd() >> 15);
+    return  (((uint32_t)A) << 16) | B;
+}
+
+void genrand_init(uint32_t val)
+{
+    if (!val) {
+        val = 1;
+    }
+    _seed = val;
+}
diff --git a/sys/random/musl_lcg.c b/sys/random/musl_lcg.c
new file mode 100644
index 0000000000..6e10a21ffb
--- /dev/null
+++ b/sys/random/musl_lcg.c
@@ -0,0 +1,45 @@
+/**
+ * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ *
+ * Code taken from Musl C library. Original copyright notice:
+ *
+ * Copyright © 2005-2014 Rich Felker, et al.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdint.h>
+
+static uint64_t _seed;
+
+void genrand_init(uint32_t s)
+{
+    _seed = s-1;
+}
+
+uint32_t genrand_uint32(void)
+{
+    _seed = 6364136223846793005ULL*_seed + 1;
+    return _seed>>32;
+}
-- 
GitLab