This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
fecpp.cpp
609 lines (529 loc) · 19.2 KB
/
fecpp.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* Forward error correction based on Vandermonde matrices
*
* (C) 1997-1998 Luigi Rizzo ([email protected])
* (C) 2009-2010 Jack Lloyd ([email protected])
* (C) 2011 Billy Brumley ([email protected])
*
* Distributed under the terms given in license.txt (Simplified BSD)
*/
#include "fecpp.h"
#include <stdexcept>
#include <vector>
#include <cstring>
namespace fecpp {
namespace {
/* Tables for arithetic in GF(2^8) using 1+x^2+x^3+x^4+x^8
*
* See Lin & Costello, Appendix A, and Lee & Messerschmitt, p. 453.
*
* Generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
* Lookup tables:
* index->polynomial form gf_exp[] contains j= \alpha^i;
* polynomial form -> index form gf_log[ j = \alpha^i ] = i
* \alpha=x is the primitive element of GF(2^m)
*
* For efficiency, gf_exp[] has size 2*GF_SIZE, so that a simple
* multiplication of two numbers can be resolved without calling mod
*/
const uint8_t GF_EXP[510] = {
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, 0x18, 0x30, 0x60, 0xC0, 0x9D,
0x27, 0x4E, 0x9C, 0x25, 0x4A, 0x94, 0x35, 0x6A, 0xD4, 0xB5, 0x77,
0xEE, 0xC1, 0x9F, 0x23, 0x46, 0x8C, 0x05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AE, 0xB6, 0x12, 0x59, 0xA5, 0x35, 0x65, 0xB8,
0xA3, 0x9E, 0xD2, 0xF7, 0x62, 0x5A, 0x85, 0x7D, 0xA8, 0x3A, 0x29,
0x71, 0xC8, 0xF6, 0xF9, 0x43, 0xD7, 0xD6, 0x10, 0x73, 0x76, 0x78,
0x99, 0x0A, 0x19, 0x91, 0x14, 0x3F, 0xE6, 0xF0, 0x86, 0xB1, 0xE2,
0xF1, 0xFA, 0x74, 0xF3, 0xB4, 0x6D, 0x21, 0xB2, 0x6A, 0xE3, 0xE7,
0xB5, 0xEA, 0x03, 0x8F, 0xD3, 0xC9, 0x42, 0xD4, 0xE8, 0x75, 0x7F,
0xFF, 0x7E, 0xFD };
static uint8_t GF_MUL_TABLE[256][256];
void init_fec()
{
static bool fec_initialized = false;
if(!fec_initialized)
{
fec_initialized = true;
for(size_t i = 0; i != 256; ++i)
for(size_t j = 0; j != 256; ++j)
GF_MUL_TABLE[i][j] = GF_EXP[(GF_LOG[i] + GF_LOG[j]) % 255];
for(size_t i = 0; i != 256; ++i)
GF_MUL_TABLE[0][i] = GF_MUL_TABLE[i][0] = 0;
}
}
/*
* addmul() computes z[] = z[] + x[] * y
*/
void addmul(uint8_t z[], const uint8_t x[], uint8_t y, size_t size)
{
if(y == 0)
return;
const uint8_t* GF_MUL_Y = GF_MUL_TABLE[y];
while(size && (uintptr_t)z % 16) // first align z to 16 bytes
{
z[0] ^= GF_MUL_Y[x[0]];
++z;
++x;
size--;
}
#if defined(FECPP_IS_X86)
if(size >= 16 && has_ssse3())
{
const size_t consumed = addmul_ssse3(z, x, y, size);
z += consumed;
x += consumed;
y += consumed;
size -= consumed;
}
if(size >= 64 && has_sse2())
{
const size_t consumed = addmul_sse2(z, x, y, size);
z += consumed;
x += consumed;
y += consumed;
size -= consumed;
}
#endif
while(size >= 16)
{
z[0] ^= GF_MUL_Y[x[0]];
z[1] ^= GF_MUL_Y[x[1]];
z[2] ^= GF_MUL_Y[x[2]];
z[3] ^= GF_MUL_Y[x[3]];
z[4] ^= GF_MUL_Y[x[4]];
z[5] ^= GF_MUL_Y[x[5]];
z[6] ^= GF_MUL_Y[x[6]];
z[7] ^= GF_MUL_Y[x[7]];
z[8] ^= GF_MUL_Y[x[8]];
z[9] ^= GF_MUL_Y[x[9]];
z[10] ^= GF_MUL_Y[x[10]];
z[11] ^= GF_MUL_Y[x[11]];
z[12] ^= GF_MUL_Y[x[12]];
z[13] ^= GF_MUL_Y[x[13]];
z[14] ^= GF_MUL_Y[x[14]];
z[15] ^= GF_MUL_Y[x[15]];
x += 16;
z += 16;
size -= 16;
}
// Clean up the trailing pieces
for(size_t i = 0; i != size; ++i)
z[i] ^= GF_MUL_Y[x[i]];
}
/*
* invert_matrix() takes a K*K matrix and produces its inverse
* (Gauss-Jordan algorithm, adapted from Numerical Recipes in C)
*/
void invert_matrix(uint8_t matrix[], size_t K)
{
class pivot_searcher
{
public:
pivot_searcher(size_t K) : ipiv(K) {}
std::pair<size_t, size_t> operator()(size_t col, const uint8_t* matrix)
{
const size_t K = ipiv.size();
if(ipiv[col] == false && matrix[col*K + col] != 0)
{
ipiv[col] = true;
return std::make_pair(col, col);
}
for(size_t row = 0; row != K; ++row)
{
if(ipiv[row])
continue;
for(size_t i = 0; i != K; ++i)
{
if(ipiv[i] == false && matrix[row*K + i] != 0)
{
ipiv[i] = true;
return std::make_pair(row, i);
}
}
}
throw std::invalid_argument("pivot not found in invert_matrix");
}
private:
// Marks elements already used as pivots
std::vector<bool> ipiv;
};
pivot_searcher pivot_search(K);
std::vector<size_t> indxc(K);
std::vector<size_t> indxr(K);
std::vector<uint8_t> id_row(K);
for(size_t col = 0; col != K; ++col)
{
/*
* Zeroing column 'col', look for a non-zero element.
* First try on the diagonal, if it fails, look elsewhere.
*/
std::pair<size_t, size_t> icolrow = pivot_search(col, matrix);
size_t icol = icolrow.first;
size_t irow = icolrow.second;
/*
* swap rows irow and icol, so afterwards the diagonal
* element will be correct. Rarely done, not worth
* optimizing.
*/
if(irow != icol)
{
for(size_t i = 0; i != K; ++i)
std::swap(matrix[irow*K + i], matrix[icol*K + i]);
}
indxr[col] = irow;
indxc[col] = icol;
uint8_t* pivot_row = &matrix[icol*K];
uint8_t c = pivot_row[icol];
if(c == 0)
throw std::invalid_argument("singlar matrix");
if(c != 1)
{ /* otherwhise this is a NOP */
/*
* this is done often, but optimizing is not so
* fruitful, at least in the obvious ways (unrolling)
*/
c = GF_INVERSE[c];
pivot_row[icol] = 1;
const uint8_t* mul_c = GF_MUL_TABLE[c];
for(size_t i = 0; i != K; ++i)
pivot_row[i] = mul_c[pivot_row[i]];
}
/*
* from all rows, remove multiples of the selected row
* to zero the relevant entry (in fact, the entry is not zero
* because we know it must be zero).
* (Here, if we know that the pivot_row is the identity,
* we can optimize the addmul).
*/
id_row[icol] = 1;
if(memcmp(pivot_row, &id_row[0], K) != 0)
{
uint8_t* p = matrix;
for(size_t i = 0; i != K; ++i)
{
if(i != icol)
{
c = p[icol];
p[icol] = 0;
addmul(p, pivot_row, c, K);
}
p += K;
}
}
id_row[icol] = 0;
} /* done all columns */
for(size_t i = 0; i != K; ++i)
{
if(indxr[i] != indxc[i])
{
for(size_t row = 0; row != K; ++row)
std::swap(matrix[row*K + indxr[i]], matrix[row*K + indxc[i]]);
}
}
}
/*
* Generate and invert a Vandermonde matrix.
*
* Only uses the second column of the matrix, containing the p_i's
* (contents - 0, GF_EXP[0...n])
*
* Algorithm borrowed from "Numerical recipes in C", section 2.8, but
* largely revised for my purposes.
*
* p = coefficients of the matrix (p_i)
* q = values of the polynomial (known)
*/
void create_inverted_vdm(uint8_t vdm[], size_t K)
{
if(K == 1) /* degenerate case, matrix must be p^0 = 1 */
{
vdm[0] = 1;
return;
}
/*
* c holds the coefficient of P(x) = Prod (x - p_i), i=0..K-1
* b holds the coefficient for the matrix inversion
*/
std::vector<uint8_t> b(K), c(K);
/*
* construct coeffs. recursively. We know c[K] = 1 (implicit)
* and start P_0 = x - p_0, then at each stage multiply by
* x - p_i generating P_i = x P_{i-1} - p_i P_{i-1}
* After K steps we are done.
*/
c[K-1] = 0; /* really -p(0), but x = -x in GF(2^m) */
for(size_t i = 1; i < K; ++i)
{
const uint8_t* mul_p_i = GF_MUL_TABLE[GF_EXP[i]];
for(size_t j = K-1 - (i - 1); j < K-1; ++j)
c[j] ^= mul_p_i[c[j+1]];
c[K-1] ^= GF_EXP[i];
}
for(size_t row = 0; row < K; ++row)
{
// synthetic division etc.
const uint8_t* mul_p_row = GF_MUL_TABLE[row == 0 ? 0 : GF_EXP[row]];
uint8_t t = 1;
b[K-1] = 1; /* this is in fact c[K] */
for(int i = K-2; i >= 0; i--)
{
b[i] = c[i+1] ^ mul_p_row[b[i+1]];
t = b[i] ^ mul_p_row[t];
}
const uint8_t* mul_t_inv = GF_MUL_TABLE[GF_INVERSE[t]];
for(size_t col = 0; col != K; ++col)
vdm[col*K + row] = mul_t_inv[b[col]];
}
}
}
/*
* This section contains the proper FEC encoding/decoding routines.
* The encoding matrix is computed starting with a Vandermonde matrix,
* and then transforming it into a systematic matrix.
*/
/*
* fec_code constructor
*/
fec_code::fec_code(size_t K_arg, size_t N_arg) :
K(K_arg), N(N_arg), enc_matrix(N * K)
{
init_fec();
if(K == 0 || N == 0 || K > 256 || N > 256 || K > N)
throw std::invalid_argument("fec_code: violated 1 <= K <= N <= 256");
std::vector<uint8_t> temp_matrix(N * K);
/*
* quick code to build systematic matrix: invert the top
* K*K vandermonde matrix, multiply right the bottom n-K rows
* by the inverse, and construct the identity matrix at the top.
*/
create_inverted_vdm(&temp_matrix[0], K);
for(size_t i = K*K; i != temp_matrix.size(); ++i)
temp_matrix[i] = GF_EXP[((i / K) * (i % K)) % 255];
/*
* the upper part of the encoding matrix is I
*/
for(size_t i = 0; i != K; ++i)
enc_matrix[i*(K+1)] = 1;
/*
* computes C = AB where A is n*K, B is K*m, C is n*m
*/
for(size_t row = K*K; row != N*K; row += K)
{
for(size_t col = 0; col != K; ++col)
{
const uint8_t* pa = &temp_matrix[row];
const uint8_t* pb = &temp_matrix[col];
uint8_t acc = 0;
for(size_t i = 0; i < K; i++, pa++, pb += K)
acc ^= GF_MUL_TABLE[*pa][*pb];
enc_matrix[row + col] = acc;
}
}
}
/*
* FEC encoding routine
*/
void fec_code::encode(
const uint8_t input[], size_t size,
std::function<void (size_t, size_t, const uint8_t[], size_t)> output)
const
{
if(size % K != 0)
throw std::invalid_argument("encode: input must be multiple of K bytes");
size_t block_size = size / K;
#if 1
for(size_t i = 0; i != K; ++i)
output(i, N, input + i*block_size, block_size);
for(size_t i = K; i != N; ++i)
{
std::vector<uint8_t> fec_buf(block_size);
for(size_t j = 0; j != K; ++j)
addmul(&fec_buf[0], input + j*block_size,
enc_matrix[i*K+j], block_size);
output(i, N, &fec_buf[0], fec_buf.size());
}
#else
for(size_t i = 0; i != K; ++i)
output(i, N, input + i*block_size, block_size);
// align??
std::vector<std::vector<uint8_t> > fec_buf(N - K);
for(size_t i = 0; i != fec_buf.size(); ++i)
fec_buf[i].resize(block_size);
size_t stride = block_size;
while(stride % 2 == 0 && stride > 64*1024)
stride >>= 1;
for(size_t i = 0; i != size; i += stride)
{
for(size_t j = K; j != N; ++j)
addmul(&fec_buf[j-K][0], input + i,
enc_matrix[j*K+i/block_size], stride);
}
for(size_t i = 0; i != fec_buf.size(); ++i)
output(i+K, N, &fec_buf[i][0], fec_buf[i].size());
#endif
}
/*
* FEC decoding routine
*/
void fec_code::decode(
const std::map<size_t, const uint8_t*>& shares,
size_t share_size,
std::function<void (size_t, size_t, const uint8_t[], size_t)> output) const
{
/*
Todo:
If shares.size() < K:
signal decoding error for missing shares < K
emit existing shares < K
(ie, partial recovery if possible)
Assert share_size % K == 0
*/
if(shares.size() < K)
throw std::logic_error("Could not decode, less than K surviving shares");
std::vector<uint8_t> m_dec(K * K);
std::vector<size_t> indexes(K);
std::vector<const uint8_t*> sharesv(K);
std::map<size_t, const uint8_t*>::const_iterator shares_b_iter =
shares.begin();
std::map<size_t, const uint8_t*>::const_reverse_iterator shares_e_iter =
shares.rbegin();
for(size_t i = 0; i != K; ++i)
{
size_t share_id = 0;
const uint8_t* share_data = 0;
if(shares_b_iter->first == i)
{
share_id = shares_b_iter->first;
share_data = shares_b_iter->second;
++shares_b_iter;
}
else
{
// if share i not found, use the unused one closest to n
share_id = shares_e_iter->first;
share_data = shares_e_iter->second;
++shares_e_iter;
}
if(share_id >= N)
throw std::logic_error("Invalid share id detected during decode");
/*
This is a systematic code (encoding matrix includes K*K identity
matrix), so shares less than K are copies of the input data,
can output directly. Also we know the encoding matrix in those rows
contains I, so we can set the single bit directly without copying
*/
if(share_id < K)
{
m_dec[i*(K+1)] = 1;
output(share_id, K, share_data, share_size);
}
else // will decode after inverting matrix
std::memcpy(&m_dec[i*K], &(enc_matrix[share_id*K]), K);
sharesv[i] = share_data;
indexes[i] = share_id;
}
/*
TODO: if all primary shares were recovered, don't invert the matrix
and return immediately
*/
invert_matrix(&m_dec[0], K);
for(size_t i = 0; i != indexes.size(); ++i)
{
if(indexes[i] >= K)
{
std::vector<uint8_t> buf(share_size);
for(size_t col = 0; col != K; ++col)
addmul(&buf[0], sharesv[col], m_dec[i*K + col], share_size);
output(i, K, &buf[0], share_size);
}
}
}
}