Пример #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;
        }
Пример #2
0
 private void Form1_Load(object sender, EventArgs e)
 {
     GF5 = new FiniteField(5);
     GF56Primitive = new Polynomial(GF5, 2, 0, 1, 4, 1, 0, 1);
     ExtF = new ExtensionField(GF56Primitive);
     lblInfo.Text = "Current Field Info: " + ExtF.ToString();
 }
Пример #3
0
        public LFSR(Polynomial feedbackPolynomial, FiniteFieldElement[] initialState)
        {
            if (feedbackPolynomial.Field.Characteristic != initialState[0].Field.Characteristic)
                throw new Exception("Both values should be defined in the same field.");
            if ((initialState.Length - 1) < feedbackPolynomial.Degree)
                throw new ArgumentOutOfRangeException("Initial state size must be greater then feedback polynomial degree.");

            this.currentState = initialState;
            this._feedbackPolynomial = feedbackPolynomial;
            periodOncedenHesaplandiysa = false;
        }
        /// <returns>Exact division q 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 = a, 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 result;
        }
        public static Polynomial ModPow(Polynomial poly, int exponent, Polynomial modulus)
        {
            Polynomial result = new Polynomial(poly.Field, 1);

            for (int i = 0; i < exponent; i++)
            {
                result = (result * poly) % modulus;
                //if (i % 1000 == 0)
                //    Debug.WriteLine(i + "/" + exponent);
            }
            return result;
        }
        public static Polynomial operator *(Polynomial poly1, Polynomial poly2)
        {
            if (poly1.Field.Characteristic != poly2.Field.Characteristic)
            {
                throw new Exception("Both polynomials should be defined in the same field.");
            }

            Polynomial p = new Polynomial(poly1.Field, new int[poly1.Degree + poly2.Degree + 1]);

            for (int i = 0; i < poly1._coefficients.Length; i++)
            {
                for (int j = 0; j < poly2._coefficients.Length; j++)
                    p._coefficients[i + j] += poly1._coefficients[i] * poly2._coefficients[j];
            }

            int plen = p._coefficients.Length;
            for (int i = 0; i < plen - 1; i++)
            {
                if (p._coefficients[plen - i - 1].Value == 0)
                    Array.Resize(ref p._coefficients, p._coefficients.Length - 1);
                else
                    break;
            }

            return p;
        }
        public static Polynomial operator -(Polynomial poly1, Polynomial poly2)
        {
            if (poly1.Field.Characteristic != poly2.Field.Characteristic)
            {
                throw new Exception("Both polynomials should be defined in the same field.");
            }

            FiniteFieldElement[] elts = poly1.Degree > poly2.Degree ? (FiniteFieldElement[])poly1.Coefficients.Clone() : (FiniteFieldElement[])poly2.Coefficients.Clone();

            Polynomial p = new Polynomial(elts);

            int len = Math.Min(poly1.Degree, poly2.Degree);

            for (int i = 0; i <= len; i++)
                p._coefficients[i] = poly1._coefficients[i] - poly2._coefficients[i];

            FiniteFieldElement sifir = new FiniteFieldElement();
            sifir.Field = p.Field;
            sifir.Value = 0;

            for (int i = len + 1; i < p._coefficients.Length; i++)
                p._coefficients[i] = sifir - p._coefficients[i];

            int plen = p._coefficients.Length;
            for (int i = 0; i < plen - 1; i++)
            {
                if (p._coefficients[plen - i - 1].Value == 0)
                    Array.Resize(ref p._coefficients, p._coefficients.Length - 1);
                else
                    break;
            }

            return p;
        }
 public ExtensionField(Polynomial primitivePolynomial)
 {
     this._baseField = primitivePolynomial.Field;
     this._primitivePolynomial = primitivePolynomial;
     this.rnd = new Random();
 }
        /// <summary>
        /// Decides if the element generates the extension field with over base field with given primitive polynomial. 
        /// </summary>
        /// <param name="primitivePolynomial">Should be in the same field with the element selected.</param>
        public bool IsPrimitiveElement(Polynomial primitivePolynomial)
        {
            if (primitivePolynomial.Field.Characteristic != this.Field.Characteristic)
            {
                throw new Exception("Given primitive polynomial should be in the same field with the polynomial.");
            }

            int[] dv = MathTools.Divisors(Math.Pow(this.Field.Characteristic, primitivePolynomial.Degree) - 1);
            for (int i = 0; i < dv.Length - 1; i++)
            {
                Polynomial test = Polynomial.ModPow(this, dv[i], primitivePolynomial);
                if (test.IsIdentity)
                    return false;
            }
            return true;
        }
 /// <summary>
 /// Generates a monic polynomial of given degree over given field F.
 /// </summary>
 public static Polynomial RandomMonic(FiniteField F, int degree)
 {
     Random rnd = new Random();
     int[] coefs = new int[degree + 1];
     for (int i = 0; i < coefs.Length - 1; i++)
         coefs[i] = rnd.Next(F.Elements[0].Value, F.Elements[F.Elements.Length - 1].Value + 1);
     coefs[coefs.Length - 1] = 1;
     Polynomial p = new Polynomial(F, coefs);
     return p;
 }
 public ExtensionFieldElement(ExtensionField ExtField, Polynomial ElementValue)
 {
     this.Value = ElementValue;
     this.Field = ExtField;
 }
        public static Polynomial Pow(Polynomial p, int exp)
        {
            Polynomial result = new Polynomial(p.Field, 1);

            for (int i = 0; i < exp; i++)
            {
                result *= p;
                //if (i % 1000 == 0)
                //    Debug.WriteLine(i + "/" + exp);
            }
            return result;
        }