Пример #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
        /// <summary>
        /// If the characteristic of the finite field is less or equal then 7 you can't use seperators.
        /// Example; For char=5 you should write string as "11232340134324320"
        /// For char=11 you should write string as "1-10-0-8-2-1-3-10-9-2"
        /// You can use . , ; _ - and space char as seperator.
        /// </summary>
        /// <param name="F"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Polynomial Parse(FiniteField F, string s, bool trimZeros)
        {
            FiniteFieldElement[] list = new FiniteFieldElement[0];

            if (F.Characteristic > 7)
            {
                string[] slist = s.Split('.', ',', ';', '_', '-', ' ');

                for (int i = 0; i < slist.Length; i++)
                {
                    FiniteFieldElement f = new FiniteFieldElement();
                    f.Field = F;
                    f.Value = int.Parse(slist[i].ToString()) % F.Characteristic;

                    Array.Resize(ref list, list.Length + 1);
                    list[list.Length - 1] = f;
                }
            }
            else
            {
                for (int i = 0; i < s.Length; i++)
                {
                    FiniteFieldElement f = new FiniteFieldElement();
                    f.Field = F;
                    f.Value = int.Parse(s[i].ToString()) % F.Characteristic;

                    Array.Resize(ref list, list.Length + 1);
                    list[list.Length - 1] = f;
                }
            }

            // Trim 0s
            if (trimZeros)
            {
                int listlen = list.Length;
                for (int i = 0; i < listlen - 1; i++)
                {
                    if (list[listlen - i - 1].Value == 0)
                    {
                        Array.Resize(ref list, list.Length - 1);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(new Polynomial(list));
        }
Пример #3
0
        public static FiniteFieldElement operator *(FiniteFieldElement element1, FiniteFieldElement element2)
        {
            if (element1.Field.Characteristic != element2.Field.Characteristic)
            {
                throw new Exception("Both elements should be in the same field.");
            }

            FiniteFieldElement result = new FiniteFieldElement();

            result.Field = element1.Field;
            result.Value = (element1.Value * element2.Value) % element1.Field.Characteristic;

            return(result);
        }
Пример #4
0
        public Polynomial(FiniteField F, params int[] coefficients)
        {
            _coefficients = new FiniteFieldElement[0];

            for (int i = 0; i < coefficients.Length; i++)
            {
                FiniteFieldElement elt = new FiniteFieldElement();
                elt.Field = F;
                elt.Value = coefficients[i] % elt.Field.Characteristic;

                Array.Resize(ref _coefficients, _coefficients.Length + 1);
                _coefficients[_coefficients.Length - 1] = elt;
            }
        }
Пример #5
0
        public void Clock()
        {
            FiniteFieldElement r = new FiniteFieldElement();

            r.Field = _feedbackPolynomial.Field;
            r.Value = 0;
            // Evaluate the current state on feedback polynomial
            for (int i = 0; i < currentState.Length; i++)
            {
                r += _feedbackPolynomial.Coefficients[i] * currentState[i];
            }
            // Shift the currentState right.
            _streamOutput = currentState[0];
            Array.Copy(currentState, 1, currentState, 0, currentState.Length - 1);
            currentState[currentState.Length - 1] = r;
        }
Пример #6
0
        /// <summary>
        /// Generates a polynomial which consists only 1 term over given field F. e.g. (F,alfa,4) returns the polynomial alfa*x^4 over F and alfa in F.
        /// </summary>
        public static Polynomial GenerateFromTerm(FiniteField F, FiniteFieldElement Coefficient, int Degree)
        {
            if (F.Characteristic != Coefficient.Field.Characteristic)
            {
                throw new Exception("Coefficient must be an element of given Finite Field F.");
            }
            FiniteFieldElement[] coefs = new FiniteFieldElement[0];
            for (int i = 0; i < Degree; i++)
            {
                Array.Resize(ref coefs, coefs.Length + 1);
                coefs[coefs.Length - 1] = F.Elements[0];
            }
            Array.Resize(ref coefs, coefs.Length + 1);
            FiniteFieldElement f = Coefficient;

            coefs[coefs.Length - 1] = f;
            return(new Polynomial(coefs));
        }
Пример #7
0
        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);
        }
Пример #8
0
        public FiniteField(int p)
        {
            if (!MathTools.IsPrime(p))
            {
                throw new Exception("p should be a prime number.");
            }

            _Elements = new FiniteFieldElement[0];

            for (int i = 0; i < p; i++)
            {
                FiniteFieldElement elt = new FiniteFieldElement();
                elt.Value = i;
                elt.Field = this;

                Array.Resize(ref _Elements, _Elements.Length + 1);
                _Elements[_Elements.Length - 1] = elt;
            }

            this._Characteristic = p;
            this._Order          = p - 1;
        }
Пример #9
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;
        }
Пример #10
0
 public void Clock()
 {
     FiniteFieldElement r = new FiniteFieldElement();
     r.Field = _feedbackPolynomial.Field;
     r.Value = 0;
     // Evaluate the current state on feedback polynomial
     for (int i = 0; i < currentState.Length; i++)
         r += _feedbackPolynomial.Coefficients[i] * currentState[i];
     // Shift the currentState right.
     _streamOutput = currentState[0];
     Array.Copy(currentState, 1, currentState, 0, currentState.Length - 1);
     currentState[currentState.Length - 1] = r;
 }
        public Polynomial(FiniteField F, params int[] coefficients)
        {
            _coefficients = new FiniteFieldElement[0];

            for (int i = 0; i < coefficients.Length; i++)
            {
                FiniteFieldElement elt = new FiniteFieldElement();
                elt.Field = F;
                elt.Value = coefficients[i] % elt.Field.Characteristic;

                Array.Resize(ref _coefficients, _coefficients.Length + 1);
                _coefficients[_coefficients.Length - 1] = elt;
            }
        }
        public static FiniteFieldElement operator -(FiniteFieldElement element1, FiniteFieldElement element2)
        {
            if (element1.Field.Characteristic != element2.Field.Characteristic)
            {
                throw new Exception("Both elements should be in the same field.");
            }

            FiniteFieldElement result = new FiniteFieldElement();
            result.Field = element1.Field;
            result.Value = (element1.Value - element2.Value) % element1.Field.Characteristic;
            if (result.Value < 0)
                result.Value += result.Field.Characteristic;
            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.");
            }

            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;
        }
 /// <summary>
 /// Generates a polynomial which consists only 1 term over given field F. e.g. (F,alfa,4) returns the polynomial alfa*x^4 over F and alfa in F.
 /// </summary>
 public static Polynomial GenerateFromTerm(FiniteField F, FiniteFieldElement Coefficient, int Degree)
 {
     if (F.Characteristic != Coefficient.Field.Characteristic)
     {
         throw new Exception("Coefficient must be an element of given Finite Field F.");
     }
     FiniteFieldElement[] coefs = new FiniteFieldElement[0];
     for (int i = 0; i < Degree; i++)
     {
         Array.Resize(ref coefs, coefs.Length + 1);
         coefs[coefs.Length - 1] = F.Elements[0];
     }
     Array.Resize(ref coefs, coefs.Length + 1);
     FiniteFieldElement f = Coefficient;
     coefs[coefs.Length - 1] = f;
     return new Polynomial(coefs);
 }
Пример #15
0
 FiniteFieldElement NonLinearCombiner(FiniteFieldElement X1, FiniteFieldElement X2, FiniteFieldElement X3, FiniteFieldElement X4)
 {
     return (X1 * X3) + (X2 * X4);
 }
        public FiniteField(int p)
        {
            if (!MathTools.IsPrime(p))
                throw new Exception("p should be a prime number.");

            _Elements = new FiniteFieldElement[0];

            for (int i = 0; i < p; i++)
            {
                FiniteFieldElement elt = new FiniteFieldElement();
                elt.Value = i;
                elt.Field = this;

                Array.Resize(ref _Elements, _Elements.Length + 1);
                _Elements[_Elements.Length - 1] = elt;
            }

            this._Characteristic = p;
            this._Order = p - 1;
        }
Пример #17
0
        private bool IsEqualStates(FiniteFieldElement[] s1, FiniteFieldElement[] s2)
        {
            bool kontrol = true;
            for (int i = 0; i < s1.Length; i++)
            {
                if (s1[i].Value != s2[i].Value)
                {
                    kontrol = false;
                    break;
                }
            }

            return kontrol;
        }
Пример #18
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;
        }
        /// <summary>
        /// If the characteristic of the finite field is less or equal then 7 you can't use seperators.
        /// Example; For char=5 you should write string as "11232340134324320"
        /// For char=11 you should write string as "1-10-0-8-2-1-3-10-9-2"
        /// You can use . , ; _ - and space char as seperator. 
        /// </summary>
        /// <param name="F"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Polynomial Parse(FiniteField F, string s, bool trimZeros)
        {
            FiniteFieldElement[] list = new FiniteFieldElement[0];

            if (F.Characteristic > 7)
            {
                string[] slist = s.Split('.', ',', ';', '_', '-', ' ');

                for (int i = 0; i < slist.Length; i++)
                {
                    FiniteFieldElement f = new FiniteFieldElement();
                    f.Field = F;
                    f.Value = int.Parse(slist[i].ToString()) % F.Characteristic;

                    Array.Resize(ref list, list.Length + 1);
                    list[list.Length - 1] = f;
                }

            }
            else
            {
                for (int i = 0; i < s.Length; i++)
                {
                    FiniteFieldElement f = new FiniteFieldElement();
                    f.Field = F;
                    f.Value = int.Parse(s[i].ToString()) % F.Characteristic;

                    Array.Resize(ref list, list.Length + 1);
                    list[list.Length - 1] = f;
                }
            }

            // Trim 0s
            if (trimZeros)
            {
                int listlen = list.Length;
                for (int i = 0; i < listlen - 1; i++)
                {
                    if (list[listlen - i - 1].Value == 0)
                        Array.Resize(ref list, list.Length - 1);
                    else
                        break;
                }
            }

            return new Polynomial(list);
        }