gf256 0.2.0

A Rust library containing Galois-field types and utilities
Documentation
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
//! ## LFSR structs and macros
//!
//! A [linear-feedback shift register (LFSR)][lfsr-wiki] is a simple method of
//! creating a pseudo-random stream of bits using only a small circuit of shifts
//! and xors.
//!
//! LFSRs can be modelled mathematically as multiplication in a Galois-field,
//! allowing efficient bit generation, both forward and backwards, and efficient
//! seeking to any state of the LFSR.
//!
//! ``` rust
//! # use std::iter;
//! use gf256::lfsr::Lfsr16;
//!
//! let mut lfsr = Lfsr16::new(1);
//! assert_eq!(lfsr.next(16), 0x0001);
//! assert_eq!(lfsr.next(16), 0x002d);
//! assert_eq!(lfsr.next(16), 0x0451);
//! assert_eq!(lfsr.next(16), 0xbdad);
//! assert_eq!(lfsr.prev(16), 0xbdad);
//! assert_eq!(lfsr.prev(16), 0x0451);
//! assert_eq!(lfsr.prev(16), 0x002d);
//! assert_eq!(lfsr.prev(16), 0x0001);
//! ```
//!
//! Note this module requires feature `lfsr`.
//!
//! A fully featured implementation of LFSRs can be found in
//! [`examples/lfsr.rs`][lfsr-example]:
//!
//! ``` bash
//! $ RUSTFLAGS="-Ctarget-cpu=native" cargo +nightly run --features nightly,thread-rng,lfsr,crc,shamir,raid,rs --example lfsr
//!
//! testing lfsr64
//! lfsr64_naive                        => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_divrem                       => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_table                        => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_small_table                  => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_barret                       => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_table_barret                 => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_small_table_barret           => 0000000000000001000000000000001b00000000000001450000000000001db7
//! rev(lfsr64_naive)                   => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_divrem)                  => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_table)                   => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_small_table)             => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_barret)                  => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_table_barret)            => b71d00000000000045010000000000001b000000000000000100000000000000
//! rev(lfsr64_small_table_barret)      => b71d00000000000045010000000000001b000000000000000100000000000000
//! lfsr64_naive+9000                   => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_divrem+9000                  => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_table+9000                   => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_small_table+9000             => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_barret+9000                  => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_table_barret+9000            => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! lfsr64_small_table_barret+9000      => 4aec56b077706daa269325545515010d361e101b06c71bad8b33b14551004b42
//! rev(lfsr64_naive+9000)              => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_divrem+9000)             => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_table+9000)              => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_small_table+9000)        => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_barret+9000)             => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_table_barret+9000)       => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! rev(lfsr64_small_table_barret+9000) => 424b005145b1338bad1bc7061b101e360d01155554259326aa6d7077b056ec4a
//! lfsr64_naive+9000-9000              => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_divrem+9000-9000             => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_table+9000-9000              => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_small_table+9000-9000        => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_barret+9000-9000             => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_table_barret+9000-9000       => 0000000000000001000000000000001b00000000000001450000000000001db7
//! lfsr64_small_table_barret+9000-9000 => 0000000000000001000000000000001b00000000000001450000000000001db7
//!
//! 2048 lfsr64 samples:
//!                                           .      .                       .
//!                  .     '  .    .  .'       .                             :
//!              .  '               '   '' '    .      '  .'  '             ':
//!           . .        '  . . .:  ::  . .' .. . '                .        .:
//!        '   ' '    .:  ' ' . .: . .  ::  .:. .    . ' . ' .      .      .::
//!             .'  .: ' ..   :.   ::.'.:. ' ' . '.'.  ' '       '         '::
//!           '    .  ' .''..:..'.: .:.  '  '.    :'':.        '           .::
//!          :   ''.. ''. '. .:':''''.:':..''...  :.  :  '.   :. '        ::::
//!             .  . .:  .' ' :':.:'. :.''::' :::'.:... :    .'           ::::
//!               ': ':''  '': :.: .:' .::::.:': ..' :.''   .' . :        ::::
//!         .  .  .    ':.' :'   ''.:. ::.'''' '. : '.  ..     .   '       :::
//!        '    . ' '.. .'  '..'.' .:'' . '. ''. ':  ' .. '     '   '       ::
//!            '  ' .    '      ...''.: .:. .' :    ..' . '   :             ::
//!                 '.         .     '  '' .      '    .  ': .'.             :
//!                    '    .. .  '.'   .             '                      :
//!     '         '    '.'.'  .    ''   :         .  .              '       ':
//!
//!                                 :   :.     .
//!     .  ...........:::...::::::.:::::::::.:::..:..::........... ..
//!
//! 64513/131072 ones (49.22%), 0.27% compressability
//! ```
//!
//! ## How do LFSRs work?
//!
//! Consider the following LFSR, where each bit represents a single-bit register,
//! and each `x` represents an xor gate, sometimes called the "taps" of an LFSR.
//!
//! We can step through some states by hand to see what the output would be:
//!
//! ``` text
//! output <-.- 1 <-- 1 <-- 0 <-- 0 <x- 1 <x- 1 <x- 0 <-- 0 <-.
//!          '-----------------------'-----'-----'------------'
//! output      internal state
//!      1      1     0     0     0     0     1     0     1
//!      1      0     0     0     1     0     1     1     1
//!      0      0     0     1     0     1     1     1     0
//!      0      0     1     0     1     1     1     0     0
//!      0      1     0     1     1     1     0     0     0
//!      1      0     1     1     0     1     1     0     1
//!      0      1     1     0     1     1     0     1     0
//!      1      1     0     1     0     1     0     0     1
//!      ...
//! ```
//!
//! Consider what this might look like in code. We can model the bit of feedback
//! as a branch on whether or not to xor our internal state with an integer
//! containing a 1 for each LFSR tap:
//!
//! ``` rust
//! let mut state: u32 = 0b11001100;
//! let mut step = || {
//!     state = state << 1;
//!     let output = (state & 0x100) >> 8;
//!     if output != 0 {
//!         state ^= 0x1d
//!     }
//!     state = 0xff & state;
//!     output
//! };
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! ```
//!
//! Normally, we think of the high bit just "falling off" when we shift the LFSR
//! state. But we could instead model it as an xor with itself, making the high
//! bit a part of our taps and removing the bit mask:
//!
//! ``` rust
//! let mut state: u32 = 0b11001100;
//! let mut step = || {
//!     state = state << 1;
//!     let output = (state & 0x100) >> 8;
//!     if output != 0 {
//!         state ^= 0x11d
//!     }
//!     output
//! };
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! ```
//!
//! This is actually polynomial division of a 9-bit number! Remember that
//! polynomial division is repeated shifts and xors until the dividend is
//! smaller than our divisor. In this case, we only need a single step.
//!
//! And if you remember your computer-science tricks, shifting by one is
//! equivalent to multiplying by 2 (this is still true with polynomial
//! multiplication because no carry could have occured).
//!
//! So instead of using bit-shifts and xors, we could use multiplication and
//! division with our polynomial types:
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! let mut state: p32 = p32(0b11001100);
//! let mut step = || {
//!     state = state * p32(2);
//!     let output = state / p32(0x11d);
//!     state = state % p32(0x11d);
//!     u32::from(output)
//! };
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! ```
//!
//! And a polynomial multiplication followed by a remainder with a constant...
//! Isn't that equivalent to Galois-field multiplication?
//!
//! Why yes it is!
//!
//! ``` rust
//! # pub use ::gf256::*;
//! use ::gf256::gf::gf;
//!
//! #[gf(polynomial=0x11d, generator=2)]
//! type gf256_lfsr;
//!
//! # fn main() {
//! let mut state: gf256_lfsr = gf256_lfsr(0b11001100);
//! let mut step = || {
//!     let output = (state & 0x80) >> 7;
//!     state = state * gf256_lfsr(2);
//!     u32::from(output)
//! };
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! assert_eq!(step(), 0);
//! assert_eq!(step(), 1);
//! # }
//! ```
//!
//! Up until this point we were just using a seemingly arbitrary sequence of taps
//! to define our LFSR. But since LFSRs are equivalent to Galois-fields, we can
//! use our knowledge of Galois-fields to come up with a good sequence of taps, aka
//! the polynomial that defines our field.
//!
//! From exploring [Galois-fields](../gf), we know that a good polynomial must be
//! "irreducible", that is it must not have any other factors or else we end up
//! with multiple smaller non-intersecting fields.
//!
//! We also know that every finite-field contains "primitive elements", which are
//! numbers in the field whose multiplicative cycle contains every element of the
//! field.
//!
//! So what we want, is an irreducible polynomial, where 2 is a primitive element
//! of the finite-field defined by said polynomial. If these conditions are met,
//! than repeated multiplications by 2 (shifting our LFSR) will actually iterate
//! through every non-zero element of our field before looping, giving us the
//! maximum cycle-length for any `n`-bit LFSR (or any pseudo-random number generator
//! with `n`-bits of state).
//!
//! These polynomials are sometimes called "primitive polynomials", but there's
//! nothing really magical about them. In order to find primitive polynomials, we
//! just brute force search all polynomials until we find one that works. There is
//! more information about this in [`gf`'s module-level documentation
//! ](../gf#finding-irreducible-polynomials-and-generators).
//!
//! Knowing that LFSRs are equivalent to polynomial multiplication and division,
//! we can also step through multiple bits at a time by multiplying by a
//! power-of-two:
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! let mut state: p32 = p32(0b11001100);
//! let mut step = |n: u32| {
//!     state = state * p32(2).pow(n);
//!     let output = state / p32(0x11d);
//!     state = state % p32(0x11d);
//!     u32::from(output)
//! };
//! assert_eq!(step(8), 0b11000101);
//! ```
//!
//! And we can create this exact LFSR using gf256:
//!
//! ``` rust
//! # pub use ::gf256::*;
//! use ::gf256::lfsr::lfsr;
//!
//! #[lfsr(polynomial=0x11d)]
//! struct Lfsr {}
//!
//! # fn main() {
//! let mut lfsr = Lfsr::new(0b11001100);
//! assert_eq!(lfsr.next(8), 0b11000101);
//! # }
//! ```
//!
//! ## Stepping backwards
//!
//! Fascinating to me was learning that you can actually run an LFSR _backwards_.
//! All you need to do is invert the taps and shift in the other direction:
//!
//! ``` text
//! assert_eq!(step(8), 0b11000101);
//!         .-> 1 --> 0 --> 1 --> 0 -x> 1 -x> 0 -x> 0 --> 1 -.-> output
//!         '------------------------'-----'-----'-----------'
//!             internal state
//!             1     1     0     1     1     0     1     0       1
//!             0     1     1     0     1     1     0     1       0
//!             1     0     1     1     1     0     0     0       1
//!             0     1     0     1     1     1     0     0       0
//!             0     0     1     0     1     1     1     0       0
//!             0     0     0     1     0     1     1     1       0
//!             1     0     0     0     0     1     0     1       1
//!             1     1     0     0     1     1     0     0       1
//!             ...
//! ```
//!
//! This works perfectly fine with powers-of-two, allowing multiple bits to be
//! stepped through at a time:
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! let mut state: p32 = p32(0b10101001);
//! let mut step = |n: u32| {
//!     state = (state.reverse_bits() >> 32u32-8) * p32(2).pow(n);
//!     let output = (state / (p32(0x11d).reverse_bits() >> 32u32-9))
//!         .reverse_bits() >> 32u32-8;
//!     state = (state % (p32(0x11d).reverse_bits() >> 32u32-9))
//!         .reverse_bits() >> 32u32-8;
//!     u32::from(output)
//! };
//! assert_eq!(step(8), 0b11000101);
//! ```
//!
//! And is supported by the LFSR structs:
//!
//! ``` rust
//! # pub use ::gf256::*;
//! use ::gf256::lfsr::lfsr;
//!
//! #[lfsr(polynomial=0x11d)]
//! struct Lfsr {}
//!
//! # fn main() {
//! let mut lfsr = Lfsr::new(0b10101001);
//! assert_eq!(lfsr.prev(8), 0b11000101);
//! # }
//! ```
//!
//! However, as far as I can tell, this doesn't generalize to non-power-of-two
//! multiplicands, which is a real shame since it would provide a much more
//! efficient division implementation for general Galois-fields.
//!
//! ## Seeking
//!
//! Perhaps the most useful feature of modeling LFSRs as Galois-fields, is that we
//! can step through multiple bits at a time. In fact, if we don't care about the
//! output, we can skip to any position in the LFSR state by exponentiation.
//!
//! If we use an efficient exponentation algorithm, such as [exponentiation by
//! squaring][exp-by-squaring], we can seek to any position in the LFSR state with
//! only `O(log log n)` multiplications:
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! let mut state: p32 = p32(0b11001100);
//! let mut skip = |mut n: u32| {
//!     // Binary exponentiation
//!     let mut a = p32(2);
//!     let mut g = p32(1);
//!     loop {
//!         if n & 1 != 0 {
//!             g = (g * a) % p32(0x11d);
//!         }
//!
//!         n >>= 1;
//!         if n == 0 {
//!             break;
//!         }
//!         a = (a * a) % p32(0x11d);
//!     };
//!
//!     // Final multiplication
//!     state = (state * g) % p32(0x11d);
//! };
//! skip(100);
//!
//! let mut step = |n: u32| {
//!     state = state * p32(2).pow(n);
//!     let output = state / p32(0x11d);
//!     state = state % p32(0x11d);
//!     u32::from(output)
//! };
//! assert_eq!(step(8), 0b10011111);
//! ```
//!
//! And with our LFSR structs:
//!
//! ``` rust
//! # pub use ::gf256::*;
//! use ::gf256::lfsr::lfsr;
//!
//! #[lfsr(polynomial=0x11d)]
//! struct Lfsr {}
//!
//! # fn main() {
//! let mut lfsr = Lfsr::new(0b11001100);
//! lfsr.skip(100);
//! assert_eq!(lfsr.next(8), 0b10011111);
//! # }
//! ```
//!
//! ## Optimizations
//!
//! Since LFSRs are equivalent to Galois-fields, they share a lot of the same
//! optimizations. With the exception that LFSRs need to worry about capturing
//! the quotient portion of division, since this forms the actual output bits.
//!
//! This naive implementation of LFSRs is available with the `naive` mode. Note this
//! uses the shift-and-xor implementation, since this is much faster than full
//! polynomial division/remainder without other optimizations. Additional modes:
//!
//! - In `table` mode, LFSRs use a precomputed division and remainder table to
//!   compute both the quotient and remainder a byte at a time.
//!
//!   This uses the same technique as the precomputed remainder tables in CRC
//!   calculation, since a CRC is just a polynomial remainder. The [crc](../crc)
//!   module-level documentation has more info on this.
//!
//! - In `small_table` mode, the same strategy as `table` mode is used, but
//!   with 16 element tables computing the quotient and remainder a nibble at a time.
//!
//! - In `barret` mode, LFSRs use [Barret-reduction][barret-reduction] to efficiently
//!   compute the remainder using only multiplication by precomputed constants.
//!
//!   However we still need to compute the quotient using naive polynomial division,
//!   making this mode less effective than in other types/utilities.
//!
//! - In `table_barret` mode, LFSRs use [Barret-reduction][barret-reduction] to find
//!   the remainder, and a precomputed division table to compute the quotient.
//!
//!   This seems to have the worst of both worlds, and does not perform that well.
//!
//! - In `small_table_barret` mode, the same strategy as `table_barret` mode is used,
//!   but with a 16 element table for the quotient, making this mode even less
//!   performant.
//!
//! By default `barret` mode is used for LFSR structs, as it outperforms all other
//! options, even when hardware carry-less multiplication is not available.
//!
//! Though note the default mode is susceptible to change.
//!
//! See also [BENCHMARKS.md][benchmarks]
//!
//! ## The `Rng` trait
//!
//! In addition to the above APIs, the LFSR structs in this module satisfy the
//! [`RngCore`](rand::RngCore) and [`SeedableRng`](rand::SeedableRng) traits found
//! in the [`rand`] crate. This allows custom LFSRs to act as drop-in replacements
//! for other pseudo-random number generators with the additional ability to seek and
//! rewind, allowing perfect replayability.
//!
//! Note! If you're just looking for a pseudo-random number generator, the
//! randomness generated by these LFSRs is equivalent to the same-sized, naive
//! [Xorshift generators][xorshift], with the same limitations and cycle-length.
//! However, Xorshift generators are much more efficient, using only a handful of
//! shifts and xors.
//!
//!
//! [lfsr-wiki]: https://en.wikipedia.org/wiki/Linear-feedback_shift_register
//! [exp-by-squaring]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
//! [barret-reduction]: https://en.wikipedia.org/wiki/Barrett_reduction
//! [xorshift]: https://en.wikipedia.org/wiki/Xorshift
//! [lfsr-example]:
//! [benchmarks]:


// macro for creating LFSR implementations
pub use gf256_macros::lfsr;


// Default LFSR structs
//
#[lfsr(polynomial=0x11d)]
pub struct Lfsr8 {}
#[lfsr(polynomial=0x1002d)]
pub struct Lfsr16 {}
#[lfsr(polynomial=0x1000000af)]
pub struct Lfsr32 {}
#[lfsr(polynomial=0x1000000000000001b)]
pub struct Lfsr64 {}


#[cfg(test)]
mod test {
    use super::*;
    use crate::p::p64;
    use crate::p::p128;
    use core::num::NonZeroU64;
    use core::num::NonZeroU128;
    use core::iter::FromIterator;
    use rand::Rng;

    extern crate alloc;
    use alloc::vec::Vec;
    use alloc::vec;
    use alloc::collections::BTreeSet;

    extern crate std;
    use std::iter;

    #[test]
    fn lfsr() {
        let mut lfsr8 = Lfsr8::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16 = Lfsr16::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32 = Lfsr32::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64 = Lfsr64::new(1);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_skip() {
        let mut lfsr8 = Lfsr8::new(1);
        lfsr8.skip(8*8);
        let buf = iter::repeat_with(|| lfsr8.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16 = Lfsr16::new(1);
        lfsr16.skip(16*8);
        let buf = iter::repeat_with(|| lfsr16.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32 = Lfsr32::new(1);
        lfsr32.skip(32*8);
        let buf = iter::repeat_with(|| lfsr32.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64 = Lfsr64::new(1);
        lfsr64.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_skip_backwards() {
        let mut lfsr8 = Lfsr8::new(1);
        lfsr8.skip(8*16);
        lfsr8.skip_backwards(8*8);
        let buf = iter::repeat_with(|| lfsr8.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16 = Lfsr16::new(1);
        lfsr16.skip(16*16);
        lfsr16.skip_backwards(16*8);
        let buf = iter::repeat_with(|| lfsr16.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32 = Lfsr32::new(1);
        lfsr32.skip(32*16);
        lfsr32.skip_backwards(32*8);
        let buf = iter::repeat_with(|| lfsr32.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64 = Lfsr64::new(1);
        lfsr64.skip(64*16);
        lfsr64.skip_backwards(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    // explicit modes
    #[lfsr(polynomial=0x11d, naive, naive_skip)]               pub struct Lfsr8Naive {}
    #[lfsr(polynomial=0x11d, table, table_skip)]               pub struct Lfsr8Table {}
    #[lfsr(polynomial=0x11d, small_table, small_table_skip)]   pub struct Lfsr8SmallTable {}
    #[lfsr(polynomial=0x11d, barret, barret_skip)]             pub struct Lfsr8Barret {}
    #[lfsr(polynomial=0x11d, table_barret, barret_skip)]       pub struct Lfsr8TableBarret {}
    #[lfsr(polynomial=0x11d, small_table_barret, barret_skip)] pub struct Lfsr8SmallTableBarret {}

    #[lfsr(polynomial=0x1002d, naive, naive_skip)]               pub struct Lfsr16Naive {}
    #[lfsr(polynomial=0x1002d, table, table_skip)]               pub struct Lfsr16Table {}
    #[lfsr(polynomial=0x1002d, small_table, small_table_skip)]   pub struct Lfsr16SmallTable {}
    #[lfsr(polynomial=0x1002d, barret, barret_skip)]             pub struct Lfsr16Barret {}
    #[lfsr(polynomial=0x1002d, table_barret, barret_skip)]       pub struct Lfsr16TableBarret {}
    #[lfsr(polynomial=0x1002d, small_table_barret, barret_skip)] pub struct Lfsr16SmallTableBarret {}

    #[lfsr(polynomial=0x1000000af, naive, naive_skip)]               pub struct Lfsr32Naive {}
    #[lfsr(polynomial=0x1000000af, table, table_skip)]               pub struct Lfsr32Table {}
    #[lfsr(polynomial=0x1000000af, small_table, small_table_skip)]   pub struct Lfsr32SmallTable {}
    #[lfsr(polynomial=0x1000000af, barret, barret_skip)]             pub struct Lfsr32Barret {}
    #[lfsr(polynomial=0x1000000af, table_barret, barret_skip)]       pub struct Lfsr32TableBarret {}
    #[lfsr(polynomial=0x1000000af, small_table_barret, barret_skip)] pub struct Lfsr32SmallTableBarret {}

    #[lfsr(polynomial=0x1000000000000001b, naive, naive_skip)]               pub struct Lfsr64Naive {}
    #[lfsr(polynomial=0x1000000000000001b, table, table_skip)]               pub struct Lfsr64Table {}
    #[lfsr(polynomial=0x1000000000000001b, small_table, small_table_skip)]   pub struct Lfsr64SmallTable {}
    #[lfsr(polynomial=0x1000000000000001b, barret, barret_skip)]             pub struct Lfsr64Barret {}
    #[lfsr(polynomial=0x1000000000000001b, table_barret, barret_skip)]       pub struct Lfsr64TableBarret {}
    #[lfsr(polynomial=0x1000000000000001b, small_table_barret, barret_skip)] pub struct Lfsr64SmallTableBarret {}

    // test explicit div/rem modes
    #[test]
    fn lfsr_naive() {
        let mut lfsr8_naive = Lfsr8Naive::new(1);
        let buf = iter::repeat_with(|| lfsr8_naive.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_naive.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_naive = Lfsr16Naive::new(1);
        let buf = iter::repeat_with(|| lfsr16_naive.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_naive.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_naive = Lfsr32Naive::new(1);
        let buf = iter::repeat_with(|| lfsr32_naive.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_naive.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_naive = Lfsr64Naive::new(1);
        let buf = iter::repeat_with(|| lfsr64_naive.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_naive.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_table() {
        let mut lfsr8_table = Lfsr8Table::new(1);
        let buf = iter::repeat_with(|| lfsr8_table.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_table.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_table = Lfsr16Table::new(1);
        let buf = iter::repeat_with(|| lfsr16_table.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_table.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_table = Lfsr32Table::new(1);
        let buf = iter::repeat_with(|| lfsr32_table.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_table.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_table = Lfsr64Table::new(1);
        let buf = iter::repeat_with(|| lfsr64_table.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_table.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_small_table() {
        let mut lfsr8_small_table = Lfsr8SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr8_small_table.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_small_table.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_small_table = Lfsr16SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr16_small_table.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_small_table.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_small_table = Lfsr32SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr32_small_table.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_small_table.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_small_table = Lfsr64SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr64_small_table.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_small_table.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_barret() {
        let mut lfsr8_barret = Lfsr8Barret::new(1);
        let buf = iter::repeat_with(|| lfsr8_barret.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_barret.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_barret = Lfsr16Barret::new(1);
        let buf = iter::repeat_with(|| lfsr16_barret.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_barret.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_barret = Lfsr32Barret::new(1);
        let buf = iter::repeat_with(|| lfsr32_barret.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_barret.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_barret = Lfsr64Barret::new(1);
        let buf = iter::repeat_with(|| lfsr64_barret.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_barret.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_table_barret() {
        let mut lfsr8_table_barret = Lfsr8TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr8_table_barret.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_table_barret.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_table_barret = Lfsr16TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr16_table_barret.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_table_barret.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_table_barret = Lfsr32TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr32_table_barret.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_table_barret.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_table_barret = Lfsr64TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr64_table_barret.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_table_barret.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_small_table_barret() {
        let mut lfsr8_small_table_barret = Lfsr8SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr8_small_table_barret.next(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x01,0x1c,0x4b,0x81,0x92,0x6e,0x41,0x5b]);
        let buf = iter::repeat_with(|| lfsr8_small_table_barret.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_small_table_barret = Lfsr16SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr16_small_table_barret.next(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0001,0x002d,0x0451,0xbdad,0x13d3,0xb877,0x94e7,0xfcb8]);
        let buf = iter::repeat_with(|| lfsr16_small_table_barret.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_small_table_barret = Lfsr32SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr32_small_table_barret.next(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x00000001,0x000000af,0x00004455,0x00295f23,0x1010111b,0xfafa511e,0x11579360,0x7d21bf13]);
        let buf = iter::repeat_with(|| lfsr32_small_table_barret.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_small_table_barret = Lfsr64SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr64_small_table_barret.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_small_table_barret.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    // test explicit skip modes
    #[test]
    fn lfsr_naive_skip() {
        let mut lfsr8_naive = Lfsr8Naive::new(1);
        lfsr8_naive.skip(8*8);
        let buf = iter::repeat_with(|| lfsr8_naive.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_naive = Lfsr16Naive::new(1);
        lfsr16_naive.skip(16*8);
        let buf = iter::repeat_with(|| lfsr16_naive.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_naive = Lfsr32Naive::new(1);
        lfsr32_naive.skip(32*8);
        let buf = iter::repeat_with(|| lfsr32_naive.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_naive = Lfsr64Naive::new(1);
        lfsr64_naive.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64_naive.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_table_skip() {
        let mut lfsr8_table = Lfsr8Table::new(1);
        lfsr8_table.skip(8*8);
        let buf = iter::repeat_with(|| lfsr8_table.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_table = Lfsr16Table::new(1);
        lfsr16_table.skip(16*8);
        let buf = iter::repeat_with(|| lfsr16_table.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_table = Lfsr32Table::new(1);
        lfsr32_table.skip(32*8);
        let buf = iter::repeat_with(|| lfsr32_table.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_table = Lfsr64Table::new(1);
        lfsr64_table.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64_table.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_small_table_skip() {
        let mut lfsr8_small_table = Lfsr8SmallTable::new(1);
        lfsr8_small_table.skip(8*8);
        let buf = iter::repeat_with(|| lfsr8_small_table.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_small_table = Lfsr16SmallTable::new(1);
        lfsr16_small_table.skip(16*8);
        let buf = iter::repeat_with(|| lfsr16_small_table.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_small_table = Lfsr32SmallTable::new(1);
        lfsr32_small_table.skip(32*8);
        let buf = iter::repeat_with(|| lfsr32_small_table.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_small_table = Lfsr64SmallTable::new(1);
        lfsr64_small_table.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64_small_table.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    #[test]
    fn lfsr_barret_skip() {
        let mut lfsr8_barret = Lfsr8Barret::new(1);
        lfsr8_barret.skip(8*8);
        let buf = iter::repeat_with(|| lfsr8_barret.prev(8)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x5b,0x41,0x6e,0x92,0x81,0x4b,0x1c,0x01]);

        let mut lfsr16_barret = Lfsr16Barret::new(1);
        lfsr16_barret.skip(16*8);
        let buf = iter::repeat_with(|| lfsr16_barret.prev(16)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xfcb8,0x94e7,0xb877,0x13d3,0xbdad,0x0451,0x002d,0x0001]);

        let mut lfsr32_barret = Lfsr32Barret::new(1);
        lfsr32_barret.skip(32*8);
        let buf = iter::repeat_with(|| lfsr32_barret.prev(32)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7d21bf13,0x11579360,0xfafa511e,0x1010111b,0x00295f23,0x00004455,0x000000af,0x00000001]);

        let mut lfsr64_barret = Lfsr64Barret::new(1);
        lfsr64_barret.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64_barret.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    // odd step sizes
    #[test]
    fn lfsr_odd_nexts() {
        let mut lfsr8 = Lfsr8Naive::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16Naive::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32Naive::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);

        let mut lfsr8 = Lfsr8Table::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16Table::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32Table::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);

        let mut lfsr8 = Lfsr8SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);

        let mut lfsr8 = Lfsr8Barret::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16Barret::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32Barret::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);

        let mut lfsr8 = Lfsr8TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);

        let mut lfsr8 = Lfsr8SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr8.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0,0x1,0x1,0xc,0x4,0xb,0x8,0x1]);
        let buf = iter::repeat_with(|| lfsr8.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x8,0xb,0x4,0xc,0x1,0x1,0x0]);
        let mut lfsr16 = Lfsr16SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr16.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000,0x100,0x2d0,0x451,0xbda,0xd13,0xd3b,0x877]);
        let buf = iter::repeat_with(|| lfsr16.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x877,0xd3b,0xd13,0xbda,0x451,0x2d0,0x100,0x000]);
        let mut lfsr32 = Lfsr32SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr32.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000,0x004000,0x0015e0,0x000445,0x28014a,0x7c8c40,0x202237,0x7afa51]);
        let buf = iter::repeat_with(|| lfsr32.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x7afa51,0x202237,0x7c8c40,0x28014a,0x000445,0x0015e0,0x004000,0x000000]);
    }

    // odd LFSR sizes
    #[lfsr(polynomial=0x13, naive, naive_skip)]               pub struct Lfsr4Naive {}
    #[lfsr(polynomial=0x13, table, table_skip)]               pub struct Lfsr4Table {}
    #[lfsr(polynomial=0x13, small_table, small_table_skip)]   pub struct Lfsr4SmallTable {}
    #[lfsr(polynomial=0x13, barret, barret_skip)]             pub struct Lfsr4Barret {}
    #[lfsr(polynomial=0x13, table_barret, barret_skip)]       pub struct Lfsr4TableBarret {}
    #[lfsr(polynomial=0x13, small_table_barret, barret_skip)] pub struct Lfsr4SmallTableBarret {}

    #[lfsr(polynomial=0x1053, naive, naive_skip)]               pub struct Lfsr12Naive {}
    #[lfsr(polynomial=0x1053, table, table_skip)]               pub struct Lfsr12Table {}
    #[lfsr(polynomial=0x1053, small_table, small_table_skip)]   pub struct Lfsr12SmallTable {}
    #[lfsr(polynomial=0x1053, barret, barret_skip)]             pub struct Lfsr12Barret {}
    #[lfsr(polynomial=0x1053, table_barret, barret_skip)]       pub struct Lfsr12TableBarret {}
    #[lfsr(polynomial=0x1053, small_table_barret, barret_skip)] pub struct Lfsr12SmallTableBarret {}

    #[lfsr(polynomial=0x800021, naive, naive_skip)]               pub struct Lfsr23Naive {}
    #[lfsr(polynomial=0x800021, table, table_skip)]               pub struct Lfsr23Table {}
    #[lfsr(polynomial=0x800021, small_table, small_table_skip)]   pub struct Lfsr23SmallTable {}
    #[lfsr(polynomial=0x800021, barret, barret_skip)]             pub struct Lfsr23Barret {}
    #[lfsr(polynomial=0x800021, table_barret, barret_skip)]       pub struct Lfsr23TableBarret {}
    #[lfsr(polynomial=0x800021, small_table_barret, barret_skip)] pub struct Lfsr23SmallTableBarret {}

    #[test]
    fn lfsr_odd_sizes() {
        let mut lfsr4 = Lfsr4Naive::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Naive::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Naive::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4Table::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Table::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Table::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23SmallTable::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4Barret::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Barret::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Barret::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23TableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr4.next(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x1,0x3,0x5,0xe,0x2,0x6,0xb,0xc]);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr12.next(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x001,0x052,0x152,0x25d,0x462,0x20c,0x5c6,0x3a7]);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23SmallTableBarret::new(1);
        let buf = iter::repeat_with(|| lfsr23.next(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000001,0x000021,0x000401,0x008421,0x100005,0x1000a1,0x101485,0x128421]);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);
    }

    #[test]
    fn lfsr_odd_sizes_skip() {
        let mut lfsr4 = Lfsr4Naive::new(1);
        lfsr4.skip(4*8);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Naive::new(1);
        lfsr12.skip(12*8);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Naive::new(1);
        lfsr23.skip(23*8);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4Table::new(1);
        lfsr4.skip(4*8);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Table::new(1);
        lfsr12.skip(12*8);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Table::new(1);
        lfsr23.skip(23*8);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4SmallTable::new(1);
        lfsr4.skip(4*8);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12SmallTable::new(1);
        lfsr12.skip(12*8);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23SmallTable::new(1);
        lfsr23.skip(23*8);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);

        let mut lfsr4 = Lfsr4Barret::new(1);
        lfsr4.skip(4*8);
        let buf = iter::repeat_with(|| lfsr4.prev(4)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xc,0xb,0x6,0x2,0xe,0x5,0x3,0x1]);
        let mut lfsr12 = Lfsr12Barret::new(1);
        lfsr12.skip(12*8);
        let buf = iter::repeat_with(|| lfsr12.prev(12)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x3a7,0x5c6,0x20c,0x462,0x25d,0x152,0x052,0x001]);
        let mut lfsr23 = Lfsr23Barret::new(1);
        lfsr23.skip(23*8);
        let buf = iter::repeat_with(|| lfsr23.prev(23)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x128421,0x101485,0x1000a1,0x100005,0x008421,0x000401,0x000021,0x000001]);
    }

    // bit-reflected LFSRs
    #[lfsr(polynomial=0x1000000000000001b, naive, naive_skip, reflected=true)]               pub struct Lfsr64NaiveReflected {}
    #[lfsr(polynomial=0x1000000000000001b, table, table_skip, reflected=true)]               pub struct Lfsr64TableReflected {}
    #[lfsr(polynomial=0x1000000000000001b, small_table, small_table_skip, reflected=true)]   pub struct Lfsr64SmallTableReflected {}
    #[lfsr(polynomial=0x1000000000000001b, barret, barret_skip, reflected=true)]             pub struct Lfsr64BarretReflected {}
    #[lfsr(polynomial=0x1000000000000001b, table_barret, barret_skip, reflected=true)]       pub struct Lfsr64TableBarretReflected {}
    #[lfsr(polynomial=0x1000000000000001b, small_table_barret, barret_skip, reflected=true)] pub struct Lfsr64SmallTableBarretReflected {}

    #[test]
    fn lfsr_reflected() {
        let mut lfsr64 = Lfsr64NaiveReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64TableReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64SmallTableReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64BarretReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64TableBarretReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64SmallTableBarretReflected::new(0x8000000000000000);
        let buf = iter::repeat_with(|| lfsr64.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x8000000000000000,0xd800000000000000,0xa280000000000000,0xedb8000000000000,0x8808800000000000,0xd58d580000000000,0xa8a28a8000000000,0xe36db63800000000]);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);
    }

    #[test]
    fn lfsr_reflected_skip() {
        let mut lfsr64 = Lfsr64NaiveReflected::new(0x8000000000000000);
        lfsr64.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64TableReflected::new(0x8000000000000000);
        lfsr64.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64SmallTableReflected::new(0x8000000000000000);
        lfsr64.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);

        let mut lfsr64 = Lfsr64BarretReflected::new(0x8000000000000000);
        lfsr64.skip(64*8);
        let buf = iter::repeat_with(|| lfsr64.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0xe36db63800000000,0xa8a28a8000000000,0xd58d580000000000,0x8808800000000000,0xedb8000000000000,0xa280000000000000,0xd800000000000000,0x8000000000000000]);
    }

    // all LFSR params
    #[lfsr(
        polynomial=0x1000000000000001b,
        u=u64,
        u2=u128,
        nzu=NonZeroU64,
        nzu2=NonZeroU128,
        p=p64,
        p2=p128,
        reflected=false,
    )]
    struct Lfsr64AllParams {}

    #[test]
    fn lfsr_all_params() {
        let mut lfsr64_all_params = Lfsr64AllParams::new(1);
        let buf = iter::repeat_with(|| lfsr64_all_params.next(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x0000000000000001,0x000000000000001b,0x0000000000000145,0x0000000000001db7,0x0000000000011011,0x00000000001ab1ab,0x0000000001514515,0x000000001c6db6c7]);
        let buf = iter::repeat_with(|| lfsr64_all_params.prev(64)).take(8).collect::<Vec<_>>();
        assert_eq!(buf, &[0x000000001c6db6c7,0x0000000001514515,0x00000000001ab1ab,0x0000000000011011,0x0000000000001db7,0x0000000000000145,0x000000000000001b,0x0000000000000001]);
    }

    // other LFSR things

    #[test]
    fn lfsr_rng_consistency() {
        // normal order
        let mut lfsr = Lfsr8::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr8::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr16::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr16::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr32::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr32::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr64::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr64::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr4Table::new(1);
        let next_bytes = iter::repeat_with(|| (lfsr.next(4) << 4) | lfsr.next(4)).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr4Table::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr12Table::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr12Table::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        let mut lfsr = Lfsr23Table::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr23Table::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);

        // reflected order
        let mut lfsr = Lfsr64TableReflected::new(1);
        let next_bytes = iter::repeat_with(|| lfsr.next(8) as u8).take(100).collect::<Vec<_>>();
        let mut rng_bytes = vec![0u8; 100];
        let mut lfsr = Lfsr64TableReflected::new(1);
        lfsr.fill(&mut rng_bytes[..]);
        assert_eq!(&next_bytes, &rng_bytes);
    }

    #[test]
    fn lfsr_uniqueness() {
        let mut lfsr = Lfsr8::new(1);
        let unique = BTreeSet::from_iter(iter::repeat_with(|| lfsr.next(8)).take(255));
        assert_eq!(unique.len(), 255);

        let mut lfsr = Lfsr64::new(1);
        let unique = BTreeSet::from_iter(iter::repeat_with(|| lfsr.next(64)).take(255));
        assert_eq!(unique.len(), 255);
    }
}