-
Notifications
You must be signed in to change notification settings - Fork 21
/
rsvalidate.C
388 lines (351 loc) · 14.1 KB
/
rsvalidate.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
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
#include <array>
#include <map>
#include <set>
#include <random>
#include <memory>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <ezpwd/asserter>
#include <ezpwd/rs>
#include <ezpwd/output>
extern "C" {
#include <rs.h> // Phil Karn's implementation
}
static const int RS_t_LIMIT = 128;
int main()
{
ezpwd::asserter assert;
const int tests = 10000;
// Track the number of success/failures, at varying amounts of error loading. +'ve numbers
// mean greater parity vs. (erasures + 2 x errors).
std::map<int,int> dcodmap; // Decoder returned success
std::map<int,int> succmap; // Decoder actually succeeded
std::map<int,int> failmap; // Decoder failed to decode to correct codeword
std::default_random_engine rnd_gen( (unsigned int)time( 0 ));
std::uniform_int_distribution<int>
rnd_dst_bool( 0, 1 ); // random boolean
std::uniform_int_distribution<int>
rnd_dst_RS_t( 1, RS_t_LIMIT ); // random parity from 1 to 128
std::uniform_int_distribution<int>
rnd_dst_uint8( 0, (1<<8)-1 ); // random uint8_t from 0 to 255
std::uniform_int_distribution<int>
rnd_dst_uint8_nz( 1, (1<<8)-1 );// random uint8_t from 1 to 255
typedef std::map<int,std::shared_ptr<ezpwd::reed_solomon_base>>
rscodec_t;
rscodec_t rscodec; // All available RS codecs
rscodec[1] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-1> );
rscodec[2] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-2> );
rscodec[3] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-3> );
rscodec[4] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-4> );
rscodec[7] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-7> );
rscodec[9] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-9> );
rscodec[12] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-12> );
rscodec[16] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-16> );
rscodec[17] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-17> );
rscodec[27] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-27> );
rscodec[46] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-46> );
rscodec[77] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-77> );
rscodec[99] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-99> );
rscodec[127] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-127> );
rscodec[128] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-128> );
rscodec[129] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-129> );
rscodec[199] = rscodec_t::mapped_type( new ezpwd::RS<255, 255-199> );
auto rsi = rscodec.end();
for ( int t = 0; t < tests; ++t ) {
int failures= assert.failures;
std::ostringstream failmsgs;
// Traverse through the available R-S codecs
if ( rsi == rscodec.end() )
rsi = rscodec.begin();
auto rs2 = rsi++->second;
// Select a payload which is a subset of the possible R-S load w/ the given parity
int parity = rs2->nroots();
int payload = std::uniform_int_distribution<int>( 1, rs2->load() )( rnd_gen );
int pad = rs2->load() - payload;
// Get a fresh data payload of the maximum possible number of payload data
std::array<uint8_t,255> buf;
for ( auto &c : buf )
c = rnd_dst_uint8( rnd_gen );
failmsgs
<< "original payload:"
<< std::endl
<< std::vector<uint8_t>( buf.begin() + pad, buf.begin() + pad + payload )
<< std::endl;
// Phil Karn's standard encoder in enc1, ours in enc2
std::array<uint8_t,255> enc1;
std::copy( buf.begin(), buf.end(), enc1.begin() );
void *rs1 = ::init_rs_char( 8, 0x011d, 1, 1, parity, pad );
::encode_rs_char( rs1, enc1.begin() + pad, enc1.begin() + pad + payload );
std::array<uint8_t,255> enc2;
std::copy( buf.begin(), buf.end(), enc2.begin() );
rs2->encode( enc2, pad );
std::vector<uint8_t> cmp2( 255, ' ' );
int cmp2cnt = 0;
for ( int i = 0; i < 255; ++i ) {
if ( enc2[i] != enc1[i] ) {
cmp2[i] = '^';
++cmp2cnt;
}
}
if ( assert.ISEQUAL( cmp2cnt, 0, "ezpwd::reed_solomon encoder didn't match legacy encoder" ))
failmsgs
<< "legacy encoded:"
<< std::endl
<< std::vector<uint8_t>( enc1.begin() + pad, enc1.begin() + pad + payload + parity )
<< std::endl
<< *rs2 << " encoded:"
<< std::endl
<< std::vector<uint8_t>( enc2.begin() + pad, enc2.begin() + pad + payload + parity )
<< std::endl
<< "encoding varies!"
<< std::endl
<< std::vector<uint8_t>( cmp2.begin() + pad, cmp2.begin() + pad + payload + parity )
<< std::endl;
//
// Test max. error and erasure load, to ensure correct decoding (with error detection
// capacity to spare, after all erasures and errors corrected); Test right past the
// edge of correction capacity, and complain if it could not correct, when it should
// be able to!
//
// erasure <= parity
// 2 * error <= parity
// erasure + 2 * error <= parity
//
// The target error load is 100% +/- 10% of the parity capacity.
std::array<uint8_t,255> err1;
std::copy( enc1.begin(), enc1.end(), err1.begin() );
std::vector<uint8_t> err1flg( 255, ' ' );
int target = std::uniform_int_distribution<int>( parity * 90 / 100, parity * 110 / 100 )( rnd_gen );
int err1cnt = 0;
int era1cnt = 0;
switch ( std::uniform_int_distribution<int>( 0, 3 )( rnd_gen )) {
case 0: default:
// No errors.
break;
case 1:
// Random number of errors/erasures (sometimes beyond capacity)
err1cnt = std::uniform_int_distribution<int>( 0, target / 2 )( rnd_gen );
era1cnt = target - err1cnt * 2;
break;
case 2:
// All errors (max capacity, and sometimes beyond)
err1cnt = target / 2;
break;
case 3:
// All erasures (max capacity, and sometimes beyond)
era1cnt = target;
break;
}
// Make certain we have enough room in the payload and parity for all the errors, erasures.
// We are going to put each error and erasure at a unique spot. This will only come up when
// we use R-S codecs with very large numbers of parity, exceeding the payload load.
err1cnt = std::min( err1cnt, payload + parity );
era1cnt = std::min( era1cnt, std::max( payload + parity - err1cnt, 0 ));
// Figure out if we should succeed. Certainly, if we have excess parity vs. error load.
// Always, if the error load < parity. Almost never, if the error load > parity.
bool succeed = ( era1cnt + 2 * err1cnt <= parity );
if ( ! succeed )
failmsgs
<< "Decoder overwhemlmed! Results non-deterministic."
<< std::endl;
failmsgs
<< "Test " << *rs2 << " w/ "
<< std::setw( 5 ) << payload << " payload. "
<< std::setw( 5 ) << t << ": "
<< std::setw( 3 ) << era1cnt << " erasures + 2 x "
<< std::setw( 3 ) << err1cnt << " errors == "
<< std::setw( 3 ) << era1cnt + err1cnt * 2 << " vs. "
<< std::setw( 3 ) << parity << " parity"
<< std::endl;
for ( int i = 0; i < err1cnt; ++i ) {
// Pick a new spot for each error
int err;
do {
err = std::uniform_int_distribution<int>( pad, pad + payload + parity - 1 )( rnd_gen );
} while ( err1[err] != enc1[err] );
err1[err] ^= std::uniform_int_distribution<int>( 1, 255 )( rnd_gen );
err1flg[err] = 'e';
}
std::vector<int> era1;
for ( int i = 0; i < era1cnt; ++i ) {
// Pick a new spot for each erasure. Also ensure the entry is modified.
int era;
do {
era = std::uniform_int_distribution<int>( pad, pad + payload + parity - 1 )( rnd_gen );
} while ( err1[era] != enc1[era] );
era1.push_back( era );
err1[era] ^= std::uniform_int_distribution<int>( 1, 255 )( rnd_gen );
err1flg[era] = 'x';
}
failmsgs
<< "Erroneous buffer: "
<< std::endl
<< std::vector<uint8_t>( err1.begin() + pad, err1.begin() + pad + payload + parity )
<< std::endl
<< std::setw( 3 ) << err1cnt << " e (error), "
<< std::setw( 3 ) << era1cnt << " x (erase) "
<< std::endl
<< std::vector<uint8_t>( err1flg.begin() + pad, err1flg.begin() + pad + payload + parity )
<< std::endl;
std::array<uint8_t,255> err2;
std::copy( err1.begin(), err1.end(), err2.begin() );
std::vector<int> era2;
for ( auto e: era1 )
era2.push_back( e - pad );
// Use the standard decoder, and check the results against the encoded data. DO NOT attempt
// to use decoder if our erasure count has already exceeded the parity; the decoder may
// overrun internal buffers (the 'lambda' buffer, to be precise, when 'no_eras' exceeds
// 'NROOTS'). Remember; the position of all corrections comes back into the erasures array,
// so we must expand it to the maximum possible number of corrections -- the parity, or
// number of roots (NROOTS). Since we don't know exactly 'til after the call, we'll resize
// it before and then shrink it after, but only use the first 'era1cnt' entries.
int res1 = -1;
era1.resize( parity );
if ( era1cnt <= parity ) {
res1 = ::decode_rs_char( rs1, &err1.front() + pad, &era1.front(), era1cnt );
if ( assert.ISTRUE( res1 <= parity, "Number of corrections incorrectly exceeded parity" ))
failmsgs
<< assert
<< std::endl;
if ( res1 > 0 )
era1.resize( res1 );
}
if ( succeed ) {
// We expect success!
if ( assert.ISEQUAL( res1, era1cnt + err1cnt, "legacy decoder result isn't sum of erasures + errors'" ))
failmsgs
<< assert
<< "Decoded buffer:"
<< std::endl
<< std::vector<uint8_t>( err1.begin() + pad, err1.begin() + pad + payload + parity )
<< std::endl;
} else if ( res1 >= 0 ) {
// The decoder may (and usually does, incorrectly, but unavoidably) resolve a
// correct "codeword", if the error density is too high...
failmsgs
<< "Decoder return successful completion(" << res1
<< ", vs " << era1cnt + err1cnt << " errors/erasures), unexpectedly! "
<< std::endl;
}
std::vector<uint8_t> dif1( 255, ' ' );
int dif1cnt = 0;
for ( int i = 0; i < 255; ++i ) {
if ( err1[i] != enc1[i] ) {
dif1[i] = '^';
++dif1cnt;
}
}
if ( succeed ) {
if ( assert.ISEQUAL( dif1cnt, 0, "legacy decoder failed" ))
failmsgs
<< assert
<< "Differences (original):"
<< std::endl
<< std::vector<uint8_t>( dif1.begin() + pad, dif1.begin() + pad + payload + parity )
<< std::endl;
} else if ( dif1cnt == 0 && ( err1cnt + era1cnt ) != 0 ) {
failmsgs
<< "Decoding resulted in correct output, unexpectedly!"
<< std::endl;
}
::free_rs_char( rs1 );
int res2 = -1;
std::vector<int> pos2;
if ( era1cnt <= parity )
res2 = rs2->decode( err2, pad, era2, &pos2 );
// If error load is below correction threshold, decoder results should always be identical,
// no matter what. However, if we've overwhelmed the R-S decoder with errors, the new
// decoder MAY return different results. This is because the Phil Karn decoder will return
// error positions in the "pad" area, if the overwhelmed R-S Galois field polynomial solves
// to roots located there! We know this is impossible (the unused "pad" area of the R-S
// decoder buffer is all zeros). Therefore, the new decoder detects this situation and
// returns a failure, instead of the (invalid) erasure positions.
if ( succeed )
if ( assert.ISEQUAL( res2, res1, "ezpwd decoder return different results" ))
failmsgs
<< assert
<< *rs2 << " decoded buffer:"
<< std::endl
<< std::vector<uint8_t>( err2.begin() + pad, err2.begin() + pad + payload + parity )
<< std::endl;
if ( res2 >= 0 && assert.ISEQUAL( res2, int( pos2.size() ), "ezpwd decoder return +'ve value, but different number of positions" ))
failmsgs
<< assert
<< *rs2 << " decoded buffer:"
<< std::endl
<< std::vector<uint8_t>( err2.begin() + pad, err2.begin() + pad + payload + parity )
<< "; wrong position count: " << pos2.size() << " vs. return value: " << res2
<< std::endl;
if ( res1 >= 0 && res2 >= 0 ) {
// Both R-S decoders claimed to solve the codeword; they should be equivalent
std::vector<uint8_t> dif2( 255, ' ' );
int dif2cnt = 0;
for ( int i = 0; i < 255; ++i ) {
if ( err2[i] != enc2[i] ) {
dif2[i] = '^';
++dif2cnt;
}
}
if ( assert.ISEQUAL( dif2cnt, dif1cnt )) // Results should be identical
failmsgs
<< assert
<< "differences:"
<< std::endl
<< std::vector<uint8_t>( dif2.begin() + pad, dif2.begin() + pad + payload + parity )
<< std::endl;
}
#if ! defined( DEBUG )
if ( assert.failures != failures )
#endif
std::cout
<< "Detected " << assert.failures - failures << " new failures!"
<< std::endl
<< failmsgs.str()
<< std::endl;
if ( era1cnt + err1cnt > 0 ) {
// Only track tests with some erasures/errors; obviously no errors/erasure tests
// will succeed! Calculate the excess/shortfall in error detection capacity, and
// use that to categorize the results.
int capacity = parity - ( era1cnt + 2 * err1cnt );
if ( res1 >= 0 )
++dcodmap[capacity]; // The decoder claimed to succeed
if ( dif1cnt == 0 )
++succmap[capacity]; // The decoder actually succeeded
else
++failmap[capacity]; // The decoder did not obtain correct results
}
}
std::set<int> indices;
for ( std::map<int,int>::iterator di = dcodmap.begin()
; di != dcodmap.end()
; ++di )
indices.insert( di->first );
for ( std::map<int,int>::iterator si = succmap.begin()
; si != succmap.end()
; ++si )
indices.insert( si->first );
for ( std::map<int,int>::iterator fi = failmap.begin()
; fi != failmap.end()
; ++fi )
indices.insert( fi->first );
std::cout
<< "parity-(era+2*err) decoded successes failures (-'ve ==> error load > parity capability)"
<< std::endl;
for ( std::set<int>::iterator ii = indices.begin()
; ii != indices.end()
; ++ii ) {
std::cout
<< std::setw( 18 ) << *ii << " "
<< std::setw( 7 ) << dcodmap[*ii] << " "
<< std::setw( 9 ) << succmap[*ii] << " "
<< std::setw( 8 ) << failmap[*ii]
<< std::endl;
if ( *ii >= 0 ) {
// Any R-S decode test with a parity >= the error loading should never fail
std::cout << assert.ISEQUAL( failmap[*ii], 0 );
}
}
return assert.failures ? 1 : 0;
}