-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicg.c
200 lines (171 loc) · 4.04 KB
/
icg.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "icg.h"
#if TEST
#include <stdio.h>
static void print_arr(big size, ubig arr[size]);
#endif
/* TODO: Fix mod_inv such that ts can't be negative.
* Taken from wikipedia's page. Ref:
* https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers
*/
static ubig mod_inv(big value, big modulus)
{
big t = 0, newt = 1;
big r = modulus, newr = value;
while (newr != 0) {
big quotient = r / newr;
big swap = newt; /* (t, newt) := (newt, t - quotient * newt) */
newt = t - quotient * newt;
t = swap;
swap = newr; /* (r, newr) := (newr, r - quotient * newr)*/
newr = r - quotient * newr;
r = swap;
}
if (r > 1) { /* value is not invertible. */
return 0;
}
if (t < 0) { /* TODO: Prevent this case. */
t += modulus;
}
return t;
}
static void gen_invs(big modulus, ubig *invs)
{
for (size_t i = 0; i < (size_t)modulus; ++i) {
invs[i] = mod_inv(i, modulus);
}
}
static void gen_seq(struct icg_tuple t, ubig *invs, ubig *seq)
{
big value = t.seed;
for (size_t i = 0; i < (size_t)t.mod + 1; ++i) {
seq[i] = value;
if (invs[value]) {
value = (t.mul * invs[value] + t.add) % t.mod;
} else {
value = t.add;
}
}
}
ubig *icg(struct icg_tuple args)
{
ubig *invs = malloc(sizeof(ubig) * args.mod);
ubig *seq = malloc(sizeof(ubig) * (args.mod + 1));
assert(args.mul >= 0 && args.add >= 0 && args.seed >= 0);
args.mul %= args.mod;
args.add %= args.mod;
args.seed %= args.mod;
gen_invs(args.mod, invs);
gen_seq(args, invs, seq);
free(invs);
return seq;
}
static void binary_out(ubig val)
{
write(STDOUT_FILENO, &val, sizeof(val) / 4);
}
static void ascii_out(ubig val)
{
printf("%llu\n", val);
}
/* Assumes all icgs use the same modulus. */
ubig *cicg(struct icg_tuple *params, size_t cnt)
{
ubig *seqs[cnt]; /* Inputs */
ubig prod = 1; /* Initial value of 1 */
for (size_t i = 0; i < cnt; ++i) {
prod *= params[i].mod;
seqs[i] = icg(params[i]);
}
ubig *seq = calloc(prod, sizeof(*seq)); /* Output sequence. */
for (size_t i = 0; i < prod; ++i) {
ubig val = 0;
for (size_t j = 0; j < cnt; ++j) {
const ubig mod = params[j].mod;
val += (prod / mod) * seqs[j][i % mod];
}
seq[i] = val % prod;
}
for (size_t i = 0; i < cnt; ++i) {
free(seqs[i]);
}
return seq;
}
/* Assumes all icgs use the same modulus. */
void cicg_writer(struct icg_tuple *params, size_t cnt, int binary_mode)
{
ubig *seqs[cnt]; /* Inputs */
ubig prod = 1; /* Initial value of 1 */
void (*out)(ubig); /* Output function */
if (binary_mode) {
out = binary_out;
} else {
out = ascii_out;
}
for (size_t i = 0; i < cnt; ++i) {
prod *= params[i].mod;
seqs[i] = icg(params[i]);
}
for (size_t i = 0; i < prod; ++i) {
ubig seq_i = 0;
for (size_t j = 0; j < cnt; ++j) {
const ubig mod = params[j].mod;
seq_i += (prod / mod) * seqs[j][i % mod];
}
seq_i %= prod;
out(seq_i);
}
for (size_t i = 0; i < cnt; ++i) {
free(seqs[i]);
}
return;
}
ubig seq_len(ubig size, ubig seq[size])
{
char *seen = malloc(sizeof(char) * size);
memset(seen, 0, sizeof(char) * size);
for (size_t i = 0; i < (size_t) size + 1; ++i) {
if (seen[seq[i]]) {
return i;
}
seen[seq[i]] = 1;
}
free(seen);
return size;
}
#if TEST
static void print_arr(big size, ubig arr[size])
{
for (size_t i = 0; i < (size_t)size; ++i) {
printf("%llu ", arr[i]);
}
printf("\n");
}
int main(int argc, char** argv)
{
if (argc != 5) {
printf("usage: %s <modulus> <multiplier> <adder> <seed>\n",
argv[0]);
return 1;
}
struct icg_tuple t = {
strtoull(argv[1], NULL, 10),
strtoull(argv[2], NULL, 10),
strtoull(argv[3], NULL, 10),
strtoull(argv[4], NULL, 10)
};
assert(t.mod >= 0 && t.mul >= 0 && t.seed >= 0);
assert(t.mul < t.mod && t.add < t.mod && t.seed < t.mod);
ubig *invs = malloc(sizeof(ubig) * t.mod);
ubig *seq = malloc(sizeof(ubig) * t.mod);
printf("Got here!\n");
gen_invs(t.mod, invs);
gen_seq(t, invs, seq);
printf("DEBUG: Inverses:\n");
print_arr(t.mod, invs);
free(invs);
printf("DEBUG: Sequence:\n");
print_arr(t.mod + 1, seq);
printf("Sequence length: %llu\n", seq_len(t.mod, seq));
free(seq);
}
#endif