Ejemplo n.º 1
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!");
            }
        }
Ejemplo n.º 2
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));
        }