-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbch1022.c
673 lines (600 loc) · 22.3 KB
/
bch1022.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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*----------------------------------------------------------------------*/
/* bch1022.c bch(1022,992) */
/* */
/* Jeff Reid 2021JAN09 11:15 */
/*----------------------------------------------------------------------*/
#include <intrin.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef unsigned long long QWORD;
typedef short SWORD;
/* GF(2^10) = x^10 + x^3 + 1 */
#define POLY 0x409
/* GF(2^10) primitive */
#define ALPHA 0x0002
/* BCH(1022, 992), BN, BK are muliples of 8 */
#define BN 1024
#define BK 992
/* number of suyndromes */
#define NSYN 6
/* ** if != 0, use extended euclid algorithm */
#define EEUCLID 0
/* ** display euclid stuff */
#define DISPLAYE 0
/* ** display stuff */
#define DISPLAY 0
typedef struct{ /* vector structure */
WORD size;
WORD data[31];
}VECTOR;
#if EEUCLID
typedef struct{ /* euclid structure */
WORD size; /* # of data words */
WORD indx; /* index to right side */
WORD data[NSYN+2]; /* left and right side data */
}EUCLID;
#endif
/* ** if QP != 0, use queryperformance for timer */
#define QP 1
#if QP
#include <math.h>
#include <windows.h>
#pragma comment(lib, "winmm.lib")
typedef LARGE_INTEGER LI64;
#else
#include <time.h>
#endif
/*----------------------------------------------------------------------*/
/* data */
/*----------------------------------------------------------------------*/
static WORD gflog2[1024]; /* gflog2 table */
static WORD gfexp2[1024]; /* gfexp2 table */
static WORD minply[3]; /* minimum polynomials */
static WORD mincnt; /* # of minimum polymials */
static BYTE minplyf[1024]; /* minimum polynomial flags */
static DWORD polytbl[256]; /* encode poly table */
static WORD syntbl[256][NSYN]; /* syndrome table */
static BYTE msg[BN/8]; /* encoded message, last 2 bits unused */
#if EEUCLID
static EUCLID E0; /* used by GenpErrors (extended Euclid) */
static EUCLID E1;
#else
static VECTOR vB; /* used by GenpErrors (Berleykamp Massey) */
static VECTOR vC;
static VECTOR vT;
static VECTOR vBx;
#endif
static VECTOR vSyndromes;
static VECTOR pErrors;
static VECTOR pLambda;
static VECTOR vLocators;
static VECTOR vOffsets;
#if QP
static LI64 liQPFrequency; /* cpu counter values */
static LI64 liStartTime;
static LI64 liStopTime;
static double dQPFrequency;
static double dStartTime;
static double dStopTime;
static double dElapsedTime;
#else
static clock_t ctTimeStart; /* clock values */
static clock_t ctTimeStop;
#endif
/*----------------------------------------------------------------------*/
/* code */
/*----------------------------------------------------------------------*/
static void GenMinPoly(void);
static void GenPolyTbl(void);
static void GenSynTbl(void);
static void Encode(void);
static void GenSyndromes(void);
static void GenpErrors(void);
static void GenOffsets(void);
static void FixErrors(void);
static int Poly2Root(VECTOR *, VECTOR *);
static void GFInit(void);
static WORD GFPwr(WORD, WORD);
static WORD GFMpy(WORD, WORD);
static WORD GFDiv(WORD, WORD);
#define GFAdd(a, b) (a^b)
#define GFSub(a, b) (a^b)
static void ShowVector(VECTOR *);
#if EEUCLID
static void ShowEuclid(EUCLID *);
#endif
/*----------------------------------------------------------------------*/
/* main */
/*----------------------------------------------------------------------*/
main()
{
WORD i;
GFInit(); /* init GF(2^10) tables */
GenMinPoly(); /* generate minimum polynomials */
GenPolyTbl(); /* generate 192 bit poly table */
GenSynTbl(); /* generate syndrome table */
for(i = 0; i < (BK/8); i++) /* generate test message */
msg[i] = (BYTE)i;
#if QP
QueryPerformanceFrequency(&liQPFrequency);
dQPFrequency = (double)liQPFrequency.QuadPart;
QueryPerformanceCounter(&liStartTime);
#else
ctTimeStart = clock();
#endif
Encode();
#if QP
QueryPerformanceCounter(&liStopTime);
dStartTime = (double)liStartTime.QuadPart;
dStopTime = (double)liStopTime.QuadPart;
dElapsedTime = (dStopTime - dStartTime) / dQPFrequency;
printf("# of seconds %f\n", dElapsedTime);
#else
ctTimeStop = clock();
printf("# of ticks %u\n", ctTimeStop - ctTimeStart);
#endif
msg[ 0] ^= 0x80; /* test 3 error bit case */
msg[ 64] ^= 0x10;
msg[127] ^= 0x08;
#if QP
QueryPerformanceCounter(&liStartTime);
#else
ctTimeStart = clock();
#endif
GenSyndromes();
GenpErrors();
GenOffsets();
FixErrors();
#if QP
QueryPerformanceCounter(&liStopTime);
dStartTime = (double)liStartTime.QuadPart;
dStopTime = (double)liStopTime.QuadPart;
dElapsedTime = (dStopTime - dStartTime) / dQPFrequency;
printf("# of seconds %f\n", dElapsedTime);
#else
ctTimeStop = clock();
printf("# of ticks %u\n", ctTimeStop - ctTimeStart);
#endif
#if DISPLAY
printf("pErrors: ");
ShowVector(&pErrors);
printf("vLocators:");
ShowVector(&vLocators);
printf("vOffsets: ");
ShowVector(&vOffsets);
#endif
GenSyndromes();
for(i = 0; i < NSYN; i++)
if(vSyndromes.data[i] != 0)
break;
if(i == NSYN)
printf("passed\n");
else
printf("failed\n");
return 0;
}
/*----------------------------------------------------------------------*/
/* GenMinPoly generate 12 mininum polynomials */
/*----------------------------------------------------------------------*/
static void GenMinPoly(void)
{
WORD poly; /* test polynomial */
WORD sum; /* sum, looking for zeroes */
WORD apwr; /* alpha to power */
WORD i,j;
/* find 3 minimum polynomials for 6 powers of 2 */
i = 0;
do{
apwr = GFPwr(2,++i);
for(poly = 0x401; poly <= 0x7ff ; poly++){
sum = 0;
for(j = 0; j <= 10; j++){
if(poly&(1<<j))
sum ^= GFPwr(apwr,j);
}
if(sum == 0){
if(minplyf[poly-0x400] != 0)
continue;
minplyf[poly-0x400] = 1;
minply[mincnt++] = poly;
break;
}
}
}while(i < NSYN);
}
/*----------------------------------------------------------------------*/
/* GenPolyTbl generate 32 bit polynomial table */
/*----------------------------------------------------------------------*/
static void GenPolyTbl(void)
{
__m128i p; /* sub-products and product */
DWORD poly; /* 32 bit polynomial */
DWORD t; /* for table generation */
DWORD b, q, i; /* byte, quotient bit, i */
/* generate poly */
p.m128i_u64[0] = minply[0];
p.m128i_u64[1] = minply[1];
p = _mm_clmulepi64_si128(p, p, 0x01);
p.m128i_u64[1] = minply[2];
p = _mm_clmulepi64_si128(p, p, 0x01);
poly = p.m128i_u32[0]<<2;
/* generate polytbl */
for(b = 0x000; b < 0x100; b++){
t = b<<24;
for(i = 0; i < 8; i++){
q = t>>31;
t = t<<1;
if(q != 0){
t ^= poly;
}
}
polytbl[b] = t;
}
}
/*----------------------------------------------------------------------*/
/* GenSynTbl generate syndrome table */
/*----------------------------------------------------------------------*/
static void GenSynTbl(void)
{
WORD i, j, k, apwr, sum;
for(k = 0; k < 0x100; k++){
for(j = 0; j < NSYN; j++){
apwr = GFPwr(2, j+1);
sum = 0;
for(i = 0; i < 8; i++){
if(k & (1<<i))
sum ^= GFPwr(apwr, i);
}
syntbl[k][j] = sum;
}
}
}
/*----------------------------------------------------------------------*/
/* Encode */
/*----------------------------------------------------------------------*/
static void Encode(void)
{
DWORD r; /* remainder */
QWORD i, j;
r = 0;
for(j = 0; j < BK/8; j += 1){
i = (r>>24)^msg[j]; /* table index */
r = r<<8;
r ^= polytbl[i];
}
msg[(BK/8)+ 0] = (BYTE)(r>>24);
msg[(BK/8)+ 1] = (BYTE)(r>>16);
msg[(BK/8)+ 2] = (BYTE)(r>> 8);
msg[(BK/8)+ 3] = (BYTE)(r>> 0);
}
/*----------------------------------------------------------------------*/
/* GenSyndromes */
/*----------------------------------------------------------------------*/
static void GenSyndromes(void)
{
WORD i, j, apwr;
vSyndromes.size = NSYN;
memset(vSyndromes.data, 0, NSYN*sizeof(WORD));
for(i = 0; i < NSYN; i++){
/* do byte at a time until last byte */
apwr = GFPwr(2, (i+1)<<3);
for(j = 0; j < (BN/8)-1; j++){
vSyndromes.data[i] = GFMpy(vSyndromes.data[i], apwr) ^
syntbl[msg[j]][i];
}
/* do last 6 bits */
apwr = GFPwr(2, i+1);
for(j = BN-8; j < BN-2; j++){
vSyndromes.data[i] = GFMpy(vSyndromes.data[i], apwr) ^
((msg[j/8]>>(7-(j%8)))&1);
}
}
}
#if EEUCLID
/*----------------------------------------------------------------------*/
/* GenpErrors generate pErrors via Euclid division algorithm */
/*----------------------------------------------------------------------*/
static void GenpErrors(void)
{
/* R[] is msb first | A[] is msb last (reversed) */
EUCLID *pED; /* R[i-2] | A[i-1] */
EUCLID *pER; /* R[i-1] | A[i-2] */
EUCLID *pET; /* temp */
int i, j;
WORD bME; /* max errors possible */
WORD bQuot; /* quotient */
/* E0 = initial ED: E0.R[-1] = x^MAXERR, E0.A[0] = 1 */
E0.size = vSyndromes.size+2;
E0.indx = vSyndromes.size+1;
E0.data[0] = 1;
memset(&E0.data[1], 0, vSyndromes.size*sizeof(WORD));
E0.data[E0.indx] = 1;
pED = &E0;
/* E1 = initial ER: E1.R[0] = syndrome polynomial, E1.A[-1] = 0 */
E1.size = vSyndromes.size+2;
E1.indx = vSyndromes.size+1;
E1.data[0] = 0;
for(i = 1; i < E1.indx; i++){
E1.data[i] = vSyndromes.data[vSyndromes.size-i];}
E1.data[E1.indx] = 0;
pER = &E1;
/* init bME */
bME = vSyndromes.size/2;
/* Euclid algorithm */
while(1){ /* while degree ER.R > max errors */
#if DISPLAYE
printf("ED: ");
ShowEuclid(pED);
printf("ER: ");
ShowEuclid(pER);
#endif
while((pER->data[0] == 0) && /* shift dvsr left until msb!=0 */
(pER->indx != 0)){ /* or fully shifted left */
pER->indx--;
memcpy(&pER->data[0], &pER->data[1], (pER->size-1)*sizeof(WORD));
pER->data[pER->size-1] = 0;}
if(pER->indx <= bME){ /* if degree ER.R[] <= bME, break */
break;}
while(1){ /* while more sub-steps */
if(pED->data[0]){ /* if ED.R[] msb!=0, update ED, ER */
bQuot = GFDiv(pED->data[0], pER->data[0]); /* Q=ED.R[msb]/ER.R[msb] */
for(i = 0; i < pER->indx; i++){ /* ED.R[]=ED.R[]-Q*ER.R[] */
pED->data[i] = GFSub(pED->data[i], GFMpy(bQuot, pER->data[i]));}
for(i = pED->indx; i < pER->size; i++){ /* ER.A[]=ER.A[]-Q*ED.A[] */
pER->data[i] = GFSub(pER->data[i], GFMpy(bQuot, pED->data[i]));}}
if(pED->indx == pER->indx){ /* if sub-steps done, break */
break;}
pED->indx--; /* shift ED left */
memcpy(&pED->data[0], &pED->data[1], (pED->size-1)*sizeof(WORD));
pED->data[pED->size-1] = 0;}
pET = pER; /* swap ED, ER */
pER = pED;
pED = pET;}
pErrors.size = pED->size-pED->indx; /* set pErrors.size */
if((pER->indx) >= pErrors.size){ /* if degree ER.R too high */
printf("GenpErrors remainder.size >= errors.size\n");
goto fail0;}
#if 0
j = pErrors.size - 1; /* right shift ER if Omega has leading zeroes */
while(pER->indx < j){
pER->indx++;
for(i = pER->size-1; i;){
i--;
pER->data[i+1] = pER->data[i];}
pER->data[0] = 0;}
#if DISPLAYE
printf("EX: ");
ShowEuclid(pER);
#endif
#endif
/* pErrors = ED.A[] without unreversing = Lambda reversed */
j = pED->indx;
for(i = 0; i < pErrors.size; i++){
pErrors.data[i] = pED->data[j];
j++;}
#if DISPLAYE
printf("pErrors (e): ");
ShowVector(&pErrors);
#endif
/* Make most significant coef pErrors == 1 (divide by it) */
bQuot = pErrors.data[0];
if(bQuot == 0){
printf("GenpErrors most sig coef of pErrors == 0\n");
pLambda.size = 1;
pLambda.data[0] = 1;
goto fail0;}
for(i = 0; i < pErrors.size; i++){
pErrors.data[i] = GFDiv(pErrors.data[i], bQuot);}
#if DISPLAYE
printf("pErrors (E): ");
ShowVector(&pErrors);
#endif
/* Find roots of pErrors (if fail, then set for no roots) */
if(Poly2Root(&vLocators, &pErrors)){ /* solve error poly */
printf("GenpErrors poly2root failed \n");
fail0:
pErrors.size = 1; /* handle no root case */
pErrors.data[0] = 1;
vLocators.size = 0;}
}
#else
/*----------------------------------------------------------------------*/
/* GenpErrors generate pErrors via Berklekamp Massey */
/* note poly most signifcant index == 0 */
/*----------------------------------------------------------------------*/
static void GenpErrors(void)
{
WORD i, j, n;
WORD L, m;
WORD b, d;
WORD db;
b = 1; /* discrepancy when L last updated */
L = 0; /* number of errors */
m = 1; /* # iterations since L last updated */
vB.size = 1;
vB.data[0] = 1;
vC.size = 1;
vC.data[0] = 1;
for(n = 0; n < vSyndromes.size; n++){
if(n&1){ /* BCH only, if odd step, d == 0 */
m += 1;
continue;}
d = vSyndromes.data[n]; /* calculate discrepancy */
for(i = 1; i <= L; i++){
d = GFAdd(d, GFMpy(vC.data[(vC.size - 1)- i], vSyndromes.data[n-i]));}
if(d == 0){ /* if 0 increment m, continue */
m += 1;
continue;}
vT.size = vC.size; /* vT = vC */
memcpy(vT.data, vC.data, vC.size*sizeof(WORD));
db = GFDiv(d,b); /* db = (d/b) */
vBx.size = vB.size+m; /* Bx = x^m B */
memcpy(vBx.data, vB.data, vB.size*sizeof(WORD));
memset(&vBx.data[vB.size], 0, m*sizeof(WORD));
for(i = 0; i < vBx.size; i++){ /* Bx *= db */
vBx.data[i] = GFMpy(vBx.data[i], db);}
j = vBx.size - vC.size; /* right shift vBx or vC */
if(((SWORD)j) > 0){
for(i = vBx.size; i > j; ){
i--;
vC.data[i] = vC.data[i-j];}
memset(vC.data, 0, j*sizeof(WORD));
vC.size += j;}
else if(((SWORD)j) < 0){
j = -j;
for(i = vC.size; i > j; ){
i--;
vBx.data[i] = vBx.data[i-j];}
memset(vBx.data, 0, j*sizeof(WORD));
vBx.size += j;}
for(i = 0; i < vC.size; i++){ /* C -= Bx */
vC.data[i] = GFSub(vC.data[i], vBx.data[i]);}
if(n < 2*L){ /* if L not increasing */
m += 1;
continue;}
vB.size = vT.size; /* B = T */
memcpy(vB.data, vT.data, vT.size*sizeof(WORD));
L = n + 1 - L; /* update L */
b = d; /* update b */
m = 1;} /* reset m */
pErrors.size = vC.size; /* pErrors = reversed VC */
for(i = 0; i < vC.size; i++)
pErrors.data[i] = vC.data[vC.size-1-i];
if(Poly2Root(&vLocators, &pErrors)){ /* solve error poly */
printf("GenpErrors poly2root failed \n");
pErrors.size = 1; /* handle no root case */
pErrors.data[0] = 1;
vLocators.size = 0;}
}
#endif
/*----------------------------------------------------------------------*/
/* GenOffsets */
/*----------------------------------------------------------------------*/
static void GenOffsets(void)
{
WORD i;
vOffsets.size = vLocators.size;
for(i = 0; i < vLocators.size; i++){
vOffsets.data[i] = BN-3-gflog2[vLocators.data[i]];
}
}
/*----------------------------------------------------------------------*/
/* FixErrors */
/*----------------------------------------------------------------------*/
static void FixErrors()
{
WORD i;
for(i = 0; i < vOffsets.size; i++)
msg[vOffsets.data[i]/8] ^= (WORD)(0x80u>>(vOffsets.data[i]%8));
}
/*----------------------------------------------------------------------*/
/* Poly2Root(pVDst, pPSrc) find roots of poly */
/*----------------------------------------------------------------------*/
static int Poly2Root(VECTOR *pVDst, VECTOR *pPSrc)
{
WORD bLcr; /* current locator */
WORD bSum; /* current sum */
WORD bV; /* index to pVDst */
WORD i,j;
pVDst->size = pPSrc->size-1; /* set dest size */
if(!pVDst->size) /* exit if null */
return(0);
bV = 0;
bLcr = 1;
for(j = 0; j < BN; j++){
bSum = 0; /* sum up terms */
for(i = 0; i < pPSrc->size; i++){
bSum = GFMpy(bSum, bLcr);
bSum = GFAdd(bSum, pPSrc->data[i]);}
if(!bSum){ /* if a root */
if(bV == pVDst->size){ /* exit if too many roots */
return(1);}
pVDst->data[bV] = bLcr; /* add locator */
bV++;}
bLcr = GFMpy(bLcr, ALPHA);} /* set up next higher alpha */
if(bV != pVDst->size) /* exit if not enough roots */
return(1);
return(0);
}
/*----------------------------------------------------------------------*/
/* GFInit init GF(2^10) tables */
/*----------------------------------------------------------------------*/
static void GFInit(void)
{
WORD *p0, *p1;
WORD d0;
d0 = 1; /* init gfexp2 table */
p0 = gfexp2;
for(p1 = p0+1024; p0 < p1;){
*p0++ = d0;
d0 <<= 1;
if(d0 & 0x400)
d0 ^= POLY;}
p0 = gfexp2; /* init gflog2 table */
p1 = gflog2;
*p1 = 0;
for(d0 = 0; d0 < 1023; d0 += 1)
*(p1+*p0++) = d0;
}
/*----------------------------------------------------------------------*/
/* GFPwr - exponentiate */
/*----------------------------------------------------------------------*/
static WORD GFPwr(WORD m0, WORD m1)
{
return gfexp2[(WORD)((gflog2[m0]*(DWORD)m1)%1023)];
}
/*----------------------------------------------------------------------*/
/* GFMpy */
/*----------------------------------------------------------------------*/
static WORD GFMpy(WORD m0, WORD m1) /* multiply */
{
int m2;
if(0 == m0 || 0 == m1)
return(0);
m2 = gflog2[m0] + gflog2[m1];
if(m2 > 1023)
m2 -= 1023;
return(gfexp2[m2]);
}
/*----------------------------------------------------------------------*/
/* GFDiv */
/*----------------------------------------------------------------------*/
static WORD GFDiv(WORD m0, WORD m1) /* divide */
{
int m2;
if(0 == m0)
return(0);
m2 = gflog2[m0] - gflog2[m1];
if(m2 < 0)
m2 += 1023;
return(gfexp2[m2]);
}
/*----------------------------------------------------------------------*/
/* ShowVector */
/*----------------------------------------------------------------------*/
static void ShowVector(VECTOR *pVSrc)
{
WORD i;
for(i = 0; i < pVSrc->size; i++ )
printf(" %04x", pVSrc->data[i]);
printf("\n");
}
#if EEUCLID
/*----------------------------------------------------------------------*/
/* ShowEuclid */
/*----------------------------------------------------------------------*/
static void ShowEuclid(EUCLID *pESrc)
{
WORD i;
for(i = 0; i < pESrc->indx; i++){
printf(" %04x", pESrc->data[i]);}
printf("|");
for( ; i < pESrc->size; i++){
printf("%04x ", pESrc->data[i]);}
printf("\n");
}
#endif