Browse code

- added ISAAC, Bob Jenkins's fast pseudo-random generator and some wrapper arround it (see http://www.burtleburtle.net/bob/rand/isaacafa.html)

Andrei Pelinescu-Onciul authored on 14/06/2007 23:23:47
Showing 8 changed files
... ...
@@ -13,7 +13,8 @@
13 13
 
14 14
 sources=$(filter-out $(auto_gen), $(wildcard *.c) $(wildcard mem/*.c) \
15 15
 		$(wildcard parser/*.c) $(wildcard parser/digest/*.c) \
16
-		$(wildcard parser/contact/*.c) $(wildcard db/*.c) ) $(auto_gen)
16
+		$(wildcard parser/contact/*.c) $(wildcard db/*.c) \
17
+		$(wildcard rand/*.c) $(wildcard rand/isaac/*.c) ) $(auto_gen)
17 18
 ifeq ($(CORE_TLS), 1)
18 19
 	sources+= $(wildcard tls/*.c)
19 20
 endif
... ...
@@ -154,6 +154,7 @@
154 154
 #ifdef USE_DST_BLACKLIST
155 155
 #include "dst_blacklist.h"
156 156
 #endif
157
+#include "rand/fastrand.h" /* seed */
157 158
 
158 159
 #include "stats.h"
159 160
 
... ...
@@ -1351,8 +1352,9 @@ try_again:
1351 1351
 	seed+=getpid()+time(0);
1352 1352
 	DBG("seeding PRNG with %u\n", seed);
1353 1353
 	srand(seed);
1354
+	fastrand_seed(rand());
1354 1355
 	srandom(rand()+time(0));
1355
-	DBG("test random number %u\n", rand());
1356
+	DBG("test random numbers %u %lu %u\n", rand(), random(), fastrand());
1356 1357
 
1357 1358
 	/*register builtin  modules*/
1358 1359
 	register_builtin_modules();
... ...
@@ -41,6 +41,7 @@
41 41
 #include "pt.h"
42 42
 #include "tcp_init.h"
43 43
 #include "sr_module.h"
44
+#include "rand/fastrand.h"
44 45
 
45 46
 #include <stdio.h>
46 47
 #include <time.h> /* time(), used to initialize random numbers */
... ...
@@ -196,6 +197,7 @@ int fork_process(int child_id, char *desc, int make_sock)
196 196
 		is_main=0; /* a forked process cannot be the "main" one */
197 197
 		process_no=child_process_no;
198 198
 		srand(new_seed1);
199
+		fastrand_seed(rand());
199 200
 		srandom(new_seed2+time(0));
200 201
 		shm_malloc_on_fork();
201 202
 #ifdef PROFILING
... ...
@@ -330,6 +332,7 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
330 330
 		is_main=0; /* a forked process cannot be the "main" one */
331 331
 		process_no=child_process_no;
332 332
 		srand(new_seed1);
333
+		fastrand_seed(rand());
333 334
 		srandom(new_seed2+time(0));
334 335
 		shm_malloc_on_fork();
335 336
 #ifdef PROFILING
336 337
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+/*
1
+ * fast pseudo random generation 
2
+ *
3
+ * $Id$
4
+ *
5
+ * Copyright (C) 2007 iptelorg GmbH
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+/*
20
+ *History:
21
+ *--------
22
+ *  2007-06-15  wrapper around isaac (see 
23
+ *              http://www.burtleburtle.net/bob/rand/isaacafa.html) (andrei)
24
+ */
25
+
26
+#include "fastrand.h"
27
+
28
+#include <stdlib.h>
29
+#include "isaac/rand.h"
30
+
31
+#define FASTRAND_MAX  ((unsigned int)(-1))
32
+
33
+static randctx is_ctx;
34
+
35
+
36
+/* side effect: seeds also random w/ seed */
37
+void fastrand_seed(unsigned int seed)
38
+{
39
+	int i;
40
+	
41
+	srandom(seed);
42
+	for (i=0; i<RANDSIZ; i++)
43
+		is_ctx.randrsl[i]=random();
44
+	randinit(&is_ctx, 1);
45
+}
46
+
47
+
48
+unsigned int fastrand()
49
+{
50
+	return rand(&is_ctx);
51
+}
52
+
53
+/* generate a random number between 0 and max ( 0 <=r<=max) */
54
+unsigned int fastrand_max(unsigned int max)
55
+{
56
+	return fastrand()%(max+1);
57
+}
0 58
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+/*
1
+ * fast pseudo random generation 
2
+ *
3
+ * $Id$
4
+ *
5
+ * Copyright (C) 2007 iptelorg GmbH
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+/*
20
+ *History:
21
+ *--------
22
+ *  2007-06-15  wrapper around isaac (see 
23
+ *              http://www.burtleburtle.net/bob/rand/isaacafa.html) (andrei)
24
+ */
25
+
26
+#ifndef _fastrand_h
27
+#define _fastrand_h
28
+
29
+
30
+/* side effect: seeds also random w/ seed */
31
+void fastrand_seed(unsigned int seed);
32
+/* generate a 32 bit random number */
33
+unsigned int fastrand();
34
+/* generate a random number between 0 and max inclusive ( 0 <= r <= max)
35
+ * should not be used for cryptography */
36
+unsigned int fastrand_max(unsigned int max);
37
+
38
+#endif
0 39
new file mode 100644
... ...
@@ -0,0 +1,137 @@
0
+/*
1
+------------------------------------------------------------------------------
2
+rand.c: By Bob Jenkins.  My random number generator, ISAAC.  Public Domain.
3
+MODIFIED:
4
+  960327: Creation (addition of randinit, really)
5
+  970719: use context, not global variables, for internal state
6
+  980324: added main (ifdef'ed out), also rearranged randinit()
7
+  010626: Note that this is public domain
8
+------------------------------------------------------------------------------
9
+*/
10
+#ifndef STANDARD
11
+#include "standard.h"
12
+#endif
13
+#ifndef RAND
14
+#include "rand.h"
15
+#endif
16
+
17
+
18
+#define ind(mm,x)  (*(ub4 *)((ub1 *)(mm) + ((x) & ((RANDSIZ-1)<<2))))
19
+#define rngstep(mix,a,b,mm,m,m2,r,x) \
20
+{ \
21
+  x = *m;  \
22
+  a = (a^(mix)) + *(m2++); \
23
+  *(m++) = y = ind(mm,x) + a + b; \
24
+  *(r++) = b = ind(mm,y>>RANDSIZL) + x; \
25
+}
26
+
27
+void     isaac(ctx)
28
+randctx *ctx;
29
+{
30
+   register ub4 a,b,x,y,*m,*mm,*m2,*r,*mend;
31
+   mm=ctx->randmem; r=ctx->randrsl;
32
+   a = ctx->randa; b = ctx->randb + (++ctx->randc);
33
+   for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; )
34
+   {
35
+      rngstep( a<<13, a, b, mm, m, m2, r, x);
36
+      rngstep( a>>6 , a, b, mm, m, m2, r, x);
37
+      rngstep( a<<2 , a, b, mm, m, m2, r, x);
38
+      rngstep( a>>16, a, b, mm, m, m2, r, x);
39
+   }
40
+   for (m2 = mm; m2<mend; )
41
+   {
42
+      rngstep( a<<13, a, b, mm, m, m2, r, x);
43
+      rngstep( a>>6 , a, b, mm, m, m2, r, x);
44
+      rngstep( a<<2 , a, b, mm, m, m2, r, x);
45
+      rngstep( a>>16, a, b, mm, m, m2, r, x);
46
+   }
47
+   ctx->randb = b; ctx->randa = a;
48
+}
49
+
50
+
51
+#define mix(a,b,c,d,e,f,g,h) \
52
+{ \
53
+   a^=b<<11; d+=a; b+=c; \
54
+   b^=c>>2;  e+=b; c+=d; \
55
+   c^=d<<8;  f+=c; d+=e; \
56
+   d^=e>>16; g+=d; e+=f; \
57
+   e^=f<<10; h+=e; f+=g; \
58
+   f^=g>>4;  a+=f; g+=h; \
59
+   g^=h<<8;  b+=g; h+=a; \
60
+   h^=a>>9;  c+=h; a+=b; \
61
+}
62
+
63
+/* if (flag==TRUE), then use the contents of randrsl[] to initialize mm[]. */
64
+void randinit(ctx, flag)
65
+randctx *ctx;
66
+word     flag;
67
+{
68
+   word i;
69
+   ub4 a,b,c,d,e,f,g,h;
70
+   ub4 *m,*r;
71
+   ctx->randa = ctx->randb = ctx->randc = 0;
72
+   m=ctx->randmem;
73
+   r=ctx->randrsl;
74
+   a=b=c=d=e=f=g=h=0x9e3779b9;  /* the golden ratio */
75
+
76
+   for (i=0; i<4; ++i)          /* scramble it */
77
+   {
78
+     mix(a,b,c,d,e,f,g,h);
79
+   }
80
+
81
+   if (flag) 
82
+   {
83
+     /* initialize using the contents of r[] as the seed */
84
+     for (i=0; i<RANDSIZ; i+=8)
85
+     {
86
+       a+=r[i  ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
87
+       e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
88
+       mix(a,b,c,d,e,f,g,h);
89
+       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
90
+       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
91
+     }
92
+     /* do a second pass to make all of the seed affect all of m */
93
+     for (i=0; i<RANDSIZ; i+=8)
94
+     {
95
+       a+=m[i  ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
96
+       e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
97
+       mix(a,b,c,d,e,f,g,h);
98
+       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
99
+       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
100
+     }
101
+   }
102
+   else
103
+   {
104
+     /* fill in m[] with messy stuff */
105
+     for (i=0; i<RANDSIZ; i+=8)
106
+     {
107
+       mix(a,b,c,d,e,f,g,h);
108
+       m[i  ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
109
+       m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
110
+     }
111
+   }
112
+
113
+   isaac(ctx);            /* fill in the first set of results */
114
+   ctx->randcnt=RANDSIZ;  /* prepare to use the first set of results */
115
+}
116
+
117
+
118
+#ifdef NEVER
119
+int main()
120
+{
121
+  ub4 i,j;
122
+  randctx ctx;
123
+  ctx.randa=ctx.randb=ctx.randc=(ub4)0;
124
+  for (i=0; i<256; ++i) ctx.randrsl[i]=(ub4)0;
125
+  randinit(&ctx, TRUE);
126
+  for (i=0; i<2; ++i)
127
+  {
128
+    isaac(&ctx);
129
+    for (j=0; j<256; ++j)
130
+    {
131
+      printf("%.8lx",ctx.randrsl[j]);
132
+      if ((j&7)==7) printf("\n");
133
+    }
134
+  }
135
+}
136
+#endif
0 137
new file mode 100644
... ...
@@ -0,0 +1,56 @@
0
+/*
1
+------------------------------------------------------------------------------
2
+rand.h: definitions for a random number generator
3
+By Bob Jenkins, 1996, Public Domain
4
+MODIFIED:
5
+  960327: Creation (addition of randinit, really)
6
+  970719: use context, not global variables, for internal state
7
+  980324: renamed seed to flag
8
+  980605: recommend RANDSIZL=4 for noncryptography.
9
+  010626: note this is public domain
10
+------------------------------------------------------------------------------
11
+*/
12
+#ifndef STANDARD
13
+#include "standard.h"
14
+#endif
15
+
16
+#ifndef RAND
17
+#define RAND
18
+#define RANDSIZL   (8)  /* I recommend 8 for crypto, 4 for simulations */
19
+#define RANDSIZ    (1<<RANDSIZL)
20
+
21
+/* context of random number generator */
22
+struct randctx
23
+{
24
+  ub4 randcnt;
25
+  ub4 randrsl[RANDSIZ];
26
+  ub4 randmem[RANDSIZ];
27
+  ub4 randa;
28
+  ub4 randb;
29
+  ub4 randc;
30
+};
31
+typedef  struct randctx  randctx;
32
+
33
+/*
34
+------------------------------------------------------------------------------
35
+ If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed.
36
+------------------------------------------------------------------------------
37
+*/
38
+void randinit(/*_ randctx *r, word flag _*/);
39
+
40
+void isaac(/*_ randctx *r _*/);
41
+
42
+
43
+/*
44
+------------------------------------------------------------------------------
45
+ Call rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value
46
+------------------------------------------------------------------------------
47
+*/
48
+#define rand(r) \
49
+   (!(r)->randcnt-- ? \
50
+     (isaac(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \
51
+     (r)->randrsl[(r)->randcnt])
52
+
53
+#endif  /* RAND */
54
+
55
+
0 56
new file mode 100644
... ...
@@ -0,0 +1,63 @@
0
+/*
1
+------------------------------------------------------------------------------
2
+Standard definitions and types, Bob Jenkins
3
+------------------------------------------------------------------------------
4
+*/
5
+/*
6
+ * ser changes:
7
+ * --------
8
+ *  2007-06-15  changed ub4 & sb4 defs to int (from long) (andrei)
9
+ */
10
+
11
+#ifndef STANDARD
12
+# define STANDARD
13
+# ifndef STDIO
14
+#  include <stdio.h>
15
+#  define STDIO
16
+# endif
17
+# ifndef STDDEF
18
+#  include <stddef.h>
19
+#  define STDDEF
20
+# endif
21
+typedef  unsigned long long  ub8;
22
+#define UB8MAXVAL 0xffffffffffffffffLL
23
+#define UB8BITS 64
24
+typedef    signed long long  sb8;
25
+#define SB8MAXVAL 0x7fffffffffffffffLL
26
+typedef  unsigned int  ub4;   /* unsigned 4-byte quantities */
27
+#define UB4MAXVAL 0xffffffff
28
+typedef    signed int  sb4;
29
+#define UB4BITS 32
30
+#define SB4MAXVAL 0x7fffffff
31
+typedef  unsigned short int  ub2;
32
+#define UB2MAXVAL 0xffff
33
+#define UB2BITS 16
34
+typedef    signed short int  sb2;
35
+#define SB2MAXVAL 0x7fff
36
+typedef  unsigned       char ub1;
37
+#define UB1MAXVAL 0xff
38
+#define UB1BITS 8
39
+typedef    signed       char sb1;   /* signed 1-byte quantities */
40
+#define SB1MAXVAL 0x7f
41
+typedef                 int  word;  /* fastest type available */
42
+
43
+#define bis(target,mask)  ((target) |=  (mask))
44
+#define bic(target,mask)  ((target) &= ~(mask))
45
+#define bit(target,mask)  ((target) &   (mask))
46
+#ifndef min
47
+# define min(a,b) (((a)<(b)) ? (a) : (b))
48
+#endif /* min */
49
+#ifndef max
50
+# define max(a,b) (((a)<(b)) ? (b) : (a))
51
+#endif /* max */
52
+#ifndef align
53
+# define align(a) (((ub4)a+(sizeof(void *)-1))&(~(sizeof(void *)-1)))
54
+#endif /* align */
55
+#ifndef abs
56
+# define abs(a)   (((a)>0) ? (a) : -(a))
57
+#endif
58
+#define TRUE  1
59
+#define FALSE 0
60
+#define SUCCESS 0  /* 1 on VAX */
61
+
62
+#endif /* STANDARD */