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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
//! ## RAID-parity functions and macros
//!
//! [RAID][raid-wiki], short for a "Redundant Array of Independent Disks", is a
//! set of schemes commonly found in storage systems, with the purpose of using
//! an array of multiple disks to provide data redundancy and/or performance
//! improvements.
//!
//! The most interesting part for us is the higher-numbered RAID levels, which
//! use extra disks to store parity information, capable of reconstructing any
//! one (for RAID 5), two (for RAID 6), and even three (for RAID 7, though this
//! name is not standardized) failed disks.
//!
//! ``` rust
//! use gf256::raid::raid7;
//!
//! // format
//! let mut buf = b"Hello World!".to_vec();
//! let mut parity1 = vec![0u8; 4];
//! let mut parity2 = vec![0u8; 4];
//! let mut parity3 = vec![0u8; 4];
//! let slices = buf.chunks(4).collect::<Vec<_>>();
//! raid7::format(&slices, &mut parity1, &mut parity2, &mut parity3);
//!
//! // corrupt
//! buf[0..8].fill(b'x');
//!
//! // repair
//! let mut slices = buf.chunks_mut(4).collect::<Vec<_>>();
//! raid7::repair(&mut slices, &mut parity1, &mut parity2, &mut parity3, &[0, 1]);
//! assert_eq!(&buf, b"Hello World!");
//! ```
//!
//! These parity-schemes aren't limited to physical disks. They can be applied
//! to any array of blocks as long as there is some mechanism to detect failures,
//! such as CRCs or other checksums.
//!
//! Compared to other, more general [Reed-Solomon](../rs) schemes, RAID-parity has
//! the nice feature that it is cheap to update a single block, requiring only extra
//! read and writes for each parity block.
//!
//! Note this module requires feature `raid`.
//!
//! A fully featured implementation of RAID-parity can be found in
//! [`examples/raid.rs`][raid-example]:
//!
//! ``` bash
//! $ RUSTFLAGS="-Ctarget-cpu=native" cargo +nightly run --features nightly,thread-rng,lfsr,crc,shamir,raid,rs --example raid
//!
//! testing raid5("Hello World?")
//! format => Hello World?U)_< 48656c6c6f20576f726c643f55295f3c
//! update => Hello World!U)_" 48656c6c6f20576f726c642155295f22
//! corrupt => Hellxxxxrld!U)_" 48656c6c78787878726c642155295f22
//! repair => Hello World!U)_" 48656c6c6f20576f726c642155295f22
//!
//! testing raid6("Hello World?")
//! format => Hello World?U)_<C.ON 48656c6c6f20576f726c643f55295f3c43884f4e
//! update => Hello World!U)_"C.O6 48656c6c6f20576f726c642155295f2243884f36
//! corrupt => xxxxo World!U)_"C.O6 787878786f20576f726c642155295f2243884f36
//! repair => Hello World!U)_"C.O6 48656c6c6f20576f726c642155295f2243884f36
//! corrupt => HellxxxxxxxxU)_"C.O6 48656c6c787878787878787855295f2243884f36
//! repair => Hello World!U)_"C.O6 48656c6c6f20576f726c642155295f2243884f36
//!
//! testing raid7("Hello World?")
//! format => Hello World?U)_<C.ON.k#. 48656c6c6f20576f726c643f55295f3c43884f4e9a6b231a
//! update => Hello World!U)_"C.O6.k#. 48656c6c6f20576f726c642155295f2243884f369a6b23e7
//! corrupt => Hello World!U)_"xxxx.k#. 48656c6c6f20576f726c642155295f22787878789a6b23e7
//! repair => Hello World!U)_"C.O6.k#. 48656c6c6f20576f726c642155295f2243884f369a6b23e7
//! corrupt => xxxxxxxxrld!U)_"C.O6.k#. 7878787878787878726c642155295f2243884f369a6b23e7
//! repair => Hello World!U)_"C.O6.k#. 48656c6c6f20576f726c642155295f2243884f369a6b23e7
//! corrupt => Hello WoxxxxU)_"xxxxxxxx 48656c6c6f20576f7878787855295f227878787878787878
//! repair => Hello World!U)_"C.O6.k#. 48656c6c6f20576f726c642155295f2243884f369a6b23e7
//!
//! block corrupted image (errors = 3/7, 42.86%):
//!
//! :::::::::::::::::::::::::::::::: .:::....:::.....' '''' ..:.': .:'..' .::::::::::::::::
//! .:::::::::::::::::::::::::::::::: :::::::::::: '. . :: ': .' . ' ..::::::::::::::::
//! .:::::::::::::::::::::::::::::::::: ::::::::::' ::. .' :. . ::' . :::::::::::::::::::
//! .:::::::::::::::::::::::::::::::::::: . :::::::::: . ::::. .: ::'.'':' ..'' .::::::::::::::::
//! .::::::::::::::::::::::::::::::::::::: :: :'::::::' :: :'::: .:'...: ''. :.'.' . :.::::::::::::::::
//! ::::::::::::::::::::::::::::::::::::::: ' ''' : ' .:.:. '::'''. : ' :'. ::::::::::::::::
//! ::::::::::::::::::::::::::::::::::::::: ..'' :::: ' :. .''. ': ..:::::::::::::::::
//! ::::::::::::::::::::::::::::::::::::::: ..:'' :'' .:'. .'' .:: ': ::::::::::::::::
//! ::::::::::::::::::::::::::::::::::::::: ' ..:. ..:' .'' ' '': ''.:::::::::::::::::
//! ..: ::::::::::::::::::::::::::::::::::::::: ..:::'.':'' ' '. ':: .': ::::::::::::::::
//! ..:::' :::::::::::::::::::::::::::::::::::::: :': ' . ..' '.: . .: . ::::::::::::::::
//! :::' ':::'':::::::::::::::::::::::::::::::: :...' : .'':..: ':. :'':.::.::::::::::::::::
//! ::' ...::::::::::::::::::::::::::::::::: ::' ..'..::''' : : '.'.' :' . ::::::::::::::::
//! ....::::::::::::::::::::::::::::::::::::::: ..:''. '' ':::.::.'': ': ::::::::::::::::
//! '::::::::''' :::::::::::::::::::::::::::::::: '::'' ...::::' :: ' :'.:. :::::::::::::::::
//! :::::::::::::::::::::::::::::::: '''':::::::::::''''::. ::. ::.':::::::::::::::::
//! ':..:. ..:' ':. ::::::::::::::::::::::::::::::::.'. . ': ': :: : ': :. :::' ::. ' :'.' :. '':.::::::::::::::::
//!
//! corrected:
//!
//! ..:::::::::::... .:::....:::.....' '''' ..:.': .:'..' .::.. .': :...:.
//! .:::::::::::::::::::.. :::::::::::: '. . :: ': .' . ' ... .:...:..
//! .::::::::::::::::::::::::. ::::::::::' ::. .' :. . ::' . :::.' :' ':... :::
//! .:::::::::::::::::::::::::::. . :::::::::: . ::::. .: ::'.'':' ..'' ..:. .: ':.. '.:.
//! .:::::::::::::::::::::::::::::: ... :: :'::::::' :: :'::: .:'...: ''. :.'.' . :. ..:' .''::'': :
//! :::::::::::::::::::::::::::::'' .. '::: ' ''' : ' .:.:. '::'''. : ' :'. : ':.: ..' : .'
//! :::::::::::::::::::::::::::''..::::: ' ..'' :::: ' :. .''. ': ..:.' ' ...::::.
//! :::::::::::::::::::::::'' ..:::::'' ..:'' :'' .:'. .'' .:: ': .' .:.' :: .'
//! :::::::::::::::::::'' ..:::::'' . ' ..:. ..:' .'' ' '': ''.::.' : :':.....
//! ..: ::::::::::::::''' ..:::::'' ..::: ..:::'.':'' ' '. ':: .': : ': .:' ' :.'
//! ..:::' :::::::::'' ..::::::'' ..::::::: :': ' . ..' '.: . .: . '' :. :'.:: :
//! :::' ':::'' ...::::::''...:::::::::: :...' : .'':..: ':. :'':.::.:': :' '.. '. :
//! ::' ...::::::'' ..:::::::::::::' ::' ..'..::''' : : '.'.' :' . '.' ': . '.':
//! ....:::::::'' ..:::::::::::::::::' ..:''. '' ':::.::.'': ': ':. '':'.' .'
//! '::::::::''' :::::::::::::::::::'' '::'' ...::::' :: ' :'.:. :: ::'' :.:'.:
//! '':::::::::::''' '''':::::::::::''''::. ::. ::.':'::':'.::'.::' :
//! ':..:. ..:' ':. :': :'..' .::. .'':.'.: :''.''' .'. . ': ': :: : ': :. :::' ::. ' :'.' :. '':..''': ' '. ':::.
//! ```
//!
//! ## How does RAID-parity work?
//!
//! The simplest parity-scheme to understand is RAID 5, aka single-parity. In this
//! scheme the parity block, `p`, is simply the xor of all of the data blocks:
//!
//! ``` text
//! p = d0 ^ d1 ^ d2 ^ ...
//! ```
//!
//! If any single data block goes bad, we can reconstruct its original value by
//! xoring the parity block with all of the other data blocks:
//!
//! ``` text
//! d1 = p ^ d0 ^ d2 ...
//! ```
//!
//! Here's some Rust code that shows this working:
//!
//! ``` rust
//! // given some data blocks
//! let mut data = vec![0u8; 10];
//! for i in 0..data.len() {
//! data[i] = i as u8;
//! }
//!
//! // calculate parity block p
//! let mut p = 0;
//! for i in 0..data.len() {
//! p ^= data[i];
//! }
//!
//! // oh no! a block is corrupted!
//! data[1] = 0xff;
//!
//! // reconstruct using our parity block p
//! let mut fixed = p;
//! for i in 0..data.len() {
//! if i != 1 {
//! fixed ^= data[i];
//! }
//! }
//! data[1] = fixed;
//!
//! for i in 0..data.len() {
//! assert_eq!(data[i], i as u8);
//! }
//! ```
//!
//! Let's think about what this means mathematically.
//!
//! xor is equivalent to addition in a finite-field, so, as long as we say we're
//! in a finite-field, we can rewrite the first equation like so:
//!
//! ``` text
//! p = d0 + d1 + d2 + ...
//!
//! or
//!
//! p = Σ di
//! i
//! ```
//!
//! If a block goes bad, we've introduced an unknown into our equation:
//!
//! ``` text
//! p = d0 + ? + d2 + ...
//! ```
//!
//! But we can solve for it! Let's call the unknown `dx`:
//!
//! ``` text
//! p = d0 + dx + d2 + ...
//!
//! dx = p - (d0 + d2 + ...)
//!
//! or
//!
//! dx = p - Σ di
//! i!=x
//! ```
//!
//! And since subtraction is also xor in a finite-field, this is the equivalent
//! to xoring the parity block and data blocks together.
//!
//! ---
//!
//! But what if two blocks go bad?
//!
//! ``` text
//! p = ? + ? + d2 + ...
//! ```
//!
//! If two blocks go bad, we have two unknowns, but only one equation. We just
//! can't find a unique solution.
//!
//! But we can if we have two equations! In RAID-parity schemes, we treat the blocks
//! as a linear system of equations. And if you recall algebra, you can find a unique
//! solution to a linear system of equations if you have more equations than unknowns,
//! and the equations are _linearly independent_ (this becomes important later).
//!
//! Say, for our second parity block, `q`, we came up with a different equation by
//! using some arbitrary constants, we'll call them `c0`, `c1`, `c2`, etc for now:
//!
//! ``` text
//! p = d0 + d1 + d2 + ...
//!
//! q = d0*c0 + d1*c1 + d2*c2 + ...
//!
//! or
//!
//! p = Σ di
//! i
//!
//! q = Σ di*ci
//! i
//! ```
//!
//! Now, if any two data blocks go bad, we have two unknowns:
//!
//! ``` text
//! p = ? + ? + d2 + ...
//!
//! q = ?*c0 + ?*c1 + d2*c2 + ...
//! ```
//!
//! But since we have two equations, we can still solve it! Let's call the
//! unknowns `dx` and `dy`:
//!
//! ``` text
//! p = dx + dy + d2 + ...
//!
//! q = dx*c0 + dy*c1 + d2*c2 + ...
//!
//! dx + dy = p - (d2 + ...)
//!
//! dx*c0 + dy*c1 = q - (d2*c2 + ...)
//!
//! or
//!
//! dx + dy = p - Σ di
//! i!=x,y
//!
//! dx*cx + dy*cy = q - Σ di*ci
//! i!=x,y
//! ```
//!
//! Solve for `dy`:
//!
//! ``` text
//! dx + dy = p - Σ di
//! i!=x,y
//!
//! dy = p - Σ di - dx
//! i!=x,y
//! ```
//!
//! Substitute `dy` and solve for `dx`:
//!
//! ``` text
//! dx*cx + (p - Σ di - dx)*cy = q - Σ di*ci
//! i!=x,y i!=x,y
//!
//! dx*cx + (p - Σ di)*cy - dx*cy = q - Σ di*ci
//! i!=x,y i!=x,y
//!
//! dx*(cx - cy) + (p - Σ di)*cy = q - Σ di*ci
//! i!=x,y i!=x,y
//!
//! dx*(cx - cy) = (q - Σ di*ci) - (p - Σ di)*cy
//! i!=x,y i!=x,y
//!
//! (q - Σ di*ci) - (p - Σ di)*cy
//! i!=x,y i!=x,y
//! dx = -----------------------------
//! cx - cy
//! ```
//!
//! Putting it all together:
//!
//! ``` text
//! (q - Σ di*ci) - (p - Σ di)*cy
//! i!=x,y i!=x,y
//! dx = -----------------------------
//! cx - cy
//!
//! dy = p - Σ di - dx
//! i!=x,y
//! ```
//!
//! This gives us two "simple" (at least for a computer) equations to find
//! `dx` and `dy`.
//!
//! We can see that we can always find a solution as long as `cx` != `cy`, otherwise
//! we end up dividing by zero.
//!
//! We can use any set of unique constants for this, but it's convenient to use
//! powers of a generator in our field, `g`. Recall that powers of a generator,
//! sometimes called a primitive element, generate all non-zero elements of a
//! given field before looping. So `g^i` is both non-zero and unique for any
//! `i` < the number of non-zero elements in the field, 255 for `GF(256)`, which
//! means our equation will always be solvable as long as we don't have more
//! than 255 disks!
//!
//! For more information on generators, see the documentation in [`gf`](../gf.rs).
//!
//! We can substitute powers of our generator `g` back into our solutions to give
//! us our final equations:
//!
//! Given parity blocks `p` and `q`:
//!
//! ``` text
//! p = d0 + d1 + d2 + ...
//!
//! q = d0*g^0 + d1*g^1 + d2*g^2 + ...
//!
//! or
//!
//! p = Σ di
//! i
//!
//! q = Σ di*g^i
//! i
//! ```
//!
//! We can solve for any two bad blocks, `dx` and `dy`:
//!
//! ``` text
//! dx + dy = p - Σ di
//! i!=x,y
//!
//! dx*g^x + dy*g^y = q - Σ di*g^i
//! i!=x,y
//!
//! or
//!
//! (q - Σ di*g^i) - (p - Σ di)*g^y
//! i!=x,y i!=x,y
//! dx = -------------------------------
//! g^x - g^y
//!
//! dy = p - Σ di - dx
//! i!=x,y
//! ```
//!
//! Let's see this in action!
//!
//! ``` rust
//! # use ::gf256::*;
//! # use ::gf256::traits::FromLossy;
//! #
//! // given some data blocks
//! let mut data = vec![gf256(0); 10];
//! for i in 0..data.len() {
//! data[i] = gf256::from_lossy(i);
//! }
//!
//! // calculate parity blocks p and q
//! //
//! // p = Σ di
//! // i
//! //
//! // q = Σ di*g^i
//! // i
//! //
//! let mut p = gf256(0);
//! let mut q = gf256(0);
//! for i in 0..data.len() {
//! p += data[i];
//! q += data[i]*gf256::GENERATOR.pow(i as u8);
//! }
//!
//! // oh no! TWO blocks are corrupted!
//! data[1] = gf256(0xff);
//! data[2] = gf256(0xff);
//!
//! // reconstruct using our parity blocks p and q
//! //
//! // (q - Σ di*g^i) - (p - Σ di)*g^y
//! // i!=x,y i!=x,y
//! // dx = -------------------------------
//! // g^x - g^y
//! //
//! // dy = p - Σ di - dx
//! // i!=x,y
//! //
//! let mut pdelta = p;
//! let mut qdelta = q;
//! for i in 0..data.len() {
//! if i != 1 && i != 2 {
//! pdelta -= data[i];
//! qdelta -= data[i]*gf256::GENERATOR.pow(i as u8);
//! }
//! }
//! let gx = gf256::GENERATOR.pow(1);
//! let gy = gf256::GENERATOR.pow(2);
//! data[1] = (qdelta - pdelta*gy) / (gx - gy);
//! data[2] = pdelta - data[1];
//!
//! for i in 0..data.len() {
//! assert_eq!(data[i], gf256::from_lossy(i));
//! }
//! ```
//!
//! ---
//!
//! Can we push this further?
//!
//! We can fudge our original equations so they're in a similar form:
//!
//! ``` text
//! p = Σ di*g^0i => dx*g^0x + dy*g^0y = p - Σ di*g^0i
//! i i!=x,y
//!
//! q = Σ di*g^1i => dx*g^1x + dy*g^1y = q - Σ di*g^1i
//! i i!=x,y
//! ```
//!
//! Which raises an interesting question, can we keep going?
//!
//! ``` text
//! p = Σ di*g^0i => dx*g^0x + dy*g^0y + dz*g^0z + dw*g^0w + ... = p - Σ di*g^0i
//! i i!=x,y,z,w,...
//!
//! q = Σ di*g^1i => dx*g^1x + dy*g^1y + dz*g^1z + dw*g^1w + ... = q - Σ di*g^1i
//! i i!=x,y,z,w,...
//!
//! r = Σ di*g^2i => dx*g^2x + dy*g^2y + dz*g^2z + dw*g^2w + ... = r - Σ di*g^2i
//! i i!=x,y,z,w,...
//!
//! s = Σ di*g^3i => dx*g^3x + dy*g^3y + dz*g^3z + dw*g^3w + ... = s - Σ di*g^3i
//! i i!=x,y,z,w,...
//!
//! ... ...
//! ```
//!
//! Perhaps surprisingly, it turns out the answer is no! At least not generally.
//!
//! Rather than trying to reason about this big mess of equations, let us just
//! consider pairs of equations. We at least need to always be able to solve
//! these if the rest of the parity blocks go bad:
//!
//! ``` text
//! dx*g^(j*x) + dy*g^(j*y) = q - Σ di*g^(j*i)
//! i!=x,y
//!
//! dx*g^(k*x) + dy*g^(k*y) = r - Σ di*g^(k*i)
//! i!=x,y
//! ```
//!
//! We can solve a system of linear equations if they are "[linearly independent
//! ][linearly-independent]", that is, no equation is a scalar-multiple of another
//! linear equation.
//!
//! Or an more mathy terms, is there a constant `c` such that `c*f(...)` = `g(...)`?
//!
//! What's interesting is we can actually allow one coefficient to be a multiple,
//! but not both. So we can phrase the question a bit differently: Is there a
//! unique `c` such that `c*g^(j*x)` = `g^(k*x)`, assuming `j` != `k` and
//! `x` < 255?
//!
//! We can reduce this a bit:
//!
//! ``` text
//! c*g^(j*x) = g^(k*x)
//!
//! c = g^(k*x - j*x)
//!
//! c = g^((k-j)*x)
//! ```
//!
//! And since our constant is arbitrary, we can substitute it for, say, `log_g(c)`.
//!
//! Except this gets a bit tricky, recall that the powers of g form a multiplicative
//! cycle equal to the number of non-zero elements in our field, 255 for `GF(256)`.
//! So when we take the logarithm, we actually end up with an equation under mod 255,
//! because the powers of `g` loops.
//!
//! We're actually dealing with two number systems here, our finite-field and the
//! infinite integers (there's probably a better way to notate this mathematically):
//!
//! ``` text
//! c = g^((k-j)*x)
//!
//! let c' = log_g(c) = log_g(g^((k-j)*x))
//!
//! c' = log_g(g^((k-j)*x))
//!
//! c' = (k-j)*x mod 255
//! ```
//!
//! So the new qutions is: is `(k-j)*x mod 255` unique for any `k` != `j`, `x` < 255?
//!
//! Unfortunately, while `x` is less than 255, `(k-j)*x` may not be.
//!
//! But there's a fun property of modular multiplication we can leverage. It turns
//! out modular multiplication by a constant will iterate all elements of the group
//! if the constant and the modulo are [_coprime_][coprime].
//!
//! For example, say we were dealing with mod 9. 3, which is not coprime with 9,
//! gets stuck in a smaller loop:
//!
//! ``` rust
//! assert_eq!((3*1) % 9, 3);
//! assert_eq!((3*2) % 9, 6);
//! assert_eq!((3*3) % 9, 0);
//! assert_eq!((3*4) % 9, 3);
//! assert_eq!((3*5) % 9, 6);
//! assert_eq!((3*6) % 9, 0);
//! assert_eq!((3*7) % 9, 3);
//! assert_eq!((3*8) % 9, 6);
//! assert_eq!((3*9) % 9, 0);
//! assert_eq!((3*10) % 9, 3);
//! // ...
//! ```
//!
//! But 7, which is coprime with 9, iterates through all elements before looping:
//!
//! ``` rust
//! assert_eq!((7*1) % 9, 7);
//! assert_eq!((7*2) % 9, 5);
//! assert_eq!((7*3) % 9, 3);
//! assert_eq!((7*4) % 9, 1);
//! assert_eq!((7*5) % 9, 8);
//! assert_eq!((7*6) % 9, 6);
//! assert_eq!((7*7) % 9, 4);
//! assert_eq!((7*8) % 9, 2);
//! assert_eq!((7*9) % 9, 0);
//! assert_eq!((7*10) % 9, 7);
//! // ...
//! ```
//!
//! So `(k-j)*x mod 255` actually _is_ unique for any `x` < 255, as long as
//! `k` != `j` and `k-j` is coprime with 255.
//!
//! So there you go!
//!
//! We can extend our RAID-parity scheme to any number of parity blocks as long
//! as we have a set of unique constants where for ANY of the two constants,
//! `k` and `j`, `k-j` is coprime with 255.
//!
//! So how many parity blocks does that give us?
//!
//! Three!
//!
//! That's right, only three. It turns out the above constraint is actually quite
//! limiting.
//!
//! Note that 255, while mostly prime, is not actually prime. And, as [Miracle Max
//! ][miracle-max] would say, mostly prime just means slightly composite:
//!
//! ``` text
//! 255 = 3 * 5 * 17
//! ```
//!
//! So if any `k-j` is a multiple of 3, it fails to be coprime with 255.
//!
//! We can choose some constants:
//!
//! ``` text
//! j = 3n+0 for any n
//! k = 3n+1 for any n
//! l = 3n+2 for any n
//! ```
//!
//! But the moment try to choose some constant `m` = `3n+3`, well, `m-j` = `3n`,
//! some multiple of 3, which can't be coprime with 255.
//!
//! And this is true for ANY word-sized field. Any word-sized field `GF(2^8)`,
//! `GF(2^16)`, `GF(2^32)`, `GF(2^(2^i))` has a multiplicative cycle of
//! length `2^(2^i)-1` ([A051179][A051179]), which is always divisible by 3.
//!
//! The good news is that this does gives us a set of three valid constants,
//! which give us three linearly independent generators:
//!
//! ``` text
//! g^(3n+0) for any n
//! g^(3n+1) for any n
//! g^(3n+2) for any n
//! ```
//!
//! And we can just set n=0 for all three constants:
//!
//! ``` text
//! g^0 = 1
//! g^1 = g
//! g^2 = g^2
//! ```
//!
//! It's worth noting that `g^2` is also a generator of the field. For similar
//! reasons to modular multiplication, for any generator `g`, `g^k` is also a
//! generator, iff `k` is coprime with the size of the multiplicative cycle. And
//! since any `2^i-1` (not divisible by 2) is always coprime with `2^j` (only
//! prime factor is 2), `g^(2^k)` is also a generator for any `k`.
//!
//! This isn't actually useful here, but it's interesting to know.
//!
//! ---
//!
//! With this, we can construct a triple-parity scheme!
//!
//! We can create three parity blocks, `p`, `q`, `r`, with three linearly
//! independent equations, by using two generators, `g` and `h`, where `h` = `g^2`:
//!
//! ``` text
//! p = d0 + d1 + d2 + ...
//!
//! q = d0*g^0 + d1*g^1 + d2*g^2 + ...
//!
//! r = d0*h^0 + d1*h^1 + d2*h^2 + ...
//!
//! or
//!
//! p = Σ di
//! i
//!
//! q = Σ di*g^i
//! i
//!
//! r = Σ di*h^i
//! i
//! ```
//!
//! If three blocks go bad, we have three equations and three unknowns, `dx`,
//! `dy`, `dz`:
//!
//! ``` text
//! dx + dy + dz = p - Σ di
//! i!=x,y,z
//!
//! dx*g^x + dy*g^y + dz*g^z = q - Σ di*g^i
//! i!=x,y,z
//!
//! dx*h^x + dy*h^y + dz*h^z = r - Σ di*h^i
//! i!=x,y,z
//! ```
//!
//! Ready for some big equations?
//!
//! Solve for `dz`:
//!
//! ``` text
//! dx + dy + dz = p - Σ di
//! i!=x,y,z
//!
//! dz = p - Σ di - dx - dy
//! i!=x,y,z
//! ```
//!
//! Substitute `dz` and solve for `dy`:
//!
//! ``` text
//! dx*g^x + dy*g^y + (p - Σ di - dx - dy)*g^z = q - Σ di*g^i
//! i!=x,y,z i!=x,y,z
//!
//! dx*g^x + dy*g^y + (p - Σ di)*g^z - dx*g^z - dy*g^z = q - Σ di*g^i
//! i!=x,y,z i!=x,y,z
//!
//! dx*(g^x - g^z) + dy*(g^y - g^z) = (q - Σ di*g^i) - (p - Σ di)*g^z
//! i!=x,y,z i!=x,y,z
//!
//! dy*(g^y - g^z) = (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! i!=x,y,z i!=x,y,z
//!
//! (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! i!=x,y,z i!=x,y,z
//! dy = ------------------------------------------------
//! g^y - g^z
//! ```
//!
//! Substitute `dy` and solve for `dx`:
//!
//! ``` text
//! ( (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z) )
//! ( i!=x,y,z i!=x,y,z )
//! dx*(h^x - h^z) + ( ------------------------------------------------ )*(h^y - h^z) + (p - Σ di)*h^z = r - Σ di*h^i
//! ( g^y - g^z ) i!=x,y,z i!=x,y,z
//!
//!
//! ( (q - Σ di*g^i) - (p - Σ di)*g^z )
//! ( i!=x,y,z i!=x,y,z ) ( dx*(g^x - g^z) )
//! dx*(h^x - h^z) + ( ------------------------------- )*(h^y - h^z) - ( -------------- )*(h^y - h^z) + (p - Σ di)*h^z = r - Σ di*h^i
//! ( g^y - g^z ) ( g^y - g^z ) i!=x,y,z i!=x,y,z
//!
//! ( (q - Σ di*g^i) - (p - Σ di)*g^z )
//! ( dx*(g^x - g^z) ) ( i!=x,y,z i!=x,y,z )
//! dx*(h^x - h^z) - ( -------------- )*(h^y - h^z) = (r - Σ di*h^i) - (p - Σ di)*h^z - ( ------------------------------- )*(h^y - h^z)
//! ( g^y - g^z ) i!=x,y,z i!=x,y,z ( g^y - g^z )
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (p - Σ di)*h^z*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) + (p - Σ di)*g^z*(h^y - h^z)
//! ( dx*(g^x - g^z) ) i!=x,y,z i!=x,y,z i!=x,y,z i!=x,y,z
//! dx*(h^x - h^z) - ( -------------- )*(h^y - h^z) = -----------------------------------------------------------------------------------------------------------------
//! ( g^y - g^z ) g^y - g^z
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(h^z*(g^y - g^z) - g^z*(h^y - h^z))
//! ( dx*(g^x - g^z) ) i!=x,y,z i!=x,y,z i!=x,y,z
//! dx*(h^x - h^z) - ( -------------- )*(h^y - h^z) = --------------------------------------------------------------------------------------------------------
//! ( g^y - g^z ) g^y - g^z
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^z - g^z*h^y + g^z*h^z)
//! ( dx*(g^x - g^z) ) i!=x,y,z i!=x,y,z i!=x,y,z
//! dx*(h^x - h^z) - ( -------------- )*(h^y - h^z) = ------------------------------------------------------------------------------------------------------------
//! ( g^y - g^z ) g^y - g^z
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! ( dx*(g^x - g^z) ) i!=x,y,z i!=x,y,z i!=x,y,z
//! dx*(h^x - h^z) - ( -------------- )*(h^y - h^z) = ----------------------------------------------------------------------------------------
//! ( g^y - g^z ) g^y - g^z
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! dx*(h^x - h^z)*(g^y - g^z) - dx*(g^x - g^z)*(h^y - h^z) i!=x,y,z i!=x,y,z i!=x,y,z
//! ------------------------------------------------------- = ----------------------------------------------------------------------------------------
//! g^y - g^z g^y - g^z
//!
//! dx*(h^x - h^z)*(g^y - g^z) - dx*(g^x - g^z)*(h^y - h^z) = (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//!
//! dx*((h^x - h^z)*(g^y - g^z) - (h^y - h^z)*(g^x - g^z)) = (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------------------------------
//! (h^x - h^z)*(g^y - g^z) - (h^y - h^z)*(g^x - g^z)
//!
//! So:
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------------------------------
//! (h^x - h^z)*(g^y - g^z) - (h^y - h^z)*(g^x - g^z)
//!
//! (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! i!=x,y,z i!=x,y,z
//! dy = ------------------------------------------------
//! g^y - g^z
//!
//! dz = p - Σ di - dx - dy
//! i!=x,y,z
//! ```
//!
//! If we use the property that `h` = `g^2`, we can simplify a little bit further:
//!
//! ``` text
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(h^y - h^z) - (p - Σ di)*(g^y*h^z - g^z*h^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------------------------------
//! (h^x - h^z)*(g^y - g^z) - (h^y - h^z)*(g^x - g^z)
//!
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(g^2y - g^2z) - (p - Σ di)*(g^y*g^2z - g^z*g^2y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = --------------------------------------------------------------------------------------------
//! (g^2x - g^2z)*(g^y - g^z) - (g^2y - g^2z)*(g^x - g^z)
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(g^2y - g^2z) - (p - Σ di)*(g^y*g^2z - g^z*g^2y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = --------------------------------------------------------------------------------------------
//! (g^x - g^y)*(g^x - g^z)*(g^y - g^z)
//!
//! (r - Σ di*h^i)*(g^y - g^z) - (q - Σ di*g^i)*(g^y - g^z)^2 - (p - Σ di)*g^y*g^z*(g^z - g^y)
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = --------------------------------------------------------------------------------------------
//! (g^x - g^y)*(g^x - g^z)*(g^y - g^z)
//!
//! (r - Σ di*h^i) - (q - Σ di*g^i)*(g^y - g^z) - (p - Σ di)*g^y*g^z
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------
//! (g^x - g^y)*(g^x - g^z)
//! ```
//!
//! Putting it all together:
//!
//! ``` text
//! (r - Σ di*h^i) - (q - Σ di*g^i)*(g^y - g^z) - (p - Σ di)*g^y*g^z
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------
//! (g^x - g^y)*(g^x - g^z)
//!
//! (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! i!=x,y,z i!=x,y,z
//! dy = ------------------------------------------------
//! g^y - g^z
//!
//! dz = p - Σ di - dx - dy
//! i!=x,y,z
//! ```
//!
//! So, to summarize, given parity blocks `p`, `q`, `r`:
//!
//! ``` text
//! p = d0 + d1 + d2 + ...
//!
//! q = d0*g^0 + d1*g^1 + d2*g^2 + ...
//!
//! r = d0*h^0 + d1*h^1 + d2*h^2 + ...
//!
//! or
//!
//! p = Σ di
//! i
//!
//! q = Σ di*g^i
//! i
//!
//! r = Σ di*h^i
//! i
//! ```
//!
//! We can solve for any three bad blocks, `dx`, `dy`, `dz`:
//!
//! ``` text
//! dx + dy + dz = p - Σ di
//! i!=x,y,z
//!
//! dx*g^x + dy*g^y + dz*g^z = q - Σ di*g^i
//! i!=x,y,z
//!
//! dx*h^x + dy*h^y + dz*h^z = r - Σ di*h^i
//! i!=x,y,z
//!
//! or
//!
//! (r - Σ di*h^i) - (q - Σ di*g^i)*(g^y - g^z) - (p - Σ di)*g^y*g^z
//! i!=x,y,z i!=x,y,z i!=x,y,z
//! dx = ----------------------------------------------------------------
//! (g^x - g^y)*(g^x - g^z)
//!
//! (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! i!=x,y,z i!=x,y,z
//! dy = ------------------------------------------------
//! g^y - g^z
//!
//! dz = p - Σ di - dx - dy
//! i!=x,y,z
//! ```
//!
//! And, the moment you've all been waiting for, lets see this in action!
//!
//! ``` rust
//! # use ::gf256::*;
//! # use ::gf256::traits::FromLossy;
//! #
//! // given some data blocks
//! let mut data = vec![gf256(0); 10];
//! for i in 0..data.len() {
//! data[i] = gf256::from_lossy(i);
//! }
//!
//! // calculate parity blocks p, q, r
//! //
//! // p = Σ di
//! // i
//! //
//! // q = Σ di*g^i
//! // i
//! //
//! // r = Σ di*h^i
//! // i
//! //
//! let mut p = gf256(0);
//! let mut q = gf256(0);
//! let mut r = gf256(0);
//! for i in 0..data.len() {
//! let g = gf256::GENERATOR.pow(i as u8);
//! let h = g*g;
//! p += data[i];
//! q += data[i]*g;
//! r += data[i]*h;
//! }
//!
//! // oh no! THREE blocks are corrupted!
//! data[1] = gf256(0xff);
//! data[2] = gf256(0xff);
//! data[3] = gf256(0xff);
//!
//! // reconstruct using our parity blocks p and q
//! //
//! // (r - Σ di*h^i) - (q - Σ di*g^i)*(g^y - g^z) - (p - Σ di)*g^y*g^z
//! // i!=x,y,z i!=x,y,z i!=x,y,z
//! // dx = ----------------------------------------------------------------
//! // (g^x - g^y)*(g^x - g^z)
//! //
//! // (q - Σ di*g^i) - (p - Σ di)*g^z - dx*(g^x - g^z)
//! // i!=x,y,z i!=x,y,z
//! // dy = ------------------------------------------------
//! // g^y - g^z
//! //
//! // dz = p - Σ di - dx - dy
//! // i!=x,y,z
//! //
//! let mut pdelta = p;
//! let mut qdelta = q;
//! let mut rdelta = r;
//! for i in 0..data.len() {
//! if i != 1 && i != 2 && i != 3 {
//! let g = gf256::GENERATOR.pow(i as u8);
//! let h = g*g;
//! pdelta -= data[i];
//! qdelta -= data[i]*g;
//! rdelta -= data[i]*h;
//! }
//! }
//! let gx = gf256::GENERATOR.pow(1);
//! let gy = gf256::GENERATOR.pow(2);
//! let gz = gf256::GENERATOR.pow(3);
//! data[1] = (rdelta - qdelta*(gy - gz) - pdelta*gy*gz) / ((gx - gy) * (gx - gz));
//! data[2] = (qdelta - pdelta*gz - data[1]*(gx - gz)) / (gy - gz);
//! data[3] = pdelta - data[1] - data[2];
//!
//! for i in 0..data.len() {
//! assert_eq!(data[i], gf256::from_lossy(i));
//! }
//! ```
//!
//! And that's RAID 7, aka triple-parity.
//!
//! There are still a number of steps not covered here. It's always possible
//! for your parity blocks themselves to fail, in which case you just need
//! to rebuild your parity blocks using the data blocks.
//!
//! It gets more tricky if both some parity blocks and some data blocks fail,
//! in which case you need to reconstruct the data blocks with whatever parity
//! blocks are left, and then rebuilding the missing parity blocks. This is
//! equivalent to solving a smaller RAID-parity scheme.
//!
//! ## RAID 7 in ZFS
//!
//! The first use of triple-parity RAID, at least that I've seen, was developed
//! by Adam Leventhal in order to increasing the resilience to disk failures
//! in ZFS. He was the one who first found that there are up to three
//! easy-to-calculate sources for linearly-independent equations, and it's the
//! scheme that he outlined that is implemented here. You can read his original
//! blog post [here][leventhal-blog].
//!
//! It's interesting to note that the original motivation for triple parity is
//! actually the growing amount of time it takes to reconstruct a drive when a
//! disk fails, which involves reading all other disks and is measured in hours.
//! The risk being that the longer reconstruction takes the more likely that
//! other disks will fail, putting you in a precarious position
//!
//! ## Limitations
//!
//! RAID 5, aka single-parity, is actually the most flexible. It can support
//! any number of blocks, though it can only repair a single block failure.
//!
//! RAID 6 and RAID 7, aka double-parity and triple-parity, rely on the uniqueness
//! of powers of a generator in the field. Because of this, these schemes are
//! limited to the number of non-zero elements in the field. In the case of `GF(256)`,
//! this limits RAID 6 and RAID 7 to 255 blocks.
//!
//! Each scheme can repair any block up to the number of parity blocks, however
//! they don't actually provide the detection of block failures. One way to do this
//! is attach a CRC or other checksum to each block.
//!
//! ## RAID8? >3 parity blocks?
//!
//! As it is, the current scheme only supports up to 3 parity blocks. But it is
//! actually possible to use a different scheme that works beyond 3 parity blocks.
//!
//! As outlined in James S. Plank’s paper, [Note: Correction to the 1997 Tutorial
//! on Reed-Solomon Coding][plank], you can construct a modified [Vandermonde matrix
//! ][vandermonde-matrix] that allows you to solve the linear system of equations for
//! any number of parity blocks.
//!
//! The downside Plank's approach is that you need to store an array of unique constants
//! for each block of data, for each parity block.
//!
//!
//! [raid-wiki]: https://en.wikipedia.org/wiki/Standard_RAID_levels
//! [linearly-independent]: https://en.wikipedia.org/wiki/Linear_independence
//! [coprime]: https://en.wikipedia.org/wiki/Coprime_integers
//! [vandermonde-matrix]: https://en.wikipedia.org/wiki/Vandermonde_matrix
//! [miracle-max]: https://www.imdb.com/title/tt0093779/characters/nm0000345
//! [A051179]: https://oeis.org/A051179
//! [leventhal-blog]: http://dtrace.org/blogs/ahl/2009/07/21/triple-parity-raid-z
//! [plank]: http://web.eecs.utk.edu/~jplank/plank/papers/CS-03-504.pdf
//! [raid-example]:
// macro for creating RAID-parity implementations
pub use raid;
// RAID-parity functions
//