-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprandom.c
More file actions
78 lines (65 loc) · 1.5 KB
/
prandom.c
File metadata and controls
78 lines (65 loc) · 1.5 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
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
#include "prandom.h"
#include "Galois_Field_256.h"
void gf256_gprn_init_t(struct gf256_gprn* gprn, GF256_t* coeff_data, GF256_t* seed_data)
{
if (!gprn)
return;
int i;
if (coeff_data)
{
for (i = 0; i < POLY_DEGREE; i++)
{
gprn->coeff[i] = coeff_data[i];
}
}
else
{
for (i = 0; i < POLY_DEGREE; i++)
{
gprn->coeff[i] = (GF256_t)(i % 256);
}
}
if (seed_data)
{
for (i = 0; i < POLY_DEGREE; i++)
{
gprn->t[i] = seed_data[i];
}
}
else
{
memset(gprn->t, 0, POLY_DEGREE);
for (i = 0; i < ARRAY_SIZE(GF256_DEGREES_AUTO); i++)
{
gprn->t[GF256_DEGREES_AUTO[i]] = GF256_ONE;
}
}
gprn->index = 0;
}
GF256_t gf256_gprn_next(struct gf256_gprn* gprn)
{
if (!gprn)
return 0;
GF256_t new_byte = 0;
int idx;
int i;
for (i = 0; i < POLY_DEGREE; i++)
{
idx = (gprn->index + i) % POLY_DEGREE;
new_byte = GF256_Add(new_byte, GF256_Mul(gprn->coeff[i], gprn->t[idx]));
}
// "circular" array
gprn->t[gprn->index] = new_byte;
gprn->index = (gprn->index + 1) % POLY_DEGREE;
return new_byte;
}
void gf256_gprn_generate(struct gf256_gprn* gprn, size_t count, GF256_t* output)
{
if (!gprn || !output || count == 0)
return;
size_t i;
for (i = 0; i < count; i++)
{
output[i] = gf256_gprn_next(gprn);
}
}