/
raid.rs
1620 lines (1530 loc) · 60.2 KB
/
raid.rs
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
//! ## 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 run --features 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 gf256_macros::raid;
// RAID-parity functions
//
#[raid(parity=1)]