(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 12631, 246]*) (*NotebookOutlinePosition[ 13322, 270]*) (* CellTagsIndexPosition[ 13278, 266]*) (*WindowFrame->Normal*) Notebook[{ Cell[TextData[{ "(*\nLFSR-- A ", StyleBox["Mathematica", FontSlant->"Italic"], " package for symbolic representation\n and computation of Linear Feedback \ Shift Registers.\n\nhttp://modp.com/release/mma_lfsr/\nVersion 2.0.0, \ 23-Aug-2005\nNick Galbreath, nickg [at] modp [dot] com\nCopyright (c) 2005\n\n\ Permission is hereby granted, free of charge, to any person obtaining a\ncopy \ of this software and associated documentation files (the\n\"Software\"), to \ deal in the Software without restriction, including\nwithout limitation the \ rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or \ sell copies of the Software, and to\npermit persons to whom the Software is \ furnished to do so, subject to\nthe following conditions:\n\nThe above \ copyright notice and this permission notice shall be included\nin all copies \ or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS \ IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING BUT NOT \ LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR \ PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT \ HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN \ ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION \ WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n*)\n" }], "Program", Evaluatable->True, InitializationCell->True, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}], Cell[BoxData[{ \(\(\(BeginPackage["\"];\)\(\[IndentingNewLine]\) \)\), "\[IndentingNewLine]", \(\(LFSR::usage\ = \ "\";\)\[IndentingNewLine]\), "\ \[IndentingNewLine]", \(\(LFSRGenerate::ussage\ = \ "\";\)\[IndentingNewLine]\), "\ \[IndentingNewLine]", \(\(LinearComplexity::usage\ = \ "\";\)\[IndentingNewLine]\), "\ \[IndentingNewLine]", \(\(LinearComplexityProfile::usage\ = \ \ "\";\)\[IndentingNewLine]\), "\[IndentingNewLine]", \(\(Begin["\<`Private`\>"];\)\[IndentingNewLine]\[IndentingNewLine] (*\ Generate\ bits\ and\ save\ state\ *) \), "\[IndentingNewLine]", \(LFSRGenerate[LFSR[l_Integer, \ cd_], \ n_Integer, \ state_Symbol]\ := \ Module[{coef, \ output, \ newbit}, \[IndentingNewLine]\ coef\ = \ PadRight[ Drop[CoefficientList[cd, \ \(Variables[cd]\)[\([1]\)]], 1], \ l]; \[IndentingNewLine]Table[\[IndentingNewLine]output\ = \ state[\([1]\)]; \[IndentingNewLine]newbit\ = \ Mod[Dot[coef, \ Reverse[state]], 2]; \[IndentingNewLine]Unevaluated[state]\ = \ Append[Rest[state], \ newbit]; \[IndentingNewLine]output, \ {n}]\[IndentingNewLine]]\ \[IndentingNewLine]\[IndentingNewLine] (*\ Generate\ bits, \ use\ given\ state, \ but\ dont\ save\ it\ *) \), "\[IndentingNewLine]", \(LFSRGenerate[LFSR[l_Integer, \ cd_], \ n_Integer, \ stateOrig_List]\ := \ Module[{coef, \ output, \ newbit, \ state = \ stateOrig}, \[IndentingNewLine]\ coef\ = \ PadRight[ Drop[CoefficientList[cd, \ \(Variables[cd]\)[\([1]\)]], 1], \ l]; \[IndentingNewLine]Table[\[IndentingNewLine]output\ = \ state[\([1]\)]; \[IndentingNewLine]newbit\ = \ Mod[Dot[coef, \ Reverse[state]], 2]; \[IndentingNewLine]state\ = \ Append[Rest[state], \ newbit]; \[IndentingNewLine]output, \ {n}]\[IndentingNewLine]]\ \[IndentingNewLine]\), "\[IndentingNewLine]", \(SetAttributes[LFSRGenerate, \ HoldRest]\[IndentingNewLine]\[IndentingNewLine] (*\ c - style\ implementation . \[IndentingNewLine]compiling\ gives\ a\ 10 x\ \(boost!\)\ *) \), "\[IndentingNewLine]", \(\(LinearComplexity\ = \ Compile[{{u, \ _Integer, \ 1}}, \ Module[{len\ = \ Length[u], \ b, c, d, p, tmp, \ l = 0, \ m = 0, \ }, \[IndentingNewLine]c = ReplacePart[Table[0, \ {len}], \ 1, 1]; \[IndentingNewLine]b = ReplacePart[Table[0, \ {len}], \ 1, 1]; \[IndentingNewLine]Do[\[IndentingNewLine]d = \ Mod[u[\([n]\)]\ + \ Dot[\ Take[c, \ {2, \ l + 1}], Reverse[Take[u, {n - l\ , \ n - 1\ }]]], \ 2]; \[IndentingNewLine]If[ d \[Equal] 1, \[IndentingNewLine]tmp\ = \ c; \[IndentingNewLine]p\ = \ Table[0, \ {len}]; \[IndentingNewLine]Do[\ \ If[ b[\([i]\)]\ \[Equal] 1, p[\([i + n - m]\)]\ = \ 1], \ {i, 1, \ l + 1}]; \[IndentingNewLine]c\ \ = \ Mod[c + p, \ 2]; \[IndentingNewLine]If[ 2*l\ \[LessEqual] \ n, \[IndentingNewLine]l\ = \ n\ - l; \[IndentingNewLine]m\ = \ n; \[IndentingNewLine]b\ = \ tmp;\[IndentingNewLine]];\[IndentingNewLine]], \ {n, \ 1, \ len}]; \[IndentingNewLine]l\[IndentingNewLine]]\ \[IndentingNewLine]];\)\n\[IndentingNewLine] (*\ very\ symbolic\ implementation\ *) \), "\[IndentingNewLine]", \(\(LinearComplexityProfile[u_List, \ t_Symbol]\ := \[IndentingNewLine]Module[{\ \ \ \ \ \[IndentingNewLine]phi\ = \ 1, \[IndentingNewLine]psi\ = \ 1\ , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ last\ feedback\ polynomial\ *) \[IndentingNewLine]eta\ = \ 0, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ new\ feedback\ polynomial\ *) \[IndentingNewLine]l\ = \ 0, \ \ \ \ \ \ \ \ \ \ \ \ (*\ linear\ complexity\ up\ to\ actual\ index\ \ *) \[IndentingNewLine]r\ = \ \(-1\), \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ last\ index\ *) \[IndentingNewLine]d\ = \ 0, \ \ (*\ discrepancy\ between\ predicted\ and\ actual\ \ *) \[IndentingNewLine]b, \ feedbacklist, \ lcp\ \ \ \ (*\ auxiliary\ variables\ *) \[IndentingNewLine]}, \ \[IndentingNewLine]lcp\ = \ Table[\[IndentingNewLine]b\ = \ Reverse[ Take[u, {n - l + 1, n}]]; \[IndentingNewLine]feedbacklist\ = \ PadRight[Drop[CoefficientList[phi, \ t], 1], \ l]; \[IndentingNewLine]d\ = \ Mod[u[\([n + 1]\)]\ + \ feedbacklist\ . \ b, \ 2]; \[IndentingNewLine]If[ d \[Equal] \ 1, \[IndentingNewLine]eta\ = \ PolynomialMod[phi\ + \ \((\ t^\((n - r\ )\)\ )\) psi, \ 2]; \[IndentingNewLine]If[ 2 l\ \[LessEqual] \ n, \[IndentingNewLine]l\ = \ n\ + 1 - l; \[IndentingNewLine]psi\ = \ phi; \[IndentingNewLine]r\ = \ n\ \[IndentingNewLine]]; \ \[IndentingNewLine]phi\ = \ eta\[IndentingNewLine]]; \[IndentingNewLine]l, \ {n, \ 0, Length[u] - 1}]; \[IndentingNewLine]{LFSR[l, phi], \ lcp}\[IndentingNewLine]];\)\[IndentingNewLine]\[IndentingNewLine] \ (*\ Slight\ conflict . \ \ Mathematica\ uses\ array\ who' s\ first\ index\ is\ 1, \ while\[IndentingNewLine]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)\)\[IndentingNewLine]*) \), "\ \[IndentingNewLine]", \(bma2[ u_List]\ := \[IndentingNewLine]Module[{\ \ \ \ \ \[IndentingNewLine]phi\ = \ 1, \[IndentingNewLine]psi\ = \ 1\ , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ last\ feedback\ polynomial\ *) \[IndentingNewLine]eta\ = \ 0, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ new\ feedback\ polynomial\ *) \[IndentingNewLine]l\ = \ 0, \ \ \ \ \ \ \ \ \ \ \ \ (*\ linear\ complexity\ up\ to\ actual\ index\ \ *) \[IndentingNewLine]r\ = \ 0, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ last\ index\ *) \[IndentingNewLine]d\ = \ 0, \ \ (*\ discrepancy\ between\ predicted\ bit\ and\ \ \ \ \ *) \[IndentingNewLine]\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (*\ true\ \(bit\ --\)\ always\ 0\ or\ 1\ in\ F_ 2\ *) \[IndentingNewLine]b, \ feedbacklist\ \ \ \ (*\ auxiliary\ variables\ *) \[IndentingNewLine]}, \ \[IndentingNewLine]linprof\ = \ Table[\[IndentingNewLine]b\ = \ Reverse[Take[ u, {n - l, n - 1}]]; \[IndentingNewLine]feedbacklist\ = \ PadRight[Drop[CoefficientList[phi, \ T], 1], \ l]; \[IndentingNewLine]d\ = \ Mod[u[\([n]\)]\ + \ feedbacklist\ . \ b, \ 2]; \[IndentingNewLine]If[ d \[Equal] \ 1, \[IndentingNewLine]eta\ = \ PolynomialMod[phi\ + \ \((\ T^\((n - r\ )\)\ )\) psi, \ 2]; \[IndentingNewLine]If[ 2*l\ < \ n, \[IndentingNewLine]l\ = \ n\ - l; \[IndentingNewLine]psi\ = \ phi; \[IndentingNewLine]r\ = \ n\ \[IndentingNewLine]]; \ \[IndentingNewLine]phi\ = \ eta\[IndentingNewLine]]; \[IndentingNewLine]l, \ {n, \ 1, Length[u]}]; \[IndentingNewLine]{LFSR[l, \ phi], \ linprof}\[IndentingNewLine]]\ \[IndentingNewLine]\), "\ \[IndentingNewLine]", \(\(End[];\)\[IndentingNewLine]\), "\[IndentingNewLine]", \(\(EndPackage[];\)\)}], "Input", InitializationCell->True] }, FrontEndVersion->"5.2 for Macintosh", ScreenRectangle->{{0, 1024}, {0, 679}}, AutoGeneratedPackage->Automatic, WindowSize->{913, 663}, WindowMargins->{{Automatic, 55}, {Automatic, 16}}, ShowSelection->True ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[1754, 51, 1525, 25, 460, "Program", Evaluatable->True, InitializationCell->True], Cell[3282, 78, 9345, 166, 2107, "Input", InitializationCell->True] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)