Beispiel #1
0
        /// <param name="s">Output sequence as polynomial</param>
        /// <param name="C">Fills the given polynomial with LFSR Connection Polynomial</param>
        /// <returns>Returns the Linear Complexity of given sequence</returns>
        public static int BerlekampMassey(Polynomial s, out Polynomial Conn)
        {
            Polynomial         C = new Polynomial(s.Field, 1);
            Polynomial         B = new Polynomial(s.Field, 1);
            int                N = s.Degree + 1;
            int                L = 0;
            int                m = 1;
            FiniteFieldElement b = s.Field.Elements[1];
            int                n;

            for (n = 0; n < N; n++)
            {
                // calculate discrepency
                FiniteFieldElement d = new FiniteFieldElement();
                d = s.Coefficients[n];
                for (int i = 1; i <= L; i++)
                {
                    try
                    {
                        d += C.Coefficients[i] * s.Coefficients[n - i];
                    }
                    catch { }
                }


                if (d.Value == 0)
                {
                    m++;
                }
                else if (2 * L <= n)
                {
                    Polynomial T = new Polynomial();

                    T = C;

                    Polynomial c1 = Polynomial.GenerateFromTerm(s.Field, d * b.Inverse, m);

                    C = C - (c1 * B);

                    L = n + 1 - L;

                    B = T;

                    b = d;

                    m = 1;
                }
                else
                {
                    Polynomial c1 = Polynomial.GenerateFromTerm(s.Field, d * b.Inverse, m);
                    C = C - (c1 * B);
                    m++;
                }
            }

            Conn = C;
            return(L);
        }
Beispiel #2
0
        /// <returns>Remainder r of polynomials a and b s.t. a = b*q + r</returns>
        public static Polynomial operator %(Polynomial a, Polynomial b)
        {
            if (a.Field.Characteristic != b.Field.Characteristic)
            {
                throw new Exception("Both polynomials should be defined in the same field.");
            }
            //if (a.Degree < b.Degree)
            //{
            //    throw new Exception("First polynomial should have higher degree.");
            //}

            Polynomial result = new Polynomial(a.Field, 0);
            Polynomial r = (Polynomial)a.MemberwiseClone(), q = new Polynomial(a.Field, 0);

            while (r.Degree >= b.Degree && b.Degree != 0)
            {
                q       = Polynomial.GenerateFromTerm(r.Field, r._coefficients[r._coefficients.Length - 1] / b._coefficients[b._coefficients.Length - 1], r.Degree - b.Degree);
                result += q;
                r       = r - (b * q);
            }

            return(r);
        }