(*
LFSR-- A Mathematica package for symbolic representation
 and computation of Linear Feedback Shift Registers.

http://modp.com/release/mma_lfsr/
Version 2.0.0, 23-Aug-2005
Nick Galbreath, nickg [at] modp [dot] com
Copyright (c) 2005

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*)

BeginPackage["modp`lfsr`"];

    
LFSR::usage  =  "LFSR[complexity, polynomial] represents an \
abstract, stateless, linear feedback shift register. For example LFSR[4, 1 + \
t + t^2]. See LFSRGenerate for more details.";

LFSRGenerate::ussage  =  "LFSRGenerate[LFSR, numbits, \
initialstate_List] generates a sequence of bits using the inital state.  \
LFSRGenerate[LFSR, numbits, initstate_Symbol] does the same thing except the \
variable initialstate is update";

LinearComplexity::usage  =  "Returns the linear complexity of a \
binary sequence. This uses a different implementation than \
LinearComplexityProfile and is 10x faster";

\(\(LinearComplexityProfile::usage\  = \ "LinearComplexityProfile[bits_Lists] determines the minimal LFSR that could generate the input sequence.  Also compute the 'linear complexity profile'";\)
\)
Begin["`Private`"];

  
LFSRGenerate[LFSR[l_Integer,  cd_],  n_Integer,  
        state_Symbol]  :=  
      Module[{coef,  output,  newbit}, 
 
        coef  =  
          PadRight[
            Drop[CoefficientList[cd,  Variables[cd][[1]]], 1],  
            l]; 
Table[
output  =  
            state[[1]]; 
newbit  =  
            Mod[Dot[coef,  Reverse[state]], 
              2]; 
Unevaluated[state]  =  
            Append[Rest[state],  
              newbit]; 
output,  {n}]
]

 (* Generate bits,  
      use given state,  
      but dont save it *) 
LFSRGenerate[LFSR[l_Integer,  cd_],  n_Integer,  
        stateOrig_List]  :=  
      Module[{coef,  output,  newbit,  
          state =  stateOrig}, 
 
        coef  =  
          PadRight[
            Drop[CoefficientList[cd,  Variables[cd][[1]]], 1],  
            l]; 
Table[
output  =  
            state[[1]]; 
newbit  =  
            Mod[Dot[coef,  Reverse[state]], 
              2]; 
state  =  
            Append[Rest[state],  
              newbit]; 
output,  {n}]
]

SetAttributes[LFSRGenerate,  
      HoldRest]

  
\(\(LinearComplexity\  = \ 
        Compile[{{u, \ _Integer, \ 1}}, \ 
          Module[{len\  = \ Length[u], \ b, c, d, p, tmp, \ l = 0, \ 
              m = 0, \ }, 
c = 
              ReplacePart[Table[0, \ {len}], \ 1, 1]; 
b = 
              ReplacePart[Table[0, \ {len}], \ 1, 
                1]; 
Do[
d = \ 
                Mod[u[\([n]\)]\  + \ 
                    Dot[\ Take[c, \ {2, \ l + 1}], 
                      Reverse[Take[u, {n - l\ , \ n - 1\ }]]], \ 
                  2]; 
If[
                d == 1, 
tmp\  = \ 
                  c; 
p\  = \ 
                  Table[0, \ {len}]; 
Do[\ \ If[
                    b[\([i]\)]\  == 1, 
                    p[\([i + n - m]\)]\  = \ 1], \ {i, 1, \ 
                    l + 1}]; 
c\ \  = \ 
                  Mod[c + p, \ 2]; 
If[
                  2*l\  ≤ \ n, 
l\  = \ 
                    n\  - l; 
m\  = \ 
                    n; 
b\  = \ 
                    tmp;
];
], \ {n, \ 
                1, \ len}]; 
l
]
];\)

 (*\ 
      very\ symbolic\ implementation\ *) \)
LinearComplexityProfile[u_List,  
          
          t_Symbol]  := 
Module[{    
phi  =  1, 
psi  =  
              1 ,                                    (* last feedback polynomial *) 
eta  =  
              0,                                       (* new feedback polynomial *) 
l  =  
              0,              (* 
              linear complexity up to actual index *) 
r  =  -1,                                                                       (* last index *) 
d  =  
              0,    (* 
              discrepancy between predicted and actual *) 
b,  feedbacklist,  
            lcp     (* 
              auxiliary variables *) 
}, 
lcp  =  
            Table[
b  =  
                Reverse[
                  Take[u, {n - l + 1, 
                      n}]]; 
feedbacklist  =  
                PadRight[Drop[CoefficientList[phi,  t], 1],  
                  l]; 
d  =  
                Mod[u[[n + 1]]  +  feedbacklist  .  b,  
                  2]; 
If[
                d ==  1, 
eta  =  
                  PolynomialMod[phi  +  ( t^(n - r ) ) psi,  
                    2]; 
If[
                  2  l  ≤  n, 
l  =  
                    n  + 1 - l; 
psi  =  
                    phi; 
r  =  
                    n 
];  
phi  =  
                  eta
]; 
l,  {n,  0, 
                Length[u] - 1}]; 
{LFSR[l, phi],  
            lcp}
];

 (* Slight conflict .   Mathematica uses array who' 
        s first index is 1,  
      while
in we need to do deal polynomials whose first index is 0.   This is the Berklekamp - 
        Massey algorithm,   but iterates starting from 1.   (same algorithm,  
          different implementations)
*) 
bma2[
        u_List]  := 
Module[{    
phi  =  1, 
psi  =  
            1 ,                                    (* last feedback polynomial *) 
eta  =  
            0,                                       (* new feedback polynomial *) 
l  =  
            0,              (* 
            linear complexity up to actual index *) 
r  =  
            0,                                                                       (* last index *) 
d  =  0,    (* 
            discrepancy between predicted bit and    *) 
                                    (* 
            true bit -- always 0 or 1 in F_  
              2 *) 
b,  
          feedbacklist     (* 
            auxiliary variables *) 
}, 
linprof  =  
          Table[
b  =  
              Reverse[Take[
                  u, {n - l, n - 1}]]; 
feedbacklist  =  
              PadRight[Drop[CoefficientList[phi,  T], 1],  
                l]; 
d  =  
              Mod[u[[n]]  +  feedbacklist  .  b,  
                2]; 
If[
              d ==  1, 
eta  =  
                PolynomialMod[phi  +  ( T^(n - r ) ) psi,  
                  2]; 
If[
                2*l  <  n, 
l  =  
                  n  - l; 
psi  =  
                  phi; 
r  =  
                  n 
];  
phi  =  
                eta
]; 
l,  {n,  1, 
              Length[u]}]; 
{LFSR[l,  phi],  
          linprof}
] 

End[];

EndPackage[];