Exemplo n.º 1
0
        /// <summary>
        /// Calculates the multiplicative inverse of <c>this</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        ///
        /// <returns>Returns <c>this</c>^(-1)</returns>
        public GF2nPolynomialElement InvertSquare()
        {
            GF2nPolynomialElement n;
            GF2nPolynomialElement u;
            int i, j, k, b;

            if (IsZero())
            {
                throw new ArithmeticException();
            }

            // b = (n-1)
            b = m_Field.Degree - 1;
            // n = a
            n = new GF2nPolynomialElement(this);
            n.m_polynomial.ExpandN((m_Degree << 1) + 32); // increase performance
            n.m_polynomial.ReduceN();
            // k = 1
            k = 1;

            // for i = (r-1) downto 0 do, r=bitlength(b)
            for (i = BigMath.FloorLog(b) - 1; i >= 0; i--)
            {
                // u = n
                u = new GF2nPolynomialElement(n);
                // for j = 1 to k do
                for (j = 1; j <= k; j++)
                {
                    u.SquareThisPreCalc(); // u = u^2
                }
                // n = nu
                n.MultiplyThisBy(u);
                // k = 2k
                k <<= 1;
                // if b(i)==1
                if ((b & m_bitMask[i]) != 0)
                {
                    // n = n^2 * b
                    n.SquareThisPreCalc();
                    n.MultiplyThisBy(this);
                    // k = k+1
                    k += 1;
                }
            }

            // outpur n^2
            n.SquareThisPreCalc();

            return(n);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Squares this GF2nPolynomialElement by using precalculated values and reducing.
        /// <para>This is supposed to de fastest when using a trinomial or pentanomial as field polynomial.
        /// Use SquareMatrix when using a ordinary polynomial as field polynomial.</para>
        /// </summary>
        ///
        /// <returns></returns>
        public GF2nPolynomialElement SquarePreCalc()
        {
            GF2nPolynomialElement result = new GF2nPolynomialElement(this);

            result.SquareThisPreCalc();
            result.ReduceThis();

            return(result);
        }
        /// <summary>
        /// Squares this GF2nPolynomialElement by using precalculated values and reducing.
        /// <para>This is supposed to de fastest when using a trinomial or pentanomial as field polynomial.
        /// Use SquareMatrix when using a ordinary polynomial as field polynomial.</para>
        /// </summary>
        /// 
        /// <returns></returns>
        public GF2nPolynomialElement SquarePreCalc()
        {
            GF2nPolynomialElement result = new GF2nPolynomialElement(this);
            result.SquareThisPreCalc();
            result.ReduceThis();

            return result;
        }
        /// <summary>
        /// Calculates the multiplicative inverse of <c>this</c> and returns the result in a new GF2nPolynomialElement
        /// </summary>
        /// 
        /// <returns>Returns <c>this</c>^(-1)</returns>
        public GF2nPolynomialElement InvertSquare()
        {
            GF2nPolynomialElement n;
            GF2nPolynomialElement u;
            int i, j, k, b;

            if (IsZero())
                throw new ArithmeticException();

            // b = (n-1)
            b = mField.Degree - 1;
            // n = a
            n = new GF2nPolynomialElement(this);
            n.polynomial.ExpandN((mDegree << 1) + 32); // increase performance
            n.polynomial.ReduceN();
            // k = 1
            k = 1;

            // for i = (r-1) downto 0 do, r=bitlength(b)
            for (i = BigMath.FloorLog(b) - 1; i >= 0; i--)
            {
                // u = n
                u = new GF2nPolynomialElement(n);
                // for j = 1 to k do
                for (j = 1; j <= k; j++)
                    u.SquareThisPreCalc(); // u = u^2

                // n = nu
                n.MultiplyThisBy(u);
                // k = 2k
                k <<= 1;
                // if b(i)==1
                if ((b & _bitMask[i]) != 0)
                {
                    // n = n^2 * b
                    n.SquareThisPreCalc();
                    n.MultiplyThisBy(this);
                    // k = k+1
                    k += 1;
                }
            }

            // outpur n^2
            n.SquareThisPreCalc();

            return n;
        }