c22414e9b779cd79123fe4390d3d0945b2d02d6c
[pintos-anon] / src / lib / random.c
1 #include "random.h"
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include "debug.h"
5
6 /* RC4-based pseudo-random number generator (PRNG).
7
8    RC4 is a stream cipher.  We're not using it here for its
9    cryptographic properties, but because it is easy to implement
10    and its output is plenty random for non-cryptographic
11    purposes.
12
13    See http://en.wikipedia.org/wiki/RC4_(cipher) for information
14    on RC4.*/
15
16 /* RC4 state. */
17 static uint8_t s[256];          /* S[]. */
18 static uint8_t s_i, s_j;        /* i, j. */
19
20 /* Already initialized? */
21 static bool inited;     
22
23 /* Swaps the bytes pointed to by A and B. */
24 static inline void
25 swap_byte (uint8_t *a, uint8_t *b) 
26 {
27   uint8_t t = *a;
28   *a = *b;
29   *b = t;
30 }
31
32 /* Initializes the PRNG with the given SEED.
33    Does nothing if the PRNG has already been initialized. */
34 void
35 random_init (unsigned seed)
36 {
37   uint8_t *seedp = (uint8_t *) &seed;
38   int i;
39   uint8_t j;
40
41   if (inited)
42     return;
43   
44   for (i = 0; i < 256; i++) 
45     s[i] = i;
46   for (i = j = 0; i < 256; i++) 
47     {
48       j += s[i] + seedp[i % sizeof seed];
49       swap_byte (s + i, s + j);
50     }
51
52   s_i = s_j = 0;
53   inited = true;
54 }
55
56 /* Writes SIZE random bytes into BUF. */
57 void
58 random_bytes (void *buf_, size_t size) 
59 {
60   uint8_t *buf;
61
62   ASSERT (inited);
63   for (buf = buf_; size-- > 0; buf++)
64     {
65       uint8_t s_k;
66       
67       s_i++;
68       s_j += s[s_i];
69       swap_byte (s + s_i, s + s_j);
70
71       s_k = s[s_i] + s[s_j];
72       *buf = s[s_k];
73     }
74 }
75
76 /* Returns a pseudo-random unsigned long.
77    Use random_ulong() % n to obtain a random number in the range
78    0...n (exclusive). */
79 unsigned long
80 random_ulong (void) 
81 {
82   unsigned long ul;
83   random_bytes (&ul, sizeof ul);
84   return ul;
85 }