blob: 12d7077c81b53ae17369cb34368ce0ceb8fe5bde [file] [log] [blame]
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00001#include "Random.h"
2
3Rand g_rand1(1);
4Rand g_rand2(2);
5Rand g_rand3(3);
6Rand g_rand4(4);
7
8//-----------------------------------------------------------------------------
9// Pseudo-random oracle. Mix avalanches x/y/z to < 0.07% bias.
10
11inline void omix ( uint32_t & x, uint32_t & y, uint32_t & z )
12{
13 uint64_t m = 0x65a3d38b;
14 uint64_t t = 0;
15
16 t = x * m; y ^= t; z ^= (t >> 32);
17 t = z * m; x ^= t; y ^= (t >> 32);
18 t = y * m; z ^= t; x ^= (t >> 32);
19 t = x * m; y ^= t; z ^= (t >> 32);
20 t = z * m; x ^= t; y ^= (t >> 32);
21 t = y * m; z ^= t; x ^= (t >> 32);
22}
23
24void oracle ( uint32_t key, uint32_t nonce, void * blob, int size )
25{
26 uint32_t x = 0x498b3bc5;
27 uint32_t y = 0x9c3ed699;
28 uint32_t z = 0x5a05089a;
29
30 x ^= key;
31 y ^= nonce;
32 z ^= size;
33
34 uint8_t * cursor = (uint8_t*)blob;
35
36 while(size)
37 {
38 omix(x,y,z);
39
40 if(size > 4)
41 {
42 *(uint32_t*)cursor = x;
43
44 cursor += 4;
45 size -= 4;
46 }
47 else
48 {
49 switch(size)
50 {
51 case 3: cursor[2] = (uint8_t)(x >> 16);
52 case 2: cursor[1] = (uint8_t)(x >> 8);
53 case 1: cursor[0] = (uint8_t)(x >> 0);
54 };
55
56 return;
57 }
58 }
59}
60
61//-----------------------------------------------------------------------------