-
Notifications
You must be signed in to change notification settings - Fork 56
/
WirehairCodec.cpp
4364 lines (3448 loc) · 151 KB
/
WirehairCodec.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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file
\brief Wirehair : Codec Implementation
\copyright Copyright (c) 2012-2018 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Wirehair nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "WirehairCodec.h"
//------------------------------------------------------------------------------
// Precompiler-conditional console output
#if defined(CAT_DUMP_PIVOT_FAIL)
#define CAT_IF_PIVOT(x) x
#else
#define CAT_IF_PIVOT(x)
#endif
#if defined(CAT_DUMP_CODEC_DEBUG)
#define CAT_IF_DUMP(x) x
#else
#define CAT_IF_DUMP(x)
#endif
#if defined(CAT_DUMP_ROWOP_COUNTERS)
#define CAT_IF_ROWOP(x) x
#else
#define CAT_IF_ROWOP(x)
#endif
#if defined(CAT_DUMP_CODEC_DEBUG) || defined(CAT_DUMP_PIVOT_FAIL) || \
defined(CAT_DUMP_ROWOP_COUNTERS) || defined(CAT_DUMP_GE_MATRIX)
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
#endif
namespace wirehair {
//------------------------------------------------------------------------------
// Stage (1) Peeling:
bool Codec::OpportunisticPeeling(
const uint16_t row_i, ///< Row index
const uint32_t row_seed ///< Row PRNG seed
)
{
PeelRow *row = &_peel_rows[row_i];
row->RecoveryId = row_seed;
row->Params.Initialize(row_seed, _p_seed, _block_count, _mix_count);
CAT_IF_DUMP(cout << "Row " << row_seed << " in slot " << row_i << " of weight "
<< row->Params.PeelCount << " [a=" << row->Params.PeelAdd << "] : ";)
PeelRowIterator iter(row->Params, _block_count, _block_next_prime);
uint16_t unmarked_count = 0;
uint16_t unmarked[2];
// Iterate columns in peeling matrix
do
{
const uint16_t column_i = iter.GetColumn();
CAT_IF_DUMP(cout << column_i << " ";)
PeelRefs *refs = &_peel_col_refs[column_i];
// If there was not enough room in the reference list:
if (refs->RowCount >= CAT_REF_LIST_MAX)
{
CAT_IF_DUMP(cout << "OpportunisticPeeling: Failure! " \
"Ran out of space for row references. CAT_REF_LIST_MAX must be increased!" << endl;)
CAT_DEBUG_BREAK();
FixPeelFailure(row, column_i);
return false;
}
// Add row reference to column
refs->Rows[refs->RowCount++] = row_i;
// If column is unmarked:
if (_peel_cols[column_i].Mark == MARK_TODO) {
unmarked[unmarked_count++ & 1] = column_i;
}
} while (iter.Iterate());
CAT_IF_DUMP(cout << endl;)
// Initialize row state
row->UnmarkedCount = unmarked_count;
switch (unmarked_count)
{
case 0:
// Link at head of defer list
row->NextRow = _defer_head_rows;
_defer_head_rows = row_i;
break;
case 1:
// Solve only unmarked column with this row
SolveWithPeel(
row,
row_i,
unmarked[0]);
break;
case 2:
// Remember which two columns were unmarked
row->Marks.Unmarked[0] = unmarked[0];
row->Marks.Unmarked[1] = unmarked[1];
// Increment weight-2 reference count for unmarked columns
_peel_cols[unmarked[0]].Weight2Refs++;
_peel_cols[unmarked[1]].Weight2Refs++;
break;
}
return true;
}
void Codec::FixPeelFailure(
PeelRow * GF256_RESTRICT row, ///< The row that failed
const uint16_t fail_column_i ///< Column end point
)
{
CAT_IF_DUMP(cout << "!!Fixing Peel Failure!! Unreferencing columns, ending at "
<< fail_column_i << " :";)
PeelRowIterator iter(row->Params, _block_count, _block_next_prime);
// Iterate columns in peeling matrix
do
{
const uint16_t column = iter.GetColumn();
if (column == fail_column_i) {
break;
}
CAT_IF_DUMP(cout << " " << column;)
PeelRefs * GF256_RESTRICT refs = &_peel_col_refs[column];
// Subtract off row count.
// This invalidates the row number that was written earlier
refs->RowCount--;
} while (iter.Iterate());
CAT_IF_DUMP(cout << endl;)
}
void Codec::PeelAvalancheOnSolve(
uint16_t column_i ///< Column that was solved
)
{
PeelRefs * GF256_RESTRICT refs = &_peel_col_refs[column_i];
uint16_t ref_row_count = refs->RowCount;
uint16_t * GF256_RESTRICT ref_rows = refs->Rows;
// Walk list of peeled rows referenced by this newly solved column
while (ref_row_count--)
{
// Update unmarked row count for this referenced row
uint16_t ref_row_i = *ref_rows++;
PeelRow * GF256_RESTRICT ref_row = &_peel_rows[ref_row_i];
uint16_t unmarked_count = --ref_row->UnmarkedCount;
// If row may be solving a column now:
if (unmarked_count == 1)
{
uint16_t new_column_i = ref_row->Marks.Unmarked[0];
// If that is this column:
if (new_column_i == column_i) {
new_column_i = ref_row->Marks.Unmarked[1];
}
/*
Rows that are to be deferred will either end up
here or below where it handles the case of there
being no columns unmarked in a row.
*/
// If column is already solved:
if (_peel_cols[new_column_i].Mark == MARK_TODO)
{
SolveWithPeel(
ref_row,
ref_row_i,
new_column_i);
continue;
}
CAT_IF_DUMP(cout << "PeelAvalancheOnSolve: Deferred(1) with column " <<
column_i << " at row " << ref_row_i << endl;)
// Link at head of defer list
ref_row->NextRow = _defer_head_rows;
_defer_head_rows = ref_row_i;
}
else if (unmarked_count == 2)
{
// Regenerate the row columns to discover which are unmarked
PeelRowIterator ref_iter(ref_row->Params, _block_count, _block_next_prime);
uint16_t store_count = 0;
// For each column:
do
{
const uint16_t ref_column_i = ref_iter.GetColumn();
PeelColumn * GF256_RESTRICT ref_col = &_peel_cols[ref_column_i];
// If column is unmarked:
if (ref_col->Mark == MARK_TODO)
{
// Store the two unmarked columns in the row
ref_row->Marks.Unmarked[store_count++] = ref_column_i;
// Increment weight-2 reference count (cannot hurt even if not true)
ref_col->Weight2Refs++;
}
} while (ref_iter.Iterate());
/*
This is a little subtle, but sometimes the avalanche will
happen here, and sometimes a row will be marked deferred.
*/
if (store_count <= 1)
{
// Insure that this row won't be processed further during this recursion
ref_row->UnmarkedCount = 0;
// If row is to be deferred:
if (store_count == 1)
{
SolveWithPeel(
ref_row,
ref_row_i,
ref_row->Marks.Unmarked[0]);
continue;
}
CAT_IF_DUMP(cout << "PeelAvalancheOnSolve: Deferred(2) with column " << column_i << " at row " << ref_row_i << endl;)
// Link at head of defer list
ref_row->NextRow = _defer_head_rows;
_defer_head_rows = ref_row_i;
}
}
}
}
void Codec::SolveWithPeel(
PeelRow * GF256_RESTRICT row, ///< Pointer to row data
uint16_t row_i, ///< Row index
uint16_t column_i ///< Column that this solves
)
{
CAT_IF_DUMP(cout << "Peel: Solved column " << column_i << " with row " << row_i << endl;)
PeelColumn * GF256_RESTRICT column = &_peel_cols[column_i];
// Mark this column as solved
column->Mark = MARK_PEEL;
// Remember which column it solves
row->Marks.Result.PeelColumn = column_i;
// Link to back of the peeled list
if (_peel_tail_rows) {
_peel_tail_rows->NextRow = row_i;
}
else {
_peel_head_rows = row_i;
}
row->NextRow = LIST_TERM;
_peel_tail_rows = row;
// Indicate that this row hasn't been copied yet
row->Marks.Result.IsCopied = 0;
// Attempt to avalanche and solve other columns
PeelAvalancheOnSolve(column_i);
// Remember which row solves the column, after done with rows list
column->PeelRow = row_i;
}
void Codec::GreedyPeeling()
{
CAT_IF_DUMP(cout << endl << "---- GreedyPeeling ----" << endl << endl;)
// Initialize list
_defer_head_columns = LIST_TERM;
_defer_count = 0;
const unsigned block_count = _block_count;
// Until all columns are marked:
for (;;)
{
uint16_t best_column_i = LIST_TERM;
unsigned best_w2_refs = 0;
unsigned best_row_count = 0;
const PeelColumn *column = _peel_cols;
// For each peel column:
for (uint16_t column_i = 0; column_i < block_count; ++column_i, ++column)
{
// If column is not marked yet:
if (column->Mark == MARK_TODO)
{
const unsigned w2_refs = column->Weight2Refs;
// If it may have the most weight-2 references:
if (w2_refs >= best_w2_refs)
{
const unsigned row_count = _peel_col_refs[column_i].RowCount;
// If it has the largest row references overall:
if (w2_refs > best_w2_refs || row_count >= best_row_count)
{
// Use that one
best_column_i = column_i;
best_w2_refs = w2_refs;
best_row_count = row_count;
}
}
}
}
// If no column was found:
if (best_column_i == LIST_TERM) {
// Peeling is complete
break;
}
// Mark column as deferred
PeelColumn *best_column = &_peel_cols[best_column_i];
best_column->Mark = MARK_DEFER;
++_defer_count;
// Add at head of deferred list
best_column->Next = _defer_head_columns;
_defer_head_columns = best_column_i;
CAT_IF_DUMP(cout << "Deferred column " << best_column_i <<
" for Gaussian elimination, which had " << best_column->Weight2Refs <<
" weight-2 row references" << endl;)
// Peel resuming from where this column left off
PeelAvalancheOnSolve(best_column_i);
}
}
//------------------------------------------------------------------------------
// Stage (2) Compression
void Codec::SetDeferredColumns()
{
CAT_IF_DUMP(cout << endl << "---- SetDeferredColumns ----" << endl << endl;)
PeelColumn * GF256_RESTRICT column;
// For each deferred column:
for (uint16_t ge_column_i = 0, defer_i = _defer_head_columns;
defer_i != LIST_TERM;
defer_i = column->Next, ++ge_column_i)
{
column = &_peel_cols[defer_i];
CAT_IF_DUMP(cout << "GE column " << ge_column_i <<
" mapped to matrix column " << defer_i << " :";)
// Get pointer to this matrix row
uint64_t *matrix_row_offset = _compress_matrix + (ge_column_i >> 6);
// Get word mask for this column bit
const uint64_t ge_mask = (uint64_t)1 << (ge_column_i & 63);
// Get references for this deferred index
const PeelRefs * GF256_RESTRICT refs = &_peel_col_refs[defer_i];
// For each affected row:
for (unsigned i = 0, count = refs->RowCount; i < count; ++i)
{
const uint16_t row_i = refs->Rows[i];
CAT_IF_DUMP(cout << " " << row_i;)
matrix_row_offset[_ge_pitch * row_i] |= ge_mask;
}
CAT_IF_DUMP(cout << endl;)
// Set column map for this GE column
_ge_col_map[ge_column_i] = defer_i;
// Set reverse mapping also
column->GEColumn = ge_column_i;
}
// Set column map for each mix column:
for (uint16_t added_i = 0, count = _mix_count; added_i < count; ++added_i)
{
CAT_DEBUG_ASSERT((unsigned)_defer_count + (unsigned)added_i < 65536);
CAT_DEBUG_ASSERT((unsigned)_block_count + (unsigned)added_i < 65536);
const uint16_t ge_column_i = _defer_count + added_i;
const uint16_t column_i = _block_count + added_i;
CAT_IF_DUMP(cout << "GE column(mix) " << ge_column_i <<
" mapped to matrix column " << column_i << endl;)
_ge_col_map[ge_column_i] = column_i;
}
}
void Codec::SetMixingColumnsForDeferredRows()
{
CAT_IF_DUMP(cout << endl << "---- SetMixingColumnsForDeferredRows ----" << endl << endl;)
PeelRow * GF256_RESTRICT row;
// For each deferred row:
for (uint16_t defer_row_i = _defer_head_rows;
defer_row_i != LIST_TERM;
defer_row_i = row->NextRow)
{
row = &_peel_rows[defer_row_i];
CAT_IF_DUMP(cout << "Deferred row " << defer_row_i << " set mix columns :";)
// Mark it as deferred for the following loop
row->Marks.Result.PeelColumn = LIST_TERM;
// Set up mixing column generator
uint64_t *ge_row = _compress_matrix + _ge_pitch * defer_row_i;
const unsigned defer_count = _defer_count;
const RowMixIterator mix(row->Params, _mix_count, _mix_next_prime);
// Generate mixing column 1
const unsigned ge_column_i = defer_count + mix.Columns[0];
ge_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
CAT_IF_DUMP(cout << " " << ge_column_i;)
// Generate mixing column 2
const unsigned ge_column_j = defer_count + mix.Columns[1];
ge_row[ge_column_j >> 6] ^= (uint64_t)1 << (ge_column_j & 63);
CAT_IF_DUMP(cout << " " << ge_column_j;)
// Generate mixing column 3
const unsigned ge_column_k = defer_count + mix.Columns[2];
ge_row[ge_column_k >> 6] ^= (uint64_t)1 << (ge_column_k & 63);
CAT_IF_DUMP(cout << " " << ge_column_k;)
CAT_IF_DUMP(cout << endl;)
}
}
void Codec::PeelDiagonal()
{
CAT_IF_DUMP(cout << endl << "---- PeelDiagonal ----" << endl << endl;)
/*
This function optimizes the block value generation by combining the first
memcpy and memxor operations together into a three-way memxor if possible,
using the is_copied row member.
*/
CAT_IF_ROWOP(unsigned rowops = 0;)
PeelRow * GF256_RESTRICT row;
// For each peeled row in forward solution order:
for (uint16_t peel_row_i = _peel_head_rows;
peel_row_i != LIST_TERM;
peel_row_i = row->NextRow)
{
row = &_peel_rows[peel_row_i];
// Lookup peeling results
const uint16_t peel_column_i = row->Marks.Result.PeelColumn;
uint64_t *ge_row = _compress_matrix + _ge_pitch * peel_row_i;
CAT_IF_DUMP(cout << "Peeled row " << peel_row_i << " for peeled column " << peel_column_i << " :";)
const unsigned defer_count = _defer_count;
const RowMixIterator mix(row->Params, _mix_count, _mix_next_prime);
// Generate mixing column 1
const unsigned ge_column_i = defer_count + mix.Columns[0];
ge_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
CAT_IF_DUMP(cout << " " << ge_column_i;)
// Generate mixing column 2
const unsigned ge_column_j = defer_count + mix.Columns[1];
ge_row[ge_column_j >> 6] ^= (uint64_t)1 << (ge_column_j & 63);
CAT_IF_DUMP(cout << " " << ge_column_j;)
// Generate mixing column 3
const unsigned ge_column_k = defer_count + mix.Columns[2];
ge_row[ge_column_k >> 6] ^= (uint64_t)1 << (ge_column_k & 63);
CAT_IF_DUMP(cout << " " << ge_column_k << endl;)
// Get pointer to output block
CAT_DEBUG_ASSERT(peel_column_i < _recovery_rows);
uint8_t * GF256_RESTRICT temp_block_src = _recovery_blocks + _block_bytes * peel_column_i;
// If row has not been copied yet:
if (!row->Marks.Result.IsCopied)
{
const uint8_t * GF256_RESTRICT block_src = _input_blocks + _block_bytes * peel_row_i;
// If this is not the last block:
if (peel_row_i != _block_count - 1) {
// Copy it directly to the output block
memcpy(temp_block_src, block_src, _block_bytes);
}
else
{
// Copy with zero padding
memcpy(temp_block_src, block_src, _input_final_bytes);
CAT_DEBUG_ASSERT(_block_bytes >= _input_final_bytes);
memset(temp_block_src + _input_final_bytes, 0, _block_bytes - _input_final_bytes);
}
CAT_IF_ROWOP(++rowops;)
CAT_IF_DUMP(cout << "-- Copied from " << peel_row_i <<
" because has not been copied yet. Output block = " <<
(unsigned)temp_block_src[0] << endl;)
// Note that we do not need to set is_copied here because no
// further rows reference this one
}
CAT_IF_DUMP(cout << "++ Adding to referencing rows:";)
PeelRefs * GF256_RESTRICT refs = &_peel_col_refs[peel_column_i];
const uint16_t * GF256_RESTRICT referencingRows = refs->Rows;
// For each row that references this one:
for (unsigned i = 0, count = refs->RowCount; i < count; ++i)
{
const uint16_t ref_row_i = referencingRows[i];
// If it references the current row:
if (ref_row_i == peel_row_i) {
// Skip this row
continue;
}
CAT_IF_DUMP(cout << " " << ref_row_i;)
uint64_t * GF256_RESTRICT ge_ref_row = _compress_matrix + _ge_pitch * ref_row_i;
// Add GE row to referencing GE row
for (unsigned j = 0; j < _ge_pitch; ++j) {
ge_ref_row[j] ^= ge_row[j];
}
PeelRow * GF256_RESTRICT ref_row = &_peel_rows[ref_row_i];
const uint16_t ref_column_i = ref_row->Marks.Result.PeelColumn;
// If row is peeled:
if (ref_column_i != LIST_TERM)
{
// Generate temporary row block value:
CAT_DEBUG_ASSERT(ref_column_i < _recovery_rows);
uint8_t * GF256_RESTRICT temp_block_dest = _recovery_blocks + _block_bytes * ref_column_i;
// If referencing row is already copied to the recovery blocks:
if (ref_row->Marks.Result.IsCopied) {
// Add this row block value to it
gf256_add_mem(temp_block_dest, temp_block_src, _block_bytes);
}
else
{
const uint8_t * GF256_RESTRICT block_src = _input_blocks + _block_bytes * ref_row_i;
// If this is not the last block:
if (ref_row_i != _block_count - 1) {
// Add this row block value with message block to it (optimization)
gf256_addset_mem(temp_block_dest, temp_block_src, block_src, _block_bytes);
}
else
{
// Add with zero padding
gf256_addset_mem(temp_block_dest, temp_block_src, block_src, _input_final_bytes);
CAT_DEBUG_ASSERT(_block_bytes >= _input_final_bytes);
memcpy(
temp_block_dest + _input_final_bytes,
temp_block_src + _input_final_bytes,
_block_bytes - _input_final_bytes);
}
ref_row->Marks.Result.IsCopied = 1;
}
CAT_IF_ROWOP(++rowops;)
} // end if referencing row is peeled
} // next referencing row
CAT_IF_DUMP(cout << endl;)
} // next peeled row
CAT_IF_ROWOP(cout << "PeelDiagonal used " << rowops << " row ops = "
<< rowops / (double)_block_count << "*N" << endl;)
}
void Codec::CopyDeferredRows()
{
CAT_IF_DUMP(cout << endl << "---- CopyDeferredRows ----" << endl << endl;)
// Get GE matrix row starting at dense rows
uint64_t * GF256_RESTRICT ge_row = _ge_matrix + _ge_pitch * _dense_count;
// For each deferred row:
for (uint16_t ge_row_i = _dense_count, defer_row_i = _defer_head_rows;
defer_row_i != LIST_TERM;
++ge_row_i, ge_row += _ge_pitch)
{
CAT_IF_DUMP(cout << "Peeled row " << defer_row_i << " for GE row " << ge_row_i << endl;)
// Get Compress matrix row
uint64_t * GF256_RESTRICT compress_row = _compress_matrix + _ge_pitch * defer_row_i;
// Copy Compress row to GE row
memcpy(ge_row, compress_row, _ge_pitch * sizeof(uint64_t));
// Set row map for this deferred row
_ge_row_map[ge_row_i] = defer_row_i;
// Get next deferred row from peeling solver output
defer_row_i = _peel_rows[defer_row_i].NextRow;
}
}
void Codec::MultiplyDenseRows()
{
CAT_IF_DUMP(cout << endl << "---- MultiplyDenseRows ----" << endl << endl;)
// Initialize PRNG
PCGRandom prng;
prng.Seed(_d_seed);
const PeelColumn * GF256_RESTRICT column = _peel_cols;
uint64_t * GF256_RESTRICT temp_row = _ge_matrix + _ge_pitch * (_dense_count + _defer_count);
const unsigned dense_count = _dense_count;
uint16_t rows[CAT_MAX_DENSE_ROWS];
uint16_t bits[CAT_MAX_DENSE_ROWS];
// For each block of columns:
for (unsigned column_i = 0, block_count = _block_count;
column_i < block_count;
column_i += dense_count, column += dense_count)
{
CAT_IF_DUMP(cout << "Shuffled dense matrix starting at column "
<< column_i << ":" << endl;)
unsigned max_x = dense_count;
// Handle final columns
if (column_i + dense_count > _block_count) {
CAT_DEBUG_ASSERT(_block_count >= column_i);
max_x = _block_count - column_i;
}
// Shuffle row and bit order
ShuffleDeck16(prng, rows, dense_count);
ShuffleDeck16(prng, bits, dense_count);
// Initialize counters
const unsigned set_count = (dense_count + 1) >> 1;
const uint16_t * GF256_RESTRICT set_bits = bits;
const uint16_t * GF256_RESTRICT clr_bits = set_bits + set_count;
CAT_IF_DUMP(uint64_t disp_row[(CAT_MAX_DENSE_ROWS + 63) / 64] = {};)
memset(temp_row, 0, _ge_pitch * sizeof(uint64_t));
// Generate first row
for (unsigned ii = 0; ii < set_count; ++ii)
{
const unsigned bit_i = set_bits[ii];
// If bit is peeled:
if (bit_i < max_x)
{
if (column[bit_i].Mark == MARK_PEEL)
{
const uint64_t * GF256_RESTRICT ge_source_row = _compress_matrix + _ge_pitch * column[bit_i].PeelRow;
// Add temp row value
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
temp_row[jj] ^= ge_source_row[jj];
}
}
else
{
const unsigned ge_column_i = column[bit_i].GEColumn;
// Set GE bit for deferred column
temp_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
}
}
CAT_IF_DUMP(disp_row[bit_i >> 6] ^= (uint64_t)1 << (bit_i & 63);)
} // next bit
// Set up generator
const uint16_t * GF256_RESTRICT row = rows;
// Store first row
CAT_IF_DUMP(for (unsigned ii = 0; ii < dense_count; ++ii) {
cout << ((disp_row[ii >> 6] & ((uint64_t)1 << (ii & 63))) ? '1' : '0');
cout << " <- going to row " << *row << endl;
})
uint64_t * GF256_RESTRICT ge_dest_row = _ge_matrix + _ge_pitch * *row++;
// Add to destination row
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
ge_dest_row[jj] ^= temp_row[jj];
}
// Reshuffle bit order: Shuffle-2 Code
ShuffleDeck16(prng, bits, dense_count);
const unsigned loop_count = (dense_count >> 1);
// Generate first half of rows
for (unsigned ii = 0; ii < loop_count; ++ii)
{
const unsigned bit0 = set_bits[ii];
const unsigned bit1 = clr_bits[ii];
// Flip bit 1
if (bit0 < max_x)
{
if (column[bit0].Mark == MARK_PEEL)
{
const uint16_t bit0_row = column[bit0].PeelRow;
const uint64_t * GF256_RESTRICT ge_source_row = _compress_matrix + _ge_pitch * bit0_row;
// Add temp row value
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
temp_row[jj] ^= ge_source_row[jj];
}
}
else
{
const unsigned ge_column_i = column[bit0].GEColumn;
// Set GE bit for deferred column
temp_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
}
}
CAT_IF_DUMP(disp_row[bit0 >> 6] ^= (uint64_t)1 << (bit0 & 63);)
// Flip bit 2
if (bit1 < max_x)
{
if (column[bit1].Mark == MARK_PEEL)
{
const uint16_t bit1_row = column[bit1].PeelRow;
const uint64_t * GF256_RESTRICT ge_source_row = _compress_matrix + _ge_pitch * bit1_row;
// Add temp row value
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
temp_row[jj] ^= ge_source_row[jj];
}
}
else
{
const unsigned ge_column_i = column[bit1].GEColumn;
// Set GE bit for deferred column
temp_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
}
}
CAT_IF_DUMP(disp_row[bit1 >> 6] ^= (uint64_t)1 << (bit1 & 63);)
// Store in row
CAT_IF_DUMP(for (unsigned jj = 0; jj < dense_count; ++jj) {
cout << ((disp_row[jj >> 6] & ((uint64_t)1 << (jj & 63))) ? '1' : '0');
cout << " <- going to row " << *row << endl;
})
ge_dest_row = _ge_matrix + _ge_pitch * (*row++);
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
ge_dest_row[jj] ^= temp_row[jj];
}
} // next row
// Reshuffle bit order: Shuffle-2 Code
ShuffleDeck16(prng, bits, dense_count);
const unsigned second_loop_count = loop_count - 1 + (dense_count & 1);
// Generate second half of rows
for (unsigned ii = 0; ii < second_loop_count; ++ii)
{
const unsigned bit0 = set_bits[ii];
const unsigned bit1 = clr_bits[ii];
// Flip bit 1
if (bit0 < max_x)
{
if (column[bit0].Mark == MARK_PEEL)
{
const uint16_t bit0_row = column[bit0].PeelRow;
const uint64_t * GF256_RESTRICT ge_source_row = _compress_matrix + _ge_pitch * bit0_row;
// Add temp row value
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
temp_row[jj] ^= ge_source_row[jj];
}
}
else
{
const unsigned ge_column_i = column[bit0].GEColumn;
// Set GE bit for deferred column
temp_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
}
}
CAT_IF_DUMP(disp_row[bit0 >> 6] ^= (uint64_t)1 << (bit0 & 63);)
// Flip bit 2
if (bit1 < max_x)
{
if (column[bit1].Mark == MARK_PEEL)
{
const uint16_t bit1_row = column[bit1].PeelRow;
const uint64_t * GF256_RESTRICT ge_source_row = _compress_matrix + _ge_pitch * bit1_row;
// Add temp row value
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
temp_row[jj] ^= ge_source_row[jj];
}
}
else
{
const unsigned ge_column_i = column[bit1].GEColumn;
// Set GE bit for deferred column
temp_row[ge_column_i >> 6] ^= (uint64_t)1 << (ge_column_i & 63);
}
}
CAT_IF_DUMP(disp_row[bit1 >> 6] ^= (uint64_t)1 << (bit1 & 63);)
// Store in row
CAT_IF_DUMP(for (unsigned kk = 0; kk < dense_count; ++kk) {
cout << ((disp_row[kk >> 6] & ((uint64_t)1 << (kk & 63))) ? '1' : '0');
cout << " <- going to row " << *row << endl;
})
ge_dest_row = _ge_matrix + _ge_pitch * (*row++);
for (unsigned jj = 0, ge_pitch = _ge_pitch; jj < ge_pitch; ++jj) {
ge_dest_row[jj] ^= temp_row[jj];
}
} // next row
CAT_IF_DUMP(cout << endl;)
} // next column
}
// This Cauchy matrix is generated by HeavyRowGenerator.cpp
// It has a special property that stacked random binary matrices do not affect
// its inversion rate. Honestly I haven't looked into why, but it's not a
// common property for Cauchy matrices, and I didn't know this was possible.
static const uint8_t kHeavyMatrix[kHeavyRows][kHeavyCols] = {
{ 0x85, 0xd3, 0x66, 0xf3, 0x38, 0x95, 0x56, 0xad, 0x57, 0xaf, 0x58, 0x48, 0xbc, 0xfa, 0x02, 0xc5, 0x43, 0xe8, },
{ 0xd3, 0x85, 0xf3, 0x66, 0x95, 0x38, 0xad, 0x56, 0xaf, 0x57, 0x48, 0x58, 0xfa, 0xbc, 0xc5, 0x02, 0xe8, 0x43, },
{ 0x82, 0x22, 0x57, 0xaf, 0x56, 0xad, 0x38, 0x95, 0x66, 0xf3, 0x43, 0xe8, 0x02, 0xc5, 0xbc, 0xfa, 0x58, 0x48, },
{ 0x22, 0x82, 0xaf, 0x57, 0xad, 0x56, 0x95, 0x38, 0xf3, 0x66, 0xe8, 0x43, 0xc5, 0x02, 0xfa, 0xbc, 0x48, 0x58, },
{ 0x51, 0x34, 0x56, 0xad, 0x57, 0xaf, 0x66, 0xf3, 0x38, 0x95, 0x02, 0xc5, 0x43, 0xe8, 0x58, 0x48, 0xbc, 0xfa, },
{ 0x34, 0x51, 0xad, 0x56, 0xaf, 0x57, 0xf3, 0x66, 0x95, 0x38, 0xc5, 0x02, 0xe8, 0x43, 0x48, 0x58, 0xfa, 0xbc, },
};
void Codec::SetHeavyRows()
{
CAT_IF_DUMP(cout << endl << "---- SetHeavyRows ----" << endl << endl;)
// Skip extra rows
uint8_t * GF256_RESTRICT heavy_offset = _heavy_matrix + _heavy_pitch * _extra_count;
uint8_t * GF256_RESTRICT heavy_row = heavy_offset;
// For each heavy matrix word:
for (unsigned row_i = 0; row_i < kHeavyRows; ++row_i, heavy_row += _heavy_pitch)
{
// NOTE: Each heavy row is a multiple of 4 bytes in size
for (unsigned col_i = 0; col_i < _heavy_columns; col_i++) {
heavy_row[col_i] = kHeavyMatrix[row_i][col_i];
}
}
#ifdef CAT_IDENTITY_LOWER_RIGHT
uint8_t * GF256_RESTRICT lower_right = heavy_offset + _heavy_columns - kHeavyRows;
// Add identity matrix to tie heavy rows to heavy mixing columns
for (unsigned ii = 0; ii < kHeavyRows; ++ii, lower_right += _heavy_pitch)
{
for (unsigned jj = 0; jj < kHeavyRows; ++jj) {
lower_right[jj] = (ii == jj) ? 1 : 0;
}
}
#endif
}
//------------------------------------------------------------------------------
// Stage (3) Gaussian Elimination
void Codec::SetupTriangle()
{
CAT_IF_DUMP(cout << endl << "---- SetupTriangle ----" << endl << endl;)
CAT_DEBUG_ASSERT((unsigned)_defer_count + (unsigned)_dense_count < 65536);
const uint16_t pivot_count = _defer_count + _dense_count;
// Initialize pivot array to just non-heavy rows
for (uint16_t pivot_i = 0; pivot_i < pivot_count; ++pivot_i) {
_pivots[pivot_i] = pivot_i;
}
// Set resume point to the first column
_next_pivot = 0;
_pivot_count = pivot_count;
// If heavy rows are used right from the start:
if (_first_heavy_column <= 0) {
InsertHeavyRows();
}
}
void Codec::InsertHeavyRows()
{
CAT_IF_DUMP(cout << endl << "---- InsertHeavyRows ----" << endl << endl;)
CAT_IF_DUMP(cout << "Converting remaining extra rows to heavy...";)
// Initialize index of first heavy pivot
unsigned first_heavy_pivot = _pivot_count;
const uint16_t column_count = _defer_count + _mix_count;
const uint16_t first_heavy_row = _defer_count + _dense_count;
// For each remaining pivot in the list:
for (int pivot_j = (int)_pivot_count - 1; pivot_j >= 0; --pivot_j)
{
CAT_DEBUG_ASSERT((unsigned)pivot_j < _pivot_count);
const uint16_t ge_row_j = _pivots[pivot_j];