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