1 #include <stdio.h>
2 
3 #include "prng.h"
4 
5 static uint32_t current=0;
6 static uint32_t seed=0;
7 
permute(uint32_t x)8 static uint32_t permute(uint32_t x)
9 {
10 	static uint32_t prime = 4294967291U;
11 	if(x>=prime) return x;
12 	uint32_t residue = ((uint64_t)x*x)%prime;
13 	if(x<=prime/2) return residue;
14 	else return prime - residue;
15 }
16 
prng_init(uint32_t val)17 void prng_init(uint32_t val)
18 {
19 	current=0;
20 	seed=val;
21 }
22 
prng_next(void)23 uint32_t prng_next(void)
24 {
25 	current=permute((permute(current) + seed) ^ 0x5bf03635);
26 	return current;
27 }
28 
prng_next64(void)29 uint64_t prng_next64(void)
30 {
31 	return (uint64_t)(prng_next())<<32|prng_next();
32 }
33 
prng_md5sum(uint8_t checksum[])34 uint8_t *prng_md5sum(uint8_t checksum[])
35 {
36 	uint8_t i=0;
37 	uint8_t j=0;
38 	uint32_t r;
39 	while(i<MD5_DIGEST_LENGTH)
40 	{
41 		r=prng_next();
42 		for(j=0; j<sizeof(r)*4; j+=8)
43 			checksum[i++]=(uint8_t)(r>>j);
44 	}
45 	return checksum;
46 }
47