-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgf.c
More file actions
289 lines (277 loc) · 9.26 KB
/
Copy pathgf.c
File metadata and controls
289 lines (277 loc) · 9.26 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "gf.h"
#define W 0x08
#define PMAX 0x03 //max parity
#define RWIDTH 0x0c //strip width
#define GFPoly 0x11d
#define GFOrder 0xff //2^W-1
/*
* These two tables represent powers and logs of 2 in the Galois field.
* These values were computed by repeatedly multiplying by 2.
* The primitive poly is x^8+x^4+x^3+x^2+1 in GF(2^8).
*/
static const uint8_t GF8Pow2[256] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9,
0x8f, 0x03, 0x06, 0x0c,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0x79, 0xf2, 0xf9, 0xef,
0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5,
0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83,
0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01
};
static const uint8_t GF8Log2[256] = {
0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6,
0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b,
0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81,
0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71,
0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21,
0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45,
0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9,
0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6,
0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd,
0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88,
0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd,
0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40,
0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e,
0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d,
0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b,
0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57,
0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d,
0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18,
0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c,
0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e,
0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd,
0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61,
0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e,
0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2,
0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76,
0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6,
0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa,
0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a,
0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51,
0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7,
0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8,
0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf,
};
uint8_t GF8Add(uint8_t a, uint8_t b)
{
return a^b;
}
uint8_t GF8Multiply(uint8_t a, uint8_t b)
{
if (a == 0 || b == 0) {
return 0;
}
int s = GF8Log2[a] + GF8Log2[b];
s %= GFOrder;
return GF8Pow2[s];
}
uint8_t GF8Invert(uint8_t a)
{
if (a == 0) return 0;
return GF8Pow2[GFOrder-GF8Log2[a]];
}
/* This cauchy matrix is generated from
* X = {0, 1, 2}
* Y = {f4, 47, a7, 7a, ba, ad, dd, 3d, d8, 72, 6c, ed}
*
* The matrix design take the AFA frame(12 disk as a group)into count,
* according to the (k, m), we use different part of Cauchy Matrix.
* for example, here k+m = 12, equal to disk number in a disk group
* (k,m) = (10,2), we will take CMat[0-1][0-9]
* (k,m) = (9, 3), we will take CMat[0-2][0-8]
*
* TODO:
* We can find a sparse matrix(when turn into bit-matrix) to promote
* the operation in the future.
*/
static uint8_t CMat[PMAX][RWIDTH] = {
{0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0a, 0x0c, 0x10, 0x11, 0x20, 0x21},
{0x8f, 0xa6, 0x46, 0xbb, 0x7b, 0x9c, 0x99, 0xab, 0x73, 0xd9, 0xec, 0x6d},
{0xd3, 0x4e, 0xc2, 0xdb, 0xc5, 0xb7, 0x91, 0xe1, 0x76, 0x28, 0x2f, 0x6a}};
uint8_t *CauchyMat(uint8_t m, uint8_t k)
{
uint8_t *mat = (uint8_t *)malloc(sizeof(uint8_t)*(m*k));
assert(mat != NULL);
#if 0
if (k > RWIDTH || m > PMAX || (k+m) != RWIDTH) {
printf("Wrong arguments\n");
free(mat);
return NULL;
}
#endif
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
mat[i*k+j] = CMat[i][j];
}
}
return mat;
}
/*
*/
void BitMat(uint8_t a, uint8_t bM[])
{
for (int i = 0; i < W; i++) {
bM[i] = GF8Multiply(a, 1<<i);
}
return;
}
void uBitMat(uint8_t e, uint8_t w, uint8_t bmat[][w])
{
assert(w == 8); // for current version W==8
for (int i = 0; i < w; ++i) {
for (int j = 0; j < w; ++j)
bmat[i][j] = GF8Multiply(e, 1<<j)&(1<<i);
}
}
uint8_t *ToBitMatrix(uint8_t k, uint8_t m, uint8_t *mat)
{
uint8_t *bitMat = (uint8_t *)malloc(sizeof(uint8_t)*(k*m*W));
assert(bitMat != NULL);
for (int i = 0; i < m; i++) {
int ind = i*k*W;
for (int j = 0; j < k; j++) {
BitMat(*(mat+i*k+j), (bitMat + ind + j*W));
}
}
return bitMat;
}
uint8_t *uToBitMatrix(uint8_t m, uint8_t k, uint8_t w, uint8_t *mat)
{
uint8_t *bitMat = (uint8_t *)malloc(sizeof(uint8_t)*k*w*m*w);
assert(bitMat != NULL);
for (int i = 0; i < m; ++i) {
int ii = i*k*w*w;
for (int j = 0; j < k; ++j) {
uint8_t e = *(mat+i*k+j);
int ij = j*w;
for (int l = 0; l < w; ++l) {
int il = l*k*w;
for (int n = 0; n < w; ++n)
*(bitMat+ii+ij+il+n) = GF8Multiply(e, 1<<n)&(1<<l)?1:0;
}
}
}
return bitMat;
}
uint8_t *uIdMatrix(uint8_t k)
{
uint8_t *idmat = (uint8_t *)malloc(sizeof(uint8_t)*k*k);
assert(idmat != NULL);
printf("Addr:%p\n", idmat);
for (int i = 0; i < k; ++i) {
int indi = i*k;
for (int j = 0; j < k; ++j) {
int ind = indi+j;
if (i == j)
*(idmat+ind) = 1;
else
*(idmat+ind) = 0;
}
}
return idmat;
}
uint8_t *uGenDecodeMatrix(uint8_t k, uint8_t *erased, uint8_t esize, uint8_t *cmat)
{
uint8_t *idmat = uIdMatrix(k);
for (int i = 0; i < esize; ++i) {
int ind = *(erased+i);
for (int j = 0; j < k; ++j) {
*(idmat+ind*k+j) = *(cmat+i*k+j);
}
}
return idmat;
}
/* 解码柯西矩阵,
*
*/
uint8_t *uGenInvertMatrix(uint8_t k, uint8_t *erased, uint8_t esize, uint8_t *cmat)
{
uint8_t eidx[esize];
int ind, inde, indi;
uint8_t *idmat = uIdMatrix(k);
/* 处理除erased行中所有非erased行数的数,O(sizeof(erased)*k)
*/
for (int er = 0; er < esize; ++er) {
ind = eidx[er] = *(erased+er);
inde = er*k;
for (int i = 0; i < k; ++i) {
indi = inde+i;
if (*(cmat+indi) != 0) {
int flag = 1;
for (int j = 0; j < esize; ++j) {
if (*(erased+j) == i) {
flag = 0;
break;
}
}
if (flag) {
*(idmat+ind*k+i) = *(cmat+indi);
*(cmat+indi) = 0;
}
}
}
}
/* 处理内层与erased行数相关的数,(sizeof(erased))^2的矩阵
*/
for (int er = 0; er < esize; ++er) {
ind = eidx[er];
inde = er*k;
uint8_t inv = GF8Invert(*(cmat+inde+ind));
for (int i = 0; i < esize; ++i) {
int tmp = *(cmat+inde+eidx[i]);
if (tmp != 0)
*(cmat+inde+eidx[i]) = GF8Multiply(inv, tmp);
}
for (int j = 0; j < k; ++j) {
int tmp = *(idmat+ind*k+j);
if (tmp != 0)
*(idmat+ind*k+j) = GF8Multiply(inv, tmp);
}
for (int i = 0; i < esize; ++i) {
int idx = i*k;
if (i != er) {
uint8_t adde = *(cmat + idx + eidx[er]);
for (int j = 0; j < esize; ++j) {
int tmpa = *(cmat + idx + eidx[j]);
int tmpb = *(cmat + er*k + eidx[j]);
*(cmat + idx + eidx[j]) = GF8Add(GF8Multiply(adde,tmpb), tmpa);
}
for (int l = 0; l < k; ++l) {
int tmpa = *(idmat + eidx[i]*k + l);
int tmpb = *(idmat + ind*k + l);
*(idmat + eidx[i]*k + l) = GF8Add(GF8Multiply(adde, tmpb), tmpa);
}
}
}
}
return idmat;
}
uint8_t *uGenInvertBitMatrix(uint8_t k, uint8_t w, uint8_t *dmat)
{
return uToBitMatrix(k, k, w, dmat);
}