tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame^] | 1 | #include "Random.h"
|
| 2 |
|
| 3 | Rand g_rand1(1);
|
| 4 | Rand g_rand2(2);
|
| 5 | Rand g_rand3(3);
|
| 6 | Rand g_rand4(4);
|
| 7 |
|
| 8 | //-----------------------------------------------------------------------------
|
| 9 | // Pseudo-random oracle. Mix avalanches x/y/z to < 0.07% bias.
|
| 10 |
|
| 11 | inline 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 |
|
| 24 | void 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 | //-----------------------------------------------------------------------------
|