in reply to Re: LFSRs & binary encode / decode functions
in thread LFSRs & binary encode / decode functions

LFSRs CAN be used to encrypt sequences of bits - this is the basic algorithm used to generate CRCs (well nowadays, they are now often generated by looking up data in tables, data generated by the LFSR). When used to generate a sequence of pseudo random numbers, the state of the LFSR is based on the initial seed. The LSB of the next state is based on the XOR of data from all of the taps. When used to encode a bit sequence, the next bit of the sequence is also XOR'ed with the tap data to generate the LSB.
  • Comment on Re^2: LFSRs & binary encode / decode functions

Replies are listed 'Best First'.
Re^3: LFSRs & binary encode / decode functions
by ikegami (Patriarch) on Jul 03, 2009 at 02:37 UTC

    Except for the initial value of the register, there are no inputs (as shown by the code below). What bit sequence are you talking about when you say "When used to encode a bit sequence"?

    use strict; use warnings; sub make_lfsr { my ($size, $taps, $seed) = @_; my $mask = 2**$size - 1; my @taps = grep $_, map $_-1, sort { $b <=> $a } (@$taps); my $r = $seed; return sub { my $bit = 0; $bit ^= ($r >> $_) & 1 for @taps; $r = (($r << 1) & $mask) | $bit; }; } sub to_binary { my ($i, $size) = @_; return substr(unpack('B32', pack('N', $i)), -$size); } { my $size = 16; my @taps = (16, 14, 13, 11, 1); my $seed = 1; my $lfsr = make_lfsr($size, \@taps, $seed); for (;;) { my $r = $lfsr->(); print(to_binary($r, $size), "\n"); last if $r == $seed; } }

    Update: Added code