forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.h
88 lines (68 loc) · 2.03 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// (C) 2017 University of Bristol. See License.txt
#ifndef _random
#define _random
#include "Tools/octetStream.h"
#include "Tools/sha1.h"
#include "Tools/aes.h"
#define USE_AES
#ifndef USE_AES
#define PIPELINES 1
#define SEED_SIZE HASH_SIZE
#define RAND_SIZE HASH_SIZE
#else
#define PIPELINES 8
#define SEED_SIZE AES_BLK_SIZE
#define RAND_SIZE (PIPELINES * AES_BLK_SIZE)
#endif
/* This basically defines a randomness expander, if using
* as a real PRG on an input stream you should first collapse
* the input stream down to a SEED, say via CBC-MAC (under 0 key)
* or via a hash
*/
// __attribute__ is needed to get the sse instructions to avoid
// seg faulting.
class PRNG
{
octet seed[SEED_SIZE];
octet state[RAND_SIZE] __attribute__((aligned (16)));
octet random[RAND_SIZE] __attribute__((aligned (16)));
#ifdef USE_AES
bool useC;
// Two types of key schedule for the different implementations
// of AES
uint KeyScheduleC[44];
octet KeySchedule[176] __attribute__((aligned (16)));
#endif
int cnt; // How many bytes of the current random value have been used
void hash(); // Hashes state to random and sets cnt=0
void next();
public:
PRNG();
// For debugging
void print_state() const;
// Set seed from dev/random
void ReSeed();
// Set seed from array
void SetSeed(unsigned char*);
void SetSeed(PRNG& G);
void InitSeed();
double get_double();
bool get_bit() { return get_uchar() & 1; }
unsigned char get_uchar();
unsigned int get_uint();
void get_bigint(bigint& res, int n_bits, bool positive = true);
void randomBnd(bigint& res, const bigint& B, bool positive=true);
bigint randomBnd(const bigint& B, bool positive=true);
word get_word()
{ word a=get_uint();
a<<=32;
a+=get_uint();
return a;
}
__m128i get_doubleword();
void get_octetStream(octetStream& ans,int len);
void get_octets(octet* ans, int len);
const octet* get_seed() const
{ return seed; }
};
#endif