-
Notifications
You must be signed in to change notification settings - Fork 25
/
md5rng.cpp
150 lines (116 loc) · 3.34 KB
/
md5rng.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* RFMIX v2.XX - Local Ancestry and Admixture Analysis
Bustamante Lab - Stanford School of Medicine
(c) 2016 Mark Hamilton Wright
This program is licensed for academic research use only
unless otherwise stated. Contact [email protected] for
commercial licensing options.
Academic and research users should cite Brian Maples'
paper describing RFMIX in any publication using RFMIX
results. Citation is printed when the program is started. */
#include "md5rng.h"
#include <stdint.h>
inline uint32_t F(uint32_t X, uint32_t Y, uint32_t Z) {
return (X & Y) | ((~X) & Z);
}
inline uint32_t G(uint32_t X, uint32_t Y, uint32_t Z) {
return (X & Z) | (Y & (~Z));
}
inline uint32_t H(uint32_t X, uint32_t Y, uint32_t Z) {
return X ^ Y ^ Z;
}
inline uint32_t I(uint32_t X, uint32_t Y, uint32_t Z) {
return Y ^ (X | (~Z));
}
#define ROT32(x,s) (((x) << (s)) | ((x) >> (32 - (s))))
inline void FF(uint32_t &a, uint32_t b, uint32_t c, uint32_t d,
uint32_t M, uint32_t s, uint32_t t) {
uint32_t tmp;
tmp = a + F(b,c,d) + M + t;
a = b + ROT32(tmp, s);
}
inline void GG(uint32_t &a, uint32_t b, uint32_t c, uint32_t d,
uint32_t M, uint32_t s, uint32_t t) {
uint32_t tmp;
tmp = a + G(b,c,d) + M + t;
a = b + ROT32(tmp, s);
}
inline void HH(uint32_t &a, uint32_t b, uint32_t c, uint32_t d,
uint32_t M, uint32_t s, uint32_t t) {
uint32_t tmp;
tmp = a + H(b,c,d) + M + t;
a = b + ROT32(tmp, s);
}
inline void II(uint32_t &a, uint32_t b, uint32_t c, uint32_t d,
uint32_t M, uint32_t s, uint32_t t) {
uint32_t tmp;
tmp = a + I(b,c,d) + M + t;
a = b + ROT32(tmp, s);
}
uint32_t md5(uint32_t k1, uint32_t k2, uint32_t k3, uint32_t k4) {
uint32_t a, b, c, d;
a = 0x01234567;
b = 0x89ABCDEF;
c = 0xFEDCBA98;
d = 0x76543210;
FF(a, b, c, d, k1, 7, 0xD76AA478);
FF(d, a, b, c, k2, 12, 0xE8C7B756);
FF(c, d, a, b, k3, 17, 0x242070DB);
FF(b, c, d, a, k4, 22, 0xC1BDCEEE);
GG(a, b, c, d, k1, 5, 0xF61E2562);
GG(d, a, b, c, k2, 9, 0xC040B340);
GG(c, d, a, b, k3, 14, 0x265E5A51);
GG(b, c, d, a, k4, 20, 0xE9B6C7AA);
HH(a, b, c, d, k1, 4, 0xFFFA3942);
HH(d, a, b, c, k2, 11, 0x8771F681);
HH(c, d, a, b, k3, 16, 0x6D9D6122);
HH(b, c, d, a, k4, 23, 0xFDE5380C);
II(a, b, c, d, k1, 6, 0xF4292244);
II(d, a, b, c, k2, 10, 0x432AFF97);
II(c, d, a, b, k3, 15, 0xAB9423A7);
II(b, c, d, a, k4, 21, 0xFC93A039);
return a ^ b ^ c ^ d;
}
md5rng::md5rng(uint32_t key) {
this->key = key;
}
md5rng::~md5rng() {
}
uint32_t md5rng::uint32(uint32_t k2, uint32_t k3, uint32_t k4) {
return md5(key, k2, k3, k4);
}
int md5rng::uniform_int(uint32_t k2, uint32_t k3, uint32_t k4, int l, int r) {
double tmp = md5(key, k2, k3, k4)/(double) 0x100000000L;
return l + (int) ((r - l)*tmp);
}
#ifdef MAIN
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int k1 = 8;
int k2 = 16;
int k3 = 1024;
uint32_t key = 0xDEADBEEF;
if (argc > 1) {
key = atoi(argv[1]);
}
if (argc > 2) {
k1 = atoi(argv[2]);
}
if (argc > 3) {
k2 = atoi(argv[3]);
}
if (argc > 4) {
k3 = atoi(argv[4]);
}
md5rng *rng = new md5rng(key);
for(int i = 0; i < k1; i++) {
for(int j = 0; j < k2; j++) {
for(int k = 0; k < k3; k++) {
uint32_t tmp = rng->uint32(i, j, k);
fwrite((void *) &tmp, sizeof(uint32_t), 1, stdout);
}
}
}
return 0;
}
#endif