Example #1
0
        /// <summary>
        /// Calculates <c>this</c> to the power of <c>K</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        ///
        /// <param name="K">The power</param>
        ///
        /// <returns>Returns <c>this</c>^<c>K</c> in a new GF2nPolynomialElement</returns>
        public GF2nPolynomialElement Power(int K)
        {
            if (K == 1)
            {
                return(new GF2nPolynomialElement(this));
            }

            GF2nPolynomialElement result = GF2nPolynomialElement.One((GF2nPolynomialField)mField);

            if (K == 0)
            {
                return(result);
            }

            GF2nPolynomialElement x = new GF2nPolynomialElement(this);

            x.polynomial.ExpandN((x.mDegree << 1) + 32); // increase performance
            x.polynomial.ReduceN();

            for (int i = 0; i < mDegree; i++)
            {
                if ((K & (1 << i)) != 0)
                {
                    result.MultiplyThisBy(x);
                }

                x.Square();
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Creates a new PolynomialGF2n from the given Bitstring <c>G</c> over the GF2nField <c>B1</c>
        /// </summary>
        ///
        /// <param name="G">The Bitstring to use</param>
        /// <param name="B1">The field</param>
        public GF2nPolynomial(GF2Polynomial G, GF2nField B1)
        {
            _size  = B1.Degree + 1;
            _coeff = new GF2nElement[_size];
            int i;

            if (B1 is GF2nONBField)
            {
                for (i = 0; i < _size; i++)
                {
                    if (G.TestBit(i))
                    {
                        _coeff[i] = GF2nONBElement.One((GF2nONBField)B1);
                    }
                    else
                    {
                        _coeff[i] = GF2nONBElement.Zero((GF2nONBField)B1);
                    }
                }
            }
            else if (B1 is GF2nPolynomialField)
            {
                for (i = 0; i < _size; i++)
                {
                    if (G.TestBit(i))
                    {
                        _coeff[i] = GF2nPolynomialElement.One((GF2nPolynomialField)B1);
                    }
                    else
                    {
                        _coeff[i] = GF2nPolynomialElement.Zero((GF2nPolynomialField)B1);
                    }
                }
            }
            else
            {
                throw new ArgumentException("GF2nPolynomial: PolynomialGF2n(Bitstring, GF2nField): B1 must be an instance of GF2nONBField or GF2nPolynomialField!");
            }
        }
Example #3
0
        /// <summary>
        /// Computes the change-of-basis matrix for basis conversion according to 1363.
        /// The result is stored in the lists fields and matrices.
        /// </summary>
        ///
        /// <param name="B1">The GF2nField to convert to</param>
        public override void ComputeCOBMatrix(GF2nField B1)
        {
            // we are in B0 here!
            if (DegreeN != B1.Degree)
            {
                throw new ArgumentException("GF2nPolynomialField.computeCOBMatrix: B1 has a different degree and thus cannot be coverted to!");
            }

            if (B1 is GF2nONBField)
            {
                // speedup (calculation is done in PolynomialElements instead of ONB)
                B1.ComputeCOBMatrix(this);
                return;
            }

            int i, j;

            GF2nElement[] gamma;
            GF2nElement   u;

            GF2Polynomial[] COBMatrix = new GF2Polynomial[DegreeN];

            for (i = 0; i < DegreeN; i++)
            {
                COBMatrix[i] = new GF2Polynomial(DegreeN);
            }

            // find Random Root
            do
            {
                // u is in representation according to B1
                u = B1.RandomRoot(FieldPoly);
            }while (u.IsZero());

            // build gamma matrix by multiplying by u
            if (u is GF2nONBElement)
            {
                gamma = new GF2nONBElement[DegreeN];
                gamma[DegreeN - 1] = GF2nONBElement.One((GF2nONBField)B1);
            }
            else
            {
                gamma = new GF2nPolynomialElement[DegreeN];
                gamma[DegreeN - 1] = GF2nPolynomialElement.One((GF2nPolynomialField)B1);
            }
            gamma[DegreeN - 2] = u;
            for (i = DegreeN - 3; i >= 0; i--)
            {
                gamma[i] = (GF2nElement)gamma[i + 1].Multiply(u);
            }

            if (B1 is GF2nONBField)
            {
                // convert horizontal gamma matrix by vertical Bitstrings
                for (i = 0; i < DegreeN; i++)
                {
                    for (j = 0; j < DegreeN; j++)
                    {
                        // TODO remember: ONB treats its Bits in reverse order !!!
                        if (gamma[i].TestBit(DegreeN - j - 1))
                        {
                            COBMatrix[DegreeN - j - 1].SetBit(DegreeN - i - 1);
                        }
                    }
                }
            }
            else
            {
                // convert horizontal gamma matrix by vertical Bitstrings
                for (i = 0; i < DegreeN; i++)
                {
                    for (j = 0; j < DegreeN; j++)
                    {
                        if (gamma[i].TestBit(j))
                        {
                            COBMatrix[DegreeN - j - 1].SetBit(DegreeN - i - 1);
                        }
                    }
                }
            }

            // store field and matrix for further use
            Fields.Add(B1);
            Matrices.Add(COBMatrix);
            // store field and inverse matrix for further use in B1
            B1.Fields.Add(this);
            B1.Matrices.Add(InvertMatrix(COBMatrix));
        }