-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbs.cpp
More file actions
executable file
·46 lines (42 loc) · 1.02 KB
/
bbs.cpp
File metadata and controls
executable file
·46 lines (42 loc) · 1.02 KB
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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <gmpxx.h>
#include "bbs.h"
using namespace std;
mpz_class BBS_BASE::operator ()(int bits)
{
mpz_class ret = 0;
for(int i = 0; i < bits; i++) {
mX = mX * mX % mN;
ret <<= 1;
ret |= mX % 2;
}
return ret;
}
void BBS_BASE::getRand(void *buf, int len)
{
unsigned long tmp = 0;
unsigned long *s = reinterpret_cast<unsigned long *>(buf);
for(int i = 0; i < len / sizeof(unsigned long); i++) {
tmp = 0;
for(int j = 0; j < sizeof(unsigned long) * 8; j++) {
mX = mX * mX % mN;
tmp <<= 1;
if(mX % 2 == 1)
tmp |= 1;
}
s[i] = tmp;
}
if(len % sizeof(unsigned long)) {
tmp = 0;
for(int j = 0; j < len % sizeof(unsigned long); j++) {
mX = mX * mX % mN;
tmp <<= 1;
if(mX % 2 == 1)
tmp |= 1;
}
char *t = reinterpret_cast<char *>(&tmp);
memcpy((char *)buf + len - len % sizeof(unsigned long), t + sizeof(unsigned long) - len % sizeof(unsigned long), len % sizeof(unsigned long));
}
}